feat(file-manager): open files in a full-window editor tab
Enter on a file handed the editor command to the shell, so the panel closed and had to be reopened, and a docked panel ran the editor squeezed into the dock. Delegate to kitty's edit protocol instead: the editor opens in a new tab with the whole window and the panel is redrawn with the same selection when it exits. The editor now comes from kitty's editor option. file-manager's editor, open_with_sudo and --open-here are kept but ignored so existing configs and mappings keep loading.
This commit is contained in:
4 files changed
+68
-160
No files matched your search
@@ -16,7 +16,6 @@ import (
|
||||
"github.com/kovidgoyal/kitty/tools/config"
|
||||
"github.com/kovidgoyal/kitty/tools/tui"
|
||||
"github.com/kovidgoyal/kitty/tools/tui/loop"
|
||||
"github.com/kovidgoyal/kitty/tools/utils/shlex"
|
||||
"github.com/kovidgoyal/kitty/tools/wcswidth"
|
||||
)
|
||||
|
||||
@@ -203,7 +202,6 @@ type Handler struct {
|
||||
message string
|
||||
result_action string
|
||||
result_path string
|
||||
open_here bool
|
||||
shortcut_tracker config.ShortcutTracker
|
||||
keyboard_shortcuts []*config.KeyAction
|
||||
}
|
||||
@@ -398,7 +396,7 @@ func (h *Handler) copy_selected_path() {
|
||||
}
|
||||
|
||||
// activate implements the primary action: the selected directory becomes the
|
||||
// working directory of the shell, the selected file is opened by the host.
|
||||
// working directory of the shell, the selected file is opened in the editor.
|
||||
func (h *Handler) activate() {
|
||||
e, ok := h.selected_entry()
|
||||
if !ok {
|
||||
@@ -410,46 +408,28 @@ func (h *Handler) activate() {
|
||||
h.change_shell_dir_to(path)
|
||||
return
|
||||
}
|
||||
if h.open_here {
|
||||
h.open_file_in_panel(path)
|
||||
h.open_file(path)
|
||||
}
|
||||
|
||||
// edit_args is the client side of kitty's edit protocol. The terminal opens the
|
||||
// editor in a new tab, which gives the file a whole window even when the panel
|
||||
// is docked. The tab closes when the editor exits and the panel is redrawn in
|
||||
// place, instead of handing the command to the shell and disappearing.
|
||||
func edit_args(path string) []string {
|
||||
return []string{"edit-in-kitty", "--type=tab", path}
|
||||
}
|
||||
|
||||
func (h *Handler) open_file(path string) {
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
h.message = err.Error()
|
||||
h.draw_screen()
|
||||
return
|
||||
}
|
||||
h.result_action, h.result_path = "open", path
|
||||
h.lp.Quit(0)
|
||||
}
|
||||
|
||||
// editor_cmd resolves the editor used by --open-here. The panel's own `editor`
|
||||
// option comes first because kitty's `editor` option is not visible to a
|
||||
// process started with launch, then the usual environment variables.
|
||||
func editor_cmd(conf *Config) []string {
|
||||
for _, candidate := range []string{conf.Editor, os.Getenv("VISUAL"), os.Getenv("EDITOR")} {
|
||||
if candidate == "" {
|
||||
continue
|
||||
}
|
||||
if words, err := shlex.Split(candidate); err == nil && len(words) > 0 {
|
||||
return words
|
||||
}
|
||||
}
|
||||
return []string{"vi"}
|
||||
}
|
||||
|
||||
// open_cmdline builds the command that opens path in the editor.
|
||||
func open_cmdline(conf *Config, path string) []string {
|
||||
cmdline := append(editor_cmd(conf), path)
|
||||
if conf.Open_with_sudo {
|
||||
// -- stops sudo from eating editor arguments that look like its own options.
|
||||
cmdline = append([]string{"sudo", "--"}, cmdline...)
|
||||
}
|
||||
return cmdline
|
||||
}
|
||||
|
||||
// open_file_in_panel runs the editor in the panel's own window. A docked panel
|
||||
// has to do this itself: kitty never sends the result of a launch --type=window-dock
|
||||
// back to the shell, so the panel would otherwise close without opening anything.
|
||||
func (h *Handler) open_file_in_panel(path string) {
|
||||
cmdline := open_cmdline(h.conf, path)
|
||||
err := h.lp.SuspendAndRun(func() error {
|
||||
cmd := exec.Command(cmdline[0], cmdline[1:]...)
|
||||
// The loop must give up the tty while edit-in-kitty talks to the terminal,
|
||||
// otherwise both processes compete for input and escape code responses.
|
||||
err = h.lp.SuspendAndRun(func() error {
|
||||
cmd := exec.Command(exe, edit_args(path)...)
|
||||
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
|
||||
return cmd.Run()
|
||||
})
|
||||
@@ -791,15 +771,9 @@ func (h *Handler) draw_screen() {
|
||||
}
|
||||
}
|
||||
|
||||
// result_payload is the JSON data handed over to the Python result handler. The
|
||||
// sudo flag travels with the result so that handle_result does not have to read
|
||||
// file-manager.conf a second time.
|
||||
// result_payload is the JSON data handed over to the Python result handler.
|
||||
func (h *Handler) result_payload() map[string]string {
|
||||
data := map[string]string{"action": h.result_action, "path": h.result_path}
|
||||
if h.result_action == "open" && h.conf.Open_with_sudo {
|
||||
data["sudo"] = "1"
|
||||
}
|
||||
return data
|
||||
return map[string]string{"action": h.result_action, "path": h.result_path}
|
||||
}
|
||||
|
||||
func load_config(opts *Options) (*Config, error) {
|
||||
@@ -828,7 +802,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, open_here: opts.OpenHere}
|
||||
h := &Handler{lp: lp, conf: conf, cwd: start_dir, show_hidden: opts.ShowHidden || conf.Show_hidden}
|
||||
h.keyboard_shortcuts = config.ResolveShortcuts(conf.KeyboardShortcuts)
|
||||
lp.MouseTrackingMode(loop.FULL_MOUSE_TRACKING)
|
||||
|
||||
|
||||
Reference in new issue
Block a user