list: compute running and today
This commit is contained in:
parent
b531f8ca20
commit
54bfa8ce8f
|
@ -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),
|
||||
|
|
|
@ -147,6 +147,12 @@ pub trait Database {
|
|||
Ok(results.into_iter().next().map(|m| m.value))
|
||||
}
|
||||
|
||||
fn last_sheet(&self) -> Result<Option<String>> {
|
||||
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])?;
|
||||
|
||||
|
|
Loading…
Reference in New Issue