Skip to main content

rust_ynab/
display.rs

1use crate::Transaction;
2use crate::milliunits_to_amount;
3
4fn truncate(s: &str, max: usize) -> String {
5    if s.chars().count() > max {
6        format!("{}...", &s[..max - 3])
7    } else {
8        s.to_string()
9    }
10}
11
12pub fn print_transaction_table(transactions: &[Transaction]) {
13    println!(
14        "{:<12}  {:<20}  {:<25}  {:>13}",
15        "Date", "Account", "Payee", "Amount"
16    );
17    println!(
18        "{:<12}  {:<20}  {:<25}  {:>13}",
19        "------------", "--------------------", "-------------------------", "-------------"
20    );
21    for tx in transactions {
22        let account = tx
23            .account_name
24            .as_deref()
25            .map(|s| truncate(s, 20))
26            .unwrap_or_default();
27        let payee = tx
28            .payee_name
29            .as_deref()
30            .map(|s| truncate(s, 25))
31            .unwrap_or_default();
32        println!(
33            "{:<12}  {:<20}  {:<25}  {:>13}",
34            tx.date,
35            account,
36            payee,
37            format!("${:.2}", milliunits_to_amount(tx.amount))
38        );
39    }
40}