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: [PATCH] feat: Implement the prompt module for jobs (#85) --- README.md | 4 +- docs/config/README.md | 23 +++++++++++ src/init.rs | 8 ++-- src/main.rs | 13 ++++++- src/modules/jobs.rs | 32 ++++++++++++++++ src/modules/mod.rs | 2 + src/print.rs | 1 + tests/testsuite/jobs.rs | 85 +++++++++++++++++++++++++++++++++++++++++ tests/testsuite/main.rs | 1 + 9 files changed, 161 insertions(+), 8 deletions(-) create mode 100644 src/modules/jobs.rs create mode 100644 tests/testsuite/jobs.rs diff --git a/README.md b/README.md index 66828ef1a..14fac3607 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@
Starship is the minimal, blazing fast, and extremely customizable prompt for any shell!
@@ -46,7 +46,7 @@ The prompt shows information need while you're working, while staying sleek and
- `»` — renamed files
- `✘` — deleted files
- Execution time of the last command if it exceeds the set threshold.
-- [PLANNED #80](https://github.com/starship/starship/issues/80) – Indicator for jobs in the background (`✦`).
+- Indicator for jobs in the background (`✦`).
## 🚀 Installation
diff --git a/docs/config/README.md b/docs/config/README.md
index 004fe9521..8177cec59 100644
--- a/docs/config/README.md
+++ b/docs/config/README.md
@@ -250,6 +250,29 @@ The module will be shown if any of the following conditions are met:
symbol = "🏎💨 "
```
+## Jobs
+
+The `jobs` module shows the current number of jobs running.
+The module will be shown only if there are background jobs running.
+The module will show the number of jobs running if there is more than 1 job, or
+more than the `threshold` config value, if it exists.
+
+### Options
+
+| Variable | Default | Description |
+| ----------- | ------- | -------------------------------- |
+| `threshold` | `1` | Show number of jobs if execeded. |
+| `disabled` | `false` | Disables the `jobs` module. |
+
+### Example
+
+```toml
+# ~/.config/starship.toml
+
+[jobs]
+threshold = 4
+```
+
## Line Break
The `line_break` module separates the prompt into two lines.
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