|
@@ -2,7 +2,10 @@ use std::fmt::Display;
|
|
|
|
|
|
use console::Style;
|
|
|
|
|
|
-use crate::data::{AccountName, ledger::{Transaction,Change}};
|
|
|
+use crate::data::{
|
|
|
+ AccountName, Decimal,
|
|
|
+ ledger::{Change, Transaction},
|
|
|
+};
|
|
|
|
|
|
#[derive(Clone, Copy, Default)]
|
|
|
enum Align {
|
|
@@ -31,64 +34,89 @@ fn show_table<'d>(cols: Vec<Column>, rows: impl Iterator<Item = Row>) {
|
|
|
let table_data = rows.collect::<Vec<_>>();
|
|
|
for row in &table_data {
|
|
|
let Row::Data(row) = &row else { continue };
|
|
|
- for (idx,rc) in row.iter().enumerate() {
|
|
|
+ for (idx, rc) in row.iter().enumerate() {
|
|
|
min_widths[idx] = min_widths[idx].max(console::measure_text_width(rc.as_str()));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
for row in table_data {
|
|
|
match row {
|
|
|
- Row::Line => for (idx, col) in min_widths.iter().enumerate() {
|
|
|
- if cols[idx].left_border {
|
|
|
- print!("+");
|
|
|
+ Row::Line => {
|
|
|
+ for (idx, col) in min_widths.iter().enumerate() {
|
|
|
+ if cols[idx].left_border {
|
|
|
+ print!("{}", boxy::Char::cross(boxy::Weight::Normal));
|
|
|
+ }
|
|
|
+ for _ in 0..=*col {
|
|
|
+ print!("{}", boxy::Char::horizontal(boxy::Weight::Normal));
|
|
|
+ }
|
|
|
+ // print!("{:-width$}", "", width = col + 1);
|
|
|
+ if cols[idx].right_border {
|
|
|
+ print!("{}", boxy::Char::cross(boxy::Weight::Normal));
|
|
|
+ }
|
|
|
}
|
|
|
- print!("{:-width$}", "", width = col + 1);
|
|
|
- if cols[idx].right_border {
|
|
|
- print!("+");
|
|
|
+ }
|
|
|
+ Row::Data(row) => {
|
|
|
+ for (idx, col) in row.into_iter().enumerate() {
|
|
|
+ if cols[idx].left_border {
|
|
|
+ print!("{}", boxy::Char::vertical(boxy::Weight::Normal));
|
|
|
+ }
|
|
|
+ match cols[idx].align {
|
|
|
+ Align::Left => print!("{col:<width$}", width = min_widths[idx] + 1),
|
|
|
+ Align::Centre => print!("{col:^width$}", width = min_widths[idx] + 1),
|
|
|
+ Align::Right => print!("{col:>width$}", width = min_widths[idx] + 1),
|
|
|
+ }
|
|
|
+ if cols[idx].right_border {
|
|
|
+ print!("{}", boxy::Char::vertical(boxy::Weight::Normal));
|
|
|
+ }
|
|
|
}
|
|
|
- },
|
|
|
- Row::Data(row) => for (idx, col) in row.into_iter().enumerate() {
|
|
|
- if cols[idx].left_border {
|
|
|
- print!("|");
|
|
|
- }
|
|
|
- match cols[idx].align {
|
|
|
- Align::Left => print!("{col:<width$}", width = min_widths[idx] + 1),
|
|
|
- Align::Centre => print!("{col:^width$}", width = min_widths[idx] + 1),
|
|
|
- Align::Right => print!("{col:>width$}", width = min_widths[idx] + 1),
|
|
|
- }
|
|
|
- if cols[idx].right_border {
|
|
|
- print!("|");
|
|
|
- }
|
|
|
- },
|
|
|
+ }
|
|
|
}
|
|
|
println!();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
#[derive(Clone, Copy, Default)]
|
|
|
-pub struct TransactionTable {
|
|
|
-}
|
|
|
+pub struct TransactionTable {}
|
|
|
|
|
|
impl TransactionTable {
|
|
|
pub fn show<'d>(self, account: AccountName, txns: impl Iterator<Item = &'d Transaction>) {
|
|
|
show_table(
|
|
|
vec![
|
|
|
// datestamp
|
|
|
- Column { align: Align::Left, left_border: false, right_border: true },
|
|
|
+ Column {
|
|
|
+ align: Align::Left,
|
|
|
+ right_border: true,
|
|
|
+ ..Default::default()
|
|
|
+ },
|
|
|
// Memo
|
|
|
- Column { align: Align::Left, ..Default::default() },
|
|
|
+ Column {
|
|
|
+ align: Align::Left,
|
|
|
+ ..Default::default()
|
|
|
+ },
|
|
|
// Amount
|
|
|
- Column { align: Align::Right, ..Default::default() },
|
|
|
+ Column {
|
|
|
+ align: Align::Right,
|
|
|
+ ..Default::default()
|
|
|
+ },
|
|
|
+ // Balance
|
|
|
+ Column {
|
|
|
+ align: Align::Right,
|
|
|
+ left_border: true,
|
|
|
+ ..Default::default()
|
|
|
+ },
|
|
|
],
|
|
|
- txns
|
|
|
- .filter_map(|txn| txn.change_for(account).map(|chg| (txn, chg)))
|
|
|
+ txns.filter_map(|txn| txn.change_for(account).map(|chg| (txn, chg)))
|
|
|
.map(|(txn, chg)| {
|
|
|
Row::Data(vec![
|
|
|
txn.datestamp.to_string(),
|
|
|
txn.title.clone().unwrap_or_else(String::new),
|
|
|
- format!("{}", chg.amount)
|
|
|
+ format!("{}", chg.amount),
|
|
|
+ chg.balance
|
|
|
+ .as_deref()
|
|
|
+ .map(Decimal::to_string)
|
|
|
+ .unwrap_or(String::new()),
|
|
|
])
|
|
|
- })
|
|
|
+ }),
|
|
|
)
|
|
|
}
|
|
|
}
|