diff options
author | Matan Kushner <hello@matchai.me> | 2019-04-04 21:35:24 -0400 |
---|---|---|
committer | Matan Kushner <hello@matchai.me> | 2019-04-04 21:35:24 -0400 |
commit | c79cbe63b147b3e6e490f662f06d317e441ad2c4 (patch) | |
tree | 704418a5cab61f354aeff8c2c1850796267acde9 /src/modules/character.rs | |
parent | 472b66894d10e5be9cfb5a40b79dc61e746c0795 (diff) |
Add stringify_segment rustdoc
Diffstat (limited to 'src/modules/character.rs')
-rw-r--r-- | src/modules/character.rs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/modules/character.rs b/src/modules/character.rs new file mode 100644 index 000000000..75d15ddc6 --- /dev/null +++ b/src/modules/character.rs @@ -0,0 +1,56 @@ +use super::Segment; +use ansi_term::{Color, Style}; +use clap::ArgMatches; + +/// Creates a segment for the prompt character +/// +/// The char segment prints an arrow character in a color dependant on the exit- +/// code of the last executed command: +/// - If the exit-code was "0", the arrow will be formatted with `COLOR_SUCCESS` +/// (green by default) +/// - If the exit-code was anything else, the arrow will be formatted with +/// `COLOR_FAILURE` (red by default) +pub fn segment(args: &ArgMatches) -> Segment { + const PROMPT_CHAR: &str = "➜ "; + const COLOR_SUCCESS: Color = Color::Green; + const COLOR_FAILURE: Color = Color::Red; + + let color; + if args.value_of("status_code").unwrap() == "0" { + color = COLOR_SUCCESS; + } else { + color = COLOR_FAILURE; + } + + Segment { + value: String::from(PROMPT_CHAR), + style: Style::from(color), + ..Default::default() + } +} + +#[cfg(test)] +mod tests { + use super::*; + use clap::{App, Arg}; + + #[test] + fn char_section_success_status() { + let args = App::new("starship") + .arg(Arg::with_name("status_code")) + .get_matches_from(vec!["starship", "0"]); + + let segment = segment(&args); + assert_eq!(segment.style, Style::from(Color::Green)); + } + + #[test] + fn char_section_failure_status() { + let args = App::new("starship") + .arg(Arg::with_name("status_code")) + .get_matches_from(vec!["starship", "1"]); + + let segment = segment(&args); + assert_eq!(segment.style, Style::from(Color::Red)); + } +} |