2019-06-10 14:56:17 +00:00
|
|
|
use ansi_term::{ANSIString, Style};
|
2019-05-01 20:34:24 +00:00
|
|
|
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).
|
2020-04-06 17:16:18 +00:00
|
|
|
#[derive(Clone)]
|
2019-04-12 21:49:20 +00:00
|
|
|
pub struct Segment {
|
2019-05-01 20:34:24 +00:00
|
|
|
/// The segment's style. If None, will inherit the style of the module containing it.
|
2020-04-06 17:16:18 +00:00
|
|
|
pub style: Option<Style>,
|
2019-05-01 20:34:24 +00:00
|
|
|
|
|
|
|
/// The string value of the current segment.
|
2020-04-06 17:16:18 +00:00
|
|
|
pub value: String,
|
2019-04-12 21:49:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Segment {
|
2020-08-17 02:16:05 +00:00
|
|
|
/// Creates a new segment.
|
|
|
|
pub fn new<T>(style: Option<Style>, value: T) -> Self
|
2019-04-12 23:11:40 +00:00
|
|
|
where
|
|
|
|
T: Into<String>,
|
|
|
|
{
|
2020-08-17 02:16:05 +00:00
|
|
|
Self {
|
|
|
|
style,
|
|
|
|
value: value.into(),
|
|
|
|
}
|
2020-01-02 04:19:08 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
// Returns the ANSIString of the segment value, not including its prefix and suffix
|
|
|
|
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-06-10 14:56:17 +00:00
|
|
|
impl fmt::Display for Segment {
|
2019-05-01 20:34:24 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.ansi_string())
|
|
|
|
}
|
|
|
|
}
|