use crate::config::Config; use crate::segment::Segment; use ansi_term::Style; use ansi_term::{ANSIString, ANSIStrings}; use std::fmt; /// 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 config: Option<&'a toml::value::Table>, /// 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: ModuleAffix, /// The collection of segments that compose this module. segments: Vec, /// The suffix used to separate the current module from the next one. suffix: ModuleAffix, } impl<'a> Module<'a> { /// Creates a module with no segments. pub fn new(name: &str, config: Option<&'a toml::value::Table>) -> Module<'a> { Module { config, name: name.to_string(), style: Style::default(), prefix: ModuleAffix::default_prefix(name.to_string()), segments: Vec::new(), suffix: ModuleAffix::default_suffix(name.to_string()), } } /// 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(name).unwrap_or(value)); self.segments.push(segment); self.segments.last_mut().unwrap() } /// Whether a module has any segments pub fn is_empty(&self) -> bool { self.segments.is_empty() } /// Get the module's prefix pub fn get_prefix(&mut self) -> &mut ModuleAffix { &mut self.prefix } /// Get the module's suffix pub fn get_suffix(&mut self) -> &mut ModuleAffix { &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