Skip to content

Commit f386274

Browse files
committed
addressing review comments
1 parent 67b0248 commit f386274

4 files changed

Lines changed: 37 additions & 23 deletions

File tree

cmd/cli/commands/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func generateInteractiveWithReadline(cmd *cobra.Command, desktopClient *desktop.
132132
fmt.Fprintln(os.Stderr, " Ctrl + w Delete the word before the cursor")
133133
fmt.Fprintln(os.Stderr, "")
134134
fmt.Fprintln(os.Stderr, " Ctrl + l Clear the screen")
135-
fmt.Fprintln(os.Stderr, " Ctrl + x Open prompt in default system text editor")
135+
fmt.Fprintln(os.Stderr, " Ctrl + x Open prompt in text editor ($EDITOR)")
136136
fmt.Fprintln(os.Stderr, " Ctrl + c Stop the model from responding")
137137
fmt.Fprintln(os.Stderr, " Ctrl + d Exit (/bye)")
138138
fmt.Fprintln(os.Stderr, "")

cmd/cli/readline/editor.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package readline
33
import (
44
"os"
55
"os/exec"
6+
"runtime"
67
"strings"
78
)
89

@@ -19,18 +20,7 @@ func runEditor(content string, defaultEditor string) (string, error) {
1920
}
2021
tmpFile.Close()
2122

22-
editor := strings.TrimSpace(os.Getenv("EDITOR"))
23-
if editor == "" {
24-
editor = defaultEditor
25-
}
26-
27-
// handle for env variables set with args
28-
parts := strings.Fields(editor)
29-
args := append(parts[1:], tmpFile.Name())
30-
cmd := exec.Command(parts[0], args...)
31-
cmd.Stdin = os.Stdin
32-
cmd.Stdout = os.Stdout
33-
cmd.Stderr = os.Stderr
23+
cmd := buildEditorCmd(defaultEditor, tmpFile.Name())
3424
if err := cmd.Run(); err != nil {
3525
return content, err
3626
}
@@ -42,3 +32,23 @@ func runEditor(content string, defaultEditor string) (string, error) {
4232

4333
return string(edited), nil
4434
}
35+
36+
func buildEditorCmd(defaultEditor string, filePath string) *exec.Cmd {
37+
editor := strings.TrimSpace(os.Getenv("EDITOR"))
38+
if editor == "" {
39+
editor = defaultEditor
40+
}
41+
42+
var cmd *exec.Cmd
43+
if runtime.GOOS == "windows" {
44+
parts := strings.Fields(editor)
45+
args := append(parts[1:], filePath)
46+
cmd = exec.Command(parts[0], args...)
47+
} else {
48+
cmd = exec.Command("sh", "-c", editor+" \"$1\"", "--", filePath)
49+
}
50+
cmd.Stdin = os.Stdin
51+
cmd.Stdout = os.Stdout
52+
cmd.Stderr = os.Stderr
53+
return cmd
54+
}

cmd/cli/readline/readline_unix.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ func openInEditor(fd uintptr, termios any, content string) (string, error) {
2626
}
2727

2828
edited, err := runEditor(content, "vi")
29-
if err != nil {
30-
SetRawMode(fd)
31-
return content, err
29+
30+
// Always restore raw mode using the original termios, whether the editor
31+
// succeeded or failed, so the terminal returns to its previous configuration.
32+
if _, restoreErr := SetRawMode(fd); restoreErr != nil {
33+
return content, restoreErr
3234
}
3335

34-
if _, err := SetRawMode(fd); err != nil {
35-
return edited, err
36+
if err != nil {
37+
return content, err
3638
}
3739

3840
return edited, nil

cmd/cli/readline/readline_windows.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ func openInEditor(fd uintptr, termios any, content string) (string, error) {
1313
}
1414

1515
edited, err := runEditor(content, "notepad")
16-
if err != nil {
17-
SetRawMode(fd)
18-
return content, err
16+
17+
// Always restore raw mode using the original state, whether the editor
18+
// succeeded or failed, so the terminal returns to its previous configuration.
19+
if _, restoreErr := SetRawMode(fd); restoreErr != nil {
20+
return content, restoreErr
1921
}
2022

21-
if _, err := SetRawMode(fd); err != nil {
22-
return edited, err
23+
if err != nil {
24+
return content, err
2325
}
2426

2527
return edited, nil

0 commit comments

Comments
 (0)