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)
end
printf \eP@kitty-%s\|\e\\ "$argv[2]"
end
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_cwd (printf "%s" "$PWD" | base64)
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 "$data" "clone"
__ksi_transmit_data (string join "," -- $data | tr -d "\t\n\r ") "clone"
end
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
end
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
end
set --local stat_result (command stat -L --format '%d:%i:%s' "$ed_filename" 2> /dev/null)
if test "$status" -ne "0"
set stat_result (command stat -L -f '%d:%i:%z' "$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
set stat_result (stat -L -f '%d:%i:%z' "$ed_filename" 2> /dev/null)
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
return 1
end
set --append data "file_inode=$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;
set --local file_data (command base64 < "$ed_filename")
if test "$file_size" -gt (math "8 * 1024 * 1024")
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"
__ksi_transmit_data (string join "," -- $data | tr -d "\t\n\r ") "edit"
set --erase data
echo "Waiting for editing to be completed..."
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"
set --erase __ksi_waiting_for_edit
set --erase __ksi_waiting_for_edit
__ksi_transmit_data "abort_signaled=interrupt" "edit"
end
end
while true
set --local started "n"
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
if test "$started" = "y"
test "$line" = "UPDATE" && break;
if test "$line" = "DONE"
set started "done"; break;
if test "$started" = "y"
test "$line" = "UPDATE" && break
if test "$line" = "DONE"
set started "done"
break
end
printf "%s\n" "$line" > /dev/stderr;
return 1;
printf "%s\n" "$line" > /dev/stderr
set --erase __ksi_waiting_for_edit
functions --erase __ksi_react_to_interrupt
return 1
else
test "$line" = "KITTY_DATA_START" && set started "y"
end
end
test "$started" = "n" && continue;
test "$started" = "n" && continue
set --local data ""
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 "$line" = "KITTY_DATA_END" && break;
test "$line" = "KITTY_DATA_END" && break
set data "$data$line"
end
if test -n "$data" -a "$started" != "done"
if test -n "$data" -a "$started" != "done"
echo "Updating $ed_filename..."
printf "%s" "$data" | command base64 -d > "$ed_filename"
printf "%s" "$data" | base64 -d > "$ed_filename"
end
test "$started" = "done" && break;
test "$started" = "done" && break
end
set --erase __ksi_waiting_for_edit
set --erase __ksi_waiting_for_edit
functions --erase __ksi_react_to_interrupt
end

View File

@ -254,7 +254,7 @@ _ksi_deferred_init() {
# 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"
fi
if [[ "$is_ssh_session" == "y" ]]; then
# show the hostname via %m for SSH sessions
functions[_ksi_precmd]+="
@ -408,7 +408,7 @@ clone-in-kitty() {
while :; do
case "$1" in
"") 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"
return
;;
@ -428,7 +428,6 @@ clone-in-kitty() {
_ksi_transmit_data "$data" "clone" "save_history"
}
edit-in-kitty() {
builtin local data=""
builtin local ed_filename=""
@ -437,7 +436,7 @@ edit-in-kitty() {
while :; do
case "$1" in
"") 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"
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; }
data="$data,file_inode=${stat_result[device]}:${stat_result[inode]}:${stat_result[size]}"
data="$data,file_data=$(builtin command base64 < "$ed_filename")"
_ksi_transmit_data "$data" "edit"
_ksi_transmit_data "$data" "edit"
data=""
builtin echo "Waiting for editing to be completed..."
builtin local started="n"