1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-11 07:40:57 +00:00
starship/src/segment.rs
Tilmann Meyer bcdf522af5
chore: remove unused name from Segment and remove some of the misleading underscores (#1584)
* chore: Remove unused name from Segment and remove some of the misleading underscores

* chore: Access members of `Segment` directly
2020-08-16 22:16:05 -04:00

42 lines
1.1 KiB
Rust

use ansi_term::{ANSIString, Style};
use std::fmt;
/// 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).
#[derive(Clone)]
pub struct Segment {
/// The segment's style. If None, will inherit the style of the module containing it.
pub style: Option<Style>,
/// The string value of the current segment.
pub value: String,
}
impl Segment {
/// Creates a new segment.
pub fn new<T>(style: Option<Style>, value: T) -> Self
where
T: Into<String>,
{
Self {
style,
value: value.into(),
}
}
// 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),
}
}
}
impl fmt::Display for Segment {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.ansi_string())
}
}