From 5d2e28b1441e3a95917b99b29c0543cc70845cd9 Mon Sep 17 00:00:00 2001 From: BlackAngelTVdev Date: Wed, 11 Nov 2026 08:48:03 +0100 Subject: [PATCH] feat(file-manager): move the shell while browsing the panel Opening a directory in the panel now hands it to the shell, so walking the tree reads like cd and the two stay on the same directory. The request is still read at the shell's next prompt, and quitting without validating withdraws a request the shell has not read yet, so Esc keeps leaving the shell where it was. --- docs/changelog.rst | 10 +++++----- docs/kittens/file-manager.rst | 22 ++++++++++++--------- kittens/file_manager/main.go | 33 +++++++++++++++++++++++++++++-- kittens/file_manager/main.py | 5 +++-- kittens/file_manager/sync.go | 6 ++++++ kittens/file_manager/sync_test.go | 30 ++++++++++++++++++++++++++++ 6 files changed, 88 insertions(+), 18 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index ce2c664..1b843ca 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -224,11 +224,11 @@ Detailed list of changes - The :doc:`file manager ` now keeps its directory in step with the shell by default: a :code:`cd` moves the panel into the same - directory and validating a directory moves the shell at its next prompt, for - bash, zsh and fish. Nothing is typed into the terminal, so no command lands - in the history and a multi-line command being typed is left untouched. Set - :code:`sync_cwd no` in :file:`file-manager.conf` to keep the two directories - independent, as before. + directory and walking through the panel moves the shell at its next prompt, + for bash, zsh and fish. Nothing is typed into the terminal, so no command + lands in the history and a multi-line command being typed is left untouched. + Set :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 ` in a left dock by default, instead of requiring a diff --git a/docs/kittens/file-manager.rst b/docs/kittens/file-manager.rst index 447ad46..8f87b8c 100644 --- a/docs/kittens/file-manager.rst +++ b/docs/kittens/file-manager.rst @@ -59,7 +59,8 @@ Actions utilisez :code:`sudo -e` depuis le shell. :kbd:`→` ou :kbd:`L` - descend dans le répertoire sélectionné sans toucher au shell. + descend dans le répertoire sélectionné ; le shell y va aussi, au prompt + suivant, quand la synchronisation est active. :kbd:`←`, :kbd:`Retour arrière` ou :kbd:`H` remonte au répertoire parent. @@ -92,17 +93,19 @@ Actions affiche ou masque les entrées cachées. :kbd:`Échap` ou :kbd:`Q` - ferme le panneau sans rien changer. + ferme le panneau sans rien changer : une demande de synchronisation que le + shell n'a pas encore lue est retirée, il reste donc où il était. Synchronisation avec le shell ------------------------------ Le panneau et le shell partagent leur répertoire courant depuis la version -0.50.1 : un :code:`cd` dans le shell amène le panneau dans le même répertoire, -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 :: +0.50.1, dans les deux sens : un :code:`cd` dans le shell amène le panneau dans +le même répertoire, et chaque répertoire ouvert dans le panneau devient aussi +celui du shell, sans même avoir à valider. 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 sync_cwd no @@ -123,9 +126,10 @@ pas soumise. l'option est désactivée. Le changement est appliqué au prompt suivant : le shell se déplace quand la - commande suivante démarre, ou qu'une ligne vide est validée, et non à - l'instant où le panneau se ferme. C'est le prix à payer pour ne jamais écrire - dans le terminal. + commande suivante démarre, ou qu'une ligne vide est validée, et non pendant + que vous parcourez le panneau. C'est le prix à payer pour ne jamais écrire + dans le terminal. Si vous quittez le panneau avec :kbd:`Échap` avant que le + shell ait lu la demande, elle est retirée et le shell ne bouge pas. Lancé dans un dock, le panneau suit la fenêtre active du groupe au moment où le dock est créé. Si vous passez ensuite à une autre fenêtre du même groupe, diff --git a/kittens/file_manager/main.go b/kittens/file_manager/main.go index a3424d9..cc1e194 100644 --- a/kittens/file_manager/main.go +++ b/kittens/file_manager/main.go @@ -206,6 +206,7 @@ type Handler struct { keyboard_shortcuts []*config.KeyAction sync *shell_sync sync_handled bool + sync_published bool } func (h *Handler) initialize() (string, error) { @@ -236,6 +237,32 @@ func (h *Handler) initialize() (string, error) { return "", nil } +// publish_browse hands the directory the user just opened in the panel to the +// shell, so that walking through the tree in the panel walks the shell through +// it too. The shell reads the request at its next prompt; until then the panel +// remembers it so that quitting can withdraw it. +func (h *Handler) publish_browse() { + if h.sync == nil || !h.sync.shell_has_hook() { + return + } + if err := h.sync.publish_request(h.cwd); err != nil { + return + } + h.sync_published = true +} + +// withdraw_sync_request drops a request published by browsing, so that leaving +// the panel without validating a directory leaves the shell where it was. It +// does nothing once the shell has read the request, which it only does at a +// prompt. +func (h *Handler) withdraw_sync_request() { + if !h.sync_published { + return + } + h.sync.withdraw_request() + h.sync_published = false +} + // follow_shell_cwd mirrors a cd made in the shell into the panel, keeping the // selection on the same name so that the cursor does not jump around. func (h *Handler) follow_shell_cwd() { @@ -318,6 +345,7 @@ func (h *Handler) change_dir(dir string) { } h.selected, h.scroll, h.message = 0, 0, "" h.draw_screen() + h.publish_browse() } func (h *Handler) move_selection(delta int) { @@ -480,8 +508,8 @@ func (h *Handler) open_file(path string) { h.draw_screen() } -// enter_dir browses into the selected directory inside the panel, leaving the -// working directory of the shell untouched. +// enter_dir browses into the selected directory inside the panel and hands it to +// the shell, so that both stay on the same directory. func (h *Handler) enter_dir() { e, ok := h.selected_entry() if !ok || !e.IsDir { @@ -518,6 +546,7 @@ func (h *Handler) change_shell_dir() { func (h *Handler) perform_action(name string) { switch name { case "quit": + h.withdraw_sync_request() h.lp.Quit(0) case "move_up": h.move_selection(-1) diff --git a/kittens/file_manager/main.py b/kittens/file_manager/main.py index d878c89..c2e031b 100644 --- a/kittens/file_manager/main.py +++ b/kittens/file_manager/main.py @@ -55,8 +55,9 @@ opt( option_type='to_bool', long_text=""" Keep the shell's working directory and the panel in step. A :code:`cd` in the -shell moves the panel to the same directory, and validating a directory in the -panel makes the shell change to it at its next prompt. +shell moves the panel to the same directory, and walking through the panel moves +the shell to the directory being browsed at its next prompt. Quitting the panel +without validating a directory withdraws a request the shell has not read yet. 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: diff --git a/kittens/file_manager/sync.go b/kittens/file_manager/sync.go index bc4b77b..16de11b 100644 --- a/kittens/file_manager/sync.go +++ b/kittens/file_manager/sync.go @@ -91,3 +91,9 @@ func (s *shell_sync) publish_request(dir string) error { } return os.Rename(tmp, s.request_path()) } + +// withdraw_request cancels a request the shell has not read yet. Removing a +// request it already consumed is not an error, the shell removes it itself. +func (s *shell_sync) withdraw_request() { + _ = os.Remove(s.request_path()) +} diff --git a/kittens/file_manager/sync_test.go b/kittens/file_manager/sync_test.go index 3a10340..36394e0 100644 --- a/kittens/file_manager/sync_test.go +++ b/kittens/file_manager/sync_test.go @@ -65,6 +65,36 @@ func TestShellHasHookNeedsAPublishedDirectory(t *testing.T) { } } +func TestPublishBrowseNeedsTheShellHookAndCanBeWithdrawn(t *testing.T) { + dir := t.TempDir() + h := &Handler{sync: new_shell_sync(dir, "11", true), cwd: dir} + h.publish_browse() + if _, err := os.Stat(filepath.Join(dir, "11.set")); err == nil { + t.Fatal("the panel must not publish before the shell proved it has the hook") + } + if h.sync_published { + t.Fatal("nothing was published") + } + if err := os.WriteFile(filepath.Join(dir, "11"), []byte(dir), 0o600); err != nil { + t.Fatal(err) + } + h.publish_browse() + if got, err := os.ReadFile(filepath.Join(dir, "11.set")); err != nil || string(got) != dir { + t.Fatalf("unexpected request %q (%v)", got, err) + } + if !h.sync_published { + t.Fatal("the request must be remembered so that quitting can withdraw it") + } + h.withdraw_sync_request() + if _, err := os.Stat(filepath.Join(dir, "11.set")); err == nil { + t.Fatal("quitting must withdraw the pending request") + } + if h.sync_published { + t.Fatal("nothing is pending anymore") + } + h.withdraw_sync_request() +} + func TestShellCwdIgnoresUnusablePaths(t *testing.T) { dir := t.TempDir() s := new_shell_sync(dir, "9", true)