1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-24 13:47:38 +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
/// `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;

View File

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

View File

@ -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),

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;
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();