refactor: Cleanup after module config refactor (#630)

This commit is contained in:
Zhenhui Xie 2019-11-07 11:38:30 +08:00 committed by Matan Kushner
parent a3d5ea3e43
commit 5bb7467b56
5 changed files with 10 additions and 109 deletions

View File

@ -240,16 +240,6 @@ impl<'a> SegmentConfig<'a> {
Self { value, style: None }
}
/// Mutably set value
pub fn set_value(&mut self, value: &'a str) {
self.value = value;
}
/// Mutably set style
pub fn set_style(&mut self, style: Style) {
self.style = Some(style);
}
/// Immutably set value
pub fn with_value(&self, value: &'a str) -> Self {
Self {

View File

@ -1,4 +1,4 @@
use crate::config::{ModuleConfig, SegmentConfig};
use crate::config::SegmentConfig;
use crate::segment::Segment;
use ansi_term::Style;
use ansi_term::{ANSIString, ANSIStrings};
@ -72,33 +72,6 @@ impl<'a> Module<'a> {
}
}
/// Get a reference to a newly created segment in the module
#[deprecated(
since = "0.20.0",
note = "please use `module.create_segment()` instead"
)]
pub fn new_segment(&mut self, name: &str, value: &str) -> &mut Segment {
let mut segment = Segment::new(name);
let segment_config_mock = SegmentConfig { value, style: None };
if let Some(toml::Value::Table(module_config)) = self.config {
if let Some(symbol) = module_config.get(name) {
let segment_config = segment_config_mock.load_config(&symbol);
segment.set_style(segment_config.style.unwrap_or(self.style));
segment.set_value(segment_config.value);
self.segments.push(segment);
return self.segments.last_mut().unwrap();
}
}
segment.set_style(segment_config_mock.style.unwrap_or(self.style));
segment.set_value(segment_config_mock.value);
self.segments.push(segment);
self.segments.last_mut().unwrap()
}
/// Get a reference to a newly created segment in the module
pub fn create_segment(&mut self, name: &str, segment_config: &SegmentConfig) -> &mut Segment {
let mut segment = Segment::new(name);
@ -109,24 +82,6 @@ impl<'a> Module<'a> {
self.segments.last_mut().unwrap()
}
/// Should config exists, get a reference to a newly created segment in the module
#[deprecated(
since = "0.20.0",
note = "please use `module.create_segment()` instead"
)]
pub fn new_segment_if_config_exists(&mut self, name: &str) -> Option<&mut Segment> {
// Use the provided value unless overwritten by config
if let Some(value) = self.config_value_str(name) {
let mut segment = Segment::new(name);
segment.set_style(self.style);
segment.set_value(value);
self.segments.push(segment);
Some(self.segments.last_mut().unwrap())
} else {
None
}
}
/// Get module's name
pub fn get_name(&self) -> &String {
&self._name
@ -183,42 +138,6 @@ impl<'a> Module<'a> {
pub fn to_string_without_prefix(&self) -> String {
ANSIStrings(&self.ansi_strings()[1..]).to_string()
}
/// Get a module's config value as a string
#[deprecated(
since = "0.20.0",
note = "please use <RootModuleConfig>::try_load(module.config) instead"
)]
pub fn config_value_str(&self, key: &str) -> Option<&str> {
<&str>::from_config(self.config?.as_table()?.get(key)?)
}
/// Get a module's config value as an int
#[deprecated(
since = "0.20.0",
note = "please use <RootModuleConfig>::try_load(module.config) instead"
)]
pub fn config_value_i64(&self, key: &str) -> Option<i64> {
<i64>::from_config(self.config?.as_table()?.get(key)?)
}
/// Get a module's config value as a bool
#[deprecated(
since = "0.20.0",
note = "please use <RootModuleConfig>::try_load(module.config) instead"
)]
pub fn config_value_bool(&self, key: &str) -> Option<bool> {
<bool>::from_config(self.config?.as_table()?.get(key)?)
}
/// Get a module's config value as a style
#[deprecated(
since = "0.20.0",
note = "please use <RootModuleConfig>::try_load(module.config) instead"
)]
pub fn config_value_style(&self, key: &str) -> Option<Style> {
<Style>::from_config(self.config?.as_table()?.get(key)?)
}
}
impl<'a> fmt::Display for Module<'a> {

View File

@ -1,4 +1,4 @@
use super::{Context, Module};
use super::{Context, Module, SegmentConfig};
use crate::config::RootModuleConfig;
use crate::configs::cmd_duration::CmdDurationConfig;
@ -36,10 +36,8 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
};
module.set_style(module_color);
module.new_segment(
"cmd_duration",
&format!("{}{}", config.prefix, render_time(elapsed)),
);
let cmd_duration_stacked = &format!("{}{}", config.prefix, render_time(elapsed));
module.create_segment("cmd_duration", &SegmentConfig::new(&cmd_duration_stacked));
module.get_prefix().set_value("");
Some(module)

View File

@ -1,6 +1,6 @@
use std::env;
use super::{Context, Module};
use super::{Context, Module, SegmentConfig};
use crate::config::RootModuleConfig;
use crate::configs::env_var::EnvVarConfig;
@ -25,10 +25,8 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
}
// 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),
);
let env_var_stacked = format!("{}{}{}", config.prefix, env_value, config.suffix);
module.create_segment("env_var", &SegmentConfig::new(&env_var_stacked));
Some(module)
}

View File

@ -1,6 +1,6 @@
use std::env;
use super::{Context, Module};
use super::{Context, Module, SegmentConfig};
use std::ffi::OsString;
use crate::config::RootModuleConfig;
@ -30,8 +30,6 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
}
};
let trim_at = module.config_value_str("trim_at").unwrap_or(".");
//rustc doesn't let you do an "if" and an "if let" in the same if statement
// if this changes in the future this can become a lot cleaner
let host = if config.trim_at != "" {
@ -45,10 +43,8 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
};
module.set_style(config.style);
module.new_segment(
"hostname",
&format!("{}{}{}", config.prefix, host, config.suffix),
);
let hostname_stacked = format!("{}{}{}", config.prefix, host, config.suffix);
module.create_segment("hostname", &SegmentConfig::new(&hostname_stacked));
module.get_prefix().set_value("on ");
Some(module)