Files
FWS-Kitty/kittens/file_manager/sync.go
T
BlackAngelTV 5d2e28b144 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.
2026-11-11 08:48:03 +01:00

100 lines
3.3 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_has_hook reports whether the shell has published its working directory
// at least once, which only the integration hook does. Without it nothing would
// consume the request, so the panel has to type the change into the shell.
func (s *shell_sync) shell_has_hook() bool {
_, err := os.Stat(s.shell_cwd_path())
return err == nil
}
// 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())
}
// 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())
}