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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use crate::config::Config;
use crate::segment::Segment;
use ansi_term::Style;
use ansi_term::{ANSIString, ANSIStrings};
use std::fmt;
pub const ALL_MODULES: &[&str] = &[
#[cfg(feature = "battery")]
"battery",
"character",
"cmd_duration",
"directory",
"git_branch",
"git_status",
"golang",
"jobs",
"line_break",
"nodejs",
"nix_shell",
"package",
"python",
"ruby",
"rust",
"username",
];
pub struct Module<'a> {
config: Option<&'a toml::value::Table>,
name: String,
style: Style,
prefix: Affix,
segments: Vec<Segment>,
suffix: Affix,
}
impl<'a> Module<'a> {
pub fn new(name: &str, config: Option<&'a toml::value::Table>) -> Module<'a> {
Module {
config,
name: name.to_string(),
style: Style::default(),
prefix: Affix::default_prefix(name),
segments: Vec::new(),
suffix: Affix::default_suffix(name),
}
}
pub fn new_segment(&mut self, name: &str, value: &str) -> &mut Segment {
let mut segment = Segment::new(name);
segment.set_style(self.style);
segment.set_value(self.config_value_str(name).unwrap_or(value));
self.segments.push(segment);
self.segments.last_mut().unwrap()
}
pub fn is_empty(&self) -> bool {
self.segments.is_empty()
}
pub fn get_prefix(&mut self) -> &mut Affix {
&mut self.prefix
}
pub fn get_suffix(&mut self) -> &mut Affix {
&mut self.suffix
}
pub fn set_style<T>(&mut self, style: T) -> &mut Module<'a>
where
T: Into<Style>,
{
self.style = style.into();
self
}
pub fn ansi_strings(&self) -> Vec<ANSIString> {
let shell = std::env::var("STARSHIP_SHELL").unwrap_or_default();
let ansi_strings = self
.segments
.iter()
.map(Segment::ansi_string)
.collect::<Vec<ANSIString>>();
let mut ansi_strings = match shell.as_str() {
"bash" => ansi_strings_modified(ansi_strings, shell),
"zsh" => ansi_strings_modified(ansi_strings, shell),
_ => ansi_strings,
};
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()
}
pub fn config_value_str(&self, key: &str) -> Option<&str> {
self.config.and_then(|config| config.get_as_str(key))
}
pub fn config_value_i64(&self, key: &str) -> Option<i64> {
self.config.and_then(|config| config.get_as_i64(key))
}
pub fn config_value_bool(&self, key: &str) -> Option<bool> {
self.config.and_then(|config| config.get_as_bool(key))
}
}
impl<'a> fmt::Display for Module<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let ansi_strings = self.ansi_strings();
write!(f, "{}", ANSIStrings(&ansi_strings))
}
}
fn ansi_strings_modified(ansi_strings: Vec<ANSIString>, shell: String) -> Vec<ANSIString> {
const ESCAPE_BEGIN: char = '\u{1b}';
const MAYBE_ESCAPE_END: char = 'm';
ansi_strings
.iter()
.map(|ansi| {
let mut escaped = false;
let final_string: String = ansi
.to_string()
.chars()
.map(|x| match x {
ESCAPE_BEGIN => {
escaped = true;
match shell.as_str() {
"bash" => String::from("\u{5c}\u{5b}\u{1b}"),
"zsh" => String::from("\u{25}\u{7b}\u{1b}"),
_ => x.to_string(),
}
}
MAYBE_ESCAPE_END => {
if escaped {
escaped = false;
match shell.as_str() {
"bash" => String::from("m\u{5c}\u{5d}"),
"zsh" => String::from("m\u{25}\u{7d}"),
_ => x.to_string(),
}
} else {
x.to_string()
}
}
_ => x.to_string(),
})
.collect();
ANSIString::from(final_string)
})
.collect::<Vec<ANSIString>>()
}
pub struct Affix {
name: String,
style: Style,
value: String,
}
impl Affix {
pub fn default_prefix(name: &str) -> Self {
Self {
name: format!("{}_prefix", name),
style: Style::default(),
value: "via ".to_string(),
}
}
pub fn default_suffix(name: &str) -> Self {
Self {
name: format!("{}_suffix", name),
style: Style::default(),
value: " ".to_string(),
}
}
pub fn set_style<T>(&mut self, style: T) -> &mut Self
where
T: Into<Style>,
{
self.style = style.into();
self
}
pub fn set_value<T>(&mut self, value: T) -> &mut Self
where
T: Into<String>,
{
self.value = value.into();
self
}
pub fn ansi_string(&self) -> ANSIString {
self.style.paint(&self.value)
}
}
impl fmt::Display for Affix {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.ansi_string())
}
}