1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-28 07:46:28 +00:00

Begin writing Node section

This commit is contained in:
Matan Kushner 2019-04-10 09:22:11 -04:00
parent 67ab2121c5
commit d5493d236d
No known key found for this signature in database
GPG Key ID: 4B98C3A8949CA8A4
5 changed files with 57 additions and 5 deletions

View File

@ -11,7 +11,7 @@ use clap::ArgMatches;
/// - If the exit-code was anything else, the arrow will be formatted with /// - If the exit-code was anything else, the arrow will be formatted with
/// `COLOR_FAILURE` (red by default) /// `COLOR_FAILURE` (red by default)
pub fn segment(args: &ArgMatches) -> Segment { pub fn segment(args: &ArgMatches) -> Segment {
const PROMPT_CHAR: &str = " "; const PROMPT_CHAR: &str = "";
const COLOR_SUCCESS: Color = Color::Green; const COLOR_SUCCESS: Color = Color::Green;
const COLOR_FAILURE: Color = Color::Red; const COLOR_FAILURE: Color = Color::Red;

View File

@ -16,7 +16,7 @@ use git2::Repository;
/// **Truncation** /// **Truncation**
/// Paths will be limited in length to `3` path components by default. /// Paths will be limited in length to `3` path components by default.
pub fn segment(_: &ArgMatches) -> Segment { pub fn segment(_: &ArgMatches) -> Segment {
const COLOR_DIR: Color = Color::Cyan; const SECTION_COLOR: Color = Color::Cyan;
const DIR_TRUNCATION_LENGTH: usize = 3; const DIR_TRUNCATION_LENGTH: usize = 3;
const HOME_SYMBOL: &str = "~"; const HOME_SYMBOL: &str = "~";
@ -49,7 +49,7 @@ pub fn segment(_: &ArgMatches) -> Segment {
Segment { Segment {
value: truncated_dir_string, value: truncated_dir_string,
style: Style::from(COLOR_DIR).bold(), style: Style::from(SECTION_COLOR).bold(),
..Default::default() ..Default::default()
} }
} }

View File

@ -1,6 +1,7 @@
mod character; mod character;
mod directory; mod directory;
mod line_break; mod line_break;
mod nodejs;
use ansi_term::Style; use ansi_term::Style;
use clap::ArgMatches; use clap::ArgMatches;
@ -32,8 +33,9 @@ impl Default for Segment {
pub fn handle(module: &str, args: &ArgMatches) -> Segment { pub fn handle(module: &str, args: &ArgMatches) -> Segment {
match module { match module {
"char" | "character" => character::segment(&args),
"dir" | "directory" => directory::segment(&args), "dir" | "directory" => directory::segment(&args),
"char" | "character" => character::segment(&args),
"node" | "nodejs" => nodejs::segment(&args),
"line_break" => line_break::segment(&args), "line_break" => line_break::segment(&args),
_ => panic!("Unknown module: {}", module), _ => panic!("Unknown module: {}", module),

50
src/modules/nodejs.rs Normal file
View File

@ -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));
}
}

View File

@ -5,7 +5,7 @@ use crate::modules;
use crate::modules::Segment; use crate::modules::Segment;
pub fn prompt(args: ArgMatches) { 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 stdout = io::stdout();
let mut handle = stdout.lock(); let mut handle = stdout.lock();