From 77ba311babe0877868934629da24787ff5fb79c1 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Wed, 21 Aug 2024 10:14:01 +0300 Subject: [PATCH] feat(main): neatly interpret plain hour in date filter --- src/helpers.rs | 20 ++++++++++++++++++-- src/main.rs | 25 ++++++++++++++----------- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/helpers.rs b/src/helpers.rs index 070df11..7cafdf7 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -1,7 +1,7 @@ -use std::fmt::Display; use std::io::{stdin, stdout, Write}; +use std::ops::Sub; -use chrono::{DateTime, Local, NaiveDateTime, TimeZone, Utc}; +use chrono::{DateTime, Local, TimeDelta, TimeZone, Utc}; use chrono::LocalResult::Single; use log::{debug, error, info, trace, warn}; use nostr_sdk::Timestamp; @@ -19,6 +19,22 @@ pub fn prompt(prompt: &str) -> Option { } } +/// Parses the hour from a plain number in the String, +/// with max of max_future hours into the future. +pub fn parse_hour(str: &str, max_future: i64) -> Option> { + str.parse::().ok().and_then(|hour| { + let now = Local::now(); + #[allow(deprecated)] + now.date().and_hms_opt(hour, 0, 0).map(|time| { + if time - now > TimeDelta::hours(max_future) { + time.sub(TimeDelta::days(1)) + } else { + time + } + }) + }) +} + pub fn parse_date(str: &str) -> Option> { // Using two libraries for better exhaustiveness, see https://github.com/uutils/parse_datetime/issues/84 match interim::parse_date_string(str, Local::now(), interim::Dialect::Us) { diff --git a/src/main.rs b/src/main.rs index 68c8fc1..f8d1eaa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,7 @@ use std::path::PathBuf; use std::str::FromStr; use std::time::Duration; +use chrono::Local; use colored::Colorize; use env_logger::{Builder, Target, WriteStyle}; use itertools::Itertools; @@ -27,7 +28,7 @@ use xdg::BaseDirectories; use crate::helpers::*; use crate::kinds::{KINDS, PROP_KINDS, PROPERTY_COLUMNS, TRACKING_KIND}; -use crate::task::{MARKER_DEPENDS, MARKER_PARENT, State}; +use crate::task::{MARKER_DEPENDS, State}; use crate::tasks::{PropertyCollection, StateFilter, Tasks}; mod helpers; @@ -403,7 +404,7 @@ async fn main() { match arg { None => { let today = Timestamp::from(Timestamp::now() - 80_000); - info!("Filtering for tasks from the last 22 hours"); + info!("Filtering for tasks created in the last 22 hours"); tasks.set_filter( tasks.filtered_tasks(tasks.get_position_ref()) .filter(|t| t.event.created_at > today) @@ -431,15 +432,17 @@ async fn main() { .collect() ) } else { - parse_date(arg).map(|time| { - info!("Filtering for tasks from {}", time); // TODO localize - tasks.set_filter( - tasks.filtered_tasks(tasks.get_position_ref()) - .filter(|t| t.event.created_at.as_u64() as i64 > time.timestamp()) - .map(|t| t.event.id) - .collect() - ); - }); + parse_hour(arg, 1) + .or_else(|| parse_date(arg).map(|utc| utc.with_timezone(&Local))) + .map(|time| { + info!("Filtering for tasks created after {}", time); + tasks.set_filter( + tasks.filtered_tasks(tasks.get_position_ref()) + .filter(|t| t.event.created_at.as_u64() as i64 > time.to_utc().timestamp()) + .map(|t| t.event.id) + .collect() + ); + }); } } }