allow overriding timesheet texts with [something]

This commit is contained in:
kageru 2020-04-20 15:31:43 +02:00
parent b2a2d29216
commit f1eeb7b24e
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
3 changed files with 24 additions and 3 deletions

View File

@ -8,7 +8,8 @@ edition = "2018"
tui = "0.8.0"
termion = "1.5"
serde_json = "1"
itertools = "0.9"
itertools = "0.9.0"
serde = { version = "1", features = ["derive"] }
time = { version = "0.2.9", features = ["serde"] }
#cmd_lib = "0.7.8"
regex = "1.3.7"
lazy_static = "1.4.0"

View File

@ -9,6 +9,8 @@ mod timesheet;
mod todolist;
mod tracc;
use tracc::Tracc;
#[macro_use]
extern crate lazy_static;
fn main() -> Result<(), io::Error> {
let stdout = io::stdout().into_raw_mode()?;

View File

@ -13,6 +13,9 @@ pub struct TimeSheet {
}
const PAUSE_TEXTS: [&str; 3] = ["lunch", "mittag", "pause"];
lazy_static! {
static ref OVERRIDE_REGEX: regex::Regex = regex::Regex::new("\\[(.*)\\]").unwrap();
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct TimePoint {
@ -48,6 +51,20 @@ fn read_times(path: &str) -> Option<Vec<TimePoint>> {
.and_then(|r| from_reader(r).ok())
}
/**
* If a time text contains "[something]",
* only use the message inside the brackets.
*/
fn effective_text(s: String) -> String {
OVERRIDE_REGEX
.captures(&s)
// index 0 is the entire string
.and_then(|caps| caps.get(1))
.map(|m| m.as_str())
.unwrap_or(&s)
.to_string()
}
impl TimeSheet {
pub fn open_or_create(path: &str) -> Self {
Self {
@ -76,7 +93,8 @@ impl TimeSheet {
// I use a BTreeMap because I need a stable output order for the iterator
// (otherwise the summary list will jump around on every input).
.fold(collections::BTreeMap::new(), |mut map, (text, duration)| {
*map.entry(text).or_insert_with(Duration::zero) += duration;
*map.entry(effective_text(text))
.or_insert_with(Duration::zero) += duration;
map
})
.into_iter()