From e6f12147df220089bfc3ecbe338297f557a5d170 Mon Sep 17 00:00:00 2001 From: Christopher Allen Lane Date: Mon, 4 Jul 2022 16:06:37 -0400 Subject: [PATCH] fix: config fixes for Windows - Update the default config file to use `more` instead of `less` as the default pager, in order to support Windows out-of-the-box. (#655, #665). - Use `terminal` Chroma formatter (rather than `terminal16m`) in order to accommodate less capable terminal emulators like `cmd.exe` by default. Similarly, default to `colorize: false` in configs (changed from `true`) (#665). - Comment out default `style` in order to avoid printing ANSI color codes into terminals without color support (#665) - Attempt to intelligently choose a default editor, rather than rely on a hard-coded `vim` in the configs. This should make it easier to use `cheat` immediately without needing to specify configs. It should also improve `cheat`'s Windows compatibility. (#665) --- cmd/cheat/str_config.go | 11 ++++++----- configs/conf.yml | 11 ++++++----- internal/config/config.go | 27 ++++++++++++++++++++++----- internal/config/config_test.go | 4 ++-- 4 files changed, 36 insertions(+), 17 deletions(-) diff --git a/cmd/cheat/str_config.go b/cmd/cheat/str_config.go index f903eb6..4970017 100644 --- a/cmd/cheat/str_config.go +++ b/cmd/cheat/str_config.go @@ -9,22 +9,23 @@ import ( func configs() string { return strings.TrimSpace(`--- # The editor to use with 'cheat -e '. Defaults to $EDITOR or $VISUAL. -editor: vim +# editor: vim # Should 'cheat' always colorize output? -colorize: true +colorize: false # Which 'chroma' colorscheme should be applied to the output? # Options are available here: # https://github.com/alecthomas/chroma/tree/master/styles -style: monokai +# style: monokai # Which 'chroma' "formatter" should be applied? # One of: "terminal", "terminal256", "terminal16m" -formatter: terminal16m +formatter: terminal # Through which pager should output be piped? (Unset this key for no pager.) -pager: less -FRX +pager: more +# pager: less -FRX # <- recommended where available # The paths at which cheatsheets are available. Tags associated with a cheatpath # are automatically attached to all cheatsheets residing on that path. diff --git a/configs/conf.yml b/configs/conf.yml index 8b9bfca..31565bd 100644 --- a/configs/conf.yml +++ b/configs/conf.yml @@ -1,21 +1,22 @@ --- # The editor to use with 'cheat -e '. Defaults to $EDITOR or $VISUAL. -editor: vim +# editor: vim # Should 'cheat' always colorize output? -colorize: true +colorize: false # Which 'chroma' colorscheme should be applied to the output? # Options are available here: # https://github.com/alecthomas/chroma/tree/master/styles -style: monokai +# style: monokai # Which 'chroma' "formatter" should be applied? # One of: "terminal", "terminal256", "terminal16m" -formatter: terminal16m +formatter: terminal # Through which pager should output be piped? (Unset this key for no pager.) -pager: less -FRX +pager: more +# pager: less -FRX # <- recommended where available # The paths at which cheatsheets are available. Tags associated with a cheatpath # are automatically attached to all cheatsheets residing on that path. diff --git a/internal/config/config.go b/internal/config/config.go index 7ae0901..1a068fd 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -4,7 +4,9 @@ import ( "fmt" "io/ioutil" "os" + "os/exec" "path/filepath" + "runtime" "strings" cp "github.com/cheat/cheat/internal/cheatpath" @@ -98,8 +100,22 @@ func New(opts map[string]interface{}, confPath string, resolve bool) (Config, er conf.Editor = os.Getenv("VISUAL") } else if os.Getenv("EDITOR") != "" { conf.Editor = os.Getenv("EDITOR") + } else if runtime.GOOS == "windows" { + conf.Editor = "notepad" } else { - return Config{}, fmt.Errorf("no editor set") + // try to fall back to `nano` + path, err := exec.LookPath("nano") + if err != nil { + return Config{}, fmt.Errorf("failed to locate nano: %s", err) + } + + // use `nano` if we found it + if path != "" { + conf.Editor = "nano" + // otherwise, give up + } else { + return Config{}, fmt.Errorf("no editor set") + } } } @@ -110,12 +126,13 @@ func New(opts map[string]interface{}, confPath string, resolve bool) (Config, er // if a chroma formatter was not provided, set a default if conf.Formatter == "" { - conf.Formatter = "terminal16m" + conf.Formatter = "terminal" } - // if a pager was not provided, set a default - if strings.TrimSpace(conf.Pager) == "" { - conf.Pager = "" + // attempt to fall back to `PAGER` if a pager is not specified in configs + conf.Pager = strings.TrimSpace(conf.Pager) + if conf.Pager == "" && os.Getenv("PAGER") != "" { + conf.Pager = os.Getenv("PAGER") } return conf, nil diff --git a/internal/config/config_test.go b/internal/config/config_test.go index c6a1dac..084312f 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -85,8 +85,8 @@ func TestEmptyEditor(t *testing.T) { // initialize a config conf, err := New(map[string]interface{}{}, mock.Path("conf/empty.yml"), false) - if err == nil { - t.Errorf("failed to return an error on empty editor") + if err != nil { + t.Errorf("failed to initialize test: %v", err) } // set editor, and assert that it is respected