diff options
author | Zhenhui Xie <xiezh0831@yahoo.co.jp> | 2019-10-15 19:34:48 +0800 |
---|---|---|
committer | Matan Kushner <hello@matchai.me> | 2019-10-15 20:34:48 +0900 |
commit | be2d5cf1cd23d2b33892445f5d73dda5b165e7a7 (patch) | |
tree | b9296cd5c5ce94a1bc9b1ae7b4922b9c093ace92 /src/modules/cmd_duration.rs | |
parent | 2fd1920f7d59831ac5a50e03813caf834e9d65fe (diff) |
refactor: Rewrite cmd_duration, directory and env_var module to use module config (#460)
This PR is a batched rewrite of the following modules:
- cmd_duration
- directory
- env_var
Diffstat (limited to 'src/modules/cmd_duration.rs')
-rw-r--r-- | src/modules/cmd_duration.rs | 25 |
1 files changed, 9 insertions, 16 deletions
diff --git a/src/modules/cmd_duration.rs b/src/modules/cmd_duration.rs index 8d4816392..a0340cb0c 100644 --- a/src/modules/cmd_duration.rs +++ b/src/modules/cmd_duration.rs @@ -1,13 +1,15 @@ -use ansi_term::Color; - use super::{Context, Module}; +use crate::config::RootModuleConfig; +use crate::configs::cmd_duration::CmdDurationConfig; + /// 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>> { let mut module = context.new_module("cmd_duration"); + let config: CmdDurationConfig = CmdDurationConfig::try_load(module.config); let arguments = &context.arguments; let elapsed = arguments @@ -16,36 +18,27 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { .parse::<u64>() .ok()?; - let prefix = module - .config_value_str("prefix") - .unwrap_or("took ") - .to_owned(); - - let signed_config_min = module.config_value_i64("min_time").unwrap_or(2); - /* TODO: Once error handling is implemented, warn the user if their config min time is nonsensical */ - if signed_config_min < 0 { + if config.min_time < 0 { log::debug!( "[WARN]: min_time in [cmd_duration] ({}) was less than zero", - signed_config_min + config.min_time ); return None; } - let config_min = signed_config_min as u64; + let config_min = config.min_time as u64; let module_color = match elapsed { time if time < config_min => return None, - _ => module - .config_value_style("style") - .unwrap_or_else(|| Color::Yellow.bold()), + _ => config.style, }; module.set_style(module_color); module.new_segment( "cmd_duration", - &format!("{}{}", prefix, render_time(elapsed)), + &format!("{}{}", config.prefix, render_time(elapsed)), ); module.get_prefix().set_value(""); |