2022-08-07 19:35:53 -07:00
set --export POSH_THEME ::CONFIG::
2023-03-26 08:26:39 -07:00
set --export POSH_SHELL_VERSION $FISH_VERSION
2024-07-10 05:28:08 -07:00
set --global POWERLINE_COMMAND oh-my-posh
2022-11-19 13:35:13 -08:00
set --global POSH_PID $fish_pid
2022-03-23 02:32:54 -07:00
set --global CONDA_PROMPT_MODIFIER false
2024-07-10 05:28:08 -07:00
set --global omp_tooltip_command ''
set --global omp_current_rprompt ''
set --global omp_transient false
# We use this to avoid unnecessary CLI calls for prompt repaint.
set --global omp_new_prompt true
2021-03-20 11:32:15 -07:00
2022-09-23 11:44:53 -07:00
# template function for context loading
function set_poshcontext
2024-07-10 05:28:08 -07:00
return
2022-09-23 11:44:53 -07:00
end
2024-07-10 05:28:08 -07:00
# NOTE: Input function calls via `commandline --function` are put into a queue and will not be executed until an outer regular function returns. See https://fishshell.com/docs/current/cmds/commandline.html.
2020-12-23 04:48:14 -08:00
function fish_prompt
2022-07-04 22:01:38 -07:00
set --local omp_status_cache_temp $status
2023-07-23 00:29:54 -07:00
set --local omp_pipestatus_cache_temp $pipestatus
2022-07-01 16:19:07 -07:00
# clear from cursor to end of screen as
# commandline --function repaint does not do this
# see https://github.com/fish-shell/fish-shell/issues/8418
printf \e \[ 0J
2024-07-10 05:28:08 -07:00
if test " $omp_transient " = true
::OMP:: print transient --config $POSH_THEME --shell fish --status $omp_status_cache --pipestatus = " $omp_pipestatus_cache " --execution-time $omp_duration --stack-count $omp_stack_count --shell-version $FISH_VERSION --no-status = $omp_no_exit_code
return
end
if test " $omp_new_prompt " = false
echo -n " $omp_current_prompt "
return
2022-03-23 02:32:54 -07:00
end
2022-03-24 04:53:20 -07:00
set --global omp_status_cache $omp_status_cache_temp
2023-07-23 00:29:54 -07:00
set --global omp_pipestatus_cache $omp_pipestatus_cache_temp
2022-03-23 02:32:54 -07:00
set --global omp_stack_count ( count $dirstack )
set --global omp_duration " $CMD_DURATION $cmd_duration "
2023-05-17 14:27:21 -07:00
set --global omp_no_exit_code false
2024-05-20 10:59:39 -07:00
2021-03-19 14:22:37 -07:00
# check if variable set, < 3.2 case
2024-07-10 05:28:08 -07:00
if set --query omp_lastcommand ; and test -z " $omp_lastcommand "
set omp_duration 0
set omp_no_exit_code true
2021-03-18 13:24:26 -07:00
end
2024-05-20 10:59:39 -07:00
2021-03-19 14:22:37 -07:00
# works with fish >=3.2
2022-03-23 02:32:54 -07:00
if set --query omp_last_status_generation ; and test " $omp_last_status_generation " = " $status_generation "
2024-07-10 05:28:08 -07:00
set omp_duration 0
set omp_no_exit_code true
2023-05-17 14:27:21 -07:00
else if test -z " $omp_last_status_generation "
2024-07-10 05:28:08 -07:00
# first execution - $status_generation is 0, $omp_last_status_generation is empty
set omp_no_exit_code true
2021-03-19 14:22:37 -07:00
end
2024-05-20 10:59:39 -07:00
2022-03-23 02:32:54 -07:00
if set --query status_generation
2024-07-10 05:28:08 -07:00
set --global omp_last_status_generation $status_generation
2021-03-19 14:22:37 -07:00
end
2024-05-20 10:59:39 -07:00
2022-09-23 11:44:53 -07:00
set_poshcontext
2024-05-20 10:59:39 -07:00
2023-02-05 00:06:26 -08:00
# validate if the user cleared the screen
set --local omp_cleared false
2023-04-18 23:29:16 -07:00
set --local last_command ( history search --max 1 )
2024-05-20 10:59:39 -07:00
2024-07-10 05:28:08 -07:00
if test " $last_command " = clear
set omp_cleared true
2023-02-05 00:06:26 -08:00
end
2024-05-20 10:59:39 -07:00
::PROMPT_MARK::
2024-07-10 05:28:08 -07:00
# The prompt is saved for possible reuse, typically a repaint after clearing the screen buffer.
set --global omp_current_prompt ( ::OMP:: print primary --config $POSH_THEME --shell fish --status $omp_status_cache --pipestatus = " $omp_pipestatus_cache " --execution-time $omp_duration --stack-count $omp_stack_count --shell-version $FISH_VERSION --cleared = $omp_cleared --no-status = $omp_no_exit_code | string collect)
echo -n " $omp_current_prompt "
2021-12-01 10:11:38 -08:00
end
function fish_right_prompt
2024-07-10 05:28:08 -07:00
if test " $omp_transient " = true
set omp_transient false
return
2022-03-23 02:32:54 -07:00
end
2024-07-10 05:28:08 -07:00
# Repaint an existing right prompt.
if test " $omp_new_prompt " = false
echo -n " $omp_current_rprompt "
return
2022-03-22 11:53:37 -07:00
end
2024-07-10 05:28:08 -07:00
set omp_new_prompt false
set --global omp_current_rprompt ( ::OMP:: print right --config $POSH_THEME --shell fish --status $omp_status_cache --pipestatus = " $omp_pipestatus_cache " --execution-time $omp_duration --stack-count $omp_stack_count --shell-version $FISH_VERSION --no-status = $omp_no_exit_code | string join '' )
echo -n " $omp_current_rprompt "
2021-03-18 13:24:26 -07:00
end
function postexec_omp --on-event fish_postexec
2024-07-10 05:28:08 -07:00
# works with fish <3.2
# pre and postexec not fired for empty command in fish >=3.2
set --global omp_lastcommand $argv
2022-11-21 09:15:13 -08:00
end
2023-05-17 14:27:21 -07:00
function preexec_omp --on-event fish_preexec
2024-07-10 05:28:08 -07:00
if test "::FTCS_MARKS::" = true
echo - ne "\e]133;C\a"
end
2023-05-17 14:27:21 -07:00
end
2022-08-22 09:41:27 -07:00
# perform cleanup so a new initialization in current session works
2024-07-10 05:28:08 -07:00
if test -n ( bind \r --user 2 > /dev/null | string match -e _omp_enter_key_handler)
bind -e \r -M default
bind -e \r -M insert
bind -e \r -M visual
2022-08-22 09:41:27 -07:00
end
2024-07-10 05:28:08 -07:00
if test -n ( bind \n --user 2 > /dev/null | string match -e _omp_enter_key_handler)
bind -e \n -M default
bind -e \n -M insert
bind -e \n -M visual
end
if test -n ( bind \c c --user 2 > /dev/null | string match -e _omp_ctrl_c_key_handler)
bind -e \c c -M default
bind -e \c c -M insert
bind -e \c c -M visual
end
if test -n ( bind \x 20 --user 2 > /dev/null | string match -e _omp_space_key_handler)
bind -e \x 20 -M default
bind -e \x 20 -M insert
2022-08-22 09:41:27 -07:00
end
2022-03-22 11:53:37 -07:00
# tooltip
2024-07-10 05:28:08 -07:00
function _omp_space_key_handler
commandline --function expand -abbr
commandline --insert ' '
# Get the first word of command line as tip.
set --local tooltip_command ( commandline --current-buffer | string trim -l | string split --allow-empty -f1 ' ' | string collect)
# Ignore an empty/repeated tooltip command.
if test -z " $tooltip_command " || test " $tooltip_command " = " $omp_tooltip_command "
return
2022-11-18 00:28:48 -08:00
end
2024-07-10 05:28:08 -07:00
set omp_tooltip_command $tooltip_command
set --local tooltip_prompt ( ::OMP:: print tooltip --config $POSH_THEME --shell fish --status $omp_status_cache --pipestatus = " $omp_pipestatus_cache " --execution-time $omp_duration --stack-count $omp_stack_count --shell-version $FISH_VERSION --command $omp_tooltip_command --no-status = $omp_no_exit_code | string join '' )
if test -z " $tooltip_prompt "
return
end
# Save the tooltip prompt to avoid unnecessary CLI calls.
set omp_current_rprompt $tooltip_prompt
commandline --function repaint
2022-03-22 11:53:37 -07:00
end
2024-07-10 05:28:08 -07:00
if test "::TOOLTIPS::" = true
bind \x 20 _omp_space_key_handler -M default
bind \x 20 _omp_space_key_handler -M insert
2022-03-22 11:53:37 -07:00
end
2022-03-23 02:32:54 -07:00
# transient prompt
2024-07-10 05:28:08 -07:00
function _omp_enter_key_handler
if commandline --paging-mode
commandline --function accept -autosuggestion
return
end
if commandline --is-valid || test -z ( commandline --current-buffer | string trim -l | string collect)
set omp_new_prompt true
set omp_tooltip_command ''
if test "::TRANSIENT::" = true
set omp_transient true
commandline --function repaint
end
end
commandline --function execute
2022-03-23 02:32:54 -07:00
end
2024-07-10 05:28:08 -07:00
function _omp_ctrl_c_key_handler
if test -z ( commandline --current-buffer | string collect)
return
end
# Render a transient prompt on Ctrl-C with non-empty command line buffer.
set omp_new_prompt true
set omp_tooltip_command ''
if test "::TRANSIENT::" = true
set omp_transient true
commandline --function repaint
end
commandline --function cancel -commandline
commandline --function repaint
2022-03-23 02:32:54 -07:00
end
2022-08-04 22:16:16 -07:00
2024-07-10 05:28:08 -07:00
bind \r _omp_enter_key_handler -M default
bind \r _omp_enter_key_handler -M insert
bind \r _omp_enter_key_handler -M visual
bind \n _omp_enter_key_handler -M default
bind \n _omp_enter_key_handler -M insert
bind \n _omp_enter_key_handler -M visual
bind \c c _omp_ctrl_c_key_handler -M default
bind \c c _omp_ctrl_c_key_handler -M insert
bind \c c _omp_ctrl_c_key_handler -M visual
2022-08-04 22:16:16 -07:00
# legacy functions
function enable_poshtooltips
2024-07-10 05:28:08 -07:00
return
2022-08-04 22:16:16 -07:00
end
function enable_poshtransientprompt
2024-07-10 05:28:08 -07:00
return
2022-08-04 22:16:16 -07:00
end
2023-03-03 11:21:35 -08:00
2024-07-10 05:28:08 -07:00
if test "::UPGRADE::" = true
echo "::UPGRADENOTICE::"
2023-03-03 11:21:35 -08:00
end
2024-06-03 02:26:05 -07:00
2024-07-10 05:28:08 -07:00
if test "::AUTOUPGRADE::" = true
::OMP:: upgrade
2024-06-03 02:26:05 -07:00
end