glow/editor/editor.go
Carlos Alexandro Becker 8b5468468a
fix: improve editor handling (#449)
* fix: improve editor handling

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* test: add tests

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
2023-01-24 14:51:11 -03:00

27 lines
534 B
Go

package editor
import (
"os"
"os/exec"
"strings"
)
const defaultEditor = "nano"
// Cmd returns a *exec.Cmd editing the given path with $EDITOR or nano if no
// $EDITOR is set.
func Cmd(path string) *exec.Cmd {
editor, args := getEditor()
return exec.Command(editor, append(args, path)...)
}
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{}
}