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/main.rs | 22 ++++++++++------- src/table.rs | 64 -------------------------------------------------- src/views.rs | 18 ++++++++++++++ src/views/txn_table.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 73 deletions(-) delete mode 100644 src/table.rs create mode 100644 src/views.rs create mode 100644 src/views/txn_table.rs diff --git a/src/main.rs b/src/main.rs index 7d94fe9..358a842 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ mod checks; mod display; -mod table; +mod views; mod ynab; fn main() { @@ -21,17 +21,21 @@ fn main() { budget.id() ))); - let inflows_table = table::inflows_table(&budget); - 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), + let inflows_table = views::inflows_table(&budget); + layout.add_child(views::vi_view( + 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), + ), ), )); - let outflows_table = table::outflows_table(&budget); - layout.add_child(cursive::views::CircularFocus::wrap_arrows( - cursive::views::BoxView::with_full_screen(outflows_table), + let outflows_table = views::outflows_table(&budget); + layout.add_child(views::vi_view( + cursive::views::CircularFocus::wrap_arrows( + cursive::views::BoxView::with_full_screen(outflows_table), + ), )); app.add_fullscreen_layer(layout); diff --git a/src/table.rs b/src/table.rs deleted file mode 100644 index 531d1e8..0000000 --- a/src/table.rs +++ /dev/null @@ -1,64 +0,0 @@ -#[derive(Clone, Copy, Eq, Hash, PartialEq)] -pub enum TxnColumn { - Date, - Payee, - Amount, -} - -type TableView = - cursive_table_view::TableView; - -impl cursive_table_view::TableViewItem - for super::ynab::Transaction -{ - fn to_column(&self, column: TxnColumn) -> String { - match column { - TxnColumn::Date => self.date.clone(), - TxnColumn::Payee => self.payee.clone(), - TxnColumn::Amount => super::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: &super::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: &super::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 -} diff --git a/src/views.rs b/src/views.rs new file mode 100644 index 0000000..b6846b8 --- /dev/null +++ b/src/views.rs @@ -0,0 +1,18 @@ +mod txn_table; +pub use txn_table::{inflows_table, outflows_table}; + +pub fn vi_view(v: V) -> impl cursive::view::View { + cursive::views::OnEventView::new(v) + .on_event('h', |s| { + s.on_event(cursive::event::Event::Key(cursive::event::Key::Left)) + }) + .on_event('j', |s| { + s.on_event(cursive::event::Event::Key(cursive::event::Key::Down)) + }) + .on_event('k', |s| { + s.on_event(cursive::event::Event::Key(cursive::event::Key::Up)) + }) + .on_event('l', |s| { + s.on_event(cursive::event::Event::Key(cursive::event::Key::Right)) + }) +} 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