oh-my-posh/src/shell/scripts/omp.bash

140 lines
3.5 KiB
Bash
Raw Normal View History

export POSH_THEME=::CONFIG::
2023-03-26 08:26:39 -07:00
export POSH_SHELL_VERSION=$BASH_VERSION
export POWERLINE_COMMAND="oh-my-posh"
export POSH_PID=$$
2021-05-20 10:24:58 -07:00
export CONDA_PROMPT_MODIFIER=false
export OSTYPE=$OSTYPE
if [[ $OSTYPE =~ ^(msys|cygwin) ]]; then
export POSH_PID=$(command cat /proc/$$/winpid)
fi
# global variables
_omp_start_time=""
_omp_stack_count=0
_omp_elapsed=-1
_omp_no_exit_code="true"
_omp_status_cache=0
_omp_pipestatus_cache=0
_omp_executable=::OMP::
# switches to enable/disable features
_omp_cursor_positioning=0
_omp_ftcs_marks=0
2022-02-20 04:56:28 -08:00
# start timer on command start
PS0='${_omp_start_time:0:$((_omp_start_time="$(_omp_start_timer)",0))}$(_omp_ftcs_command_start)'
2022-02-20 04:56:28 -08:00
# set secondary prompt
_omp_secondary_prompt=$("$_omp_executable" print secondary --shell=bash --shell-version="$BASH_VERSION")
2021-03-02 11:37:31 -08:00
function _omp_set_cursor_position() {
# not supported in Midnight Commander
# see https://github.com/JanDeDobbeleer/oh-my-posh/issues/3415
if [[ $_omp_cursor_positioning == 0 ]] || [[ -v MC_SID ]]; then
return
fi
local oldstty=$(stty -g)
stty raw -echo min 0
local COL
local ROW
IFS=';' read -rsdR -p $'\E[6n' ROW COL
stty "$oldstty"
export POSH_CURSOR_LINE=${ROW#*[}
export POSH_CURSOR_COLUMN=${COL}
}
function _omp_start_timer() {
"$_omp_executable" get millis
}
function _omp_ftcs_command_start() {
if [[ $_omp_ftcs_marks == 1 ]]; then
printf "\e]133;C\a"
fi
}
# template function for context loading
function set_poshcontext() {
return
}
function _omp_print_primary() {
# Avoid unexpected expansions.
shopt -u promptvars
local prompt
if shopt -oq posix; then
# Disable in POSIX mode.
prompt='[NOTICE: Oh My Posh prompt is not supported in POSIX mode]\n\u@\h:\w\$ '
else
prompt=$("$_omp_executable" print primary --shell=bash --shell-version="$BASH_VERSION" --status="$_omp_status_cache" --pipestatus="${_omp_pipestatus_cache[*]}" --execution-time="$_omp_elapsed" --stack-count="$_omp_stack_count" --no-status="$_omp_no_exit_code" --terminal-width="${COLUMNS-0}" | tr -d '\0')
fi
echo "${prompt@P}"
# Allow command substitution in PS0.
shopt -s promptvars
}
function _omp_print_secondary() {
# Avoid unexpected expansions.
shopt -u promptvars
if shopt -oq posix; then
# Disable in POSIX mode.
echo '> '
else
echo "${_omp_secondary_prompt@P}"
fi
# Allow command substitution in PS0.
shopt -s promptvars
}
function _omp_hook() {
_omp_status_cache=$? _omp_pipestatus_cache=("${PIPESTATUS[@]}")
if [[ ${#BP_PIPESTATUS[@]} -ge ${#_omp_pipestatus_cache[@]} ]]; then
_omp_pipestatus_cache=("${BP_PIPESTATUS[@]}")
fi
_omp_stack_count=$((${#DIRSTACK[@]} - 1))
if [[ $_omp_start_time ]]; then
local omp_now=$("$_omp_executable" get millis --shell=bash)
_omp_elapsed=$((omp_now - _omp_start_time))
_omp_start_time=""
_omp_no_exit_code="false"
2021-03-02 11:37:31 -08:00
fi
if [[ ${_omp_pipestatus_cache[-1]} != "$_omp_status_cache" ]]; then
_omp_pipestatus_cache=("$_omp_status_cache")
fi
set_poshcontext
_omp_set_cursor_position
PS1='$(_omp_print_primary)'
PS2='$(_omp_print_secondary)'
2020-12-23 04:12:10 -08:00
return $_omp_status_cache
}
function _omp_install_hook() {
[[ $TERM = linux ]] && return
local cmd
for cmd in "${PROMPT_COMMAND[@]}"; do
if [[ $cmd = "_omp_hook" ]]; then
return
fi
done
PROMPT_COMMAND=(_omp_hook "${PROMPT_COMMAND[@]}")
}
_omp_install_hook