diff --git a/go.mod b/go.mod index ccac98c..d2be9ce 100644 --- a/go.mod +++ b/go.mod @@ -22,6 +22,7 @@ require ( github.com/google/go-cmp v0.6.0 github.com/gorilla/websocket v1.5.3 github.com/klauspost/compress v1.20.0 + github.com/mattn/go-shellwords v1.0.15 github.com/ncruces/zenity v0.10.15 github.com/rivo/uniseg v0.4.7 github.com/sahilm/fuzzy v0.1.3 diff --git a/go.sum b/go.sum index 906b06d..1d5b519 100644 --- a/go.sum +++ b/go.sum @@ -68,6 +68,8 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0 github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/lucasb-eyer/go-colorful v1.4.1 h1:1EO+WB73+EH8EVbzlrG3KLAfEypQWVHIBqlTf+2hNss= github.com/lucasb-eyer/go-colorful v1.4.1/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/mattn/go-shellwords v1.0.15 h1:rx0n8+ZdM9JWZMlr2BMPAjtLU0rfluLNtwMC2FJOTtY= +github.com/mattn/go-shellwords v1.0.15/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y= github.com/ncruces/zenity v0.10.15 h1:UaHekkxwt7QjoZmMWtzhqSEtQT97/Cux6FDBWwfyuqk= github.com/ncruces/zenity v0.10.15/go.mod h1:45d81tt+vF/UjBjlWE/vvE0oD0hWuHUQld4vPbA4VxE= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/internal/config/editor.go b/internal/config/editor.go new file mode 100644 index 0000000..c48fad6 --- /dev/null +++ b/internal/config/editor.go @@ -0,0 +1,19 @@ +package config + +import ( + "errors" + "os/exec" + + "github.com/mattn/go-shellwords" +) + +func (cfg *Config) EditorCommand(path string) (*exec.Cmd, error) { + args, err := shellwords.Parse(cfg.Editor) + if err != nil { + return nil, err + } + if len(args) == 0 { + return nil, errors.New("editor command is empty") + } + return exec.Command(args[0], append(args[1:], path)...), nil +} diff --git a/internal/config/editor_default.go b/internal/config/editor_default.go deleted file mode 100644 index 0e20f03..0000000 --- a/internal/config/editor_default.go +++ /dev/null @@ -1,11 +0,0 @@ -//go:build !unix - -package config - -import ( - "os/exec" -) - -func (cfg *Config) CreateEditorCommand(path string) *exec.Cmd { - return exec.Command(cfg.Editor, path) -} diff --git a/internal/config/editor_unix.go b/internal/config/editor_unix.go deleted file mode 100644 index cec488a..0000000 --- a/internal/config/editor_unix.go +++ /dev/null @@ -1,11 +0,0 @@ -//go:build unix - -package config - -import ( - "os/exec" -) - -func (cfg *Config) CreateEditorCommand(path string) *exec.Cmd { - return exec.Command("sh", "-c", cfg.Editor+" \"$@\"", cfg.Editor, path) -} diff --git a/internal/ui/chat/composer.go b/internal/ui/chat/composer.go index be08d5d..81876f1 100644 --- a/internal/ui/chat/composer.go +++ b/internal/ui/chat/composer.go @@ -829,8 +829,9 @@ func (c *composer) editor() tview.Cmd { _, _ = file.WriteString(text) _ = file.Close() - cmd := cfg.CreateEditorCommand(name) - if cmd == nil { + cmd, err := cfg.EditorCommand(name) + if err != nil { + slog.Error("failed to create editor command", "err", err) return nil } cmd.Stdin = os.Stdin