From 54bfa8ce8f125c150dce796b3075a2451a0f5144 Mon Sep 17 00:00:00 2001 From: Abraham Toriz Date: Thu, 15 Jul 2021 13:11:55 -0500 Subject: [PATCH] list: compute running and today --- src/commands/list.rs | 28 ++++++++++++++++++++++------ src/database.rs | 6 ++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/commands/list.rs b/src/commands/list.rs index 3003fbb..f271730 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -2,7 +2,7 @@ use std::convert::TryFrom; use std::io::Write; use clap::ArgMatches; -use chrono::{Utc, Duration}; +use chrono::{Utc, Duration, Local}; use itertools::Itertools; use crate::error::{Error, Result}; @@ -41,6 +41,7 @@ impl<'a> Command<'a> for ListCommand { { // no sheet, list sheets let now = Utc::now(); + let today = Local::now().date().and_hms(0, 0, 0).with_timezone(&Utc); let mut sheets = if args.all { db.entries_all_visible(None, None)? } else { @@ -48,6 +49,7 @@ impl<'a> Command<'a> for ListCommand { }; let current = db.current_sheet()?.unwrap_or("".into()); + let last = db.last_sheet()?.unwrap_or("".into()); sheets.sort_unstable_by_key(|e| e.sheet.clone()); @@ -56,14 +58,28 @@ impl<'a> Command<'a> for ListCommand { .group_by(|e| e.sheet.clone()) .into_iter() .map(|(key, group)| { + let entries: Vec<_> = group.into_iter().collect(); + ( - if current == key { "*".into() } else { "".into() }, + if current == key { + "*".into() + } else { + if last == key { "-".into() } else { "".into() } + }, key, - "running".into(), - "today".into(), + + format_duration( + now - entries.iter().filter(|e| e.end.is_none()).next().map(|e| e.end).flatten().unwrap_or(now) + ), + + format_duration( + entries.iter().filter(|e| e.start > today).fold(Duration::seconds(0), |acc, e| { + acc + (e.end.unwrap_or(now) - e.start) + }) + ), // total - format_duration(group.fold(Duration::seconds(0), |acc, e| { + format_duration(entries.into_iter().fold(Duration::seconds(0), |acc, e| { acc + (e.end.unwrap_or(now) - e.start) })), ) @@ -81,7 +97,7 @@ impl<'a> Command<'a> for ListCommand { Col::min_width(9).and_alignment(Right), // today - Col::min_width(7).and_alignment(Right), + Col::min_width(9).and_alignment(Right), // accumulated Col::min_width(12).and_alignment(Right), diff --git a/src/database.rs b/src/database.rs index 41fcbb2..28ad14d 100644 --- a/src/database.rs +++ b/src/database.rs @@ -147,6 +147,12 @@ pub trait Database { Ok(results.into_iter().next().map(|m| m.value)) } + fn last_sheet(&self) -> Result> { + let results = self.meta_query("select * from meta where key='last_sheet'", &[])?; + + Ok(results.into_iter().next().map(|m| m.value)) + } + fn set_current_sheet(&mut self, sheet: &str) -> Result<()> { self.execute("INSERT INTO meta (key, value) VALUES ('current_sheet', ?1)", &[&sheet])?;