From 84f13d18068a37c6eaabeedcc8113f4e11531f4c Mon Sep 17 00:00:00 2001 From: kageru Date: Sun, 19 Apr 2020 19:04:27 +0200 Subject: [PATCH] clean up time grouping --- Cargo.toml | 3 ++- src/timesheet.rs | 38 ++++++++++++-------------------------- src/tracc.rs | 24 ++++++++++-------------- 3 files changed, 24 insertions(+), 41 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 465d42b..8473af5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" tui = "0.8.0" termion = "1.5" serde_json = "1" +itertools = "0.9" serde = { version = "1", features = ["derive"] } time = { version = "0.2.9", features = ["serde"] } -itertools = "0.9" +#cmd_lib = "0.7.8" diff --git a/src/timesheet.rs b/src/timesheet.rs index ed80e6b..f4807c3 100644 --- a/src/timesheet.rs +++ b/src/timesheet.rs @@ -75,34 +75,20 @@ impl TimeSheet { } pub fn time_by_tasks(&self) -> String { - let mut time_by_task = std::collections::HashMap::new(); - let durations = self - .times - //.iter() - //.tuple_windows() - .windows(2) - .map(|ts| { - let prev = &ts[0]; - let next = &ts[1]; - let diff = next.time - prev.time; - (prev.text.clone(), diff) - }); - //.fold( - //std::collections::HashMap::new(), - //|mut map, (text, duration)| { - // *map.entry(text).or_insert(time::Duration::zero()) += duration; - // map - //}, - //); - for (text, duration) in durations { - *time_by_task.entry(text).or_insert(time::Duration::zero()) += duration; - } - let mut times: Vec<_> = time_by_task + self.times + .iter() + .tuple_windows() + .map(|(prev, next)| (prev.text.clone(), next.time - prev.time)) + .fold( + std::collections::BTreeMap::new(), + |mut map, (text, duration)| { + *map.entry(text).or_insert(time::Duration::zero()) += duration; + map + }, + ) .into_iter() .map(|(text, duration)| format!("{}: {}", text, format_duration(&duration))) - .collect(); - times.sort(); - times.join("; ") + .join(" | ") } pub fn sum_as_str(&self) -> String { diff --git a/src/tracc.rs b/src/tracc.rs index 8376d3a..336d413 100644 --- a/src/tracc.rs +++ b/src/tracc.rs @@ -1,4 +1,4 @@ -use super::timesheet::{TimePoint, TimeSheet}; +use super::timesheet::TimeSheet; use super::todolist::TodoList; use std::default::Default; use std::fmt; @@ -70,12 +70,7 @@ impl Tracc { } Key::Char('a') | Key::Char('A') => self.set_mode(Mode::Insert)?, Key::Char(' ') => with_focused!(ListView::toggle_current), - // dd - Key::Char('d') => { - if let Key::Char('d') = inputs.next().unwrap()? { - with_focused!(ListView::remove_current) - } - } + Key::Char('d') => with_focused!(ListView::remove_current), Key::Char('p') => with_focused!(ListView::paste), Key::Char('\t') => { self.focus = match self.focus { @@ -142,9 +137,9 @@ impl Tracc { .direction(Direction::Vertical) .constraints( [ - Constraint::Percentage(42), - Constraint::Percentage(42), - Constraint::Percentage(16), + Constraint::Percentage(40), + Constraint::Percentage(40), + Constraint::Percentage(20), ] .as_ref(), ) @@ -153,12 +148,13 @@ impl Tracc { .render(&mut frame, chunks[0]); selectable_list(" 🕑 ", &printable_times, bottom_focus).render(&mut frame, chunks[1]); Paragraph::new( - [Text::raw(format!( - "Sum for today: {}\n{}", - total_time, times - ))] + [ + Text::raw(format!("Sum for today: {}\n", total_time)), + Text::raw(times) + ] .iter(), ) + .wrap(true) .block(Block::default().borders(Borders::ALL)) .render(&mut frame, chunks[2]); })?;