Skip to content

Commit

Permalink
fix(shell): prioritize $? for consistent status in Bash/Zsh
Browse files Browse the repository at this point in the history
  • Loading branch information
lewis-yeung authored and JanDeDobbeleer committed Jun 20, 2024
1 parent 9f2c6d1 commit 7f418d7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 35 deletions.
13 changes: 8 additions & 5 deletions src/shell/scripts/omp.bash
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function _omp_start_timer() {
}

function _omp_ftcs_command_start() {
if [ "::FTCS_MARKS::" == "true" ]; then
if [[ "::FTCS_MARKS::" == "true" ]]; then
printf "\e]133;C\a"
fi
}
Expand All @@ -55,12 +55,15 @@ function _omp_hook() {
local omp_elapsed=-1
local no_exit_code="true"

if [[ -n "$omp_start_time" ]]; then
if [[ "$omp_start_time" ]]; then
local omp_now=$(::OMP:: get millis --shell=bash)
omp_elapsed=$((omp_now-omp_start_time))
omp_elapsed=$((omp_now - omp_start_time))
omp_start_time=""
no_exit_code="false"
fi
if [[ "${pipeStatus[-1]}" != "$ret" ]]; then
pipeStatus=("$ret")
fi

set_poshcontext
_set_posh_cursor_position
Expand All @@ -69,11 +72,11 @@ function _omp_hook() {
return $ret
}

if [ "$TERM" != "linux" ] && [ -x "$(command -v ::OMP::)" ] && ! [[ "$PROMPT_COMMAND" =~ "_omp_hook" ]]; then
if [[ "$TERM" != "linux" ]] && [[ -x "$(command -v ::OMP::)" ]] && ! [[ "$PROMPT_COMMAND" =~ "_omp_hook" ]]; then
PROMPT_COMMAND="_omp_hook; $PROMPT_COMMAND"
fi

if [ "::UPGRADE::" == "true" ]; then
if [[ "::UPGRADE::" == "true" ]]; then
echo "::UPGRADENOTICE::"
fi

Expand Down
64 changes: 34 additions & 30 deletions src/shell/scripts/omp.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function _set_posh_cursor_position() {
# not supported in Midnight Commander
# see https://github.com/JanDeDobbeleer/oh-my-posh/issues/3415
if [[ "::CURSOR::" != "true" ]] || [[ -v MC_SID ]]; then
return
return
fi

local oldstty=$(stty -g)
Expand Down Expand Up @@ -44,7 +44,8 @@ function prompt_ohmyposh_preexec() {
}

function prompt_ohmyposh_precmd() {
omp_last_error=$? pipeStatus=(${pipestatus[@]})
omp_last_error=$?
local pipeStatus=(${pipestatus[@]})
omp_stack_count=${#dirstack[@]}
omp_elapsed=-1
no_exit_code="true"
Expand All @@ -53,6 +54,9 @@ function prompt_ohmyposh_precmd() {
omp_elapsed=$(($omp_now-$omp_start_time))
no_exit_code="false"
fi
if [[ "${pipeStatus[-1]}" != "$omp_last_error" ]]; then
pipeStatus=("$omp_last_error")
fi
count=$((POSH_PROMPT_COUNT+1))
export POSH_PROMPT_COUNT=$count
set_poshcontext
Expand All @@ -67,24 +71,24 @@ add-zsh-hook precmd prompt_ohmyposh_precmd
add-zsh-hook preexec prompt_ohmyposh_preexec

# perform cleanup so a new initialization in current session works
if [[ "$(zle -lL self-insert)" = *"_posh-tooltip"* ]]; then
zle -N self-insert
if [[ "$(bindkey " ")" = *"_posh-tooltip"* ]]; then
bindkey " " self-insert
fi
if [[ "$(zle -lL zle-line-init)" = *"_posh-zle-line-init"* ]]; then
zle -N zle-line-init
fi

function _posh-tooltip() {
# https://github.com/zsh-users/zsh-autosuggestions - clear suggestion to avoid keeping it after the newly inserted space
if [[ -n "$(zle -lL autosuggest-clear)" ]]; then
if [[ "$(zle -lL autosuggest-clear)" ]]; then
# only if suggestions not disabled (variable not set)
if ! [[ -v _ZSH_AUTOSUGGEST_DISABLED ]]; then
zle autosuggest-clear
fi
fi
zle .self-insert
# https://github.com/zsh-users/zsh-autosuggestions - fetch new suggestion after the space
if [[ -n "$(zle -lL autosuggest-fetch)" ]]; then
if [[ "$(zle -lL autosuggest-fetch)" ]]; then
# only if suggestions not disabled (variable not set)
if ! [[ -v _ZSH_AUTOSUGGEST_DISABLED ]]; then
zle autosuggest-fetch
Expand All @@ -107,30 +111,30 @@ function _posh-tooltip() {
}

function _posh-zle-line-init() {
[[ $CONTEXT == start ]] || return 0
[[ $CONTEXT == start ]] || return 0

# Start regular line editor
(( $+zle_bracketed_paste )) && print -r -n - $zle_bracketed_paste[1]
zle .recursive-edit
local -i ret=$?
(( $+zle_bracketed_paste )) && print -r -n - $zle_bracketed_paste[2]
# Start regular line editor.
(( $+zle_bracketed_paste )) && print -r -n - $zle_bracketed_paste[1]
zle .recursive-edit
local -i ret=$?
(( $+zle_bracketed_paste )) && print -r -n - $zle_bracketed_paste[2]

eval "$(::OMP:: print transient --status="$omp_last_error" --execution-time="$omp_elapsed" --stack-count="$omp_stack_count" --config="$POSH_THEME" --eval --shell=zsh --shell-version="$ZSH_VERSION" --no-status="$no_exit_code")"
zle .reset-prompt
eval "$(::OMP:: print transient --status="$omp_last_error" --execution-time="$omp_elapsed" --stack-count="$omp_stack_count" --config="$POSH_THEME" --eval --shell=zsh --shell-version="$ZSH_VERSION" --no-status="$no_exit_code")"
zle .reset-prompt

# If we received EOT, we exit the shell
if [[ $ret == 0 && $KEYS == $'\4' ]]; then
exit
fi
# Exit the shell if we receive EOT.
if [[ $ret == 0 && $KEYS == $'\4' ]]; then
exit
fi

# Ctrl-C
if (( ret )); then
zle .send-break
else
# Enter
zle .accept-line
fi
return ret
if ((ret)); then
# TODO (fix): this is not equal to sending a SIGINT, since the status code ($?) is set to 1 instead of 130.
zle .send-break
else
# Enter
zle .accept-line
fi
return ret
}

function enable_poshtooltips() {
Expand All @@ -146,10 +150,10 @@ function enable_poshtransientprompt() {
bindkey '^[[F' end-of-line
bindkey '^[[H' beginning-of-line
_widgets=$(zle -la)
if [[ -n "${_widgets[(r)down-line-or-beginning-search]}" ]]; then
if [[ "${_widgets[(r)down-line-or-beginning-search]}" ]]; then
bindkey '^[[B' down-line-or-beginning-search
fi
if [[ -n "${_widgets[(r)up-line-or-beginning-search]}" ]]; then
if [[ "${_widgets[(r)up-line-or-beginning-search]}" ]]; then
bindkey '^[[A' up-line-or-beginning-search
fi
}
Expand All @@ -163,9 +167,9 @@ if [[ "::TRANSIENT::" = "true" ]]; then
fi

if [[ "::UPGRADE::" = "true" ]]; then
echo "::UPGRADENOTICE::"
echo "::UPGRADENOTICE::"
fi

if [[ "::AUTOUPGRADE::" = "true" ]]; then
::OMP:: upgrade
::OMP:: upgrade
fi

0 comments on commit 7f418d7

Please sign in to comment.