use crate::config::{ModuleConfig, SegmentConfig}; use crate::segment::Segment; use ansi_term::Style; use ansi_term::{ANSIString, ANSIStrings}; use std::fmt; // List of all modules // Keep these ordered alphabetically. // Default ordering is handled in configs/mod.rs pub const ALL_MODULES: &[&str] = &[ "aws", #[cfg(feature = "battery")] "battery", "character", "cmd_duration", "conda", "directory", "dotnet", "env_var", "git_branch", "git_state", "git_status", "golang", "hostname", "java", "jobs", "kubernetes", "line_break", "memory_usage", "nix_shell", "nodejs", "package", "python", "ruby", "rust", "time", "username", ]; /// A module is a collection of segments showing data for a single integration /// (e.g. The git module shows the current git branch and status) pub struct Module<'a> { /// The module's configuration map if available pub config: Option<&'a toml::Value>, /// The module's name, to be used in configuration and logging. _name: String, /// The styling to be inherited by all segments contained within this module. style: Style, /// The prefix used to separate the current module from the previous one. prefix: Affix, /// The collection of segments that compose this module. segments: Vec, /// The suffix used to separate the current module from the next one. suffix: Affix, } impl<'a> Module<'a> { /// Creates a module with no segments. pub fn new(name: &str, config: Option<&'a toml::Value>) -> Module<'a> { Module { config, _name: name.to_string(), style: Style::default(), prefix: Affix::default_prefix(name), segments: Vec::new(), suffix: Affix::default_suffix(name), } } /// 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); segment.set_style(segment_config.style.unwrap_or(self.style)); segment.set_value(segment_config.value); self.segments.push(segment); 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 } /// Whether a module has non-empty segments pub fn is_empty(&self) -> bool { self.segments.iter().all(|segment| segment.is_empty()) } /// Get the module's prefix pub fn get_prefix(&mut self) -> &mut Affix { &mut self.prefix } /// Get the module's suffix pub fn get_suffix(&mut self) -> &mut Affix { &mut self.suffix } /// Sets the style of the segment. /// /// Accepts either `Color` or `Style`. pub fn set_style(&mut self, style: T) -> &mut Module<'a> where T: Into