Skip to content

Commit 770642d

Browse files
committed
Show today's time since last entry
Also refactor show_today() and show_week() into show().
1 parent 365c7e5 commit 770642d

2 files changed

Lines changed: 38 additions & 18 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ Total work done: 11 h 3 min
1919
Total slacking: 3 h 44 min
2020
2121
22-
command (:h for help) or entry: :h
22+
0h 23 min since last entry; command (:h for help) or entry
23+
> :h
2324
2425
:w - switch to weekly mode
2526
:d - switch to daily mode
@@ -28,7 +29,8 @@ command (:h for help) or entry: :h
2829
2930
Any other input is the description of a task that you just finished.
3031
31-
command (:h for help) or entry:
32+
0h 24 min since last entry; command (:h for help) or entry
33+
>
3234
```
3335

3436
Operation

src/main.rs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ mod store;
44
use std::error::Error;
55
use std::io::{self, Write};
66

7+
use chrono::prelude::*;
8+
79
use store::Timelog;
810

911
enum TimeMode {
@@ -34,24 +36,41 @@ Any other input is the description of a task that you just finished."
3436
);
3537
}
3638

37-
fn show_today(timelog: &Timelog) {
38-
println!("Work done today:");
39-
let a = activity::Activities::new_from_entries(timelog.get_today());
40-
println!("{}", a);
41-
}
39+
fn show(timelog: &Timelog, mode: &TimeMode) {
40+
clear_screen();
41+
let entries = match mode {
42+
TimeMode::Day => {
43+
println!("Work done today:");
44+
timelog.get_today()
45+
}
46+
TimeMode::Week => {
47+
println!("Work done this week:");
48+
timelog.get_this_week()
49+
}
50+
};
4251

43-
fn show_week(timelog: &Timelog) {
44-
println!("Work done this week:");
45-
let a = activity::Activities::new_from_entries(timelog.get_this_week());
52+
let a = activity::Activities::new_from_entries(entries);
4653
println!("{}", a);
4754
}
4855

49-
fn show(timelog: &Timelog, mode: &TimeMode) {
50-
clear_screen();
51-
match mode {
52-
TimeMode::Day => show_today(timelog),
53-
TimeMode::Week => show_week(timelog),
54-
}
56+
fn show_prompt(timelog: &Timelog) -> Result<(), io::Error> {
57+
let since_last = timelog
58+
.get_today()
59+
.last()
60+
.map(|e| Local::now().naive_local().signed_duration_since(e.stop));
61+
62+
let since_str = match since_last {
63+
None => "no entries yet today".to_string(),
64+
Some(d) => format!(
65+
"{} h {} min since last entry",
66+
d.num_hours(),
67+
d.num_minutes() % 60
68+
),
69+
};
70+
71+
print!("\n{}; type command (:h for help) or entry\n> ", since_str);
72+
io::stdout().flush()?;
73+
Ok(())
5574
}
5675

5776
fn main() -> Result<(), Box<dyn Error>> {
@@ -62,8 +81,7 @@ fn main() -> Result<(), Box<dyn Error>> {
6281
show(&timelog, &time_mode);
6382

6483
while running {
65-
print!("\ncommand (:h for help) or entry: ");
66-
io::stdout().flush()?;
84+
show_prompt(&timelog)?;
6785
let input = stdin_line()?;
6886
match input.as_str() {
6987
":q" => running = false,

0 commit comments

Comments
 (0)