Skip to content

Commit

Permalink
Improve stats
Browse files Browse the repository at this point in the history
  • Loading branch information
KuabeM committed Jun 1, 2024
1 parent e52f1f1 commit 7d495d0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
25 changes: 17 additions & 8 deletions src/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use chrono::Duration;
use serde::{Deserialize, Serialize};

use std::convert::TryFrom;
use std::fmt::Display;
use std::fs::{File, OpenOptions};
use std::ops::Add;
use std::path::Path;
Expand Down Expand Up @@ -59,12 +60,6 @@ impl From<DurationDef> for Duration {
}
}

impl ToString for DurationDef {
fn to_string(&self) -> String {
self.inner.to_string()
}
}

/// Wrapper around chrono::Duration for serde support
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, Copy)]
pub(crate) struct DurationDef {
Expand All @@ -73,6 +68,17 @@ pub(crate) struct DurationDef {
inner: Duration,
}

impl Display for DurationDef {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{:02}:{:02}h",
self.inner.num_hours(),
self.inner.num_minutes() % 60
)
}
}

impl Add for DurationDef {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Expand Down Expand Up @@ -182,7 +188,7 @@ impl TimeBalance {
let stop = if start.naive_local().date() != time.naive_local().date() {
println!(
"You started working on {}, do you really want to stop today? [y/N]",
start.format("%D.%M")
start.format("%d.%m.")
);
match YesNo::wait_for_decision()? {
YesNo::Yes => time,
Expand Down Expand Up @@ -628,7 +634,10 @@ mod tests {
.checked_add(&Duration::seconds(1))
.expect("adding works");
let durdef = DurationDef::from(dur);
assert_eq!(durdef.to_string(), dur.to_string());
assert_eq!(
durdef.to_string(),
format!("{}:{}h", dur.num_hours(), dur.num_minutes() % 60)
);

let dur_back = Duration::from(&durdef);
assert_eq!(dur_back, durdef.into());
Expand Down
23 changes: 10 additions & 13 deletions src/commands/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,34 +76,31 @@ fn monthly_stats(balance: &TimeBalance, year: i32, month: Month) -> Result<()> {
}
cur_w
}) {
let dur = group.fold(chrono::Duration::zero(), |dur, (_, d)| {
dur.checked_add(&d.into()).unwrap()
});
println!(
" Week {:2}: {:02}:{:02}h",
week,
dur.num_hours(),
dur.num_minutes() % 60
);
let dur: DurationDef = group
.fold(chrono::Duration::zero(), |dur, (_, d)| {
dur.checked_add(&d.into()).unwrap()
})
.into();
println!(" Week {:2}: {:02}", week, dur);
}
}
Ok(())
}

/// Print current state of started work, running and finished breaks.
fn show_state(balance: &TimeBalance) {
let break_state = balance.break_state();
let dur = if let Some((dur, start)) = balance.start_state() {
let total: DurationDef = (dur - break_state.sum).into();
println!(
"Started at {}, worked {:02}:{:02}h since then.",
"Started at {}, worked {} since then.",
start.with_timezone(&chrono::Local).format("%H:%M"),
dur.num_hours(),
dur.num_minutes() % 60
total
);
dur
} else {
Duration::zero()
};
let break_state = balance.break_state();
let break_str = break_state
.breaks
.iter()
Expand Down

0 comments on commit 7d495d0

Please sign in to comment.