Merge branch 'feat/cwd-sync'

Keep the file manager panel and the shell in the same directory in both
directions, through the shell integration instead of typing commands.
This commit is contained in:
BlackAngelTV committed 2026-11-12 07:32:09 +01:00
commit 38c4975943
13 files changed
+546 -10

No files matched your search

+82 -5
View File
@@ -221,6 +221,9 @@ type Handler struct {
result_path string
shortcut_tracker config.ShortcutTracker
keyboard_shortcuts []*config.KeyAction
sync *shell_sync
sync_handled bool
sync_published bool
}
func (h *Handler) initialize() (string, error) {
@@ -235,11 +238,69 @@ func (h *Handler) initialize() (string, error) {
if err = h.load(h.cwd); err != nil {
return "", err
}
if h.sync != nil {
// Adopt the directory the shell is already in as the reference point so
// that an explicit --start-dir is not overridden by the first tick.
h.sync.last = h.sync.shell_cwd()
if _, err = h.lp.AddTimer(400*time.Millisecond, true, func(loop.IdType) error {
h.follow_shell_cwd()
return nil
}); err != nil {
return "", err
}
}
h.draw_screen()
h.lp.SendOverlayReady()
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() {
if h.sync == nil {
return
}
dir := h.sync.new_target(h.cwd)
if dir == "" {
return
}
name := ""
if e, ok := h.selected_entry(); ok {
name = e.Name
}
if err := h.load(dir); err != nil {
return
}
h.select_name(name)
h.draw_screen()
}
// load reads dir and adopts it as the current directory. Selection is clamped
// rather than reset so that reloading a directory after an action keeps the
// cursor near where it was.
@@ -309,6 +370,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) {
@@ -483,8 +545,8 @@ func (h *Handler) open_file_in(path, window_type 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 {
@@ -496,6 +558,14 @@ func (h *Handler) enter_dir() {
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.
// 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)
}
@@ -546,7 +616,10 @@ var action_catalog = []action_info{
// Actions that need the panel state are plain functions of the handler, so the
// palette and the configurable shortcuts go through the same code path.
var action_handlers = map[string]func(*Handler){
"quit": func(h *Handler) { h.lp.Quit(0) },
"quit": func(h *Handler) {
h.withdraw_sync_request()
h.lp.Quit(0)
},
"move_up": func(h *Handler) { h.move_selection(-1) },
"move_down": func(h *Handler) { h.move_selection(1) },
"page_up": func(h *Handler) {
@@ -1136,7 +1209,11 @@ func (h *Handler) draw_screen() {
// result_payload is the JSON data handed over to the Python result handler.
func (h *Handler) result_payload() map[string]string {
return map[string]string{"action": h.result_action, "path": h.result_path}
ans := map[string]string{"action": h.result_action, "path": h.result_path}
if h.sync_handled {
ans["sync"] = "1"
}
return ans
}
func load_config(opts *Options) (*Config, error) {
@@ -1165,7 +1242,7 @@ func main(_ *cli.Command, opts *Options, args []string) (rc int, err error) {
if err != nil {
return 1, err
}
h := &Handler{lp: lp, conf: conf, cwd: start_dir, show_hidden: opts.ShowHidden || conf.Show_hidden}
h := &Handler{lp: lp, conf: conf, cwd: start_dir, show_hidden: opts.ShowHidden || conf.Show_hidden, sync: new_shell_sync_from_env(conf.Sync_cwd)}
h.keyboard_shortcuts = config.ResolveShortcuts(conf.KeyboardShortcuts)
lp.MouseTrackingMode(loop.FULL_MOUSE_TRACKING)