impl -d 1h2m3s to modify duration of an add

main v0.0.3
Bel LaPointe 2024-04-08 21:29:13 -06:00
parent ad22e13ca3
commit 5e832956db
1 changed files with 18 additions and 10 deletions

View File

@ -42,25 +42,28 @@ fn main() {
let mut flags = Flags::parse(); let mut flags = Flags::parse();
flags.log = flags.log || flags.since.is_some(); flags.log = flags.log || flags.since.is_some();
clock(&flags.f, &flags.clock).unwrap(); let duration = parse_duration(&flags.duration).unwrap();
add(&flags.f, &flags.add, &flags.tag).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(); 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 { match clock {
true => add(&f, &Some("".to_string()), &None), true => add(&f, &Some("".to_string()), &None, duration),
false => Ok(()), false => Ok(()),
} }
} }
fn add(f: &String, x: &Option<String>, tag: &Option<String>) -> Result<(), String> { fn add(f: &String, x: &Option<String>, tag: &Option<String>, duration: &u64) -> Result<(), String> {
match x { match x {
Some(x) => { Some(x) => {
let mut tsheet = load(&f)?; let mut tsheet = load(&f)?;
tsheet.add( tsheet.add(
x.to_string(), x.to_string(),
tag.clone().unwrap_or("".to_string()), tag.clone().unwrap_or("".to_string()),
*duration,
); );
save(&f, tsheet)?; save(&f, tsheet)?;
}, },
@ -315,8 +318,13 @@ impl TSheet {
result result
} }
fn add(&mut self, x: String, tag: String) { fn add(&mut self, x: String, tag: String, duration: u64) {
self.xs.push(new_x(SystemTime::now(), x, tag)); let now = system_time_to_unix_seconds(&SystemTime::now());
self.xs.push(new_x(
now - duration as i64,
x,
tag,
));
} }
fn sorted(&self) -> TSheet { fn sorted(&self) -> TSheet {
@ -345,9 +353,9 @@ fn timestamp(t: &i64) -> String {
dt.format("%Y-%m-%d").to_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{ X{
t: system_time_to_unix_seconds(&t), t: t,
x: x, x: x,
tag: tag, tag: tag,
} }
@ -362,7 +370,7 @@ mod test_tsheet {
let mut given = TSheet{xs: vec![ let mut given = TSheet{xs: vec![
X{t: 1, x: "def".to_string(), tag: "abc".to_string()}, 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_eq!(given.xs.len(), 2);
assert!(given.xs[1].t != 1); assert!(given.xs[1].t != 1);
assert_eq!(given.xs[1].x, "ghi".to_string()); assert_eq!(given.xs[1].x, "ghi".to_string());