enhance(tasks): revamp time formatting

This commit is contained in:
xeruf 2025-05-20 20:20:05 +02:00
parent d3039cee7f
commit 8655a21396

View file

@ -736,8 +736,8 @@ impl TasksRelay {
"path" => self.get_task_path(Some(task.get_id())),
"rpath" => self.get_relative_path(task.get_id()),
// TODO format strings configurable
"time" => format_secs("MMMm", self.time_tracked(task.get_id())),
"rtime" => format_secs("HHH:MM", self.total_time_tracked(task.get_id())),
"time" => format_secs("TTTT", self.time_tracked(task.get_id())),
"rtime" => format_secs("TT:TT", self.total_time_tracked(task.get_id())),
prop => task.get(prop).unwrap_or_default(),
}
}
@ -1551,7 +1551,7 @@ impl Display for TasksRelay {
writeln!(lock,
"{count} visible tasks{}",
format_secs(" tracked a total of HHhMMm", total_time)
format_secs(" tracked a total of T", total_time)
)?;
Ok(())
}
@ -1593,24 +1593,23 @@ where
}
}
/// Formats the given seconds according to the given format.
/// - MMM - minutes
/// - MM - minutes of the hour
/// - HH - hours
///
/// Formats seconds using a formatting scheme.
/// Returns an empty string if under one minute.
fn format_secs(format: &str, secs: u64) -> String {
// TODO format to only hours/days for large values
Some(secs / 60)
.filter(|t| t > &0)
.map_or(String::new(), |mins| {
let hours = mins / 60;
format
.replace("MMM", &format!("{:3}", mins))
.replace("HHH", &format!("{:>3}", mins.div(60)))
.replace("HH", &format!("{:>2}", mins.div(60)))
.replace("H", &mins.div(60).to_string())
.replace("MM", &format!("{:02}", mins.rem(60)))
.replace("M", &mins.to_string())
.replace("TTTT",
&(if mins > 999 { format!("h{:03}", hours) }
else { format!("{:>3}m", mins) }))
.replace("TT:TT",
&(if hours > 99 { format!("h{:>4}", hours) }
else { format!("{:>2}:{:02}", hours, mins.rem(60)) }))
.replace("T",
&(if hours > 99 { format!("{}h", hours) }
else { format!("{}h{:02}m", hours, mins) }))
})
}