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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
use crate::utils;
use std::env;
use dirs::home_dir;
use toml::value::Table;
use ansi_term::Color;
pub trait Config {
fn initialize() -> Table;
fn config_from_file() -> Option<Table>;
fn get_module_config(&self, module_name: &str) -> Option<&Table>;
// Config accessor methods
fn get_as_bool(&self, key: &str) -> Option<bool>;
fn get_as_str(&self, key: &str) -> Option<&str>;
fn get_as_i64(&self, key: &str) -> Option<i64>;
fn get_as_array(&self, key: &str) -> Option<&Vec<toml::value::Value>>;
fn get_as_ansi_style(&self, key: &str) -> Option<ansi_term::Style>;
// Internal implementation for accessors
fn get_config(&self, key: &str) -> Option<&toml::value::Value>;
}
impl Config for Table {
/// Initialize the Config struct
fn initialize() -> Table {
if let Some(file_data) = Self::config_from_file() {
return file_data;
}
Self::new()
}
/// Create a config from a starship configuration file
fn config_from_file() -> Option<Table> {
let file_path = if let Ok(path) = env::var("STARSHIP_CONFIG") {
// Use $STARSHIP_CONFIG as the config path if available
log::debug!("STARSHIP_CONFIG is set: \n{}", &path);
path
} else {
// Default to using ~/.config/starship.toml
log::debug!("STARSHIP_CONFIG is not set");
let config_path = home_dir()?.join(".config/starship.toml");
let config_path_str = config_path.to_str()?.to_owned();
log::debug!("Using default config path: {}", config_path_str);
config_path_str
};
let toml_content = match utils::read_file(&file_path) {
Ok(content) => {
log::trace!("Config file content: \n{}", &content);
Some(content)
}
Err(e) => {
log::debug!("Unable to read config file content: \n{}", &e);
None
}
}?;
let config = toml::from_str(&toml_content).ok()?;
log::debug!("Config parsed: \n{:?}", &config);
Some(config)
}
/// Get the subset of the table for a module by its name
fn get_module_config(&self, module_name: &str) -> Option<&toml::value::Table> {
let module_config = self.get(module_name).and_then(toml::Value::as_table);
if module_config.is_some() {
log::debug!(
"Config found for \"{}\": \n{:?}",
&module_name,
&module_config
);
} else {
log::trace!("No config found for \"{}\"", &module_name);
}
module_config
}
/// Get the config value for a given key
fn get_config(&self, key: &str) -> Option<&toml::value::Value> {
log::trace!("Looking for config key \"{}\"", key);
let config_value = self.get(key);
if config_value.is_some() {
log::trace!("Config found for \"{}\": {:?}", key, &config_value);
} else {
log::trace!("No value found for \"{}\"", key);
}
config_value
}
/// Get a key from a module's configuration as a boolean
fn get_as_bool(&self, key: &str) -> Option<bool> {
let value = self.get_config(key)?;
let bool_value = value.as_bool();
if bool_value.is_none() {
log::debug!(
"Expected \"{}\" to be a boolean. Instead received {} of type {}.",
key,
value,
value.type_str()
);
}
bool_value
}
/// Get a key from a module's configuration as a string
fn get_as_str(&self, key: &str) -> Option<&str> {
let value = self.get_config(key)?;
let str_value = value.as_str();
if str_value.is_none() {
log::debug!(
"Expected \"{}\" to be a string. Instead received {} of type {}.",
key,
value,
value.type_str()
);
}
str_value
}
/// Get a key from a module's configuration as an integer
fn get_as_i64(&self, key: &str) -> Option<i64> {
let value = self.get_config(key)?;
let i64_value = value.as_integer();
if i64_value.is_none() {
log::debug!(
"Expected \"{}\" to be an integer. Instead received {} of type {}.",
key,
value,
value.type_str()
);
}
i64_value
}
/// Get a key from a module's configuration as a vector
fn get_as_array(&self, key: &str) -> Option<&Vec<toml::value::Value>> {
let value = self.get_config(key)?;
let array_value = value.as_array();
if array_value.is_none() {
log::debug!(
"Expected \"{}\" to be a array. Instead received {} of type {}.",
key,
value,
value.type_str()
);
}
array_value
}
/// Get a text key and attempt to interpret it into an ANSI style.
fn get_as_ansi_style(&self, key: &str) -> Option<ansi_term::Style> {
let style_string = self.get_as_str(key)?;
parse_style_string(style_string)
}
}
/** Parse a style string which represents an ansi style. Valid tokens in the style
string include the following:
- 'fg:<color>' (specifies that the color read should be a foreground color)
- 'bg:<color>' (specifies that the color read should be a background color)
- 'underline'
- 'bold'
- 'italic'
- '<color>' (see the parse_color_string doc for valid color strings)
*/
fn parse_style_string(style_string: &str) -> Option<ansi_term::Style> {
let tokens = style_string.split_whitespace();
let mut style = ansi_term::Style::new();
// If col_fg is true, color the foreground. If it's false, color the background.
let mut col_fg: bool;
for token in tokens {
let token = token.to_lowercase();
// Check for FG/BG identifiers and strip them off if appropriate
let token = if token.as_str().starts_with("fg:") {
col_fg = true;
token.trim_start_matches("fg:").to_owned()
} else if token.as_str().starts_with("bg:") {
col_fg = false;
token.trim_start_matches("bg:").to_owned()
} else {
col_fg = true; // Bare colors are assumed to color the foreground
token
};
match token.as_str() {
"underline" => style = style.underline(),
"bold" => style = style.bold(),
"italic" => style = style.italic(),
"dimmed" => style = style.dimmed(),
"none" => return Some(ansi_term::Style::new()), // Overrides other toks
// Try to see if this token parses as a valid color string
color_string => {
// Match found: set either fg or bg color
if let Some(ansi_color) = parse_color_string(color_string) {
if col_fg {
style = style.fg(ansi_color);
} else {
style = style.on(ansi_color);
}
} else {
// Match failed: skip this token and log it
log::debug!("Could not parse token in color string: {}", token)
}
}
}
}
Some(style)
}
/** Parse a string that represents a color setting, returning None if this fails
There are three valid color formats:
- #RRGGBB (a hash followed by an RGB hex)
- u8 (a number from 0-255, representing an ANSI color)
- colstring (one of the 16 predefined color strings)
*/
fn parse_color_string(color_string: &str) -> Option<ansi_term::Color>
|