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

+7 -6
View File
@@ -222,12 +222,13 @@ Detailed list of changes
0.50.1 [unreleased] 0.50.1 [unreleased]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- The :doc:`file manager <kittens/file-manager>` can now keep its directory in - The :doc:`file manager <kittens/file-manager>` now keeps its directory in
step with the shell: with :code:`sync_cwd yes`, a :code:`cd` moves the panel step with the shell by default: a :code:`cd` moves the panel into the same
into the same directory and validating a directory moves the shell at its directory and validating a directory moves the shell at its next prompt, for
next prompt, for bash, zsh and fish. Nothing is typed into the terminal, so bash, zsh and fish. Nothing is typed into the terminal, so no command lands
no command lands in the history and a multi-line command being typed is left in the history and a multi-line command being typed is left untouched. Set
untouched. :code:`sync_cwd no` in :file:`file-manager.conf` to keep the two directories
independent, as before.
- :kbd:`Ctrl+Shift+E` now opens the :doc:`file manager kitten - :kbd:`Ctrl+Shift+E` now opens the :doc:`file manager kitten
<kittens/file-manager>` in a left dock by default, instead of requiring a <kittens/file-manager>` in a left dock by default, instead of requiring a
+6 -7
View File
@@ -98,15 +98,14 @@ Actions
Synchronisation avec le shell Synchronisation avec le shell
------------------------------ ------------------------------
Par défaut, le panneau et le shell gardent chacun leur répertoire : le panneau Le panneau et le shell partagent leur répertoire courant depuis la version
part de celui du shell, mais un :code:`cd` d'un côté ne change rien de l'autre. 0.50.1 : un :code:`cd` dans le shell amène le panneau dans le même répertoire,
L'option ``sync_cwd`` les tient synchronisés dans les deux sens :: et valider un répertoire dans le panneau amène le shell vers celui-ci. Pour que
chacun garde son propre répertoire, alors que le panneau part toujours de celui
du shell et n'en change plus ::
# ~/.config/kitty/file-manager.conf # ~/.config/kitty/file-manager.conf
sync_cwd yes sync_cwd no
Un :code:`cd` dans le shell amène alors le panneau dans le même répertoire, et
valider un répertoire dans le panneau amène le shell vers celui-ci.
L'échange ne passe pas par le terminal. kitty exporte un répertoire de session L'échange ne passe pas par le terminal. kitty exporte un répertoire de session
dans :envvar:`KITTY_CWD_SYNC_DIR` ; le shell y publie son répertoire courant et dans :envvar:`KITTY_CWD_SYNC_DIR` ; le shell y publie son répertoire courant et
+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 h.result_action, h.result_path = "cd", path
// With the sync on, the shell reads the request from its session file at the // 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. // 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.sync_handled = true
} }
h.lp.Quit(0) h.lp.Quit(0)
+3 -2
View File
@@ -51,7 +51,7 @@ agr('shell', 'Shell synchronisation')
opt( opt(
'sync_cwd', 'sync_cwd',
'no', 'yes',
option_type='to_bool', option_type='to_bool',
long_text=""" long_text="""
Keep the shell's working directory and the panel in step. A :code:`cd` in the 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: 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 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 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) { func TestEditArgs(t *testing.T) {
got := edit_args("/tmp/a.py") got := edit_args("/tmp/a.py")
want := []string{"edit-in-kitty", "--type=tab", "/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) 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" } 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 // 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 // or relative path, or one that no longer exists, is treated as unknown rather
// than followed, as the shell may be between two writes. // 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) { func TestShellCwdIgnoresUnusablePaths(t *testing.T) {
dir := t.TempDir() dir := t.TempDir()
s := new_shell_sync(dir, "9", true) s := new_shell_sync(dir, "9", true)