Files
greq/internal/tui/editor_picker.go
Mester Gábor 2dd6519168 Initial commit
2026-03-19 07:12:03 +01:00

79 lines
1.6 KiB
Go

package tui
import (
"os/exec"
)
// candidateEditors is the ordered list shown in the picker.
// Only editors actually found on PATH are offered.
var candidateEditors = []struct {
bin string // executable name
label string // human-readable label
}{
{"nvim", "Neovim"},
{"vim", "Vim"},
{"vi", "Vi"},
{"nano", "Nano"},
{"micro", "Micro"},
{"hx", "Helix"},
{"emacs", "Emacs (terminal)"},
{"code", "VS Code (code --wait)"},
{"gedit", "Gedit"},
{"kate", "Kate"},
}
type editorChoice struct {
bin string
label string
// For VS Code we need the --wait flag so the terminal blocks until closed.
args []string
}
// availableEditors returns only the editors found on the current PATH.
func availableEditors() []editorChoice {
var found []editorChoice
for _, c := range candidateEditors {
if path, err := exec.LookPath(c.bin); err == nil {
choice := editorChoice{bin: path, label: c.label}
if c.bin == "code" {
choice.args = []string{"--wait"}
}
found = append(found, choice)
}
}
return found
}
// editorCommand returns (binary, extraArgs) for a saved editor string.
// e.g. "code --wait" → ("code", ["--wait"])
func editorCommand(saved string) (string, []string) {
if saved == "" {
return "", nil
}
// Handle "code --wait" style strings stored in config
parts := splitArgs(saved)
if len(parts) == 1 {
return parts[0], nil
}
return parts[0], parts[1:]
}
func splitArgs(s string) []string {
var out []string
cur := ""
for _, r := range s {
if r == ' ' {
if cur != "" {
out = append(out, cur)
cur = ""
}
} else {
cur += string(r)
}
}
if cur != "" {
out = append(out, cur)
}
return out
}