diff options
author | Jesse Luehrs <doy@tozt.net> | 2019-08-12 02:41:34 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2019-08-12 02:41:34 -0400 |
commit | 701cf5719e7c9e4c03111a73b74c16d065ac8799 (patch) | |
tree | f841a3e01cfa17657a3db163fd0351247949f5d5 | |
parent | 3450a6aa3af8ac57e2b8931ef1d63cadccdbd44c (diff) | |
download | ynab-api-701cf5719e7c9e4c03111a73b74c16d065ac8799.tar.gz ynab-api-701cf5719e7c9e4c03111a73b74c16d065ac8799.zip |
implement multi-selection of rows
-rw-r--r-- | src/views/txn_table.rs | 58 | ||||
-rw-r--r-- | src/ynab/budget.rs | 2 | ||||
-rw-r--r-- | src/ynab/transaction.rs | 1 |
3 files changed, 54 insertions, 7 deletions
diff --git a/src/views/txn_table.rs b/src/views/txn_table.rs index 7773e30..0ad73da 100644 --- a/src/views/txn_table.rs +++ b/src/views/txn_table.rs @@ -1,18 +1,44 @@ +use cursive::view::{Identifiable, ViewWrapper}; + #[derive(Clone, Copy, Eq, Hash, PartialEq)] pub enum TxnColumn { + Selected, Date, Payee, Amount, } -type TableView = +type TxnTableView = cursive_table_view::TableView<crate::ynab::Transaction, TxnColumn>; +pub struct TableView { + view: cursive::views::OnEventView<cursive::views::IdView<TxnTableView>>, +} + +impl cursive::view::ViewWrapper for TableView { + cursive::wrap_impl!( + self.view: + cursive::views::OnEventView<cursive::views::IdView<TxnTableView>> + ); +} + +impl TableView { + pub fn len(&self) -> usize { + self.view.get_inner().with_view(|v| v.len()).unwrap() + } +} impl cursive_table_view::TableViewItem<TxnColumn> for crate::ynab::Transaction { fn to_column(&self, column: TxnColumn) -> String { match column { + TxnColumn::Selected => { + if self.selected { + "[x]".to_string() + } else { + "[ ]".to_string() + } + } TxnColumn::Date => self.date.clone(), TxnColumn::Payee => self.payee.clone(), TxnColumn::Amount => crate::ynab::format_amount(self.amount), @@ -24,6 +50,7 @@ impl cursive_table_view::TableViewItem<TxnColumn> Self: Sized, { match column { + TxnColumn::Selected => std::cmp::Ordering::Equal, TxnColumn::Date => self.date.cmp(&other.date), TxnColumn::Payee => self.payee.cmp(&other.payee), TxnColumn::Amount => self.amount.cmp(&other.amount), @@ -38,7 +65,7 @@ pub fn inflows_table(budget: &crate::ynab::Budget) -> TableView { .filter(|t| !t.reimbursed && t.amount > 0) .cloned() .collect(); - txn_table(inflows) + txn_table(inflows, "inflows_table") } pub fn outflows_table(budget: &crate::ynab::Budget) -> TableView { @@ -48,17 +75,34 @@ pub fn outflows_table(budget: &crate::ynab::Budget) -> TableView { .filter(|t| !t.reimbursed && t.amount <= 0) .cloned() .collect(); - txn_table(outflows) + txn_table(outflows, "outflows_table") } -fn txn_table(txns: Vec<crate::ynab::Transaction>) -> TableView { +fn txn_table( + txns: Vec<crate::ynab::Transaction>, + id: &'static str, +) -> TableView { let mut table = cursive_table_view::TableView::new() + .column(TxnColumn::Selected, "Sel", |c| c.width(3)) .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 + .default_column(TxnColumn::Date) + .with_id(id); + table.get_mut().set_items(txns); + let view = + cursive::views::OnEventView::new(table).on_event(' ', move |s| { + s.call_on( + &cursive::view::Selector::Id(id), + |v: &mut TxnTableView| { + if let Some(idx) = v.item() { + let txn = v.borrow_item_mut(idx).unwrap(); + txn.selected = !txn.selected; + } + }, + ); + }); + TableView { view } } diff --git a/src/ynab/budget.rs b/src/ynab/budget.rs index fba6dd0..161adb0 100644 --- a/src/ynab/budget.rs +++ b/src/ynab/budget.rs @@ -76,6 +76,7 @@ impl Budget { amount: t.amount, total_amount: t.amount, reimbursed, + selected: false, }) } } @@ -118,6 +119,7 @@ impl Budget { amount: st.amount, total_amount: t.amount, reimbursed, + selected: false, }) } } diff --git a/src/ynab/transaction.rs b/src/ynab/transaction.rs index d7903dc..326ee83 100644 --- a/src/ynab/transaction.rs +++ b/src/ynab/transaction.rs @@ -5,4 +5,5 @@ pub struct Transaction { pub amount: i64, pub total_amount: i64, pub reimbursed: bool, + pub selected: bool, } |