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/env_var.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/env_var.rs')
-rw-r--r-- | src/modules/env_var.rs | 29 |
1 files changed, 15 insertions, 14 deletions
diff --git a/src/modules/env_var.rs b/src/modules/env_var.rs index bb46456a1..f4905fda3 100644 --- a/src/modules/env_var.rs +++ b/src/modules/env_var.rs @@ -1,8 +1,10 @@ -use ansi_term::Color; use std::env; use super::{Context, Module}; +use crate::config::RootModuleConfig; +use crate::configs::env_var::EnvVarConfig; + /// Creates a module with the value of the chosen environment variable /// /// Will display the environment variable's value if all of the following criteria are met: @@ -11,23 +13,22 @@ use super::{Context, Module}; /// - a variable named as the value of env_var.variable is defined pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { let mut module = context.new_module("env_var"); - let module_style = module - .config_value_style("style") - .unwrap_or_else(|| Color::Black.bold().dimmed()); - - let env_name = module.config_value_str("variable")?; + let config: EnvVarConfig = EnvVarConfig::try_load(module.config); - let default_value = module.config_value_str("default"); + let env_value = get_env_value(config.variable?, config.default)?; - let env_value = get_env_value(env_name, default_value)?; + module.set_style(config.style); + module.get_prefix().set_value("with "); - let prefix = module.config_value_str("prefix").unwrap_or("").to_owned(); - let suffix = module.config_value_str("suffix").unwrap_or("").to_owned(); + if let Some(symbol) = config.symbol { + module.create_segment("symbol", &symbol); + } - module.set_style(module_style); - module.get_prefix().set_value("with "); - module.new_segment_if_config_exists("symbol"); - module.new_segment("env_var", &format!("{}{}{}", prefix, env_value, suffix)); + // TODO: Use native prefix and suffix instead of stacking custom ones together with env_value. + module.new_segment( + "env_var", + &format!("{}{}{}", config.prefix, env_value, config.suffix), + ); Some(module) } |