mirror of
https://seed.radicle.dev/rad:z39mP9rQAaGmERfUMPULfPUi473tY.git
synced 2026-11-21 18:40:46 +08:00
lib: Show hint when table is empty
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use std::cmp;
|
||||
use std::vec;
|
||||
|
||||
use tokio::sync::mpsc::UnboundedSender;
|
||||
@@ -264,7 +265,7 @@ impl Render<()> for Notifications {
|
||||
.unwrap_or("-".to_string());
|
||||
let length = self.props.notifications.len().to_string();
|
||||
|
||||
span::badge(format!("{}/{}", step, length))
|
||||
span::badge(format!("{}/{}", cmp::min(&step, &length), length))
|
||||
};
|
||||
|
||||
self.header.render::<B>(
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use std::cmp;
|
||||
use std::vec;
|
||||
|
||||
use ratatui::style::Stylize;
|
||||
@@ -293,7 +294,7 @@ impl Render<()> for Issues {
|
||||
.unwrap_or("-".to_string());
|
||||
let length = self.props.issues.len().to_string();
|
||||
|
||||
span::badge(format!("{}/{}", step, length))
|
||||
span::badge(format!("{}/{}", cmp::min(&step, &length), length))
|
||||
};
|
||||
|
||||
self.header.render::<B>(
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use std::cmp;
|
||||
use std::vec;
|
||||
|
||||
use ratatui::style::Stylize;
|
||||
@@ -306,7 +307,7 @@ impl Render<()> for Patches {
|
||||
.unwrap_or("-".to_string());
|
||||
let length = self.props.patches.len().to_string();
|
||||
|
||||
span::badge(format!("{}/{}", step, length))
|
||||
span::badge(format!("{}/{}", cmp::min(&step, &length), length))
|
||||
};
|
||||
|
||||
self.header.render::<B>(
|
||||
|
||||
@@ -31,3 +31,23 @@ pub fn default_page(area: Rect, context_h: u16, shortcuts_h: u16) -> DefaultPage
|
||||
shortcuts: layout[2],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn centered_rect(r: Rect, percent_x: u16, percent_y: u16) -> Rect {
|
||||
let layout = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([
|
||||
Constraint::Percentage((100 - percent_y) / 2),
|
||||
Constraint::Percentage(percent_y),
|
||||
Constraint::Percentage((100 - percent_y) / 2),
|
||||
])
|
||||
.split(r);
|
||||
|
||||
Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints([
|
||||
Constraint::Percentage((100 - percent_x) / 2),
|
||||
Constraint::Percentage(percent_x),
|
||||
Constraint::Percentage((100 - percent_x) / 2),
|
||||
])
|
||||
.split(layout[1])[1]
|
||||
}
|
||||
|
||||
+35
-17
@@ -10,6 +10,7 @@ use ratatui::prelude::*;
|
||||
use ratatui::widgets::{Block, BorderType, Borders, Cell, Row, TableState};
|
||||
|
||||
use super::theme::style;
|
||||
use super::{layout, span};
|
||||
|
||||
pub trait Widget<S, A> {
|
||||
fn new(state: &S, action_tx: UnboundedSender<A>) -> Self
|
||||
@@ -205,23 +206,40 @@ where
|
||||
(true, true) => Borders::LEFT | Borders::RIGHT,
|
||||
};
|
||||
|
||||
let rows = props
|
||||
.items
|
||||
.iter()
|
||||
.map(|item| Row::new(item.to_row()))
|
||||
.collect::<Vec<_>>();
|
||||
let rows = ratatui::widgets::Table::default()
|
||||
.rows(rows)
|
||||
.widths(widths)
|
||||
.column_spacing(1)
|
||||
.block(
|
||||
Block::default()
|
||||
.border_style(style::border(props.focus))
|
||||
.border_type(BorderType::Rounded)
|
||||
.borders(borders),
|
||||
)
|
||||
.highlight_style(style::highlight());
|
||||
if !props.items.is_empty() {
|
||||
let rows = props
|
||||
.items
|
||||
.iter()
|
||||
.map(|item| Row::new(item.to_row()))
|
||||
.collect::<Vec<_>>();
|
||||
let rows = ratatui::widgets::Table::default()
|
||||
.rows(rows)
|
||||
.widths(widths)
|
||||
.column_spacing(1)
|
||||
.block(
|
||||
Block::default()
|
||||
.border_style(style::border(props.focus))
|
||||
.border_type(BorderType::Rounded)
|
||||
.borders(borders),
|
||||
)
|
||||
.highlight_style(style::highlight());
|
||||
|
||||
frame.render_stateful_widget(rows, area, &mut self.state.clone());
|
||||
frame.render_stateful_widget(rows, area, &mut self.state.clone());
|
||||
} else {
|
||||
let block = Block::default()
|
||||
.border_style(style::border(props.focus))
|
||||
.border_type(BorderType::Rounded)
|
||||
.borders(borders);
|
||||
|
||||
frame.render_widget(block, area);
|
||||
|
||||
let center = layout::centered_rect(area, 50, 10);
|
||||
let hint = span::default("Nothing to show".to_string())
|
||||
.centered()
|
||||
.light_magenta()
|
||||
.dim();
|
||||
|
||||
frame.render_widget(hint, center);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user