1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
use cursive::view::{Identifiable, ViewWrapper};
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
pub enum TxnColumn {
Selected,
Date,
Payee,
Amount,
}
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),
}
}
fn cmp(&self, other: &Self, column: TxnColumn) -> std::cmp::Ordering
where
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),
}
}
}
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, "inflows_table")
}
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, "outflows_table")
}
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)
.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 }
}
|