1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
use ansi_term::{ANSIString, ANSIStrings, 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).
pub struct Segment {
/// 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.
value: String,
/// The suffix used following the contents of a segment.
suffix: Option<SegmentAffix>,
}
impl Segment {
/// Creates a new segment with default fields.
pub fn new(name: &str) -> Segment {
Segment {
name: name.to_string(),
style: None,
prefix: None,
value: "".to_string(),
suffix: None,
}
}
/// Sets the style of the segment.
///
/// Accepts either `Color` or `Style`.
pub fn set_style<T>(&mut self, style: T) -> &mut Segment
where
T: Into<Style>,
{
self.style = Some(style.into());
self
}
/// Sets the value of the segment.
pub fn set_value<T>(&mut self, value: T) -> &mut Segment
where
T: Into<String>,
{
self.value = value.into();
self
}
// 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),
}
}
/// 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>>()
}
}
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))
}
}
/// 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,
/// The affix's style. If None, will inherit the style of the segment containing it.
style: Option<Style>,
/// The string value of the affix.
value: String,
}
impl SegmentAffix {
/// Creates a segment affix with no contents.
pub fn new() -> SegmentAffix {
SegmentAffix {
name: String::new(),
style: None,
value: String::new(),
}
}
/// 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),
}
}
}
impl fmt::Display for SegmentAffix {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.ansi_string())
}
}
|