diff options
author | Zhenhui Xie <xiezh0831@126.com> | 2019-09-30 12:03:07 +0800 |
---|---|---|
committer | Kevin Song <chipbuster@users.noreply.github.com> | 2019-09-29 23:03:07 -0500 |
commit | 61604a4a8e7f407290ef4cf5205be6f4e2fd4dcf (patch) | |
tree | 62272811f5f7fb285700b70eee5ade48b35ce300 /src/module.rs | |
parent | 34b8ef0b6feedcc6b674df1f1ee8e27c1a20b1c5 (diff) |
feat: Allow segment-specific styling (#378)
Adds the ability to style individual segments in the prompt.
The segment documentation is not fully updated in this commit and is waiting on
a config refactor so that we can write unified docs.
Diffstat (limited to 'src/module.rs')
-rw-r--r-- | src/module.rs | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/src/module.rs b/src/module.rs index 65e69c36d..b9e71cbec 100644 --- a/src/module.rs +++ b/src/module.rs @@ -1,4 +1,5 @@ use crate::config::Config; +use crate::config::SegmentConfig; use crate::segment::Segment; use ansi_term::Style; use ansi_term::{ANSIString, ANSIStrings}; @@ -70,9 +71,14 @@ impl<'a> Module<'a> { /// Get a reference to a newly created segment in the module pub fn new_segment(&mut self, name: &str, value: &str) -> &mut Segment { let mut segment = Segment::new(name); - segment.set_style(self.style); - // Use the provided value unless overwritten by config - segment.set_value(self.config_value_str(name).unwrap_or(value)); + if let Some(segment_config) = self.config_value_segment_config(name) { + segment.set_style(segment_config.style.unwrap_or(self.style)); + segment.set_value(segment_config.value.unwrap_or(value)); + } else { + segment.set_style(self.style); + // Use the provided value unless overwritten by config + segment.set_value(self.config_value_str(name).unwrap_or(value)); + } self.segments.push(segment); self.segments.last_mut().unwrap() @@ -168,6 +174,12 @@ impl<'a> Module<'a> { pub fn config_value_array(&self, key: &str) -> Option<&Vec<toml::Value>> { self.config.and_then(|config| config.get_as_array(key)) } + + /// Get a module's config value as a table of segment config + pub fn config_value_segment_config(&self, key: &str) -> Option<SegmentConfig> { + self.config + .and_then(|config| config.get_as_segment_config(key)) + } } impl<'a> fmt::Display for Module<'a> { |