summaryrefslogtreecommitdiffstats
path: root/ui/editor.go
blob: 60cc32033859522ffbc2021993594c0dd006a789 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package ui

import (
	"os"
	"os/exec"
	"strings"

	tea "github.com/charmbracelet/bubbletea"
)

const defaultEditor = "nano"

type editorFinishedMsg struct{ err error }

func openEditor(path string) tea.Cmd {
	editor, args := getEditor()
	cmd := exec.Command(editor, append(args, path)...)
	cb := func(err error) tea.Msg {
		return editorFinishedMsg{err}
	}
	return tea.ExecProcess(cmd, cb)
}

func getEditor() (string, []string) {
	editor := strings.Fields(os.Getenv("EDITOR"))
	if len(editor) > 1 {
		return editor[0], editor[1:]
	}
	if len(editor) == 1 {
		return editor[0], []string{}
	}
	return defaultEditor, []string{}
}