Implement edit-in-kitty for bash
This commit is contained in:
parent
d71f18566e
commit
bd84ec03ce
@ -308,6 +308,23 @@ builtin unset -f _ksi_main
|
||||
case :$SHELLOPTS: in
|
||||
*:posix:*) ;;
|
||||
*)
|
||||
|
||||
_ksi_transmit_data() {
|
||||
builtin local data
|
||||
data="${1//[[:space:]]}"
|
||||
builtin local pos=0
|
||||
builtin local chunk_num=0
|
||||
while [ $pos -lt ${#data} ]; do
|
||||
builtin local chunk="${data:$pos:2048}"
|
||||
pos=$(($pos+2048))
|
||||
builtin printf '\eP@kitty-%s|%s:%s\e\\' "${2}" "${chunk_num}" "${chunk}"
|
||||
chunk_num=$(($chunk_num+1))
|
||||
done
|
||||
# save history so it is available in new shell
|
||||
[ "$3" = "save_history" ] && builtin history -a
|
||||
builtin printf '\eP@kitty-%s|\e\\' "${2}"
|
||||
}
|
||||
|
||||
clone-in-kitty() {
|
||||
builtin local data="shell=bash,pid=$$,cwd=$(builtin printf "%s" "$PWD" | builtin command base64),envfmt=bash,env=$(builtin export | builtin command base64)"
|
||||
while :; do
|
||||
@ -321,18 +338,84 @@ clone-in-kitty() {
|
||||
esac
|
||||
shift
|
||||
done
|
||||
data="${data//[[:space:]]}"
|
||||
builtin local pos=0
|
||||
builtin local chunk_num=0
|
||||
while [ $pos -lt ${#data} ]; do
|
||||
builtin local chunk="${data:$pos:2048}"
|
||||
pos=$(($pos+2048))
|
||||
builtin printf '\eP@kitty-clone|%s:%s\e\\' "${chunk_num}" "${chunk}"
|
||||
chunk_num=$(($chunk_num+1))
|
||||
_ksi_transmit_data "$data" "clone" "save_history"
|
||||
}
|
||||
|
||||
edit-in-kitty() {
|
||||
builtin local data=""
|
||||
builtin local ed_filename=""
|
||||
builtin local usage="Usage: edit-in-kitty [OPTIONS] FILE"
|
||||
data="cwd=$(builtin printf "%s" "$PWD" | builtin command base64)"
|
||||
while :; do
|
||||
case "$1" in
|
||||
"") break;;
|
||||
-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
|
||||
;;
|
||||
*) data="$data,a=$(builtin printf "%s" "$1" | builtin command base64)"; ed_filename="$1";;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
# append current commands to history file
|
||||
builtin history -a
|
||||
builtin printf '\eP@kitty-clone|\e\\'
|
||||
[ -z "$ed_filename" ] && {
|
||||
builtin echo "$usage" > /dev/stderr
|
||||
return 1
|
||||
}
|
||||
[ -r "$ed_filename" -a -w "$ed_filename" ] || {
|
||||
builtin echo "$ed_filename is not readable and writable" > /dev/stderr
|
||||
return 1
|
||||
}
|
||||
[ ! -f "$ed_filename" ] && {
|
||||
builtin echo "$ed_filename is not a file" > /dev/stderr
|
||||
return 1
|
||||
}
|
||||
builtin local stat_result=""
|
||||
stat_result=$(builtin command stat -L --format '%d:%i:%s' "$ed_filename" 2> /dev/null)
|
||||
[ $? != 0 ] && stat_result=$(builtin command stat -L -f '%d:%i:%z' "$ed_filename" 2> /dev/null)
|
||||
[ -z "$stat_result" ] && { builtin echo "Failed to stat the file: $ed_filename" > /dev/stderr; return 1; }
|
||||
data="$data,file_inode=$stat_result"
|
||||
builtin local file_size=$(builtin echo "$stat_result" | builtin command cut -d: -f3)
|
||||
[ "$file_size" -gt $((8 * 1024 * 1024)) ] && { builtin echo "File is too large for performant editing"; return 1; }
|
||||
data="$data,file_data=$(builtin command cat "$ed_filename" | builtin command base64)"
|
||||
_ksi_transmit_data "$data" "edit"
|
||||
data=""
|
||||
builtin echo "Waiting for editing to be completed..."
|
||||
_ksi_wait_for_complete() {
|
||||
builtin local started="n"
|
||||
builtin local line=""
|
||||
builtin local old_tty_settings=$(builtin command stty -g)
|
||||
builtin command stty "-echo"
|
||||
builtin trap -- "builtin command stty '$old_tty_settings'" RETURN
|
||||
builtin trap -- "builtin command stty '$old_tty_settings'; _ksi_transmit_data 'abort_signaled=interrupt' 'edit'; builtin exit 1;" SIGINT SIGTERM
|
||||
while :; do
|
||||
started="n"
|
||||
while IFS= read -r line; do
|
||||
if [ "$started" = "y" ]; then
|
||||
[ "$line" = "UPDATE" ] && break;
|
||||
[ "$line" = "DONE" ] && { started="done"; break; }
|
||||
builtin printf "%s\n" "$line" > /dev/stderr;
|
||||
return 1;
|
||||
else
|
||||
[ "$line" = "KITTY_DATA_START" ] && started="y"
|
||||
fi
|
||||
done
|
||||
[ "$started" = "n" ] && continue;
|
||||
data=""
|
||||
while IFS= read -r line; do
|
||||
[ "$line" = "KITTY_DATA_END" ] && break;
|
||||
data="$data$line"
|
||||
done
|
||||
[ -n "$data" -a "$started" != "done" ] && {
|
||||
builtin echo "Updating $ed_filename..."
|
||||
builtin printf "%s" "$data" | builtin command base64 -d > "$ed_filename"
|
||||
}
|
||||
[ "$started" = "done" ] && break;
|
||||
done
|
||||
}
|
||||
$(_ksi_wait_for_complete > /dev/tty)
|
||||
builtin local rc=$?
|
||||
builtin unset -f _ksi_wait_for_complete
|
||||
return $rc
|
||||
}
|
||||
;;
|
||||
esac
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user