Refactor the fish implementation of edit-in-kitty

Clean up global variable and signal handling function before exit.
For commands allowing the use of valid functions or binary executables.
This commit is contained in:
pagedown 2022-06-28 11:31:42 +08:00
parent 617115d447
commit 01df828353
No known key found for this signature in database
GPG Key ID: E921CF18AC8FF6EB
2 changed files with 40 additions and 32 deletions

View File

@ -165,7 +165,6 @@ function __ksi_transmit_data -d "Transmit data to kitty using chunked DCS escape
set chunk_num (math $chunk_num + 1) set chunk_num (math $chunk_num + 1)
end end
printf \eP@kitty-%s\|\e\\ "$argv[2]" printf \eP@kitty-%s\|\e\\ "$argv[2]"
end end
function clone-in-kitty -d "Clone the current fish session into a new kitty window" function clone-in-kitty -d "Clone the current fish session into a new kitty window"
@ -187,8 +186,7 @@ function clone-in-kitty -d "Clone the current fish session into a new kitty wind
set --local b64_envs (string join0 -- $envs | base64) set --local b64_envs (string join0 -- $envs | base64)
set --local b64_cwd (printf "%s" "$PWD" | base64) set --local b64_cwd (printf "%s" "$PWD" | base64)
set --prepend data "shell=fish" "pid=$fish_pid" "cwd=$b64_cwd" "env=$b64_envs" set --prepend data "shell=fish" "pid=$fish_pid" "cwd=$b64_cwd" "env=$b64_envs"
set data (string join "," -- $data | tr -d "\t\n\r ") __ksi_transmit_data (string join "," -- $data | tr -d "\t\n\r ") "clone"
__ksi_transmit_data "$data" "clone"
end end
function edit-in-kitty -d "Edit the specified file in a new kitty overlay using your preferred editor, even over SSH" function edit-in-kitty -d "Edit the specified file in a new kitty overlay using your preferred editor, even over SSH"
@ -217,62 +215,73 @@ function edit-in-kitty -d "Edit the specified file in a new kitty overlay using
return 1 return 1
end end
if test ! -f "$ed_filename" if test ! -f "$ed_filename"
builtin echo "$ed_filename is not a file" > /dev/stderr echo "$ed_filename is not a file" > /dev/stderr
return 1 return 1
end end
set --local stat_result (command stat -L --format '%d:%i:%s' "$ed_filename" 2> /dev/null) set --local stat_result (stat -L --format '%d:%i:%s' "$ed_filename" 2> /dev/null)
if test "$status" -ne "0" if test "$status" -ne 0
set stat_result (command stat -L -f '%d:%i:%z' "$ed_filename" 2> /dev/null) set stat_result (stat -L -f '%d:%i:%z' "$ed_filename" 2> /dev/null)
end end
if test -z "$stat_result" if test "$status" -ne 0 || test -z "$stat_result"
echo "Failed to stat the file: $ed_filename" > /dev/stderr echo "Failed to stat the file: $ed_filename" > /dev/stderr
return 1 return 1
end end
set --append data "file_inode=$stat_result" set --append data "file_inode=$stat_result"
set --local file_size (string match -rg '\d+:\d+:(\d+)' "$stat_result") set --local file_size (string match -rg '\d+:\d+:(\d+)' "$stat_result")
test "$file_size" -gt (math "8 * 1024 * 1024") && begin; echo "File is too large for performant editing"; return 1; end; if test "$file_size" -gt (math "8 * 1024 * 1024")
set --local file_data (command base64 < "$ed_filename") echo "File is too large for performant editing" > /dev/stderr
return 1
end
set --local file_data (base64 < "$ed_filename")
set --append data "file_data=$file_data" set --append data "file_data=$file_data"
__ksi_transmit_data (string join "," -- $data | tr -d "\t\n\r ") "edit" __ksi_transmit_data (string join "," -- $data | tr -d "\t\n\r ") "edit"
set --erase data
echo "Waiting for editing to be completed..." echo "Waiting for editing to be completed..."
set --global __ksi_waiting_for_edit "y" set --global __ksi_waiting_for_edit "y"
set --erase data
function __ksi_react_to_interrupt -s SIGINT function __ksi_react_to_interrupt --on-signal SIGINT
functions --erase __ksi_react_to_interrupt
if test "$__ksi_waiting_for_edit" = "y" if test "$__ksi_waiting_for_edit" = "y"
set --erase __ksi_waiting_for_edit set --erase __ksi_waiting_for_edit
__ksi_transmit_data "abort_signaled=interrupt" "edit" __ksi_transmit_data "abort_signaled=interrupt" "edit"
end end
end end
while true while true
set --local started "n" set --local started "n"
while true while true
command stty "-echo"; set --local line (command head -n1 < /dev/tty); stty "-echo"
set --local line (head -n1 < /dev/tty)
test -z "$line" && break test -z "$line" && break
if test "$started" = "y" if test "$started" = "y"
test "$line" = "UPDATE" && break; test "$line" = "UPDATE" && break
if test "$line" = "DONE" if test "$line" = "DONE"
set started "done"; break; set started "done"
break
end end
printf "%s\n" "$line" > /dev/stderr; printf "%s\n" "$line" > /dev/stderr
return 1; set --erase __ksi_waiting_for_edit
functions --erase __ksi_react_to_interrupt
return 1
else else
test "$line" = "KITTY_DATA_START" && set started "y" test "$line" = "KITTY_DATA_START" && set started "y"
end end
end end
test "$started" = "n" && continue; test "$started" = "n" && continue
set --local data "" set --local data ""
while true while true
command stty "-echo"; set --local line (command head -n1 < /dev/tty); stty "-echo"
set --local line (head -n1 < /dev/tty)
test -z "$line" && break test -z "$line" && break
test "$line" = "KITTY_DATA_END" && break; test "$line" = "KITTY_DATA_END" && break
set data "$data$line" set data "$data$line"
end end
if test -n "$data" -a "$started" != "done" if test -n "$data" -a "$started" != "done"
echo "Updating $ed_filename..." echo "Updating $ed_filename..."
printf "%s" "$data" | command base64 -d > "$ed_filename" printf "%s" "$data" | base64 -d > "$ed_filename"
end end
test "$started" = "done" && break; test "$started" = "done" && break
end end
set --erase __ksi_waiting_for_edit set --erase __ksi_waiting_for_edit
functions --erase __ksi_react_to_interrupt functions --erase __ksi_react_to_interrupt
end end

