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.
This commit is contained in:
6 files changed
+88
-18
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user