feat(file-manager): open files in the editor from the panel

Files used to open in a new tab, which loses the shell the user was
working in. Hand the editor command to that shell instead, optionally
through sudo for files owned by root.

A docked panel cannot do that: kitty never routes the result of a
launch --type=window-dock back to the shell, so Enter used to close the
panel without opening anything. Add --open-here, which suspends the
panel and runs the editor in its own window. The editor is resolved
from the new `editor` option, because kitty's own editor option is not
visible to a process started with launch.
This commit is contained in:
BlackAngelTV committed 2026-11-05 15:48:01 +01:00
1 parent fb87a7306f
commit 86982b79be
3 files changed
+212 -15

No files matched your search

+90
View File
@@ -161,6 +161,96 @@ func TestResolveStartDirAcceptsFiles(t *testing.T) {
}
}
func TestOpenWithSudoOption(t *testing.T) {
c := NewConfig()
if c.Open_with_sudo {
t.Fatal("files must not be opened with sudo by default")
}
if err := c.Parse("open_with_sudo", "yes"); err != nil {
t.Fatal(err)
}
if !c.Open_with_sudo {
t.Fatal("open_with_sudo = yes must enable sudo")
}
if err := c.Parse("open_with_sudo", "no"); err != nil {
t.Fatal(err)
}
if c.Open_with_sudo {
t.Fatal("open_with_sudo = no must disable sudo")
}
}
func TestEditorCommand(t *testing.T) {
t.Setenv("VISUAL", "")
t.Setenv("EDITOR", "")
if got := editor_cmd(NewConfig()); len(got) != 1 || got[0] != "vi" {
t.Fatalf("expected the vi fallback, got %#v", got)
}
t.Setenv("EDITOR", "nano")
if got := editor_cmd(NewConfig()); len(got) != 1 || got[0] != "nano" {
t.Fatalf("EDITOR must be used when the option is empty, got %#v", got)
}
t.Setenv("VISUAL", "code -w")
if got := editor_cmd(NewConfig()); len(got) != 2 || got[0] != "code" || got[1] != "-w" {
t.Fatalf("VISUAL must win over EDITOR, got %#v", got)
}
conf := NewConfig()
conf.Editor = "micro --wrap"
if got := editor_cmd(conf); len(got) != 2 || got[0] != "micro" || got[1] != "--wrap" {
t.Fatalf("the editor option must win over the environment, got %#v", got)
}
}
func TestOpenCmdline(t *testing.T) {
conf := NewConfig()
conf.Editor = "nano"
got := open_cmdline(conf, "/tmp/a.py")
if len(got) != 2 || got[0] != "nano" || got[1] != "/tmp/a.py" {
t.Fatalf("unexpected command line: %#v", got)
}
conf.Open_with_sudo = true
got = open_cmdline(conf, "/tmp/a.py")
want := []string{"sudo", "--", "nano", "/tmp/a.py"}
if len(got) != len(want) {
t.Fatalf("expected %#v, got %#v", want, got)
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("expected %#v, got %#v", want, got)
}
}
}
func TestLoadConfigReadsOpenWithSudo(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "file-manager.conf")
if err := os.WriteFile(path, []byte("open_with_sudo yes\n"), 0o644); err != nil {
t.Fatal(err)
}
conf, err := load_config(&Options{Config: []string{path}})
if err != nil {
t.Fatal(err)
}
if !conf.Open_with_sudo {
t.Fatal("open_with_sudo yes in file-manager.conf must enable sudo")
}
}
func TestResultPayloadCarriesSudoOnlyWhenOpening(t *testing.T) {
h := &Handler{conf: NewConfig(), result_action: "open", result_path: "/tmp/a.py"}
if _, ok := h.result_payload()["sudo"]; ok {
t.Fatal("sudo must not be requested when the option is disabled")
}
h.conf.Open_with_sudo = true
if got := h.result_payload()["sudo"]; got != "1" {
t.Fatalf("sudo must be requested when opening a file, got %q", got)
}
h.result_action = "cd"
if _, ok := h.result_payload()["sudo"]; ok {
t.Fatal("changing directory must never ask for sudo")
}
}
func TestTruncateToWidth(t *testing.T) {
if got := truncate_to_width("abcdef", 3); got != "ab\u2026" {
t.Fatalf("unexpected truncation %q", got)