View File

@ -254,7 +254,7 @@ _ksi_deferred_init() {
# OpenSSH's sshd creates entries in utmp for every login so use those # OpenSSH's sshd creates entries in utmp for every login so use those
[[ "$(builtin command who -m 2> /dev/null)" =~ "\([a-fA-F.:0-9]+\)$" ]] && is_ssh_session="y" [[ "$(builtin command who -m 2> /dev/null)" =~ "\([a-fA-F.:0-9]+\)$" ]] && is_ssh_session="y"
fi fi
if [[ "$is_ssh_session" == "y" ]]; then if [[ "$is_ssh_session" == "y" ]]; then
# show the hostname via %m for SSH sessions # show the hostname via %m for SSH sessions
functions[_ksi_precmd]+=" functions[_ksi_precmd]+="
@ -408,7 +408,7 @@ clone-in-kitty() {
while :; do while :; do
case "$1" in case "$1" in
"") break;; "") break;;
-h|--help) -h|--help)
builtin printf "%s\n\n%s\n" "Clone the current zsh session into a new kitty window." "For usage instructions see: https://sw.kovidgoyal.net/kitty/shell-integration/#clone-shell" builtin printf "%s\n\n%s\n" "Clone the current zsh session into a new kitty window." "For usage instructions see: https://sw.kovidgoyal.net/kitty/shell-integration/#clone-shell"
return return
;; ;;
@ -428,7 +428,6 @@ clone-in-kitty() {
_ksi_transmit_data "$data" "clone" "save_history" _ksi_transmit_data "$data" "clone" "save_history"
} }
edit-in-kitty() { edit-in-kitty() {
builtin local data="" builtin local data=""
builtin local ed_filename="" builtin local ed_filename=""
@ -437,7 +436,7 @@ edit-in-kitty() {
while :; do while :; do
case "$1" in case "$1" in
"") break;; "") break;;
-h|--help) -h|--help)
builtin printf "%s\n\n%s\n\n%s\n" "$usage" "Edit the specified file in a kitty overlay window. Works over SSH as well." "For usage instructions see: https://sw.kovidgoyal.net/kitty/shell-integration/#edit-file" builtin printf "%s\n\n%s\n\n%s\n" "$usage" "Edit the specified file in a kitty overlay window. Works over SSH as well." "For usage instructions see: https://sw.kovidgoyal.net/kitty/shell-integration/#edit-file"
return return
;; ;;
@ -463,7 +462,7 @@ edit-in-kitty() {
[ "${stat_result[size]}" -gt $((8 * 1024 * 1024)) ] && { builtin echo "File is too large for performant editing"; return 1; } [ "${stat_result[size]}" -gt $((8 * 1024 * 1024)) ] && { builtin echo "File is too large for performant editing"; return 1; }
data="$data,file_inode=${stat_result[device]}:${stat_result[inode]}:${stat_result[size]}" data="$data,file_inode=${stat_result[device]}:${stat_result[inode]}:${stat_result[size]}"
data="$data,file_data=$(builtin command base64 < "$ed_filename")" data="$data,file_data=$(builtin command base64 < "$ed_filename")"
_ksi_transmit_data "$data" "edit" _ksi_transmit_data "$data" "edit"
data="" data=""
builtin echo "Waiting for editing to be completed..." builtin echo "Waiting for editing to be completed..."
builtin local started="n" builtin local started="n"