From 7ead2b55eb58a878dc3ea8d87204f84862bd8148 Mon Sep 17 00:00:00 2001 From: Rich Lafferty Date: Fri, 27 Dec 2024 06:14:10 -0400 Subject: [PATCH] fix: handle variable bash $SHLVL behavior with explicit option (#4912) * Accept shlvl as --shlvl option * For bash only, pass --shlvl explicitly * fmt * Apply suggestion Co-authored-by: David Knaack * Apply suggestion Co-authored-by: David Knaack * parse_jobs -> parse_i64 --------- Co-authored-by: David Knaack --- src/context.rs | 10 +++++++--- src/init/starship.bash | 2 +- src/modules/shlvl.rs | 5 ++++- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/context.rs b/src/context.rs index dd27bfe6..4b5b4d96 100644 --- a/src/context.rs +++ b/src/context.rs @@ -868,8 +868,11 @@ pub struct Properties { #[clap(short = 'k', long, default_value = "viins")] pub keymap: String, /// The number of currently running jobs - #[clap(short, long, default_value_t, value_parser=parse_jobs)] + #[clap(short, long, default_value_t, value_parser=parse_i64)] pub jobs: i64, + /// The current value of SHLVL, for shells that mis-handle it in $() + #[clap(long, value_parser=parse_i64)] + pub shlvl: Option, } impl Default for Properties { @@ -883,6 +886,7 @@ impl Default for Properties { cmd_duration: None, keymap: "viins".to_string(), jobs: 0, + shlvl: None, } } } @@ -896,8 +900,8 @@ fn parse_trim(value: &str) -> Option> { Some(F::from_str(value)) } -fn parse_jobs(jobs: &str) -> Result { - parse_trim(jobs).unwrap_or(Ok(0)) +fn parse_i64(value: &str) -> Result { + parse_trim(value).unwrap_or(Ok(0)) } fn default_width() -> usize { diff --git a/src/init/starship.bash b/src/init/starship.bash index 5ed7a8ed..c1960b6b 100644 --- a/src/init/starship.bash +++ b/src/init/starship.bash @@ -69,7 +69,7 @@ starship_precmd() { eval "$STARSHIP_PROMPT_COMMAND" fi - local -a ARGS=(--terminal-width="${COLUMNS}" --status="${STARSHIP_CMD_STATUS}" --pipestatus="${STARSHIP_PIPE_STATUS[*]}" --jobs="${NUM_JOBS}") + local -a ARGS=(--terminal-width="${COLUMNS}" --status="${STARSHIP_CMD_STATUS}" --pipestatus="${STARSHIP_PIPE_STATUS[*]}" --jobs="${NUM_JOBS}" --shlvl="${SHLVL}") # Prepare the timer data, if needed. if [[ -n "${STARSHIP_START_TIME-}" ]]; then STARSHIP_END_TIME=$(::STARSHIP:: time) diff --git a/src/modules/shlvl.rs b/src/modules/shlvl.rs index 03e32728..06648c61 100644 --- a/src/modules/shlvl.rs +++ b/src/modules/shlvl.rs @@ -9,7 +9,10 @@ use std::convert::TryInto; const SHLVL_ENV_VAR: &str = "SHLVL"; pub fn module<'a>(context: &'a Context) -> Option> { - let shlvl = context.get_env(SHLVL_ENV_VAR)?.parse::().ok()?; + let props = &context.properties; + let shlvl = props + .shlvl + .or_else(|| context.get_env(SHLVL_ENV_VAR)?.parse::().ok())?; let mut module = context.new_module("shlvl"); let config: ShLvlConfig = ShLvlConfig::try_load(module.config);