From 82cf484cedd0e989b39af760d6cc83b500454610 Mon Sep 17 00:00:00 2001 From: John Letey <30328854+johnletey@users.noreply.github.com> Date: Mon, 12 Aug 2019 18:42:33 +0100 Subject: feat: Implement the prompt module for jobs (#85) --- src/init.rs | 8 ++++---- src/main.rs | 13 +++++++++++-- src/modules/jobs.rs | 32 ++++++++++++++++++++++++++++++++ src/modules/mod.rs | 2 ++ src/print.rs | 1 + 5 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 src/modules/jobs.rs (limited to 'src') diff --git a/src/init.rs b/src/init.rs index 3941e92dc..a80dcd5ca 100644 --- a/src/init.rs +++ b/src/init.rs @@ -58,7 +58,7 @@ https://github.com/starship/starship/issues/124 const BASH_INIT: &str = r##" starship_precmd() { - PS1="$(starship prompt --status=$?)"; + PS1="$(starship prompt --status=$? --jobs=$(jobs -p | wc -l))"; }; PROMPT_COMMAND=starship_precmd; "##; @@ -83,10 +83,10 @@ starship_precmd() { if [[ $STARSHIP_START_TIME ]]; then STARSHIP_END_TIME="$(date +%s)"; STARSHIP_DURATION=$((STARSHIP_END_TIME - STARSHIP_START_TIME)); - PROMPT="$(starship prompt --status=$STATUS --cmd-duration=$STARSHIP_DURATION)"; + PROMPT="$(starship prompt --status=$STATUS --cmd-duration=$STARSHIP_DURATION --jobs=$(jobs | wc -l))"; unset STARSHIP_START_TIME; else - PROMPT="$(starship prompt --status=$STATUS)"; + PROMPT="$(starship prompt --status=$STATUS --jobs=$(jobs | wc -l))"; fi }; starship_preexec(){ @@ -108,6 +108,6 @@ function fish_prompt; set -l exit_code $status; set -l CMD_DURATION "$CMD_DURATION$cmd_duration"; set -l starship_duration (math --scale=0 "$CMD_DURATION / 1000"); - starship prompt --status=$exit_code --cmd-duration=$starship_duration; + starship prompt --status=$exit_code --cmd-duration=$starship_duration --jobs=(count (jobs -p)); end; "##; diff --git a/src/main.rs b/src/main.rs index fa6f1488b..c95b5fd21 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,6 +43,13 @@ fn main() { .help("The execution duration of the last command, in seconds") .takes_value(true); + let jobs_arg = Arg::with_name("jobs") + .short("j") + .long("jobs") + .value_name("JOBS") + .help("The number of currently running jobs") + .takes_value(true); + let matches = App::new("starship") .about("The cross-shell prompt for astronauts. ☄🌌️") // pull the version number from Cargo.toml @@ -61,7 +68,8 @@ fn main() { .about("Prints the full starship prompt") .arg(&status_code_arg) .arg(&path_arg) - .arg(&cmd_duration_arg), + .arg(&cmd_duration_arg) + .arg(&jobs_arg), ) .subcommand( SubCommand::with_name("module") @@ -73,7 +81,8 @@ fn main() { ) .arg(&status_code_arg) .arg(&path_arg) - .arg(&cmd_duration_arg), + .arg(&cmd_duration_arg) + .arg(&jobs_arg), ) .get_matches(); diff --git a/src/modules/jobs.rs b/src/modules/jobs.rs new file mode 100644 index 000000000..4a89a1af3 --- /dev/null +++ b/src/modules/jobs.rs @@ -0,0 +1,32 @@ +use ansi_term::Color; + +use super::{Context, Module}; + +/// Creates a segment to show if there are any active jobs running +pub fn module<'a>(context: &'a Context) -> Option> { + let mut module = context.new_module("jobs")?; + + let threshold = module.config_value_i64("threshold").unwrap_or(1); + + const JOB_CHAR: &str = "✦"; + let module_color = Color::Blue.bold(); + + module.set_style(module_color); + + let arguments = &context.arguments; + let num_of_jobs = arguments + .value_of("jobs") + .unwrap_or("0") + .parse::() + .ok()?; + if num_of_jobs == 0 { + return None; + } + module.new_segment("symbol", JOB_CHAR); + if num_of_jobs > threshold { + module.new_segment("number", &num_of_jobs.to_string()); + } + module.get_prefix().set_value(""); + + Some(module) +} diff --git a/src/modules/mod.rs b/src/modules/mod.rs index 5f3fcbbaf..9171f2a3e 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -5,6 +5,7 @@ mod directory; mod git_branch; mod git_status; mod golang; +mod jobs; mod line_break; mod nodejs; mod package; @@ -30,6 +31,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option> { "username" => username::module(context), "battery" => battery::module(context), "cmd_duration" => cmd_duration::module(context), + "jobs" => jobs::module(context), _ => panic!("Unknown module: {}", module), } diff --git a/src/print.rs b/src/print.rs index 400020baa..6526991b3 100644 --- a/src/print.rs +++ b/src/print.rs @@ -19,6 +19,7 @@ const PROMPT_ORDER: &[&str] = &[ "go", "cmd_duration", "line_break", + "jobs", "battery", "character", ]; -- cgit v1.2.3