2019-08-08 17:25:30 +00:00
|
|
|
use ansi_term::Color;
|
|
|
|
|
|
|
|
use super::{Context, Module};
|
|
|
|
|
|
|
|
/// 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-08-08 17:25:30 +00:00
|
|
|
|
|
|
|
let arguments = &context.arguments;
|
|
|
|
let elapsed = arguments
|
|
|
|
.value_of("cmd_duration")
|
|
|
|
.unwrap_or("invalid_time")
|
|
|
|
.parse::<u64>()
|
2019-09-25 08:13:58 +00:00
|
|
|
.ok()?;
|
2019-08-08 17:25:30 +00:00
|
|
|
|
2019-09-25 08:13:58 +00:00
|
|
|
let signed_config_min = module.config_value_i64("min_time").unwrap_or(2);
|
2019-08-08 17:25:30 +00:00
|
|
|
|
|
|
|
/* TODO: Once error handling is implemented, warn the user if their config
|
|
|
|
min time is nonsensical */
|
|
|
|
if signed_config_min < 0 {
|
|
|
|
log::debug!(
|
|
|
|
"[WARN]: min_time in [cmd_duration] ({}) was less than zero",
|
|
|
|
signed_config_min
|
|
|
|
);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let config_min = signed_config_min as u64;
|
|
|
|
|
|
|
|
let module_color = match elapsed {
|
2019-09-25 08:13:58 +00:00
|
|
|
time if time < config_min => return None,
|
2019-09-08 00:33:06 +00:00
|
|
|
_ => module
|
|
|
|
.config_value_style("style")
|
|
|
|
.unwrap_or_else(|| Color::Yellow.bold()),
|
2019-08-08 17:25:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.set_style(module_color);
|
2019-09-25 08:13:58 +00:00
|
|
|
module.new_segment("cmd_duration", &format!("took {}", render_time(elapsed)));
|
2019-08-08 17:25:30 +00:00
|
|
|
module.get_prefix().set_value("");
|
|
|
|
|
|
|
|
Some(module)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render the time into a nice human-readable string
|
2019-09-25 08:13:58 +00:00
|
|
|
fn render_time(raw_seconds: u64) -> String {
|
2019-08-08 17:25:30 +00:00
|
|
|
// Calculate a simple breakdown into days/hours/minutes/seconds
|
|
|
|
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
|
|
|
|
|
|
|
let rendered_components: Vec<String> = components
|
|
|
|
.iter()
|
|
|
|
.zip(&suffixes)
|
|
|
|
.map(render_time_component)
|
|
|
|
.collect();
|
|
|
|
rendered_components.join("")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Render a single component of the time string, giving an empty string if component is zero
|
|
|
|
fn render_time_component((component, suffix): (&u64, &&str)) -> String {
|
|
|
|
match component {
|
|
|
|
0 => String::new(),
|
|
|
|
n => format!("{}{}", n, suffix),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_10s() {
|
2019-09-25 08:13:58 +00:00
|
|
|
assert_eq!(render_time(10 as u64), "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-09-25 08:13:58 +00:00
|
|
|
assert_eq!(render_time(90 as u64), "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-09-25 08:13:58 +00:00
|
|
|
assert_eq!(render_time(10110 as u64), "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-09-25 08:13:58 +00:00
|
|
|
assert_eq!(render_time(86400 as u64), "1d")
|
2019-08-08 17:25:30 +00:00
|
|
|
}
|
|
|
|
}
|