-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathui.rs
More file actions
87 lines (81 loc) · 2.81 KB
/
ui.rs
File metadata and controls
87 lines (81 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use super::{app::App, data::DigitiserData};
use ratatui::{
Frame,
prelude::{Alignment, Backend, Constraint, Direction, Layout, Rect},
style::{Color, Modifier, Style},
text::Text,
widgets::{Block, Borders, Cell, Paragraph, Row, Table},
};
/// Draws the ui based on the current app state.
pub(crate) fn ui<B: Backend>(frame: &mut Frame<B>, app: &mut App) {
// Split terminal into different-sized chunks.
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Min(1), Constraint::Length(3)].as_ref())
.split(frame.size());
// Draw all widgets.
draw_table(frame, app, chunks[0]);
draw_help(frame, chunks[1]);
}
/// Draws a help box containing key binding information in a given chunk.
fn draw_help<B: Backend>(frame: &mut Frame<B>, chunk: Rect) {
let help = Paragraph::new(Text::styled(
"<UP>: Previous row | <DOWN>: Next row | <LEFT>: Previous Channel | <RIGHT>: Next Channel | <q>: Quit",
Style::default().add_modifier(Modifier::DIM),
))
.alignment(Alignment::Center)
.block(
Block::default()
.borders(Borders::ALL)
.style(Style::default())
.title("Help"),
);
frame.render_widget(help, chunk)
}
/// Draws the main table in a given chunk.
fn draw_table<B: Backend>(frame: &mut Frame<B>, app: &mut App, chunk: Rect) {
let widths: Vec<Constraint> = DigitiserData::width_percentages()
.into_iter()
.map(Constraint::Percentage)
.collect();
let table = Table::new(
// Turn table data into rows with given formatting.
app.table_body.iter().map(|item| {
// Calculate height based on line count.
let height = item
.iter()
.map(|content| content.chars().filter(|c| *c == '\n').count())
.max()
.unwrap_or(0)
+ 1;
// Apply formatting to each cell.
let cells = item.iter().map(|c| Cell::from(c.clone()));
Row::new(cells).height(height as u16).bottom_margin(1)
}),
)
// Add table headers with given formatting.
.header(
Row::new(
app.table_headers
.iter()
.map(|h| Cell::from(h.clone().replace(' ', "\n"))),
)
.style(
Style::default()
.add_modifier(Modifier::BOLD)
.add_modifier(Modifier::REVERSED),
)
.height(3)
.bottom_margin(2),
)
// Modify table style.
.widths(&widths)
.column_spacing(1)
.highlight_style(
Style::default()
.fg(Color::LightMagenta)
.add_modifier(Modifier::BOLD),
)
.block(Block::default().borders(Borders::ALL));
frame.render_stateful_widget(table, chunk, &mut app.table_state);
}