feat(file-manager): sync the working directory by default

The panel and the shell now share their directory out of the box; whoever wants
the previous independence sets sync_cwd no. The request is only written once the
shell has published its directory, since that file is what proves its
integration hook will read the request: a shell without shell integration still
gets the cd typed into the terminal instead of losing the change.
This commit is contained in:
BlackAngelTV committed 2026-11-11 08:35:19 +01:00
1 parent 1b330bcf81
commit e7e4e849eb
7 files changed
+61 -16

No files matched your search

+4 -1
View File
@@ -495,7 +495,10 @@ func (h *Handler) change_shell_dir_to(path string) {
h.result_action, h.result_path = "cd", path
// With the sync on, the shell reads the request from its session file at the
// next prompt, so the handler must not also type the change into its pty.
if h.sync != nil && h.sync.publish_request(path) == nil {
// The request is only written once the shell proved it has the hook by
// publishing its directory: a shell without shell integration would
// otherwise never see it and the change would be silently lost.
if h.sync != nil && h.sync.shell_has_hook() && h.sync.publish_request(path) == nil {
h.sync_handled = true
}
h.lp.Quit(0)
+3 -2
View File
@@ -51,7 +51,7 @@ agr('shell', 'Shell synchronisation')
opt(
'sync_cwd',
'no',
'yes',
option_type='to_bool',
long_text="""
Keep the shell's working directory and the panel in step. A :code:`cd` in the
@@ -62,7 +62,8 @@ The exchange goes through two small files in the session directory kitty
exports as :envvar:`KITTY_CWD_SYNC_DIR`, so nothing is typed into the shell:
the command never lands in the history and a half typed multi-line command is
left untouched. Bash, zsh and fish are supported by their kitty shell
integration; other shells keep the current behaviour.
integration; other shells keep the current behaviour. Set :code:`sync_cwd no`
to keep the two directories independent.
""",
)
+13
View File
@@ -180,6 +180,19 @@ func TestOpenWithSudoOption(t *testing.T) {
}
}
func TestSyncCwdIsOnByDefault(t *testing.T) {
c := NewConfig()
if !c.Sync_cwd {
t.Fatal("the panel must follow the shell by default")
}
if err := c.Parse("sync_cwd", "no"); err != nil {
t.Fatal(err)
}
if c.Sync_cwd {
t.Fatal("sync_cwd = no must keep the two directories independent")
}
}
func TestEditArgs(t *testing.T) {
got := edit_args("/tmp/a.py")
want := []string{"edit-in-kitty", "--type=tab", "/tmp/a.py"}
+8
View File
@@ -42,6 +42,14 @@ func new_shell_sync_from_env(enabled bool) *shell_sync {
func (s *shell_sync) shell_cwd_path() string { return filepath.Join(s.dir, s.window_id) }
func (s *shell_sync) request_path() string { return s.shell_cwd_path() + ".set" }
// shell_has_hook reports whether the shell has published its working directory
// at least once, which only the integration hook does. Without it nothing would
// consume the request, so the panel has to type the change into the shell.
func (s *shell_sync) shell_has_hook() bool {
_, err := os.Stat(s.shell_cwd_path())
return err == nil
}
// shell_cwd returns the working directory last published by the shell. An empty
// or relative path, or one that no longer exists, is treated as unknown rather
// than followed, as the shell may be between two writes.
+20
View File
@@ -45,6 +45,26 @@ func TestPublishRequestIsReadableAndLeavesNoTemporaryFile(t *testing.T) {
}
}
func TestShellHasHookNeedsAPublishedDirectory(t *testing.T) {
dir := t.TempDir()
s := new_shell_sync(dir, "5", true)
if s.shell_has_hook() {
t.Fatal("the shell published nothing yet")
}
if err := s.publish_request(dir); err != nil {
t.Fatal(err)
}
if s.shell_has_hook() {
t.Fatal("a request must not be mistaken for a published directory")
}
if err := os.WriteFile(filepath.Join(dir, "5"), []byte(dir), 0o600); err != nil {
t.Fatal(err)
}
if !s.shell_has_hook() {
t.Fatal("a published directory means the shell has the hook")
}
}
func TestShellCwdIgnoresUnusablePaths(t *testing.T) {
dir := t.TempDir()
s := new_shell_sync(dir, "9", true)