12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- use std::collections::BTreeMap;
- use itertools::Itertools;
- use crate::data::{Root, SourceFile, Span, Spanned};
- use super::{Balance, LedgerEntry};
- fn print_ledger_entry(root: &Root, entry: &LedgerEntry, padding: usize) {
- println!(
- "{}-{:02}-{:02}: {}",
- entry.datestamp.0,
- entry.datestamp.1,
- entry.datestamp.2,
- entry.title.as_ref().map(String::as_str).unwrap_or("")
- );
- let force_show = entry.balances.iter().unique_by(|b| *b.account).count() > 1;
- for bal in &entry.balances {
- let spacer = if bal.amount.is_sign_positive() {
- " "
- } else {
- ""
- };
- if Some(bal.unit.as_ref()) == root.account_spec(*bal.account).unwrap().unit.as_ref()
- && !force_show
- {
- println!(
- " - {:padding$}: {spacer}{}",
- bal.account,
- bal.amount,
- padding = padding
- );
- } else {
- println!(
- " - {:padding$}: {spacer}{} {}",
- bal.account,
- bal.amount,
- bal.unit,
- padding = padding
- );
- }
- }
- // empty line afterwards
- println!("");
- }
- pub fn print_ledger<'l>(root: &Root, entries: impl Iterator<Item = &'l Spanned<LedgerEntry>>) {
- let mut ordering = BTreeMap::<SourceFile, BTreeMap<Span, &LedgerEntry>>::new();
- entries.for_each(|e| {
- ordering
- .entry(e.span().context)
- .or_default()
- .insert(e.span(), e.as_ref());
- });
- let Some(padding) = root.spec_root.accounts.keys().map(|k| k.len()).max() else {
- // no accounts
- return;
- };
- for (filename, entries) in ordering {
- println!(
- "==== file {} ====",
- std::path::Path::new(filename.as_ref()).display()
- );
- for (_span, le) in entries {
- print_ledger_entry(root, le, padding);
- }
- }
- }
|