1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-16 10:05:13 +00:00
starship/src/modules/character.rs

56 lines
1.5 KiB
Rust
Raw Normal View History

2019-04-04 16:18:02 +00:00
use super::Segment;
2019-04-04 00:14:26 +00:00
use ansi_term::{Color, Style};
2019-04-04 02:57:50 +00:00
use clap::ArgMatches;
2019-04-04 00:14:26 +00:00
2019-04-04 18:18:15 +00:00
/// Creates a segment for the prompt character
2019-04-05 01:35:24 +00:00
///
2019-04-04 16:18:02 +00:00
/// 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)
2019-04-04 02:57:50 +00:00
pub fn segment(args: &ArgMatches) -> Segment {
2019-04-04 00:14:26 +00:00
const PROMPT_CHAR: &str = "";
const COLOR_SUCCESS: Color = Color::Green;
const COLOR_FAILURE: Color = Color::Red;
2019-04-08 03:28:38 +00:00
let color = if args.value_of("status_code").unwrap() == "0" {
COLOR_SUCCESS
2019-04-04 00:14:26 +00:00
} else {
2019-04-08 03:28:38 +00:00
COLOR_FAILURE
};
2019-04-04 00:14:26 +00:00
Segment {
value: String::from(PROMPT_CHAR),
2019-04-04 02:57:50 +00:00
style: Style::from(color),
2019-04-04 16:18:02 +00:00
..Default::default()
2019-04-04 00:14:26 +00:00
}
}
2019-04-04 02:57:50 +00:00
#[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));
}
}