on main help sign in
refactor(tui): move ExecShell to uiutil package
commit7fd14ff479be2d2f14d0be0b6a68f65f8bb79e6d authorAyman Bagabas <ayman.bagabas@gmail.com> dateTue 16 Dec 2025, 13:58 (1 year ago) parent4529235
2 files changed+22−13
internal/tui/util/shell.go+2−13 internal/uiutil/uiutil.go+20
internal/tui/util/shell.go+2−13
@@ -2,25 +2,14 @@ package util
22
33import (
44 "context"
5− "errors"
6− "os/exec"
75
86 tea "charm.land/bubbletea/v2"
9− "mvdan.cc/sh/v3/shell"
7+ "github.com/charmbracelet/crush/internal/uiutil"
108)
119
1210// ExecShell parses a shell command string and executes it with exec.Command.
1311// Uses shell.Fields for proper handling of shell syntax like quotes and
1412// arguments while preserving TTY handling for terminal editors.
1513func 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)
14+ return uiutil.ExecShell(ctx, cmdStr, callback)
2615}
internal/uiutil/uiutil.go+20
@@ -4,10 +4,14 @@
44package uiutil
55
66import (
7+ "context"
8+ "errors"
79 "log/slog"
10+ "os/exec"
811 "time"
912
1013 tea "charm.land/bubbletea/v2"
14+ "mvdan.cc/sh/v3/shell"
1115)
1216
1317type Cursor interface {
@@ -60,3 +64,19 @@ type (
6064 }
6165 ClearStatusMsg struct{}
6266)
67+
68+// ExecShell parses a shell command string and executes it with exec.Command.
69+// Uses shell.Fields for proper handling of shell syntax like quotes and
70+// arguments while preserving TTY handling for terminal editors.
71+func ExecShell(ctx context.Context, cmdStr string, callback tea.ExecCallback) tea.Cmd {
72+ fields, err := shell.Fields(cmdStr, nil)
73+ if err != nil {
74+ return ReportError(err)
75+ }
76+ if len(fields) == 0 {
77+ return ReportError(errors.New("empty command"))
78+ }
79+
80+ cmd := exec.CommandContext(ctx, fields[0], fields[1:]...)
81+ return tea.ExecProcess(cmd, callback)
82+}