mirror of
https://github.com/charmbracelet/glow.git
synced 2026-08-09 09:49:09 +02:00
Introduce key binding to allow users to open their `EDITOR` to edit local markdown files in stash and pager view.
33 lines
627 B
Go
33 lines
627 B
Go
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{}
|
|
}
|