From 10d6b82be501f219eb35b1cd94e1f0d461e9e165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sat, 31 Jul 2021 02:35:39 +0200 Subject: simple sorting --- cli/src/main.rs | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) (limited to 'cli/src') diff --git a/cli/src/main.rs b/cli/src/main.rs index 254a5e2..c7b300b 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -44,6 +44,10 @@ enum Command { }, #[structopt(setting = AppSettings::AllowLeadingHyphen)] Show { + //TODO multiple sorts (sort iequals with later sorts) + #[structopt(long)] + sort: Option, + filters: Vec, }, } @@ -65,6 +69,25 @@ impl std::str::FromStr for ListTarget { } } +#[derive(Debug)] +#[derive(StructOpt)] +enum SortTarget { + Amount, + Date, //TODO ? +} + +impl std::str::FromStr for SortTarget { + type Err = String; + + fn from_str(s: &str) -> Result { + match s { + "amount" => Ok(SortTarget::Amount), + "date" => Ok(SortTarget::Date), + _ => Err(format!("Unknown sort target: {:?}", s)), + } + } +} + #[derive(Debug)] #[derive(StructOpt)] struct Mn { @@ -109,14 +132,18 @@ fn main() { println!("{}", store.categories().join("\n")); } Command::Show { - filters + sort, + filters, } => { let mut search = Search::new(store.transactions()); if !filters.is_empty() { search = search.parse(filters.join(" ")); } let mut transactions = search.get(); - transactions.sort_by(|t1, t2| t1.date.cmp(&t2.date)); + transactions.sort_by(|t1, t2| match sort { + Some(SortTarget::Amount) => t1.amount.cmp(&t2.amount), + Some(SortTarget::Date) | None => t1.date.cmp(&t2.date), + }); println!("{}", Table::new(transactions).with(Style::psql())); } } -- cgit v1.2.1