diff options
author | Matan Kushner <hello@matchai.me> | 2019-04-04 20:33:36 -0400 |
---|---|---|
committer | Matan Kushner <hello@matchai.me> | 2019-04-04 20:33:36 -0400 |
commit | 168d568d549cf55848a33921403f5f1be577742e (patch) | |
tree | c9c00921a4ad42cbd3bdf3877088cf587eab3cf0 | |
parent | 52a529c62736d5186453f095b15b0ce038ec72a7 (diff) |
Add line_sep section
-rw-r--r-- | benches/benchmarks.rs | 4 | ||||
-rw-r--r-- | src/main.rs | 3 | ||||
-rw-r--r-- | src/modules/line_sep.rs | 13 | ||||
-rw-r--r-- | src/modules/mod.rs | 2 | ||||
-rw-r--r-- | src/print.rs | 26 |
5 files changed, 35 insertions, 13 deletions
diff --git a/benches/benchmarks.rs b/benches/benchmarks.rs index 42a763b11..e72d9b090 100644 --- a/benches/benchmarks.rs +++ b/benches/benchmarks.rs @@ -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) }); } } diff --git a/src/main.rs b/src/main.rs index d6694e3dd..7b10ddeba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,8 @@ #[macro_use] extern crate clap; +extern crate rayon; +extern crate ansi_term; +extern crate dirs; mod modules; mod print; diff --git a/src/modules/line_sep.rs b/src/modules/line_sep.rs new file mode 100644 index 000000000..dda14b8f8 --- /dev/null +++ b/src/modules/line_sep.rs @@ -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() + } +} diff --git a/src/modules/mod.rs b/src/modules/mod.rs index 13b18fa13..e55ac6366 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -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), } diff --git a/src/print.rs b/src/print.rs index c2951967d..50c691da2 100644 --- a/src/print.rs +++ b/src/print.rs @@ -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 } |