cheat/internal/config/editor.go
Christopher Allen Lane d598d96fce fix(Config): colorization without pager (#687)
Fix an issue whereby colorization would output ANSI codes if a pager was
not configured.

The solution here is to stop guessing about the state of the user's
system at runtime, as well as the user's intention. The installer now
chooses an appropriate installer when generating configs, and no longer
bothers searching for pagers at runtime.
2022-08-07 10:19:56 -04:00

31 lines
627 B
Go

package config
import (
"fmt"
"os"
"os/exec"
"runtime"
)
// Editor attempts to locate an editor that's appropriate for the environment.
func Editor() (string, error) {
// default to `notepad.exe` on Windows
if runtime.GOOS == "windows" {
return "notepad", nil
}
// look for `nano` on the `PATH`
nano, _ := exec.LookPath("nano")
// search for `$VISUAL`, `$EDITOR`, and then `nano`, in that order
for _, editor := range []string{os.Getenv("VISUAL"), os.Getenv("EDITOR"), nano} {
if editor != "" {
return editor, nil
}
}
// return an error if no path is found
return "", fmt.Errorf("no editor set")
}