From e519c3f4a6ff633eb049f7fa57a3c06e032466b2 Mon Sep 17 00:00:00 2001 From: Matan Kushner Date: Wed, 3 Apr 2019 22:57:50 -0400 Subject: [PATCH] Set status with arg rather than env --- src/main.rs | 11 +++++++--- src/modules/char.rs | 51 +++++++++++++++++++++++++++++---------------- src/modules/mod.rs | 5 +++-- src/print.rs | 5 +++-- 4 files changed, 47 insertions(+), 25 deletions(-) diff --git a/src/main.rs b/src/main.rs index ab7e04e5..7cbe7d22 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ mod modules; mod print; use ansi_term::Style; -use clap::App; +use clap::{App, Arg}; pub struct Segment { style: Style, @@ -16,14 +16,19 @@ pub struct Segment { } fn main() { - App::new("Starship") + let args = App::new("Starship") .about("The cross-platform prompt for astronauts. ✨🚀") // pull the version number from Cargo.toml .version(crate_version!()) // pull the authors from Cargo.toml .author(crate_authors!()) .after_help("https://github.com/matchai/starship") + .arg( + Arg::with_name("status_code") + .help("The status code of the previously run command") + .required(true), + ) .get_matches(); - print::prompt(); + print::prompt(args); } diff --git a/src/modules/char.rs b/src/modules/char.rs index 96387c74..b5d6183a 100644 --- a/src/modules/char.rs +++ b/src/modules/char.rs @@ -1,34 +1,49 @@ use crate::Segment; use ansi_term::{Color, Style}; -use std::env; +use clap::ArgMatches; -pub fn segment() -> Segment { +pub fn segment(args: &ArgMatches) -> Segment { const PROMPT_CHAR: &str = "➜ "; const COLOR_SUCCESS: Color = Color::Green; const COLOR_FAILURE: Color = Color::Red; - let default_prefix = Segment { - value: String::from("testPrefix"), - style: Style::default(), - prefix: None, - suffix: None, - }; - let color; - if let Ok(status) = env::var("status") { - if status == "0" { - color = COLOR_SUCCESS; - } else { - color = COLOR_FAILURE; - } + if args.value_of("status_code").unwrap() == "0" { + color = COLOR_SUCCESS; } else { - panic!("No status environment variable provided"); + color = COLOR_FAILURE; } Segment { - prefix: Some(Box::new(default_prefix)), + prefix: None, value: String::from(PROMPT_CHAR), - style: Style::new().fg(color), + style: Style::from(color), suffix: None, } } + +#[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/modules/mod.rs b/src/modules/mod.rs index 8fe6d63a..52474cbc 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -1,10 +1,11 @@ mod char; use crate::Segment; +use clap::ArgMatches; -pub fn handle(module: &str) -> Segment { +pub fn handle(module: &str, args: &ArgMatches) -> Segment { match module { - "char" => char::segment(), + "char" => char::segment(&args), _ => panic!("Unknown module: {}", module), } diff --git a/src/print.rs b/src/print.rs index 2f7ca099..1fc6e535 100644 --- a/src/print.rs +++ b/src/print.rs @@ -1,11 +1,12 @@ use crate::modules; use crate::Segment; +use clap::ArgMatches; -pub fn prompt() { +pub fn prompt(args: ArgMatches) { let default_prompt = vec!["char"]; for module in default_prompt { - let segment = modules::handle(module); + let segment = modules::handle(module, &args); print_segment(segment); } }