Skip to content

Commit

Permalink
Add line_sep section
Browse files Browse the repository at this point in the history
  • Loading branch information
Matan Kushner committed Apr 5, 2019
1 parent 52a529c commit 168d568
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
4 changes: 2 additions & 2 deletions benches/benchmarks.rs
Expand Up @@ -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)
});
}

Expand All @@ -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)
});
}
}
3 changes: 3 additions & 0 deletions 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;
Expand Down
13 changes: 13 additions & 0 deletions 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()
}
}
2 changes: 2 additions & 0 deletions src/modules/mod.rs
@@ -1,5 +1,6 @@
mod char;
mod dir;
mod line_sep;

use clap::ArgMatches;
use ansi_term::Style;
Expand Down Expand Up @@ -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),
}
Expand Down
26 changes: 15 additions & 11 deletions src/print.rs
Expand Up @@ -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
}

0 comments on commit 168d568

Please sign in to comment.