Minor refactoring for fish clone-in-kitty and allow use without env -0

The builtin option from the latest version of fish is not used for now.
clone-in-kitty has been tested with fish 3.2.x.
BusyBox env does not support the -0 option.
This commit is contained in:
pagedown 2022-04-14 21:32:28 +08:00
parent aab6f3e450
commit af6b1837cb
No known key found for this signature in database
GPG Key ID: E921CF18AC8FF6EB

View File

@ -6,7 +6,7 @@
if set -q KITTY_FISH_XDG_DATA_DIR if set -q KITTY_FISH_XDG_DATA_DIR
if set -q XDG_DATA_DIRS if set -q XDG_DATA_DIRS
set --global --export --path XDG_DATA_DIRS "$XDG_DATA_DIRS" set --global --export --path XDG_DATA_DIRS "$XDG_DATA_DIRS"
if set -l index (contains -i "$KITTY_FISH_XDG_DATA_DIR" $XDG_DATA_DIRS) if set --local index (contains --index "$KITTY_FISH_XDG_DATA_DIR" $XDG_DATA_DIRS)
set --erase --global XDG_DATA_DIRS[$index] set --erase --global XDG_DATA_DIRS[$index]
test -n "$XDG_DATA_DIRS" || set --erase --global XDG_DATA_DIRS test -n "$XDG_DATA_DIRS" || set --erase --global XDG_DATA_DIRS
end end
@ -22,7 +22,7 @@ not functions -q __ksi_schedule || exit 0
# Check fish version 3.3.0+ efficiently and fallback to check the minimum working version 3.2.0, exit on outdated versions. # Check fish version 3.3.0+ efficiently and fallback to check the minimum working version 3.2.0, exit on outdated versions.
# "Warning: Update fish to version 3.3.0+ to enable kitty shell integration.\n" # "Warning: Update fish to version 3.3.0+ to enable kitty shell integration.\n"
set -q fish_killring || set -q status_generation || string match -qnv "3.1.*" "$version" set -q fish_killring || set -q status_generation || string match -qnv "3.1.*" "$version"
or echo -en "\eP@kitty-print|V2FybmluZzogVXBkYXRlIGZpc2ggdG8gdmVyc2lvbiAzLjMuMCsgdG8gZW5hYmxlIGtpdHR5IHNoZWxsIGludGVncmF0aW9uLgo=\e\\" && exit 0 || exit 0 # " or echo -en \eP@kitty-print\|V2FybmluZzogVXBkYXRlIGZpc2ggdG8gdmVyc2lvbiAzLjMuMCsgdG8gZW5hYmxlIGtpdHR5IHNoZWxsIGludGVncmF0aW9uLgo=\e\\ && exit 0 || exit 0
function __ksi_schedule --on-event fish_prompt -d "Setup kitty integration after other scripts have run, we hope" function __ksi_schedule --on-event fish_prompt -d "Setup kitty integration after other scripts have run, we hope"
functions --erase __ksi_schedule functions --erase __ksi_schedule
@ -108,29 +108,33 @@ function __ksi_schedule --on-event fish_prompt -d "Setup kitty integration after
end end
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"
set --function data (printf "%s" "$PWD" | command base64) set --local data
set -l env (command env -0 | command base64)
set --function data "pid=$fish_pid,cwd=$data,env=$env"
for a in $argv for a in $argv
if builtin test "$a" = -h -o "$a" = --help if contains -- "$a" -h --help
builtin printf "%s\n\n%s\n" "Clone the current fish session into a new kitty window." "For usage instructions see: https://sw.kovidgoyal.net/kitty/shell-integration/#clone-shell" echo "Clone the current fish session into a new kitty window."
echo
echo "For usage instructions see: https://sw.kovidgoyal.net/kitty/shell-integration/#clone-shell"
return return
end end
set --local ea (builtin printf "%s" "$a" | command base64) set --local ea (printf "%s" "$a" | base64)
set --function data "$data,a=$ea" set --append data "a=$ea"
end end
set --function data (builtin printf "%s" "$data" | command tr -d "\n\t\r ") set --local envs
set --function pos 1 for e in (set --export --names)
set --function chunk_num 0 set --append envs "$e=$$e"
set --function data_len (builtin string length "$data")
echo $data_len
while builtin test $pos -le $data_len;
set -l chunk (builtin string sub -s $pos -l 2048 $data)
set --function pos (math $pos + 2048)
builtin printf '\eP@kitty-clone|%s:%s\e\\' "$chunk_num" "$chunk" # '
set --function chunk_num (math $chunk_num + 1)
end end
builtin printf '\eP@kitty-clone|\e\\' # ' set --local b64_envs (string join0 $envs | base64)
set --local b64_cwd (printf "%s" "$PWD" | base64)
set --prepend data "pid=$fish_pid" "cwd=$b64_cwd" "env=$b64_envs"
set data (string join "," -- $data | tr -d "\t\n\r ")
set --local data_len (string length -- "$data")
set --local pos 1
set --local chunk_num 0
while test "$pos" -le $data_len
printf \eP@kitty-clone\|%s:%s\e\\ "$chunk_num" (string sub --start $pos --length 2048 -- $data | string collect)
set pos (math $pos + 2048)
set chunk_num (math $chunk_num + 1)
end
echo -en \eP@kitty-clone\|\e\\
end end