diff options
author | Jesse Luehrs <doy@tozt.net> | 2019-08-12 00:27:25 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2019-08-12 00:27:25 -0400 |
commit | 7d2e68156630af27539448a05347d6c55555c6ba (patch) | |
tree | 96334dd0bc345efef5b8243c8423dcada7f1b67c /src/views/txn_table.rs | |
parent | ca12c28f744c34b27c57dd832ac5f713eff61ab4 (diff) | |
download | ynab-api-7d2e68156630af27539448a05347d6c55555c6ba.tar.gz ynab-api-7d2e68156630af27539448a05347d6c55555c6ba.zip |
add vi key bindings
Diffstat (limited to 'src/views/txn_table.rs')
-rw-r--r-- | src/views/txn_table.rs | 64 |
1 files changed, 64 insertions, 0 deletions
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<crate::ynab::Transaction, TxnColumn>; + +impl cursive_table_view::TableViewItem<TxnColumn> + 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<crate::ynab::Transaction>) -> 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 +} |