tui/widgets/splits/
grid.rs1use super::{
4 COL_FROM, COL_FROM_COMM, COL_TO, COL_TO_AMOUNT, COL_TO_COMM, COL_VALUE, SplitRow, SplitsWidget,
5};
6use crate::widgets::grid::{Grid, Item};
7use crate::widgets::rows::empty_note;
8
9const TAGS_INDENT: u16 = 4;
11const ACCOUNT_MIN: u16 = 12;
12const ACCOUNT_MAX: u16 = 40;
13const COMMODITY_WIDTH: u16 = 9;
15const AMOUNT_WIDTH: u16 = 10;
16const TAG_COUNT_WIDTH: u16 = 3;
17
18impl SplitsWidget {
19 #[must_use]
24 pub fn grid(&self, active: bool, suggestion: Option<&str>, add_key: Option<&str>) -> Grid<'_> {
25 let mut grid = Grid::default();
26 for (at, row) in self.rows.iter().enumerate() {
27 let here = at == self.row_focus;
28 if here {
29 grid.focus_line = grid.lines.len();
30 }
31 grid.push_cells(0, self.row_items(at, row, active));
32 if self.tags_open && here {
33 grid.append(tag_grid(row, active, add_key), TAGS_INDENT, true);
34 if let Some(line) = suggestion {
35 grid.push_note(TAGS_INDENT, line);
36 }
37 }
38 }
39 grid
40 }
41
42 fn row_items<'a>(&self, at: usize, row: &'a SplitRow, active: bool) -> Vec<Item<'a>> {
43 let here = at == self.row_focus;
44 let focus = |col| active && !self.tags_open && here && col == self.col_focus;
45 let account = |col, select| {
46 let item = Item::select(select, focus(col)).at(at, col);
47 item.flex(ACCOUNT_MIN, ACCOUNT_MAX).tail()
48 };
49 let commodity = |col, select| {
50 let item = Item::select(select, focus(col)).at(at, col);
51 item.fixed(COMMODITY_WIDTH)
52 };
53 let amount = |col, amount: &'a crate::widgets::AmountWidget| {
54 let editor = amount.editor();
55 let item = Item::text(amount.value(), editor.cursor(), focus(col));
56 item.scrolled(editor.scroll())
57 .at(at, col)
58 .fixed(AMOUNT_WIDTH)
59 };
60 let to_commodity = row.to_commodity.value();
61 let conversion = (!to_commodity.is_empty() && row.from_commodity.value() != to_commodity)
62 || !row.to_amount.value().is_empty()
63 || (here && !self.tags_open && self.col_focus == COL_TO_AMOUNT);
64 let (arrow, to_amount) = if conversion {
65 ("→", amount(COL_TO_AMOUNT, &row.to_amount))
66 } else {
67 ("", Item::label(""))
68 };
69 vec![
70 Item::label(format!("{}.", at + 1)).frozen(),
71 Item::label(format!("#{}", row.tags.rows().len()))
72 .fixed(TAG_COUNT_WIDTH)
73 .frozen(),
74 account(COL_FROM, &row.from).frozen(),
75 Item::label("→"),
76 account(COL_TO, &row.to),
77 commodity(COL_FROM_COMM, &row.from_commodity),
78 commodity(COL_TO_COMM, &row.to_commodity),
79 amount(COL_VALUE, &row.value),
80 Item::label(arrow),
81 to_amount,
82 ]
83 }
84}
85
86fn tag_grid<'a>(row: &'a SplitRow, active: bool, add_key: Option<&str>) -> Grid<'a> {
88 let note = empty_note("tags", add_key);
89 row.tags.grid(active, None, Some(¬e)).of_tags()
90}
91
92#[cfg(test)]
93mod tests;