Skip to content

Commit 03079f2

Browse files
committed
add time sum in view command
1 parent 8d35d2c commit 03079f2

4 files changed

Lines changed: 62 additions & 20 deletions

File tree

Cargo.lock

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ chrono = "0.4.38"
1111
clap = { version = "4", features = ["derive"] }
1212
dirs = "5"
1313
edit = "0.1.5"
14+
fancy-duration = { version = "0.9.2", features = ["chrono"] }
1415
figment = { version = "0.10", features = ["json", "yaml", "toml", "env"] }
1516
inquire = { version = "0.7.4", features = ["date", "editor"] }
1617
regex = "1.11.1"

src/commands/view.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
use std::fmt;
2-
31
use super::Command;
42
use crate::{
53
config::Config,
64
store::Store,
75
util::{parse_date, select_date},
86
};
97
use anyhow::Result;
10-
use chrono::Local;
8+
use chrono::{Duration, Local};
119
use clap::Args;
12-
use regex::Regex;
13-
use yansi::{Paint, Style};
10+
use fancy_duration::AsFancyDuration;
11+
use yansi::Paint;
1412

1513
/// Display tracking list entries
1614
#[derive(Args)]
@@ -38,7 +36,20 @@ impl Command for View {
3836
let mut entries = store.list(date)?;
3937
entries.sort_by_key(|e| e.timestamp);
4038

39+
let mut sum = Duration::zero();
40+
let mut pause_time = Duration::zero();
41+
let mut last_timestamp = None;
42+
4143
for (i, e) in entries.iter().enumerate() {
44+
if let Some(last_timestamp) = last_timestamp {
45+
match e.message_matches(&config.pause_regex)? {
46+
true => pause_time += e.timestamp - last_timestamp,
47+
false => sum += e.timestamp - last_timestamp,
48+
}
49+
}
50+
51+
last_timestamp = Some(e.timestamp);
52+
4253
print!(
4354
"{}{}{} ",
4455
"[".dim(),
@@ -49,6 +60,12 @@ impl Command for View {
4960
println!("{}", e.formatted(config, self.long)?);
5061
}
5162

63+
println!(
64+
"\n {} ({})",
65+
sum.fancy_duration().truncate(2).to_string().cyan().bold(),
66+
format!("{} pause", pause_time.fancy_duration().truncate(2)).green()
67+
);
68+
5269
Ok(())
5370
}
5471
}

src/util.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use anyhow::Result;
33
use chrono::{Datelike, Duration, Local, NaiveDate};
44
use inquire::DateSelect;
55
use regex::Regex;
6-
use std::{fmt, io::Write};
6+
use std::fmt;
77
use yansi::{Paint, Style};
88

99
static STYLE_START: Style = Style::new().cyan();
@@ -28,7 +28,7 @@ impl Entry {
2828
"{} {} {}",
2929
self.timestamp_formatted().rgb(244, 9, 84),
3030
":".dim(),
31-
style_entry(&self.message, config)?
31+
self.style_message(config)?
3232
)?;
3333
if let Some(ref long_message) = self.long {
3434
if long {
@@ -39,6 +39,22 @@ impl Entry {
3939
}
4040
Ok(())
4141
}
42+
43+
pub fn message_matches(&self, rx: &str) -> Result<bool> {
44+
Ok(Regex::new(rx)?.is_match(&self.message))
45+
}
46+
47+
fn style_message<'a>(&'a self, config: &Config) -> Result<Box<dyn fmt::Display + 'a>> {
48+
Ok(if self.message_matches(&config.start_regex)? {
49+
Box::new(self.message.paint(STYLE_START))
50+
} else if self.message_matches(&config.pause_regex)? {
51+
Box::new(self.message.paint(STYLE_PAUSE))
52+
} else if self.message_matches(&config.end_regex)? {
53+
Box::new(self.message.paint(STYLE_END))
54+
} else {
55+
Box::new(&self.message)
56+
})
57+
}
4258
}
4359

4460
fn format_long(long: &str) -> String {
@@ -50,18 +66,6 @@ fn format_long(long: &str) -> String {
5066
str
5167
}
5268

53-
fn style_entry<'a>(v: &'a str, config: &Config) -> Result<Box<dyn fmt::Display + 'a>> {
54-
Ok(if Regex::new(&config.start_regex)?.is_match(v) {
55-
Box::new(v.paint(STYLE_START))
56-
} else if Regex::new(&config.pause_regex)?.is_match(v) {
57-
Box::new(v.paint(STYLE_PAUSE))
58-
} else if Regex::new(&config.end_regex)?.is_match(v) {
59-
Box::new(v.paint(STYLE_END))
60-
} else {
61-
Box::new(v)
62-
})
63-
}
64-
6569
pub fn parse_date(date: &str) -> Result<NaiveDate> {
6670
let today = Local::now().date_naive();
6771

@@ -106,7 +110,7 @@ impl<'a> FormatableEntry<'a> {
106110
}
107111
}
108112

109-
impl<'a> fmt::Display for FormatableEntry<'a> {
113+
impl fmt::Display for FormatableEntry<'_> {
110114
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
111115
self.entry
112116
.format(f, self.config, self.long)

0 commit comments

Comments
 (0)