Use an associative array for prompt data

This commit is contained in:
Kovid Goyal 2021-07-15 07:26:03 +05:30
parent d1015de700
commit 2e5308ad40
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -4,12 +4,10 @@ args=($@)
() { () {
if [[ ! -o interactive ]]; then return; fi if [[ ! -o interactive ]]; then return; fi
typeset -g kitty_prompt_state="first-run" typeset -g -A kitty_prompt=([state]='first-run' [cursor]='y' [title]='y')
typeset -g kitty_prompt_cursor="y"
typeset -g kitty_prompt_title="y"
(( ${args[(I)no-cursor]} )) && kitty_prompt_cursor="n" (( ${args[(I)no-cursor]} )) && kitty_prompt[cursor]="n"
(( ${args[(I)no-title]} )) && kitty_prompt_title="n" (( ${args[(I)no-title]} )) && kitty_prompt[title]="n"
function debug() { function debug() {
# print a line to STDOUT of parent kitty process # print a line to STDOUT of parent kitty process
@ -19,14 +17,16 @@ args=($@)
function change-cursor-shape () { function change-cursor-shape () {
# change cursor shape depending on mode # change cursor shape depending on mode
if [[ "$kitty_prompt_cursor" == "y" ]]; then if [[ "$kitty_prompt[cursor]" == "y" ]]; then
if [[ "$KEYMAP" == vicmd ]]; then case $KEYMAP in
# the command mode for vi vicmd | visual)
printf "\e[2 q" # blinking block cursor # the command mode for vi
else printf "\e[2 q" # blinking block cursor
# the insert mode for vi ;;
printf "\e[5 q" # blinking bar cursor *)
fi printf "\e[5 q" # blinking bar cursor
;;
esac
fi fi
} }
@ -49,15 +49,15 @@ args=($@)
# tell kitty to mark the current cursor position using OSC 133 # tell kitty to mark the current cursor position using OSC 133
osc "133;$1" osc "133;$1"
} }
typeset -g kitty_prompt_start_mark="%{$(mark A)%}" kitty_prompt[start_mark]="%{$(mark A)%}"
function set_title() { function set_title() {
if [[ "$kitty_prompt_title" == "y" ]]; then osc "2;$1"; fi if [[ "$kitty_prompt[title]" == "y" ]]; then osc "2;$1"; fi
} }
function kitty_precmd() { function kitty_precmd() {
local cmd_status=$? local cmd_status=$?
if [[ "$kitty_prompt_state" == "first-run" ]]; then if [[ "$kitty_prompt[state]" == "first-run" ]]; then
# compdef is only defined if compinit has been called # compdef is only defined if compinit has been called
if whence compdef > /dev/null; then if whence compdef > /dev/null; then
compdef _kitty kitty compdef _kitty kitty
@ -65,22 +65,22 @@ args=($@)
fi fi
# Set kitty window title to the cwd # Set kitty window title to the cwd
set_title "${PWD/$HOME/~}" set_title "${PWD/$HOME/~}"
if [[ "$kitty_prompt_state" == "preexec" ]]; then if [[ "$kitty_prompt[state]" == "preexec" ]]; then
mark "D;$cmd_status" mark "D;$cmd_status"
else else
if [[ "$kitty_prompt_state" != "first-run" ]]; then mark "D"; fi if [[ "$kitty_prompt[state]" != "first-run" ]]; then mark "D"; fi
fi fi
# we must use PS1 to set the prompt start mark as precmd functions are # we must use PS1 to set the prompt start mark as precmd functions are
# not called when the prompt is redrawn after a window resize or when a background # not called when the prompt is redrawn after a window resize or when a background
# job finishes # job finishes
if [[ "$PS1" != *"$kitty_prompt_start_mark"* ]]; then PS1="$kitty_prompt_start_mark$PS1" fi if [[ "$PS1" != *"$kitty_prompt[start_mark]"* ]]; then PS1="$kitty_prompt[start_mark]$PS1" fi
kitty_prompt_state="precmd" kitty_prompt[state]="precmd"
} }
function kitty_zle_line_init() { function kitty_zle_line_init() {
mark "B" mark "B"
change-cursor-shape; change-cursor-shape;
kitty_prompt_state="line-init" kitty_prompt[state]="line-init"
} }
function kitty_zle_line_init_with_orginal() { zle kitty-zle-line-init-original; kitty_zle_line_init } function kitty_zle_line_init_with_orginal() { zle kitty-zle-line-init-original; kitty_zle_line_init }
zle -A zle-line-init kitty-zle-line-init-original 2>/dev/null zle -A zle-line-init kitty-zle-line-init-original 2>/dev/null
@ -92,7 +92,7 @@ args=($@)
function kitty_zle_line_finish() { function kitty_zle_line_finish() {
change-cursor-shape; change-cursor-shape;
kitty_prompt_state="line-finish" kitty_prompt[state]="line-finish"
} }
function kitty_zle_line_finish_with_orginal() { zle kitty-zle-line-finish-original; kitty_zle_line_finish } function kitty_zle_line_finish_with_orginal() { zle kitty-zle-line-finish-original; kitty_zle_line_finish }
zle -A zle-line-finish kitty-zle-line-finish-original 2>/dev/null zle -A zle-line-finish kitty-zle-line-finish-original 2>/dev/null
@ -106,9 +106,9 @@ args=($@)
mark "C" mark "C"
# Set kitty window title to the currently executing command # Set kitty window title to the currently executing command
set_title "$1" set_title "$1"
kitty_prompt_state="preexec" kitty_prompt[state]="preexec"
# remove the prompt mark sequence while the command is executing as it could read/modify the value of PS1 # remove the prompt mark sequence while the command is executing as it could read/modify the value of PS1
PS1="${PS1//$kitty_prompt_start_mark/}" PS1="${PS1//$kitty_prompt[start_mark]/}"
} }
typeset -a -g precmd_functions typeset -a -g precmd_functions