Add line_sep section

This commit is contained in:
Matan Kushner 2019-04-04 20:33:36 -04:00
parent 52a529c627
commit 168d568d54
No known key found for this signature in database
GPG Key ID: 4B98C3A8949CA8A4
5 changed files with 35 additions and 13 deletions

View File

@ -27,7 +27,7 @@ mod tests {
.get_matches_from(vec!["starship", "0"]);
let segment = modules::handle("char", &args);
print::print_segment(segment)
print::stringify_segment(segment)
});
}
@ -39,7 +39,7 @@ mod tests {
.get_matches_from(vec!["starship", "0"]);
let segment = modules::handle("dir", &args);
print::print_segment(segment)
print::stringify_segment(segment)
});
}
}

View File

@ -1,5 +1,8 @@
#[macro_use]
extern crate clap;
extern crate rayon;
extern crate ansi_term;
extern crate dirs;
mod modules;
mod print;

13
src/modules/line_sep.rs Normal file
View File

@ -0,0 +1,13 @@
use super::Segment;
use clap::ArgMatches;
/// Creates a segment for the line break
pub fn segment(_: &ArgMatches) -> Segment {
const LINE_ENDING: &str = "\n";
Segment {
value: String::from(LINE_ENDING),
suffix: None,
..Default::default()
}
}

View File

@ -1,5 +1,6 @@
mod char;
mod dir;
mod line_sep;
use clap::ArgMatches;
use ansi_term::Style;
@ -33,6 +34,7 @@ pub fn handle(module: &str, args: &ArgMatches) -> Segment {
match module {
"char" => char::segment(&args),
"dir" => dir::segment(&args),
"line_sep" => line_sep::segment(&args),
_ => panic!("Unknown module: {}", module),
}

View File

@ -3,29 +3,33 @@ use crate::modules::Segment;
use clap::ArgMatches;
pub fn prompt(args: ArgMatches) {
let default_prompt = vec!["dir", "char"];
let default_prompt = vec!["dir", "line_sep", "char"];
for module in default_prompt {
let segment = modules::handle(module, &args);
print_segment(segment);
}
default_prompt.into_iter()
.map(|module| modules::handle(module, &args))
.map(|segment| stringify_segment(segment))
.for_each(|segment_string| print!("{}", segment_string));
}
pub fn print_segment(segment: Segment) {
pub fn stringify_segment(segment: Segment) -> String {
let Segment {
prefix,
value,
style,
suffix,
} = segment;
} = segment;
let mut segment_string = String::new();
if let Some(prefix) = prefix {
print_segment(*prefix);
segment_string += &stringify_segment(*prefix);
}
print!("{}", style.paint(value));
segment_string += &style.paint(value).to_string();
if let Some(suffix) = suffix {
print_segment(*suffix);
segment_string += &stringify_segment(*suffix);
}
segment_string
}