feat(file-manager): sync the working directory with the shell
The panel and the shell each kept their own directory. The new sync_cwd option keeps them in step both ways, through files in a session directory exported by kitty rather than by typing a command into the terminal: the shell applies the directory chosen in the panel from its prompt hook with builtin cd, so nothing lands in the history and a half typed multi-line command is not submitted.
This commit is contained in:
12 files changed
+425
-6
No files matched your search
@@ -204,6 +204,8 @@ type Handler struct {
|
||||
result_path string
|
||||
shortcut_tracker config.ShortcutTracker
|
||||
keyboard_shortcuts []*config.KeyAction
|
||||
sync *shell_sync
|
||||
sync_handled bool
|
||||
}
|
||||
|
||||
func (h *Handler) initialize() (string, error) {
|
||||
@@ -218,11 +220,43 @@ 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
|
||||
}
|
||||
|
||||
// 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.
|
||||
@@ -459,6 +493,11 @@ 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.
|
||||
if h.sync != nil && h.sync.publish_request(path) == nil {
|
||||
h.sync_handled = true
|
||||
}
|
||||
h.lp.Quit(0)
|
||||
}
|
||||
|
||||
@@ -776,7 +815,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) {
|
||||
@@ -805,7 +848,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)
|
||||
|
||||
|
||||
Reference in new issue
Block a user