From 8dcdd9d9ddb624dbce98ee9e1b53a2988e7c7fce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Wed, 4 Aug 2021 21:42:01 +0200 Subject: add week bucket --- cli/src/search.rs | 16 ++++++++++++++++ cli/todo | 2 -- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/cli/src/search.rs b/cli/src/search.rs index 47726a3..fb7f5dc 100644 --- a/cli/src/search.rs +++ b/cli/src/search.rs @@ -1,3 +1,4 @@ +use chrono::Datelike; use chrono::{naive::NaiveDate, Duration}; use nom::branch::alt; use nom::bytes::complete::{is_not, tag, take}; @@ -139,6 +140,10 @@ impl Constraint { preceded(tag("amount:"), parse_comparison), |(amount, comparison)| Constraint::AmountCompare(amount, comparison), ), + map( + tag("week"), + |_| Constraint::Filters(this_week()), + ), map( delimited(char('('), separated_list0(space1, Filter::parse), char(')')), Constraint::Filters, @@ -147,6 +152,17 @@ impl Constraint { } } +fn this_week() -> Vec { + let today = chrono::Local::today().naive_utc(); + let to_last_monday = Duration::days(today.weekday().num_days_from_monday() as i64); + let last_monday = today - to_last_monday; + let next_monday = last_monday + Duration::weeks(1); + vec![ + Filter::Intersect(Constraint::After(DateIsh::Absolute(last_monday))), + Filter::Intersect(Constraint::Before(DateIsh::Absolute(next_monday))), + ] +} + fn string(i: &str) -> nom::IResult<&str, &str> { alt(( delimited(char('"'), is_not("\""), char('"')), diff --git a/cli/todo b/cli/todo index 46ef985..b4e2cff 100644 --- a/cli/todo +++ b/cli/todo @@ -3,8 +3,6 @@ search / display / count stores > handle to/from differently for expense/income? expense/income - time frames - buckets (this week) group by category by time frame -- cgit v1.2.1