From 770642d83ce78768f3dc877a359fc26c4d80b2a2 Mon Sep 17 00:00:00 2001 From: Martin Pitt Date: Sun, 12 Jun 2022 13:29:33 +0200 Subject: [PATCH] Show today's time since last entry Also refactor show_today() and show_week() into show(). --- README.md | 6 ++++-- src/main.rs | 50 ++++++++++++++++++++++++++++++++++---------------- 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 76aa40e..955b220 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,8 @@ Total work done: 11 h 3 min Total slacking: 3 h 44 min -command (:h for help) or entry: :h +0h 23 min since last entry; command (:h for help) or entry +> :h :w - switch to weekly mode :d - switch to daily mode @@ -28,7 +29,8 @@ command (:h for help) or entry: :h Any other input is the description of a task that you just finished. -command (:h for help) or entry: +0h 24 min since last entry; command (:h for help) or entry +> ``` Operation diff --git a/src/main.rs b/src/main.rs index d22fda5..f8019fb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,8 @@ mod store; use std::error::Error; use std::io::{self, Write}; +use chrono::prelude::*; + use store::Timelog; enum TimeMode { @@ -34,24 +36,41 @@ Any other input is the description of a task that you just finished." ); } -fn show_today(timelog: &Timelog) { - println!("Work done today:"); - let a = activity::Activities::new_from_entries(timelog.get_today()); - println!("{}", a); -} +fn show(timelog: &Timelog, mode: &TimeMode) { + clear_screen(); + let entries = match mode { + TimeMode::Day => { + println!("Work done today:"); + timelog.get_today() + } + TimeMode::Week => { + println!("Work done this week:"); + timelog.get_this_week() + } + }; -fn show_week(timelog: &Timelog) { - println!("Work done this week:"); - let a = activity::Activities::new_from_entries(timelog.get_this_week()); + let a = activity::Activities::new_from_entries(entries); println!("{}", a); } -fn show(timelog: &Timelog, mode: &TimeMode) { - clear_screen(); - match mode { - TimeMode::Day => show_today(timelog), - TimeMode::Week => show_week(timelog), - } +fn show_prompt(timelog: &Timelog) -> Result<(), io::Error> { + let since_last = timelog + .get_today() + .last() + .map(|e| Local::now().naive_local().signed_duration_since(e.stop)); + + let since_str = match since_last { + None => "no entries yet today".to_string(), + Some(d) => format!( + "{} h {} min since last entry", + d.num_hours(), + d.num_minutes() % 60 + ), + }; + + print!("\n{}; type command (:h for help) or entry\n> ", since_str); + io::stdout().flush()?; + Ok(()) } fn main() -> Result<(), Box> { @@ -62,8 +81,7 @@ fn main() -> Result<(), Box> { show(&timelog, &time_mode); while running { - print!("\ncommand (:h for help) or entry: "); - io::stdout().flush()?; + show_prompt(&timelog)?; let input = stdin_line()?; match input.as_str() { ":q" => running = false,