feat(shell): restore line logic for zsh and bash

This commit is contained in:
Jan De Dobbeleer 2023-02-02 14:02:46 +01:00 committed by Jan De Dobbeleer
parent bc21ba3aef
commit 221b2d7d51
3 changed files with 47 additions and 2 deletions

View file

@ -9,6 +9,26 @@ PS0='${omp_start_time:0:$((omp_start_time="$(_omp_start_timer)",0))}'
# set secondary prompt
PS2="$(::OMP:: print secondary --config="$POSH_THEME" --shell=bash --shell-version="$BASH_VERSION")"
function _set_posh_cursor_position() {
# not supported in Midnight Commander
# see https://github.com/JanDeDobbeleer/oh-my-posh/issues/3415
if [[ -v MC_SID ]]; then
return
fi
local oldstty=$(stty -g)
stty raw -echo min 0
local COL
local ROW
IFS=';' read -sdR -p $'\E[6n' ROW COL
stty $oldstty
export POSH_CURSOR_LINE=${ROW#*[}
export POSH_CURSOR_COLUMN=${COL}
}
function _omp_start_timer() {
::OMP:: get millis
}
@ -28,6 +48,7 @@ function _omp_hook() {
omp_start_time=""
fi
set_poshcontext
_set_posh_cursor_position
PS1="$(::OMP:: print primary --config="$POSH_THEME" --shell=bash --shell-version="$BASH_VERSION" --error="$ret" --execution-time="$omp_elapsed" --stack-count="$omp_stack_count" | tr -d '\0')"
return $ret
}

View file

@ -7,6 +7,28 @@ export POSH_PROMPT_COUNT=0
# set secondary prompt
PS2="$(::OMP:: print secondary --config="$POSH_THEME" --shell=zsh)"
function _set_posh_cursor_position() {
# not supported in Midnight Commander
# see https://github.com/JanDeDobbeleer/oh-my-posh/issues/3415
if [[ -v MC_SID ]]; then
return
fi
local oldstty=$(stty -g)
stty raw -echo min 0
local pos
echo -en "\033[6n" > /dev/tty
read -r -d R pos
pos=${pos:2} # strip off the esc-[
local parts=(${(s:;:)pos})
stty $oldstty
export POSH_CURSOR_LINE=${parts[1]}
export POSH_CURSOR_COLUMN=${parts[2]}
}
# template function for context loading
function set_poshcontext() {
return
@ -27,6 +49,7 @@ function prompt_ohmyposh_precmd() {
count=$((POSH_PROMPT_COUNT+1))
export POSH_PROMPT_COUNT=$count
set_poshcontext
_set_posh_cursor_position
eval "$(::OMP:: print primary --config="$POSH_THEME" --error="$omp_last_error" --execution-time="$omp_elapsed" --stack-count="$omp_stack_count" --eval --shell=zsh --shell-version="$ZSH_VERSION")"
unset omp_start_time
}

View file

@ -86,8 +86,9 @@ Tells the engine what to do with the block. There are two options:
### Newline
Start the block on a new line - defaults to `false`. For `pwsh` and `cmd` this will not print a newline that's defined
on the first block when the prompt is on the first line (when using clear), or when the shell session starts (1st prompt).
Start the block on a new line - defaults to `false`. For `bash`, `zsh`, `pwsh` and `cmd` this will not print a newline
that's defined on the first block when the prompt is on the first line (when using clear), or when the shell session
starts (1st prompt).
### Alignment