From d5493d236d402497509b7a3248dd8919bbb34c6c Mon Sep 17 00:00:00 2001 From: Matan Kushner Date: Wed, 10 Apr 2019 09:22:11 -0400 Subject: [PATCH] Begin writing Node section --- src/modules/character.rs | 2 +- src/modules/directory.rs | 4 ++-- src/modules/mod.rs | 4 +++- src/modules/nodejs.rs | 50 ++++++++++++++++++++++++++++++++++++++++ src/print.rs | 2 +- 5 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 src/modules/nodejs.rs diff --git a/src/modules/character.rs b/src/modules/character.rs index 7bdbe638..4c83c509 100644 --- a/src/modules/character.rs +++ b/src/modules/character.rs @@ -11,7 +11,7 @@ use clap::ArgMatches; /// - 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 PROMPT_CHAR: &str = "➜"; const COLOR_SUCCESS: Color = Color::Green; const COLOR_FAILURE: Color = Color::Red; diff --git a/src/modules/directory.rs b/src/modules/directory.rs index f7a95584..20dcae3d 100644 --- a/src/modules/directory.rs +++ b/src/modules/directory.rs @@ -16,7 +16,7 @@ use git2::Repository; /// **Truncation** /// Paths will be limited in length to `3` path components by default. pub fn segment(_: &ArgMatches) -> Segment { - const COLOR_DIR: Color = Color::Cyan; + const SECTION_COLOR: Color = Color::Cyan; const DIR_TRUNCATION_LENGTH: usize = 3; const HOME_SYMBOL: &str = "~"; @@ -49,7 +49,7 @@ pub fn segment(_: &ArgMatches) -> Segment { Segment { value: truncated_dir_string, - style: Style::from(COLOR_DIR).bold(), + style: Style::from(SECTION_COLOR).bold(), ..Default::default() } } diff --git a/src/modules/mod.rs b/src/modules/mod.rs index 87ce82e2..a51904dd 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -1,6 +1,7 @@ mod character; mod directory; mod line_break; +mod nodejs; use ansi_term::Style; use clap::ArgMatches; @@ -32,8 +33,9 @@ impl Default for Segment { pub fn handle(module: &str, args: &ArgMatches) -> Segment { match module { - "char" | "character" => character::segment(&args), "dir" | "directory" => directory::segment(&args), + "char" | "character" => character::segment(&args), + "node" | "nodejs" => nodejs::segment(&args), "line_break" => line_break::segment(&args), _ => panic!("Unknown module: {}", module), diff --git a/src/modules/nodejs.rs b/src/modules/nodejs.rs new file mode 100644 index 00000000..16e76841 --- /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)); + } +} diff --git a/src/print.rs b/src/print.rs index 55e05ed8..4251a720 100644 --- a/src/print.rs +++ b/src/print.rs @@ -5,7 +5,7 @@ use crate::modules; use crate::modules::Segment; pub fn prompt(args: ArgMatches) { - let default_prompt = vec!["directory", "line_break", "character"]; + let default_prompt = vec!["directory", "node", "line_break", "character"]; let stdout = io::stdout(); let mut handle = stdout.lock();