aboutsummaryrefslogtreecommitdiffstats
path: root/src/table.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/table.rs')
-rw-r--r--src/table.rs64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/table.rs b/src/table.rs
new file mode 100644
index 0000000..531d1e8
--- /dev/null
+++ b/src/table.rs
@@ -0,0 +1,64 @@
+#[derive(Clone, Copy, Eq, Hash, PartialEq)]
+pub enum TxnColumn {
+ Date,
+ Payee,
+ Amount,
+}
+
+type TableView =
+ cursive_table_view::TableView<super::ynab::Transaction, TxnColumn>;
+
+impl cursive_table_view::TableViewItem<TxnColumn>
+ 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<super::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
+}