From 3afea0972c5e8cc8d48f50feef5d49b35a9270f9 Mon Sep 17 00:00:00 2001 From: Chris Lane Date: Sat, 23 Nov 2019 13:47:08 -0500 Subject: [PATCH 1/2] fix: colorization errors - Corrects an error with `--search`. Previously, `--search` was not aware of whether it was outputted to a TTY, and would apply colorization at all times. This resulted in unwanted behavior when, for example, piping search results into a paginator. - Corrects an error with `--color`. Previously, `--color` would be ignored if output was being written to a non-TTY. This made it impossible, for example, to `cheat tar --color | less -R`, as colorization would always be stripped. The behavior of `--color` has been modified such that it now behaves similarly to `--color=always` in other applications. --- cmd/cheat/cmd_search.go | 8 +------- cmd/cheat/cmd_view.go | 16 +--------------- internal/config/color.go | 26 ++++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 22 deletions(-) create mode 100644 internal/config/color.go diff --git a/cmd/cheat/cmd_search.go b/cmd/cheat/cmd_search.go index ebc53e1..5c64da9 100644 --- a/cmd/cheat/cmd_search.go +++ b/cmd/cheat/cmd_search.go @@ -38,12 +38,6 @@ func cmdSearch(opts map[string]interface{}, conf config.Config) { // sort the cheatsheets alphabetically, and search for matches for _, sheet := range sheets.Sort(consolidated) { - // colorize output? - colorize := false - if conf.Colorize == true || opts["--colorize"] == true { - colorize = true - } - // assume that we want to perform a case-insensitive search for pattern := "(?i)" + phrase @@ -60,7 +54,7 @@ func cmdSearch(opts map[string]interface{}, conf config.Config) { } // search the sheet - matches := sheet.Search(reg, colorize) + matches := sheet.Search(reg, conf.Color(opts)) // display the results if len(matches) > 0 { diff --git a/cmd/cheat/cmd_view.go b/cmd/cheat/cmd_view.go index 9d9997f..09a41dc 100644 --- a/cmd/cheat/cmd_view.go +++ b/cmd/cheat/cmd_view.go @@ -6,7 +6,6 @@ import ( "strings" "github.com/alecthomas/chroma/quick" - "github.com/mattn/go-isatty" "github.com/cheat/cheat/internal/config" "github.com/cheat/cheat/internal/sheets" @@ -44,20 +43,7 @@ func cmdView(opts map[string]interface{}, conf config.Config) { os.Exit(0) } - // apply colorization if so configured ... - colorize := conf.Colorize - - // ... or if --colorized were passed ... - if opts["--colorize"] == true { - colorize = true - } - - // ... unless we're outputting to a non-TTY - if !isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd()) { - colorize = false - } - - if !colorize { + if !conf.Color(opts) { fmt.Print(sheet.Text) os.Exit(0) } diff --git a/internal/config/color.go b/internal/config/color.go new file mode 100644 index 0000000..ad16b3b --- /dev/null +++ b/internal/config/color.go @@ -0,0 +1,26 @@ +package config + +import ( + "os" + + "github.com/mattn/go-isatty" +) + +// Color indicates whether colorization should be applied to the output +func (c *Config) Color(opts map[string]interface{}) bool { + + // default to the colorization specified in the configs... + colorize := c.Colorize + + // ... however, only apply colorization if we're writing to a tty... + if !isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd()) { + colorize = false + } + + // ... *unless* the --colorize flag was passed + if opts["--colorize"] == true { + colorize = true + } + + return colorize +} From 973a1f59ea08fd528e82210dbf59046ce6f40ef5 Mon Sep 17 00:00:00 2001 From: Chris Lane Date: Sun, 24 Nov 2019 12:03:26 -0500 Subject: [PATCH 2/2] chore: version bump Bumps version to `3.2.1`. --- cmd/cheat/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/cheat/main.go b/cmd/cheat/main.go index 0e46cfa..887308c 100755 --- a/cmd/cheat/main.go +++ b/cmd/cheat/main.go @@ -13,7 +13,7 @@ import ( "github.com/cheat/cheat/internal/config" ) -const version = "3.2.0" +const version = "3.2.1" func main() {