starship/src/print.rs

66 lines
1.5 KiB
Rust
Raw Normal View History

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