2019-05-01 20:34:24 +00:00
|
|
|
use ansi_term::{ANSIString, ANSIStrings, Style};
|
|
|
|
use std::fmt;
|
2019-04-12 21:49:20 +00:00
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
/// A segment is a single configurable element in a module. This will usually
|
|
|
|
/// contain a data point to provide context for the prompt's user
|
|
|
|
/// (e.g. The version that software is running).
|
2019-04-12 21:49:20 +00:00
|
|
|
pub struct Segment {
|
2019-05-01 20:34:24 +00:00
|
|
|
/// The segment's name, to be used in configuration and logging.
|
|
|
|
name: String,
|
|
|
|
|
|
|
|
/// The segment's style. If None, will inherit the style of the module containing it.
|
|
|
|
style: Option<Style>,
|
|
|
|
|
|
|
|
/// The prefix used to preceed the contents of a segment.
|
|
|
|
prefix: Option<SegmentAffix>,
|
|
|
|
|
|
|
|
/// The string value of the current segment.
|
2019-04-12 21:49:20 +00:00
|
|
|
value: String,
|
2019-05-01 20:34:24 +00:00
|
|
|
|
|
|
|
/// The suffix used following the contents of a segment.
|
|
|
|
suffix: Option<SegmentAffix>,
|
2019-04-12 21:49:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Segment {
|
2019-05-01 20:34:24 +00:00
|
|
|
/// Creates a new segment with default fields.
|
|
|
|
pub fn new(name: &str) -> Segment {
|
|
|
|
Segment {
|
|
|
|
name: name.to_string(),
|
|
|
|
style: None,
|
2019-04-12 21:49:20 +00:00
|
|
|
prefix: None,
|
2019-05-01 20:34:24 +00:00
|
|
|
value: "".to_string(),
|
2019-04-12 21:49:20 +00:00
|
|
|
suffix: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
/// Sets the style of the segment.
|
2019-04-13 04:33:50 +00:00
|
|
|
///
|
2019-04-13 03:11:00 +00:00
|
|
|
/// Accepts either `Color` or `Style`.
|
2019-04-12 23:18:47 +00:00
|
|
|
pub fn set_style<T>(&mut self, style: T) -> &mut Segment
|
2019-04-12 22:12:29 +00:00
|
|
|
where
|
|
|
|
T: Into<Style>,
|
|
|
|
{
|
2019-05-01 20:34:24 +00:00
|
|
|
self.style = Some(style.into());
|
2019-04-12 21:49:20 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
/// Sets the value of the segment.
|
2019-04-12 23:18:47 +00:00
|
|
|
pub fn set_value<T>(&mut self, value: T) -> &mut Segment
|
2019-04-12 23:11:40 +00:00
|
|
|
where
|
|
|
|
T: Into<String>,
|
|
|
|
{
|
|
|
|
self.value = value.into();
|
2019-04-12 21:49:20 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
// Returns the ANSIString of the segment value, not including its prefix and suffix
|
|
|
|
fn value_ansi_string(&self) -> ANSIString {
|
|
|
|
match self.style {
|
|
|
|
Some(style) => style.paint(&self.value),
|
|
|
|
None => ANSIString::from(&self.value),
|
|
|
|
}
|
2019-04-12 21:49:20 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
/// 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 prefix = self.prefix.as_ref().and_then(|p| Some(p.ansi_string()));
|
|
|
|
let suffix = self.suffix.as_ref().and_then(|s| Some(s.ansi_string()));
|
|
|
|
let value = Some(self.value_ansi_string());
|
|
|
|
|
|
|
|
// Remove `None` values from the vector
|
|
|
|
vec![prefix, value, suffix]
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|e| e)
|
|
|
|
.collect::<Vec<ANSIString>>()
|
2019-04-12 21:49:20 +00:00
|
|
|
}
|
2019-05-01 20:34:24 +00:00
|
|
|
}
|
2019-04-12 21:49:20 +00:00
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
impl fmt::Display for Segment {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let ansi_strings = self.ansi_strings();
|
|
|
|
write!(f, "{}", ANSIStrings(&ansi_strings))
|
|
|
|
}
|
|
|
|
}
|
2019-04-15 15:40:18 +00:00
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
/// Segment affixes are to be used for the prefix or suffix of a segment.
|
|
|
|
/// By default they will inherit the styling of its segment, unless otherwise specified.
|
|
|
|
pub struct SegmentAffix {
|
|
|
|
/// The affix's name, to be used in configuration and logging.
|
|
|
|
name: String,
|
2019-04-15 15:40:18 +00:00
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
/// The affix's style. If None, will inherit the style of the segment containing it.
|
|
|
|
style: Option<Style>,
|
2019-04-15 15:40:18 +00:00
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
/// The string value of the affix.
|
|
|
|
value: String,
|
|
|
|
}
|
2019-04-15 15:40:18 +00:00
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
impl SegmentAffix {
|
|
|
|
/// Creates a segment affix with no contents.
|
|
|
|
pub fn new() -> SegmentAffix {
|
|
|
|
SegmentAffix {
|
|
|
|
name: String::new(),
|
|
|
|
style: None,
|
|
|
|
value: String::new(),
|
2019-04-12 21:49:20 +00:00
|
|
|
}
|
2019-05-01 20:34:24 +00:00
|
|
|
}
|
2019-04-12 21:49:20 +00:00
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
/// Generates the colored ANSIString output.
|
|
|
|
pub fn ansi_string(&self) -> ANSIString {
|
|
|
|
match self.style {
|
|
|
|
Some(style) => style.paint(&self.value),
|
|
|
|
None => ANSIString::from(&self.value),
|
2019-04-12 21:49:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
impl fmt::Display for SegmentAffix {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.ansi_string())
|
|
|
|
}
|
|
|
|
}
|