diff options
author | Thomas Lee <pbzweihander@gmail.com> | 2019-10-06 00:07:00 +0900 |
---|---|---|
committer | Matan Kushner <hello@matchai.me> | 2019-10-06 00:07:00 +0900 |
commit | 27e8c8f3ebae1fd5123df28aaee8f7d379d12026 (patch) | |
tree | 56e44d21fcccd4a0a17629feea48c9292c4c6c69 /src/modules | |
parent | bc9e44f45ca9bbdf681fcfe6e23cd0542a54ec60 (diff) |
refactor: Rewrite `time` module to use module config (#483)
- Config option renamed: `12hr` → `use_12hr`
Diffstat (limited to 'src/modules')
-rw-r--r-- | src/modules/time.rs | 43 |
1 files changed, 23 insertions, 20 deletions
diff --git a/src/modules/time.rs b/src/modules/time.rs index 344c17cfb..7009f984e 100644 --- a/src/modules/time.rs +++ b/src/modules/time.rs @@ -1,29 +1,22 @@ -use ansi_term::Color; use chrono::{DateTime, Local}; use super::{Context, Module}; +use crate::config::{RootModuleConfig, SegmentConfig}; +use crate::configs::time::TimeConfig; + /// Outputs the current time pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { - let mut module = context.new_module("time"); + const TIME_PREFIX: &str = "at "; - if module.config_value_bool("disabled").unwrap_or(true) { + let mut module = context.new_module("time"); + let config: TimeConfig = TimeConfig::try_load(module.config); + if config.disabled { return None; - } - - let module_style = module - .config_value_style("style") - .unwrap_or_else(|| Color::Yellow.bold()); - module.set_style(module_style); + }; - // Load module settings - let is_12hr = module.config_value_bool("12hr").unwrap_or(false); - - let default_format = if is_12hr { "%r" } else { "%T" }; - let time_format = module - .config_value_str("format") - .unwrap_or(default_format) - .to_owned(); + let default_format = if config.use_12hr { "%r" } else { "%T" }; + let time_format = config.format.unwrap_or(default_format); log::trace!( "Timer module is enabled with format string: {}", @@ -31,9 +24,19 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { ); let local: DateTime<Local> = Local::now(); - let formatted_time_string = format_time(&time_format, local); - module.new_segment("time", &formatted_time_string); - module.get_prefix().set_value("at "); + let formatted_time_string = format_time(time_format, local); + + module.set_style(config.style); + + module.get_prefix().set_value(TIME_PREFIX); + + module.create_segment( + "time", + &SegmentConfig { + value: &formatted_time_string, + style: None, + }, + ); Some(module) } |