Skip to content

Commit c9b7b44

Browse files
committed
fixing review comments
1 parent f386274 commit c9b7b44

3 files changed

Lines changed: 20 additions & 6 deletions

File tree

cmd/cli/readline/editor.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ func runEditor(content string, defaultEditor string) (string, error) {
3030
return content, err
3131
}
3232

33-
return string(edited), nil
33+
result := strings.TrimRight(string(edited), "\r\n")
34+
35+
return result, nil
3436
}
3537

3638
func buildEditorCmd(defaultEditor string, filePath string) *exec.Cmd {
@@ -41,9 +43,7 @@ func buildEditorCmd(defaultEditor string, filePath string) *exec.Cmd {
4143

4244
var cmd *exec.Cmd
4345
if runtime.GOOS == "windows" {
44-
parts := strings.Fields(editor)
45-
args := append(parts[1:], filePath)
46-
cmd = exec.Command(parts[0], args...)
46+
cmd = exec.Command("cmd", "/C", editor+" \""+filePath+"\"")
4747
} else {
4848
cmd = exec.Command("sh", "-c", editor+" \"$1\"", "--", filePath)
4949
}

cmd/cli/readline/editor_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ func TestRunEditor(t *testing.T) {
3737
input: "",
3838
expected: "new content",
3939
},
40+
{
41+
name: "strips trailing newline",
42+
mockEditorScript: `printf "edited\n" > "$1"`,
43+
input: "",
44+
expected: "edited",
45+
},
46+
{
47+
name: "strips trailing carriage return and newline",
48+
mockEditorScript: `printf "edited\r\n" > "$1"`,
49+
input: "",
50+
expected: "edited",
51+
},
4052
}
4153

4254
for _, tt := range tests {

cmd/cli/readline/readline.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,11 @@ func (i *Instance) Readline() (string, error) {
212212
case CharCtrlX:
213213
fd := os.Stdin.Fd()
214214
edited, err := openInEditor(fd, i.Terminal.termios, buf.String())
215-
if err == nil {
216-
buf.Replace([]rune(edited))
215+
if err != nil {
216+
fmt.Fprintf(os.Stderr, "error opening editor: %s\n", err)
217+
break
217218
}
219+
buf.Replace([]rune(edited))
218220
case CharCtrlZ:
219221
fd := os.Stdin.Fd()
220222
return handleCharCtrlZ(fd, i.Terminal.termios)

0 commit comments

Comments
 (0)