Skip to content

Commit 3480ae6

Browse files
committed
fallback to vi for editor if none set
1 parent 4f9188e commit 3480ae6

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

internal/tui/submitview/preview.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,16 @@ func (m Model) handleEditorFinished(msg editorFinishedMsg) (tea.Model, tea.Cmd)
9393
}
9494

9595
// resolveEditor returns the configured editor command, checking GH_EDITOR,
96-
// VISUAL, then EDITOR. It returns "" when none are set.
96+
// VISUAL, then EDITOR. If none are set, it falls back to vi when available.
9797
func resolveEditor() string {
9898
for _, key := range []string{"GH_EDITOR", "VISUAL", "EDITOR"} {
9999
if v := strings.TrimSpace(os.Getenv(key)); v != "" {
100100
return v
101101
}
102102
}
103+
if _, err := exec.LookPath("vi"); err == nil {
104+
return "vi"
105+
}
103106
return ""
104107
}
105108

internal/tui/submitview/preview_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package submitview
22

33
import (
44
"os"
5+
"path/filepath"
56
"reflect"
7+
"runtime"
68
"testing"
79

810
tea "github.com/charmbracelet/bubbletea"
@@ -29,6 +31,7 @@ func TestResolveEditor(t *testing.T) {
2931
t.Setenv("GH_EDITOR", "")
3032
t.Setenv("VISUAL", "")
3133
t.Setenv("EDITOR", "")
34+
t.Setenv("PATH", "")
3235
assert.Equal(t, "", resolveEditor())
3336

3437
t.Setenv("EDITOR", "nano")
@@ -37,12 +40,27 @@ func TestResolveEditor(t *testing.T) {
3740
assert.Equal(t, "vim", resolveEditor())
3841
t.Setenv("GH_EDITOR", "code --wait")
3942
assert.Equal(t, "code --wait", resolveEditor())
43+
44+
t.Setenv("GH_EDITOR", "")
45+
t.Setenv("VISUAL", "")
46+
t.Setenv("EDITOR", "")
47+
binDir := t.TempDir()
48+
viName := "vi"
49+
if runtime.GOOS == "windows" {
50+
viName += ".exe"
51+
}
52+
require.NoError(t, os.WriteFile(filepath.Join(binDir, viName), nil, 0o755))
53+
t.Setenv("PATH", binDir)
54+
assert.Equal(t, "vi", resolveEditor())
55+
t.Setenv("EDITOR", "nano")
56+
assert.Equal(t, "nano", resolveEditor())
4057
}
4158

4259
func TestOpenEditor_NoEditorSet(t *testing.T) {
4360
t.Setenv("GH_EDITOR", "")
4461
t.Setenv("VISUAL", "")
4562
t.Setenv("EDITOR", "")
63+
t.Setenv("PATH", "")
4664

4765
m := testModel(t, newNodes())
4866
updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyCtrlE})

0 commit comments

Comments
 (0)