From cc00db276c203bdcff9ca32d857d23e5dc61f400 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yaroslav=20de=20la=20Pe=C3=B1a=20Smirnov?= Date: Tue, 8 Dec 2020 00:39:16 +0300 Subject: Change way that shared expenses are calculated The way that shared expenses are recorded and calculated has changed. Now instead of specifying how among many people the expense was divided, the names of the persons are specified. You can also specified expenses that are "owed", or in other words, things that you paid for other people, or maybe loans you made. Don't know why I didn't think of this system before. Sometimes good ideas just come at the least expected times. Also removed some unused fields. --- src/main.rs | 50 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 1dd437b..dec53ff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,7 @@ fn main() { let no_color = matches.occurrences_of("plain") > 0; let force_color = matches.occurrences_of("force-color") > 0; + let consider_owed = matches.occurrences_of("consider-owed") > 0; let input = matches.value_of("INPUT").unwrap(); let account = match budget::parse_account(input) { @@ -28,7 +29,7 @@ fn main() { ::std::process::exit(1); } }; - let maybe_calculated = budget::calculate(&account); + let maybe_calculated = budget::calculate(&account, consider_owed); if no_color && !force_color { colored::control::set_override(false); @@ -36,7 +37,7 @@ fn main() { colored::control::set_override(true); } - output(account, maybe_calculated); + output(account, maybe_calculated, consider_owed); } fn get_cli_matches() -> ArgMatches<'static> { @@ -44,6 +45,16 @@ fn get_cli_matches() -> ArgMatches<'static> { .version(crate_version!()) .author(crate_authors!()) .about(crate_description!()) + .arg( + Arg::with_name("consider-owed") + .short("w") + .long("consider-owed") + .help( + "Take into account what's owed when calculating the total \ + and subtotals." + ) + .takes_value(false) + ) .arg( Arg::with_name("plain") .short("p") @@ -60,21 +71,25 @@ fn get_cli_matches() -> ArgMatches<'static> { .help( "Forces colorized output even when piping. Takes \ precedence over --plain flag and NO_COLOR environment \ - variable", + variable.", ) .takes_value(false), ) .arg( Arg::with_name("INPUT") .help("Expenses file to calculate from. For more information \ - on the format of this file see 'man 5 finbudg'") + on the format of this file see 'man 5 finbudg'.") .required(true) .index(1), ) .get_matches() } -fn output(account: Account, maybe_calculated: Option) { +fn output( + account: Account, + maybe_calculated: Option, + consider_owed: bool, +) { println!( "{}", format!( @@ -166,20 +181,31 @@ fn output(account: Account, maybe_calculated: Option) { println!(); - for (n, owed) in calculated.total_owed.iter() { + for (person, owed) in calculated.owed.iter() { println!( - "{} person(s) owe you in shared expenses: {:.2}", - n - 1, + "{} owes you in shared expenses: {:.2}", + person, owed, ); + } - if *n > 2 { - println!("Each owes you: {}", *owed / (*n as f64 - 1.0)); + if calculated.owed.len() > 0 { + println!("In total you're owed: {:.2}", calculated.total_owed); + if consider_owed { + println!( + "Supposing you've been repaid, you should be left with: {:.2}", + calculated.balance + calculated.total_owed, + ); + } else { + println!( + "Assuming you haven't been repaid, you're left with: {:.2}", + calculated.balance - calculated.total_owed, + ); } - - println!(); } + println!(); + println!("Days until balance runs out:"); let days_left_output = format!("{:.2}", calculated.days_left,); -- cgit v1.2.3