|
1 |
| -use ansi_term::Color; |
2 | 1 | use chrono::{DateTime, Local};
|
3 | 2 |
|
4 | 3 | use super::{Context, Module};
|
5 | 4 |
|
| 5 | +use crate::config::{RootModuleConfig, SegmentConfig}; |
| 6 | +use crate::configs::time::TimeConfig; |
| 7 | + |
6 | 8 | /// Outputs the current time
|
7 | 9 | pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
8 |
| - let mut module = context.new_module("time"); |
| 10 | + const TIME_PREFIX: &str = "at "; |
9 | 11 |
|
10 |
| - if module.config_value_bool("disabled").unwrap_or(true) { |
| 12 | + let mut module = context.new_module("time"); |
| 13 | + let config: TimeConfig = TimeConfig::try_load(module.config); |
| 14 | + if config.disabled { |
11 | 15 | return None;
|
12 |
| - } |
13 |
| - |
14 |
| - let module_style = module |
15 |
| - .config_value_style("style") |
16 |
| - .unwrap_or_else(|| Color::Yellow.bold()); |
17 |
| - module.set_style(module_style); |
| 16 | + }; |
18 | 17 |
|
19 |
| - // Load module settings |
20 |
| - let is_12hr = module.config_value_bool("12hr").unwrap_or(false); |
21 |
| - |
22 |
| - let default_format = if is_12hr { "%r" } else { "%T" }; |
23 |
| - let time_format = module |
24 |
| - .config_value_str("format") |
25 |
| - .unwrap_or(default_format) |
26 |
| - .to_owned(); |
| 18 | + let default_format = if config.use_12hr { "%r" } else { "%T" }; |
| 19 | + let time_format = config.format.unwrap_or(default_format); |
27 | 20 |
|
28 | 21 | log::trace!(
|
29 | 22 | "Timer module is enabled with format string: {}",
|
30 | 23 | time_format
|
31 | 24 | );
|
32 | 25 |
|
33 | 26 | let local: DateTime<Local> = Local::now();
|
34 |
| - let formatted_time_string = format_time(&time_format, local); |
35 |
| - module.new_segment("time", &formatted_time_string); |
36 |
| - module.get_prefix().set_value("at "); |
| 27 | + let formatted_time_string = format_time(time_format, local); |
| 28 | + |
| 29 | + module.set_style(config.style); |
| 30 | + |
| 31 | + module.get_prefix().set_value(TIME_PREFIX); |
| 32 | + |
| 33 | + module.create_segment( |
| 34 | + "time", |
| 35 | + &SegmentConfig { |
| 36 | + value: &formatted_time_string, |
| 37 | + style: None, |
| 38 | + }, |
| 39 | + ); |
37 | 40 |
|
38 | 41 | Some(module)
|
39 | 42 | }
|
|
0 commit comments