1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-06-12 21:32:21 +00:00
starship/src/print.rs

66 lines
1.5 KiB
Rust
Raw Normal View History

2019-04-08 21:35:38 +00:00
use clap::ArgMatches;
2019-04-11 23:31:30 +00:00
use std::io::{self, Write};
2019-04-08 21:35:38 +00:00
2019-04-04 00:14:26 +00:00
use crate::modules;
2019-04-04 16:18:02 +00:00
use crate::modules::Segment;
2019-04-04 00:14:26 +00:00
2019-04-04 02:57:50 +00:00
pub fn prompt(args: ArgMatches) {
2019-04-10 13:22:11 +00:00
let default_prompt = vec!["directory", "node", "line_break", "character"];
2019-04-04 00:14:26 +00:00
2019-04-11 23:31:30 +00:00
// TODO:
// - List files in directory
// - Index binaries in PATH
2019-04-08 21:35:38 +00:00
let stdout = io::stdout();
let mut handle = stdout.lock();
// Write a new line before the prompt
write!(handle, "{}", "\n").unwrap();
2019-04-11 23:31:30 +00:00
2019-04-05 01:35:24 +00:00
default_prompt
.into_iter()
2019-04-05 00:33:36 +00:00
.map(|module| modules::handle(module, &args))
2019-04-08 03:28:38 +00:00
.map(stringify_segment)
2019-04-08 21:35:38 +00:00
.for_each(|segment_string| write!(handle, "{}", segment_string).unwrap());
2019-04-04 00:14:26 +00:00
}
2019-04-05 01:35:24 +00:00
/// 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, "-> ");
/// ```
2019-04-05 00:33:36 +00:00
pub fn stringify_segment(segment: Segment) -> String {
2019-04-04 00:14:26 +00:00
let Segment {
prefix,
value,
style,
suffix,
2019-04-05 01:35:24 +00:00
} = segment;
2019-04-05 00:33:36 +00:00
let mut segment_string = String::new();
2019-04-04 00:14:26 +00:00
if let Some(prefix) = prefix {
2019-04-05 00:33:36 +00:00
segment_string += &stringify_segment(*prefix);
2019-04-04 00:14:26 +00:00
}
2019-04-05 01:35:24 +00:00
2019-04-05 00:33:36 +00:00
segment_string += &style.paint(value).to_string();
2019-04-04 00:14:26 +00:00
if let Some(suffix) = suffix {
2019-04-05 00:33:36 +00:00
segment_string += &stringify_segment(*suffix);
2019-04-04 00:14:26 +00:00
}
2019-04-05 00:33:36 +00:00
segment_string
2019-04-04 00:14:26 +00:00
}