From 5e832956db90771451f6564960fda6c77afe5d33 Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Mon, 8 Apr 2024 21:29:13 -0600 Subject: [PATCH] impl -d 1h2m3s to modify duration of an add --- src/main.rs | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7f3d64e..5beda5d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,25 +42,28 @@ fn main() { let mut flags = Flags::parse(); flags.log = flags.log || flags.since.is_some(); - clock(&flags.f, &flags.clock).unwrap(); - add(&flags.f, &flags.add, &flags.tag).unwrap(); + let duration = parse_duration(&flags.duration).unwrap(); + + clock(&flags.f, &(flags.clock || flags.duration.is_some()), &duration).unwrap(); + add(&flags.f, &flags.add, &flags.tag, &0).unwrap(); log(&flags.f, &flags.log, &flags.since, &flags.verbose, &flags.precision.unwrap_or(0)).unwrap(); } -fn clock(f: &String, clock: &bool) -> Result<(), String> { +fn clock(f: &String, clock: &bool, duration: &u64) -> Result<(), String> { match clock { - true => add(&f, &Some("".to_string()), &None), + true => add(&f, &Some("".to_string()), &None, duration), false => Ok(()), } } -fn add(f: &String, x: &Option, tag: &Option) -> Result<(), String> { +fn add(f: &String, x: &Option, tag: &Option, duration: &u64) -> Result<(), String> { match x { Some(x) => { let mut tsheet = load(&f)?; tsheet.add( x.to_string(), tag.clone().unwrap_or("".to_string()), + *duration, ); save(&f, tsheet)?; }, @@ -315,8 +318,13 @@ impl TSheet { result } - fn add(&mut self, x: String, tag: String) { - self.xs.push(new_x(SystemTime::now(), x, tag)); + fn add(&mut self, x: String, tag: String, duration: u64) { + let now = system_time_to_unix_seconds(&SystemTime::now()); + self.xs.push(new_x( + now - duration as i64, + x, + tag, + )); } fn sorted(&self) -> TSheet { @@ -345,9 +353,9 @@ fn timestamp(t: &i64) -> String { dt.format("%Y-%m-%d").to_string() } -fn new_x(t: SystemTime, x: String, tag: String) -> X { +fn new_x(t: i64, x: String, tag: String) -> X { X{ - t: system_time_to_unix_seconds(&t), + t: t, x: x, tag: tag, } @@ -362,7 +370,7 @@ mod test_tsheet { let mut given = TSheet{xs: vec![ X{t: 1, x: "def".to_string(), tag: "abc".to_string()}, ]}; - given.add("ghi".to_string(), "".to_string()); + given.add("ghi".to_string(), "".to_string(), 0); assert_eq!(given.xs.len(), 2); assert!(given.xs[1].t != 1); assert_eq!(given.xs[1].x, "ghi".to_string());