From b2a2d2921672c42f5b43c296cbc80ef4de8a34b7 Mon Sep 17 00:00:00 2001 From: kageru Date: Mon, 20 Apr 2020 14:54:47 +0200 Subject: [PATCH] exclude pause time in calculations --- src/timesheet.rs | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/timesheet.rs b/src/timesheet.rs index f8295e0..425ff62 100644 --- a/src/timesheet.rs +++ b/src/timesheet.rs @@ -2,11 +2,7 @@ use super::listview::ListView; use itertools::Itertools; use serde::{Deserialize, Serialize}; use serde_json::from_reader; -use std::collections; -use std::default; -use std::fmt; -use std::fs; -use std::io; +use std::{collections, default, fmt, fs, io, iter}; use time::{Duration, OffsetDateTime, Time}; pub struct TimeSheet { @@ -16,6 +12,8 @@ pub struct TimeSheet { pub editing_time: bool, } +const PAUSE_TEXTS: [&str; 3] = ["lunch", "mittag", "pause"]; + #[derive(Serialize, Deserialize, Clone, Debug)] pub struct TimePoint { text: String, @@ -68,10 +66,10 @@ impl TimeSheet { &self.times[self.selected] } - pub fn time_by_tasks(&self) -> String { + fn grouped_times(&self) -> impl Iterator { self.times .iter() - .chain(std::iter::once(&TimePoint::new("end"))) + .chain(iter::once(&TimePoint::new("end"))) .tuple_windows() .map(|(prev, next)| (prev.text.clone(), next.time - prev.time)) // Fold into a map to group by description. @@ -82,21 +80,19 @@ impl TimeSheet { map }) .into_iter() - .filter(|(_, duration)| duration.whole_seconds() > 1) + .filter(|(text, _)| !PAUSE_TEXTS.contains(&text.as_str())) + } + + pub fn time_by_tasks(&self) -> String { + self.grouped_times() .map(|(text, duration)| format!("{}: {}", text, format_duration(&duration))) .join(" | ") } pub fn sum_as_str(&self) -> String { let total = self - .times - .iter() - .map(|tp| tp.time) - .chain(std::iter::once(OffsetDateTime::now_local().time())) - .tuple_windows() - .fold(Duration::zero(), |total, (last, next)| { - total + (next - last) - }); + .grouped_times() + .fold(Duration::zero(), |total, (_, d)| total + d); format_duration(&total) } }