diff options
Diffstat (limited to 'src/modules/nodejs.rs')
-rw-r--r-- | src/modules/nodejs.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/modules/nodejs.rs b/src/modules/nodejs.rs new file mode 100644 index 000000000..16e76841b --- /dev/null +++ b/src/modules/nodejs.rs @@ -0,0 +1,50 @@ +use super::Segment; +use std::process::Command; +use ansi_term::{Color, Style}; +use clap::ArgMatches; + +/// Creates a segment with the current Node.js version +pub fn segment(args: &ArgMatches) -> Segment { + const NODE_CHAR: &str = "⬢ "; + const SECTION_COLOR: Color = Color::Green; + + let version = match Command::new("node").arg("--version").output() { + Ok(output) => String::from_utf8(output.stdout).unwrap(), + Err(e) => { + println!("{:?}", e); + return Segment::default(); + } + }; + + Segment { + value: format!("{}{}", NODE_CHAR, version), + style: Style::from(SECTION_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)); + } +} |