mirror of
https://github.com/starship/starship.git
synced 2026-06-23 02:05:51 +07:00
feat: Implement the prompt module for jobs (#85)
This commit is contained in:
committed by
Matan Kushner
parent
3f6fe50adb
commit
82cf484ced
+4
-4
@@ -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;
|
||||
"##;
|
||||
|
||||
+11
-2
@@ -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();
|
||||
|
||||
|
||||
@@ -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<Module<'a>> {
|
||||
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::<i64>()
|
||||
.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)
|
||||
}
|
||||
@@ -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<Module<'a>> {
|
||||
"username" => username::module(context),
|
||||
"battery" => battery::module(context),
|
||||
"cmd_duration" => cmd_duration::module(context),
|
||||
"jobs" => jobs::module(context),
|
||||
|
||||
_ => panic!("Unknown module: {}", module),
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ const PROMPT_ORDER: &[&str] = &[
|
||||
"go",
|
||||
"cmd_duration",
|
||||
"line_break",
|
||||
"jobs",
|
||||
"battery",
|
||||
"character",
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user