From 701cf5719e7c9e4c03111a73b74c16d065ac8799 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Mon, 12 Aug 2019 02:41:34 -0400 Subject: implement multi-selection of rows --- src/views/txn_table.rs | 58 +++++++++++++++++++++++++++++++++++++++++++------ src/ynab/budget.rs | 2 ++ 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; +pub struct TableView { + view: cursive::views::OnEventView>, +} + +impl cursive::view::ViewWrapper for TableView { + cursive::wrap_impl!( + self.view: + cursive::views::OnEventView> + ); +} + +impl TableView { + pub fn len(&self) -> usize { + self.view.get_inner().with_view(|v| v.len()).unwrap() + } +} impl cursive_table_view::TableViewItem 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 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) -> TableView { +fn txn_table( + txns: Vec, + 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, } -- cgit v1.2.3-54-g00ecf