2019-07-27 22:25:13 +00:00
|
|
|
use crate::config::Config;
|
2019-05-01 20:34:24 +00:00
|
|
|
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)
|
2019-06-10 14:56:17 +00:00
|
|
|
pub struct Module<'a> {
|
|
|
|
/// The module's configuration map if available
|
|
|
|
config: Option<&'a toml::value::Table>,
|
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
/// 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.
|
2019-07-31 23:48:51 +00:00
|
|
|
prefix: Affix,
|
2019-05-01 20:34:24 +00:00
|
|
|
|
|
|
|
/// The collection of segments that compose this module.
|
|
|
|
segments: Vec<Segment>,
|
|
|
|
|
|
|
|
/// The suffix used to separate the current module from the next one.
|
2019-07-31 23:48:51 +00:00
|
|
|
suffix: Affix,
|
2019-05-01 20:34:24 +00:00
|
|
|
}
|
|
|
|
|
2019-06-10 14:56:17 +00:00
|
|
|
impl<'a> Module<'a> {
|
2019-05-01 20:34:24 +00:00
|
|
|
/// Creates a module with no segments.
|
2019-06-10 14:56:17 +00:00
|
|
|
pub fn new(name: &str, config: Option<&'a toml::value::Table>) -> Module<'a> {
|
2019-05-01 20:34:24 +00:00
|
|
|
Module {
|
2019-06-10 14:56:17 +00:00
|
|
|
config,
|
2019-05-01 20:34:24 +00:00
|
|
|
name: name.to_string(),
|
|
|
|
style: Style::default(),
|
2019-07-31 23:48:51 +00:00
|
|
|
prefix: Affix::default_prefix(name),
|
2019-05-01 20:34:24 +00:00
|
|
|
segments: Vec::new(),
|
2019-07-31 23:48:51 +00:00
|
|
|
suffix: Affix::default_suffix(name),
|
2019-05-01 20:34:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a reference to a newly created segment in the module
|
2019-07-14 15:15:47 +00:00
|
|
|
pub fn new_segment(&mut self, name: &str, value: &str) -> &mut Segment {
|
2019-05-01 20:34:24 +00:00
|
|
|
let mut segment = Segment::new(name);
|
|
|
|
segment.set_style(self.style);
|
2019-06-10 14:56:17 +00:00
|
|
|
// Use the provided value unless overwritten by config
|
2019-07-14 15:15:47 +00:00
|
|
|
segment.set_value(self.config_value(name).unwrap_or(value));
|
2019-05-01 20:34:24 +00:00
|
|
|
self.segments.push(segment);
|
|
|
|
|
|
|
|
self.segments.last_mut().unwrap()
|
|
|
|
}
|
|
|
|
|
2019-05-14 04:43:11 +00:00
|
|
|
/// Whether a module has any segments
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.segments.is_empty()
|
|
|
|
}
|
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
/// Get the module's prefix
|
2019-07-31 23:48:51 +00:00
|
|
|
pub fn get_prefix(&mut self) -> &mut Affix {
|
2019-05-01 20:34:24 +00:00
|
|
|
&mut self.prefix
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the module's suffix
|
2019-07-31 23:48:51 +00:00
|
|
|
pub fn get_suffix(&mut self) -> &mut Affix {
|
2019-05-01 20:34:24 +00:00
|
|
|
&mut self.suffix
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the style of the segment.
|
|
|
|
///
|
|
|
|
/// Accepts either `Color` or `Style`.
|
2019-06-10 14:56:17 +00:00
|
|
|
pub fn set_style<T>(&mut self, style: T) -> &mut Module<'a>
|
2019-05-01 20:34:24 +00:00
|
|
|
where
|
|
|
|
T: Into<Style>,
|
|
|
|
{
|
|
|
|
self.style = style.into();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a vector of colored ANSIString elements to be later used with
|
|
|
|
/// `ANSIStrings()` to optimize ANSI codes
|
|
|
|
pub fn ansi_strings(&self) -> Vec<ANSIString> {
|
|
|
|
let mut ansi_strings = self
|
|
|
|
.segments
|
|
|
|
.iter()
|
2019-07-31 23:48:51 +00:00
|
|
|
.map(Segment::ansi_string)
|
2019-05-01 20:34:24 +00:00
|
|
|
.collect::<Vec<ANSIString>>();
|
|
|
|
|
|
|
|
ansi_strings.insert(0, self.prefix.ansi_string());
|
|
|
|
ansi_strings.push(self.suffix.ansi_string());
|
|
|
|
|
|
|
|
ansi_strings
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_string_without_prefix(&self) -> String {
|
|
|
|
ANSIStrings(&self.ansi_strings()[1..]).to_string()
|
|
|
|
}
|
2019-06-10 14:56:17 +00:00
|
|
|
|
|
|
|
/// Get a module's config value as a string
|
2019-07-14 15:15:47 +00:00
|
|
|
fn config_value(&self, key: &str) -> Option<&str> {
|
|
|
|
self.config.and_then(|config| config.get_as_str(key))
|
2019-06-10 14:56:17 +00:00
|
|
|
}
|
2019-07-29 01:05:13 +00:00
|
|
|
|
|
|
|
/// Get a module's config value as an int
|
|
|
|
pub fn config_value_i64(&self, key: &str) -> Option<i64> {
|
|
|
|
self.config.and_then(|config| config.get_as_i64(key))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a module's config value as a bool
|
|
|
|
pub fn config_value_bool(&self, key: &str) -> Option<bool> {
|
|
|
|
self.config.and_then(|config| config.get_as_bool(key))
|
|
|
|
}
|
2019-05-01 20:34:24 +00:00
|
|
|
}
|
|
|
|
|
2019-06-10 14:56:17 +00:00
|
|
|
impl<'a> fmt::Display for Module<'a> {
|
2019-05-01 20:34:24 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let ansi_strings = self.ansi_strings();
|
|
|
|
write!(f, "{}", ANSIStrings(&ansi_strings))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Module affixes are to be used for the prefix or suffix of a module.
|
2019-07-31 23:48:51 +00:00
|
|
|
pub struct Affix {
|
2019-05-01 20:34:24 +00:00
|
|
|
/// The affix's name, to be used in configuration and logging.
|
|
|
|
name: String,
|
|
|
|
|
|
|
|
/// The affix's style.
|
|
|
|
style: Style,
|
|
|
|
|
|
|
|
/// The string value of the affix.
|
|
|
|
value: String,
|
|
|
|
}
|
|
|
|
|
2019-07-31 23:48:51 +00:00
|
|
|
impl Affix {
|
|
|
|
pub fn default_prefix(name: &str) -> Self {
|
|
|
|
Self {
|
2019-05-01 20:34:24 +00:00
|
|
|
name: format!("{}_prefix", name),
|
|
|
|
style: Style::default(),
|
|
|
|
value: "via ".to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 23:48:51 +00:00
|
|
|
pub fn default_suffix(name: &str) -> Self {
|
|
|
|
Self {
|
2019-05-01 20:34:24 +00:00
|
|
|
name: format!("{}_suffix", name),
|
|
|
|
style: Style::default(),
|
|
|
|
value: " ".to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the style of the module.
|
|
|
|
///
|
|
|
|
/// Accepts either `Color` or `Style`.
|
2019-07-31 23:48:51 +00:00
|
|
|
pub fn set_style<T>(&mut self, style: T) -> &mut Self
|
2019-05-01 20:34:24 +00:00
|
|
|
where
|
|
|
|
T: Into<Style>,
|
|
|
|
{
|
|
|
|
self.style = style.into();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the value of the module.
|
2019-07-31 23:48:51 +00:00
|
|
|
pub fn set_value<T>(&mut self, value: T) -> &mut Self
|
2019-05-01 20:34:24 +00:00
|
|
|
where
|
|
|
|
T: Into<String>,
|
|
|
|
{
|
|
|
|
self.value = value.into();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generates the colored ANSIString output.
|
|
|
|
pub fn ansi_string(&self) -> ANSIString {
|
|
|
|
self.style.paint(&self.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 23:48:51 +00:00
|
|
|
impl fmt::Display for Affix {
|
2019-05-01 20:34:24 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.ansi_string())
|
|
|
|
}
|
|
|
|
}
|