Enter on a file (archives still go to 6.5): an executable opens a Run / Edit / View menu and never runs by itself (U-P1-3, N-P0-2; Run pauses for Enter afterwards); text goes to $VISUAL/$EDITOR (parsed with quotes); other files go to xdg-open, reaped in the background (no zombies: U-P1-15). A file on SSH (or in an archive) is downloaded to the run dir; edits are uploaded back via WriteAtomic only if the copy changed, and if the server file changed meanwhile the user chooses overwrite / save a copy beside / keep the local copy (U-P1-12). p mirrors with a reference per mount (U-P1-14, N-P1-12); S sorts (saved to the config); y copies paths. The session goes through internal/state: restored at start (remote and archive panels as their local place), saved every 30 s when changed, on SIGHUP (tui.Signal) and in main's teardown (App.Shutdown). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
140 lines
3.3 KiB
Go
140 lines
3.3 KiB
Go
package tui
|
|
|
|
// external.go is, with deps.go, the only file of internal/tui allowed to
|
|
// touch os and os/exec (TestImportsGuard): external programs (editor, Run,
|
|
// xdg-open) and the clipboard.
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"runtime"
|
|
"strings"
|
|
|
|
"github.com/atotto/clipboard"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/charmbracelet/x/ansi"
|
|
|
|
"vcom/internal/state"
|
|
)
|
|
|
|
func fillExternal(d *Deps) {
|
|
if d.Exec == nil {
|
|
d.Exec = func(c *exec.Cmd, done tea.ExecCallback) tea.Cmd { return tea.ExecProcess(c, done) }
|
|
}
|
|
if len(d.Opener) == 0 {
|
|
d.Opener = []string{"xdg-open"}
|
|
if runtime.GOOS == "darwin" {
|
|
d.Opener = []string{"open"}
|
|
}
|
|
}
|
|
if d.Getenv == nil {
|
|
d.Getenv = os.Getenv
|
|
}
|
|
if d.RunDir == "" {
|
|
d.RunDir = os.TempDir()
|
|
}
|
|
if d.Clipboard == nil {
|
|
d.Clipboard = func(s string) error {
|
|
if err := clipboard.WriteAll(s); err == nil {
|
|
return nil
|
|
}
|
|
// No X/Wayland clipboard tool: OSC 52 (terminal clipboard).
|
|
_, err := fmt.Fprint(os.Stderr, ansi.SetSystemClipboard(s))
|
|
return err
|
|
}
|
|
}
|
|
if d.SaveSession == nil {
|
|
d.SaveSession = state.Save
|
|
}
|
|
}
|
|
|
|
// shellSplit splits a command line like a POSIX shell would for words:
|
|
// quotes and backslashes are honoured (U-P2: strings.Fields broke
|
|
// EDITOR="code --wait" with quoted paths).
|
|
func shellSplit(s string) ([]string, error) {
|
|
var out []string
|
|
var cur strings.Builder
|
|
inWord, quote := false, byte(0)
|
|
for i := 0; i < len(s); i++ {
|
|
c := s[i]
|
|
switch {
|
|
case quote == '\'':
|
|
if c == '\'' {
|
|
quote = 0
|
|
} else {
|
|
cur.WriteByte(c)
|
|
}
|
|
case quote == '"':
|
|
switch {
|
|
case c == '"':
|
|
quote = 0
|
|
case c == '\\' && i+1 < len(s) && strings.ContainsRune(`"\$`+"`", rune(s[i+1])):
|
|
i++
|
|
cur.WriteByte(s[i])
|
|
default:
|
|
cur.WriteByte(c)
|
|
}
|
|
case c == '\'' || c == '"':
|
|
quote, inWord = c, true
|
|
case c == '\\' && i+1 < len(s):
|
|
i++
|
|
cur.WriteByte(s[i])
|
|
inWord = true
|
|
case c == ' ' || c == '\t' || c == '\n':
|
|
if inWord {
|
|
out = append(out, cur.String())
|
|
cur.Reset()
|
|
inWord = false
|
|
}
|
|
default:
|
|
cur.WriteByte(c)
|
|
inWord = true
|
|
}
|
|
}
|
|
if quote != 0 {
|
|
return nil, errors.New("unterminated quote")
|
|
}
|
|
if inWord {
|
|
out = append(out, cur.String())
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// editorCmd builds the editor command for a local file: $VISUAL, $EDITOR,
|
|
// else vi.
|
|
func editorCmd(getenv func(string) string, path string) (*exec.Cmd, error) {
|
|
line := getenv("VISUAL")
|
|
if strings.TrimSpace(line) == "" {
|
|
line = getenv("EDITOR")
|
|
}
|
|
if strings.TrimSpace(line) == "" {
|
|
line = "vi"
|
|
}
|
|
argv, err := shellSplit(line)
|
|
if err != nil || len(argv) == 0 {
|
|
return nil, fmt.Errorf("cannot parse $EDITOR %q: %v", line, err)
|
|
}
|
|
return exec.Command(argv[0], append(argv[1:], path)...), nil
|
|
}
|
|
|
|
// runCmd runs a local executable in dir and waits for Enter afterwards so
|
|
// its output can be read (U-P1-3).
|
|
func runCmd(path, dir string) *exec.Cmd {
|
|
script := `"$0"; s=$?; printf '\n[exit %d] Press Enter to return to vcom' "$s"; read _`
|
|
c := exec.Command("sh", "-c", script, path)
|
|
c.Dir = dir
|
|
return c
|
|
}
|
|
|
|
// openDetached starts the desktop opener and reaps it in the background:
|
|
// no zombie processes (U-P1-15).
|
|
func openDetached(opener []string, path string) error {
|
|
c := exec.Command(opener[0], append(opener[1:], path)...)
|
|
if err := c.Start(); err != nil {
|
|
return err
|
|
}
|
|
go func() { _ = c.Wait() }()
|
|
return nil
|
|
}
|