feat(file-manager): sync the working directory with the shell

The panel and the shell each kept their own directory. The new sync_cwd option
keeps them in step both ways, through files in a session directory exported by
kitty rather than by typing a command into the terminal: the shell applies the
directory chosen in the panel from its prompt hook with builtin cd, so nothing
lands in the history and a half typed multi-line command is not submitted.
This commit is contained in:
BlackAngelTV committed 2026-11-11 08:20:11 +01:00
1 parent f16ed6cbfe
commit 1b330bcf81
12 files changed
+425 -6

No files matched your search

+32 -1
View File
@@ -213,6 +213,10 @@ _ksi_main() {
builtin printf "\e]7;kitty-shell-cwd://%s%s\a" "$HOSTNAME" "$PWD"
fi
fi
if [[ -n "${_ksi_cwd_sync_file:-}" ]]; then
_ksi_cwd_sync_apply
fi
}
if [[ "${_ksi_prompt[cursor]}" == "y" ]]; then
@@ -265,8 +269,34 @@ _ksi_main() {
_ksi_prompt[ps2]+="\[\e]133;A;k=s\a\]"
fi
builtin alias edit-in-kitty="kitten edit-in-kitty"
# Optional working directory sync with the file manager panel (sync_cwd).
# The panel publishes the directory it wants in <dir>/<window id>.set and we
# apply it at prompt time instead of typing a cd into the terminal: the
# command stays out of the history and a half typed multi-line command is
# not submitted. We publish our own directory the same way so that the panel
# can follow a cd made here.
if [[ -n "$KITTY_CWD_SYNC_DIR" && -n "$KITTY_WINDOW_ID" && -d "$KITTY_CWD_SYNC_DIR" ]]; then
_ksi_cwd_sync_file="$KITTY_CWD_SYNC_DIR/$KITTY_WINDOW_ID"
_ksi_cwd_sync_publish() {
builtin printf '%s' "$PWD" > "$_ksi_cwd_sync_file" 2> /dev/null
builtin return 0
}
_ksi_cwd_sync_apply() {
builtin local ret=$? target
if [[ -f "$_ksi_cwd_sync_file.set" ]]; then
IFS= builtin read -r target < "$_ksi_cwd_sync_file.set"
builtin command rm -f -- "$_ksi_cwd_sync_file.set"
if [[ -n "$target" && -d "$target" ]]; then
builtin cd -- "$target"
fi
fi
_ksi_cwd_sync_publish
builtin return $ret
}
_ksi_cwd_sync_publish
fi
builtin alias edit-in-kitty="kitten edit-in-kitty"
if [[ "${_ksi_prompt[complete]}" == "y" ]]; then
_ksi_completions() {
@@ -315,6 +345,7 @@ _ksi_main() {
# from the shell
builtin local pc
pc='builtin declare -F _ksi_prompt_command > /dev/null 2> /dev/null && _ksi_prompt_command'
if [[ -n "${_ksi_cwd_sync_file:-}" ]]; then pc="$pc; _ksi_cwd_sync_apply"; fi
if [[ -z "${PROMPT_COMMAND[*]}" ]]; then
PROMPT_COMMAND=([0]="$pc")
elif [[ $(builtin declare -p PROMPT_COMMAND 2> /dev/null) =~ 'declare -a PROMPT_COMMAND' ]]; then