From aef8e74b849c7861dce63c0e2c1d0a8bdfa379fe Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 18 Aug 2019 14:07:55 -0400 Subject: split out the full table view into a separate view --- src/main.rs | 2 +- src/views.rs | 5 ++- src/views/txn_table.rs | 93 +++++++------------------------------------------ src/views/txn_tables.rs | 80 ++++++++++++++++++++++++++++++++++++++++++ src/views/util.rs | 5 +++ 5 files changed, 103 insertions(+), 82 deletions(-) create mode 100644 src/views/txn_tables.rs create mode 100644 src/views/util.rs diff --git a/src/main.rs b/src/main.rs index 8a380e2..f7ce70f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,7 +27,7 @@ fn main() { "=".repeat(term_width), ))); - layout.add_child(views::txn_tables(&budget)); + layout.add_child(views::TxnTables::new("txn_tables", &budget)); app.set_user_data(budget); app.add_fullscreen_layer(layout); diff --git a/src/views.rs b/src/views.rs index 984a5e8..6129e53 100644 --- a/src/views.rs +++ b/src/views.rs @@ -1,2 +1,5 @@ mod txn_table; -pub use txn_table::txn_tables; +mod txn_tables; +mod util; + +pub use txn_tables::TxnTables; diff --git a/src/views/txn_table.rs b/src/views/txn_table.rs index 493d12f..43bb2c4 100644 --- a/src/views/txn_table.rs +++ b/src/views/txn_table.rs @@ -1,7 +1,7 @@ use cursive::view::{Identifiable, View, ViewWrapper}; #[derive(Clone, Copy, Eq, Hash, PartialEq)] -enum TxnColumn { +pub enum TxnColumn { Selected, Date, Account, @@ -12,18 +12,15 @@ enum TxnColumn { type TxnTableView = cursive_table_view::TableView; -struct TableView { - view: cursive::views::OnEventView>, +pub struct TxnTable { + view: super::util::FullView, } -impl cursive::view::ViewWrapper for TableView { - cursive::wrap_impl!( - self.view: - cursive::views::OnEventView> - ); +impl cursive::view::ViewWrapper for TxnTable { + cursive::wrap_impl!(self.view: super::util::FullView); } -impl TableView { +impl TxnTable { pub fn new( txns: Vec, id: &'static str, @@ -71,9 +68,12 @@ impl TableView { outflows.iter().chain(inflows.iter()).collect(); let err = budget.reconcile_transactions(&txns); if let Some(err) = err { - s.add_layer(dialog(&format!("Error: {}", err))) + s.add_layer(super::util::dialog(&format!( + "Error: {}", + err + ))) } else { - s.add_layer(dialog(&format!( + s.add_layer(super::util::dialog(&format!( "Successfully updated {} transactions", txns.len() ))); @@ -119,7 +119,7 @@ impl TableView { .unwrap(); } } else { - s.add_layer(dialog(&format!( + s.add_layer(super::util::dialog(&format!( "Selected amount is {}, must be 0", crate::ynab::format_amount(total_amount) ))) @@ -258,7 +258,7 @@ impl TableView { render_selected_total(s); }); - TableView { view } + TxnTable { view } } pub fn len(&self) -> usize { @@ -322,69 +322,6 @@ impl cursive_table_view::TableViewItem } } -fn inflows_table(budget: &crate::ynab::Budget) -> TableView { - let inflows = budget - .reimbursables() - .iter() - .filter(|t| !t.reimbursed && t.amount > 0) - .cloned() - .collect(); - TableView::new(inflows, "inflows_table") -} - -fn outflows_table(budget: &crate::ynab::Budget) -> TableView { - let outflows = budget - .reimbursables() - .iter() - .filter(|t| !t.reimbursed && t.amount <= 0) - .cloned() - .collect(); - TableView::new(outflows, "outflows_table") -} - -pub fn txn_tables(budget: &crate::ynab::Budget) -> impl cursive::view::View { - let mut layout = cursive::views::LinearLayout::vertical(); - - layout.add_child( - cursive::views::TextView::new("Selected: $0.00 (0 transactions)") - .h_align(cursive::align::HAlign::Right) - .with_id("selected_total"), - ); - - let mut inflows_table = inflows_table(&budget); - layout.add_child(cursive::views::TextView::new(format!( - "Inflows: {} ({} transaction{}", - crate::ynab::format_amount(inflows_table.amount()), - inflows_table.len(), - if inflows_table.len() == 1 { ") " } else { "s)" } - ))); - layout.add_child(cursive::views::CircularFocus::wrap_arrows( - cursive::views::BoxView::with_min_height( - std::cmp::min(std::cmp::max(inflows_table.len(), 1), 5) + 2, - cursive::views::BoxView::with_full_width(inflows_table), - ), - )); - - layout.add_child(cursive::views::TextView::new(" ")); - - let mut outflows_table = outflows_table(&budget); - layout.add_child(cursive::views::TextView::new(format!( - "Outflows: {} ({} transaction{}", - crate::ynab::format_amount(outflows_table.amount()), - outflows_table.len(), - if outflows_table.len() == 1 { - ") " - } else { - "s)" - } - ))); - layout.add_child(cursive::views::CircularFocus::wrap_arrows( - cursive::views::BoxView::with_full_screen(outflows_table), - )); - - layout -} - fn render_selected_total(s: &mut cursive::Cursive) { let outflows: Vec<_> = s .call_on_id("outflows_table", |v: &mut TxnTableView| { @@ -431,7 +368,3 @@ fn render_selected_total(s: &mut cursive::Cursive) { v.set_content(sstr); }); } - -fn dialog(s: &str) -> impl cursive::view::View { - cursive::views::Panel::new(cursive::views::Dialog::info(s)) -} diff --git a/src/views/txn_tables.rs b/src/views/txn_tables.rs new file mode 100644 index 0000000..36e535b --- /dev/null +++ b/src/views/txn_tables.rs @@ -0,0 +1,80 @@ +use cursive::view::Identifiable; + +pub struct TxnTables { + view: super::util::FullView, +} + +impl cursive::view::ViewWrapper for TxnTables { + cursive::wrap_impl!( + self.view: super::util::FullView + ); +} + +impl TxnTables { + pub fn new(id: &'static str, budget: &crate::ynab::Budget) -> Self { + let mut layout = cursive::views::LinearLayout::vertical(); + + layout.add_child( + cursive::views::TextView::new("Selected: $0.00 (0 transactions)") + .h_align(cursive::align::HAlign::Right) + .with_id("selected_total"), + ); + + let mut inflows_table = inflows_table(&budget); + layout.add_child(cursive::views::TextView::new(format!( + "Inflows: {} ({} transaction{}", + crate::ynab::format_amount(inflows_table.amount()), + inflows_table.len(), + if inflows_table.len() == 1 { ") " } else { "s)" } + ))); + layout.add_child(cursive::views::CircularFocus::wrap_arrows( + cursive::views::BoxView::with_min_height( + std::cmp::min(std::cmp::max(inflows_table.len(), 1), 5) + 2, + cursive::views::BoxView::with_full_width(inflows_table), + ), + )); + + layout.add_child(cursive::views::TextView::new(" ")); + + let mut outflows_table = outflows_table(&budget); + layout.add_child(cursive::views::TextView::new(format!( + "Outflows: {} ({} transaction{}", + crate::ynab::format_amount(outflows_table.amount()), + outflows_table.len(), + if outflows_table.len() == 1 { + ") " + } else { + "s)" + } + ))); + layout.add_child(cursive::views::CircularFocus::wrap_arrows( + cursive::views::BoxView::with_full_screen(outflows_table), + )); + + TxnTables { + view: cursive::views::OnEventView::new(layout.with_id(id)), + } + } +} + +fn inflows_table(budget: &crate::ynab::Budget) -> super::txn_table::TxnTable { + let inflows = budget + .reimbursables() + .iter() + .filter(|t| !t.reimbursed && t.amount > 0) + .cloned() + .collect(); + super::txn_table::TxnTable::new(inflows, "inflows_table") +} + +fn outflows_table( + budget: &crate::ynab::Budget, +) -> super::txn_table::TxnTable { + let outflows = budget + .reimbursables() + .iter() + .filter(|t| !t.reimbursed && t.amount <= 0) + .cloned() + .collect(); + super::txn_table::TxnTable::new(outflows, "outflows_table") +} diff --git a/src/views/util.rs b/src/views/util.rs new file mode 100644 index 0000000..1dec69a --- /dev/null +++ b/src/views/util.rs @@ -0,0 +1,5 @@ +pub type FullView = cursive::views::OnEventView>; + +pub fn dialog(s: &str) -> impl cursive::view::View { + cursive::views::Panel::new(cursive::views::Dialog::info(s)) +} -- cgit v1.2.3-54-g00ecf