on main help sign in
fix(editor): fix opening `$EDITOR` w/ and w/o args (#1520)
4f37cffAmolith
internal/tui/components/chat/editor/editor.go+2−6
@@ -6,7 +6,6 @@ import (
6 "math/rand"
6 "math/rand"
7 "net/http"
7 "net/http"
8 "os"
8 "os"
9− "os/exec"
10 "path/filepath"
9 "path/filepath"
11 "runtime"
10 "runtime"
12 "slices"
11 "slices"
@@ -112,11 +111,8 @@ func (m *editorCmp) openEditor(value string) tea.Cmd {
112 if _, err := tmpfile.WriteString(value); err != nil {
111 if _, err := tmpfile.WriteString(value); err != nil {
113 return util.ReportError(err)
112 return util.ReportError(err)
114 }
113 }
115− c := exec.CommandContext(context.TODO(), editor, tmpfile.Name())
114+ cmdStr := editor + " " + tmpfile.Name()
116− c.Stdin = os.Stdin
117− c.Stdout = os.Stdout
118− c.Stderr = os.Stderr
119− return tea.ExecProcess(c, func(err error) tea.Msg {
115+ return util.ExecShell(context.TODO(), cmdStr, func(err error) tea.Msg {
120 if err != nil {
116 if err != nil {
121 return util.ReportError(err)
117 return util.ReportError(err)
122 }
118 }
internal/tui/util/shell.gonew file+26
@@ -0,0 +1,26 @@
1+package util
2+
3+import (
4+ "context"
5+ "errors"
6+ "os/exec"
7+
8+ tea "charm.land/bubbletea/v2"
9+ "mvdan.cc/sh/v3/shell"
10+)
11+
12+// ExecShell parses a shell command string and executes it with exec.Command.
13+// Uses shell.Fields for proper handling of shell syntax like quotes and
14+// arguments while preserving TTY handling for terminal editors.
15+func ExecShell(ctx context.Context, cmdStr string, callback tea.ExecCallback) tea.Cmd {
16+ fields, err := shell.Fields(cmdStr, nil)
17+ if err != nil {
18+ return ReportError(err)
19+ }
20+ if len(fields) == 0 {
21+ return ReportError(errors.New("empty command"))
22+ }
23+
24+ cmd := exec.CommandContext(ctx, fields[0], fields[1:]...)
25+ return tea.ExecProcess(cmd, callback)
26+}