Files
FWS-Kitty/kittens/file_manager/sync.go
T
BlackAngelTV 1b330bcf81 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.
2026-11-11 08:20:11 +01:00

86 lines
2.7 KiB
Go

// License: GPLv3 Copyright: 2026, FWS
package file_manager
import (
"os"
"path/filepath"
"strings"
)
// shell_sync is the file based channel between a shell and the panel. kitty
// exports KITTY_CWD_SYNC_DIR to every window and KITTY_SPECIAL_WINDOW_PARENT to
// the special windows it opens for another one, so both sides agree on two file
// names without a socket or a new protocol: the shell publishes its working
// directory in <dir>/<window id>, the panel publishes the directory it wants in
// <dir>/<window id>.set. The shell applies that request from its prompt hook,
// which keeps the command out of the history and leaves a half typed multi-line
// command alone.
type shell_sync struct {
dir string
window_id string
last string
}
func new_shell_sync(dir, window_id string, enabled bool) *shell_sync {
if !enabled || dir == "" || window_id == "" {
return nil
}
if info, err := os.Stat(dir); err != nil || !info.IsDir() {
return nil
}
return &shell_sync{dir: dir, window_id: window_id}
}
// new_shell_sync_from_env is nil whenever the option is off or kitty did not
// give the panel a shell window to talk to, in which case the panel keeps its
// own behaviour and types the change into the shell instead.
func new_shell_sync_from_env(enabled bool) *shell_sync {
return new_shell_sync(os.Getenv("KITTY_CWD_SYNC_DIR"), os.Getenv("KITTY_SPECIAL_WINDOW_PARENT"), enabled)
}
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" }
// 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
// than followed, as the shell may be between two writes.
func (s *shell_sync) shell_cwd() string {
b, err := os.ReadFile(s.shell_cwd_path())
if err != nil {
return ""
}
p := strings.TrimSpace(string(b))
if !filepath.IsAbs(p) {
return ""
}
if info, err := os.Stat(p); err != nil || !info.IsDir() {
return ""
}
return p
}
// new_target returns the directory the panel should move to, or "" when the
// shell has not moved since the last call or already sits in current.
func (s *shell_sync) new_target(current string) string {
p := s.shell_cwd()
if p == "" || p == s.last {
return ""
}
s.last = p
if p == current {
return ""
}
return p
}
// publish_request hands a directory to the shell. The write goes through a
// temporary file so that the shell never reads a half written path.
func (s *shell_sync) publish_request(dir string) error {
tmp := s.request_path() + ".tmp"
if err := os.WriteFile(tmp, []byte(dir), 0o600); err != nil {
return err
}
return os.Rename(tmp, s.request_path())
}