From 7d2e68156630af27539448a05347d6c55555c6ba Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Mon, 12 Aug 2019 00:27:25 -0400 Subject: add vi key bindings --- src/views/txn_table.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/views/txn_table.rs (limited to 'src/views') diff --git a/src/views/txn_table.rs b/src/views/txn_table.rs new file mode 100644 index 0000000..7773e30 --- /dev/null +++ b/src/views/txn_table.rs @@ -0,0 +1,64 @@ +#[derive(Clone, Copy, Eq, Hash, PartialEq)] +pub enum TxnColumn { + Date, + Payee, + Amount, +} + +type TableView = + cursive_table_view::TableView; + +impl cursive_table_view::TableViewItem + for crate::ynab::Transaction +{ + fn to_column(&self, column: TxnColumn) -> String { + match column { + TxnColumn::Date => self.date.clone(), + TxnColumn::Payee => self.payee.clone(), + TxnColumn::Amount => crate::ynab::format_amount(self.amount), + } + } + + fn cmp(&self, other: &Self, column: TxnColumn) -> std::cmp::Ordering + where + Self: Sized, + { + match column { + TxnColumn::Date => self.date.cmp(&other.date), + TxnColumn::Payee => self.payee.cmp(&other.payee), + TxnColumn::Amount => self.amount.cmp(&other.amount), + } + } +} + +pub fn inflows_table(budget: &crate::ynab::Budget) -> TableView { + let inflows = budget + .reimbursables() + .iter() + .filter(|t| !t.reimbursed && t.amount > 0) + .cloned() + .collect(); + txn_table(inflows) +} + +pub fn outflows_table(budget: &crate::ynab::Budget) -> TableView { + let outflows = budget + .reimbursables() + .iter() + .filter(|t| !t.reimbursed && t.amount <= 0) + .cloned() + .collect(); + txn_table(outflows) +} + +fn txn_table(txns: Vec) -> TableView { + let mut table = cursive_table_view::TableView::new() + .column(TxnColumn::Date, "Date", |c| c.width(10)) + .column(TxnColumn::Payee, "Payee", |c| c) + .column(TxnColumn::Amount, "Amount", |c| { + c.align(cursive::align::HAlign::Right).width(10) + }) + .default_column(TxnColumn::Date); + table.set_items(txns); + table +} -- cgit v1.2.3-54-g00ecf