summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMatan Kushner <hello@matchai.me>2019-04-10 09:22:11 -0400
committerMatan Kushner <hello@matchai.me>2019-04-10 09:22:11 -0400
commitd5493d236d402497509b7a3248dd8919bbb34c6c (patch)
treeeab7cbc2a99d817da27504b4fe851888b9b321b6 /src
parent67ab2121c578a59c076269597d1f2fcf71af2f57 (diff)
Begin writing Node section
Diffstat (limited to 'src')
-rw-r--r--src/modules/character.rs2
-rw-r--r--src/modules/directory.rs4
-rw-r--r--src/modules/mod.rs4
-rw-r--r--src/modules/nodejs.rs50
-rw-r--r--src/print.rs2
5 files changed, 57 insertions, 5 deletions
diff --git a/src/modules/character.rs b/src/modules/character.rs
index 7bdbe6387..4c83c5096 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 f7a95584f..20dcae3dc 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 87ce82e2e..a51904dd1 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 000000000..16e76841b
--- /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 55e05ed82..4251a7207 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();