2020-07-07 22:45:32 +00:00
|
|
|
use super::{Context, Module, RootModuleConfig};
|
2019-08-08 17:25:30 +00:00
|
|
|
|
2019-10-15 11:34:48 +00:00
|
|
|
use crate::configs::cmd_duration::CmdDurationConfig;
|
2020-07-07 22:45:32 +00:00
|
|
|
use crate::formatter::StringFormatter;
|
2019-10-15 11:34:48 +00:00
|
|
|
|
2019-08-08 17:25:30 +00:00
|
|
|
/// Outputs the time it took the last command to execute
|
|
|
|
///
|
|
|
|
/// Will only print if last command took more than a certain amount of time to
|
|
|
|
/// execute. Default is two seconds, but can be set by config option `min_time`.
|
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
2019-09-09 23:14:38 +00:00
|
|
|
let mut module = context.new_module("cmd_duration");
|
2019-10-15 11:34:48 +00:00
|
|
|
let config: CmdDurationConfig = CmdDurationConfig::try_load(module.config);
|
2019-08-08 17:25:30 +00:00
|
|
|
|
2019-10-20 08:26:27 +00:00
|
|
|
let props = &context.properties;
|
|
|
|
let elapsed = props
|
|
|
|
.get("cmd_duration")
|
|
|
|
.unwrap_or(&"invalid_time".into())
|
2019-12-19 22:38:06 +00:00
|
|
|
.parse::<u128>()
|
2019-09-25 08:13:58 +00:00
|
|
|
.ok()?;
|
2019-08-08 17:25:30 +00:00
|
|
|
|
|
|
|
/* TODO: Once error handling is implemented, warn the user if their config
|
|
|
|
min time is nonsensical */
|
2019-10-15 11:34:48 +00:00
|
|
|
if config.min_time < 0 {
|
2019-08-08 17:25:30 +00:00
|
|
|
log::debug!(
|
|
|
|
"[WARN]: min_time in [cmd_duration] ({}) was less than zero",
|
2019-10-15 11:34:48 +00:00
|
|
|
config.min_time
|
2019-08-08 17:25:30 +00:00
|
|
|
);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
if elapsed < config.min_time as u128 {
|
|
|
|
return None;
|
|
|
|
}
|
2019-08-08 17:25:30 +00:00
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
|
|
|
formatter
|
|
|
|
.map_style(|variable| match variable {
|
|
|
|
"style" => Some(Ok(config.style)),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map(|variable| match variable {
|
|
|
|
"duration" => Some(Ok(render_time(elapsed, config.show_milliseconds))),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.parse(None)
|
|
|
|
});
|
2019-08-08 17:25:30 +00:00
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
module.set_segments(match parsed {
|
|
|
|
Ok(segments) => segments,
|
|
|
|
Err(error) => {
|
|
|
|
log::warn!("Error in module `cmd_duration`: \n{}", error);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
});
|
2019-08-08 17:25:30 +00:00
|
|
|
|
|
|
|
Some(module)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render the time into a nice human-readable string
|
2019-12-19 22:38:06 +00:00
|
|
|
fn render_time(raw_millis: u128, show_millis: bool) -> String {
|
|
|
|
// Calculate a simple breakdown into days/hours/minutes/seconds/milliseconds
|
|
|
|
let (millis, raw_seconds) = (raw_millis % 1000, raw_millis / 1000);
|
2019-08-08 17:25:30 +00:00
|
|
|
let (seconds, raw_minutes) = (raw_seconds % 60, raw_seconds / 60);
|
|
|
|
let (minutes, raw_hours) = (raw_minutes % 60, raw_minutes / 60);
|
|
|
|
let (hours, days) = (raw_hours % 24, raw_hours / 24);
|
|
|
|
|
2019-09-25 08:13:58 +00:00
|
|
|
let components = [days, hours, minutes, seconds];
|
|
|
|
let suffixes = ["d", "h", "m", "s"];
|
2019-08-08 17:25:30 +00:00
|
|
|
|
2019-12-19 22:38:06 +00:00
|
|
|
let mut rendered_components: Vec<String> = components
|
2019-08-08 17:25:30 +00:00
|
|
|
.iter()
|
|
|
|
.zip(&suffixes)
|
|
|
|
.map(render_time_component)
|
|
|
|
.collect();
|
2019-12-19 22:38:06 +00:00
|
|
|
if show_millis || raw_millis < 1000 {
|
|
|
|
rendered_components.push(render_time_component((&millis, &"ms")));
|
|
|
|
}
|
2019-08-08 17:25:30 +00:00
|
|
|
rendered_components.join("")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Render a single component of the time string, giving an empty string if component is zero
|
2019-12-19 22:38:06 +00:00
|
|
|
fn render_time_component((component, suffix): (&u128, &&str)) -> String {
|
2019-08-08 17:25:30 +00:00
|
|
|
match component {
|
|
|
|
0 => String::new(),
|
|
|
|
n => format!("{}{}", n, suffix),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2019-12-19 22:38:06 +00:00
|
|
|
#[test]
|
|
|
|
fn test_500ms() {
|
|
|
|
assert_eq!(render_time(500 as u128, true), "500ms")
|
|
|
|
}
|
2019-08-08 17:25:30 +00:00
|
|
|
#[test]
|
|
|
|
fn test_10s() {
|
2019-12-19 22:38:06 +00:00
|
|
|
assert_eq!(render_time(10_000 as u128, true), "10s")
|
2019-08-08 17:25:30 +00:00
|
|
|
}
|
2019-08-10 19:47:34 +00:00
|
|
|
#[test]
|
2019-08-08 17:25:30 +00:00
|
|
|
fn test_90s() {
|
2019-12-19 22:38:06 +00:00
|
|
|
assert_eq!(render_time(90_000 as u128, true), "1m30s")
|
2019-08-08 17:25:30 +00:00
|
|
|
}
|
2019-08-10 19:47:34 +00:00
|
|
|
#[test]
|
2019-08-08 17:25:30 +00:00
|
|
|
fn test_10110s() {
|
2019-12-19 22:38:06 +00:00
|
|
|
assert_eq!(render_time(10_110_000 as u128, true), "2h48m30s")
|
2019-08-08 17:25:30 +00:00
|
|
|
}
|
2019-08-10 19:47:34 +00:00
|
|
|
#[test]
|
2019-08-08 17:25:30 +00:00
|
|
|
fn test_1d() {
|
2019-12-19 22:38:06 +00:00
|
|
|
assert_eq!(render_time(86_400_000 as u128, true), "1d")
|
2019-08-08 17:25:30 +00:00
|
|
|
}
|
|
|
|
}
|