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
|
use clap::ArgMatches;
use std::io::{self, Write};
use crate::modules;
use crate::modules::Segment;
pub fn prompt(args: ArgMatches) {
let default_prompt = vec!["directory", "nodejs", "line_break", "character"];
// TODO:
// - List files in directory
// - Index binaries in PATH
let stdout = io::stdout();
let mut handle = stdout.lock();
// Write a new line before the prompt
writeln!(handle).unwrap();
default_prompt
.into_iter()
.map(|module| modules::handle(module, &args))
.map(stringify_segment)
.for_each(|segment_string| write!(handle, "{}", segment_string).unwrap());
}
/// Create a string with the formatted contents of a segment
///
/// Will recursively also format the prefix and suffix of the segment being
/// stringified.
///
/// # Example
/// ```
/// use starship::modules::Segment;
///
/// let segment = Segment {
/// value: String::from("->"),
/// ..Default::default()
/// };
///
/// let result = starship::print::stringify_segment(segment);
/// assert_eq!(result, "-> ");
/// ```
pub fn stringify_segment(segment: Segment) -> String {
let Segment {
prefix,
value,
style,
suffix,
} = segment;
let mut segment_string = String::new();
if let Some(prefix) = prefix {
segment_string += &stringify_segment(*prefix);
}
segment_string += &style.paint(value).to_string();
if let Some(suffix) = suffix {
segment_string += &stringify_segment(*suffix);
}
segment_string
}
|