Skip to content

Commit

Permalink
Show today's time since last entry
Browse files Browse the repository at this point in the history
Also refactor show_today() and show_week() into show().
  • Loading branch information
martinpitt committed Jun 12, 2022
1 parent 365c7e5 commit 770642d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
50 changes: 34 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ mod store;
use std::error::Error;
use std::io::{self, Write};

use chrono::prelude::*;

use store::Timelog;

enum TimeMode {
Expand Down Expand Up @@ -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<dyn Error>> {
Expand All @@ -62,8 +81,7 @@ fn main() -> Result<(), Box<dyn Error>> {
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,
Expand Down

0 comments on commit 770642d

Please sign in to comment.