mirror of
https://github.com/cheat/cheat.git
synced 2024-10-31 21:21:02 +01:00
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.
This commit is contained in:
parent
f86633ca1c
commit
3afea0972c
@ -38,12 +38,6 @@ func cmdSearch(opts map[string]interface{}, conf config.Config) {
|
|||||||
// sort the cheatsheets alphabetically, and search for matches
|
// sort the cheatsheets alphabetically, and search for matches
|
||||||
for _, sheet := range sheets.Sort(consolidated) {
|
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 <phrase>
|
// assume that we want to perform a case-insensitive search for <phrase>
|
||||||
pattern := "(?i)" + phrase
|
pattern := "(?i)" + phrase
|
||||||
|
|
||||||
@ -60,7 +54,7 @@ func cmdSearch(opts map[string]interface{}, conf config.Config) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// search the sheet
|
// search the sheet
|
||||||
matches := sheet.Search(reg, colorize)
|
matches := sheet.Search(reg, conf.Color(opts))
|
||||||
|
|
||||||
// display the results
|
// display the results
|
||||||
if len(matches) > 0 {
|
if len(matches) > 0 {
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alecthomas/chroma/quick"
|
"github.com/alecthomas/chroma/quick"
|
||||||
"github.com/mattn/go-isatty"
|
|
||||||
|
|
||||||
"github.com/cheat/cheat/internal/config"
|
"github.com/cheat/cheat/internal/config"
|
||||||
"github.com/cheat/cheat/internal/sheets"
|
"github.com/cheat/cheat/internal/sheets"
|
||||||
@ -44,20 +43,7 @@ func cmdView(opts map[string]interface{}, conf config.Config) {
|
|||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// apply colorization if so configured ...
|
if !conf.Color(opts) {
|
||||||
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 {
|
|
||||||
fmt.Print(sheet.Text)
|
fmt.Print(sheet.Text)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
26
internal/config/color.go
Normal file
26
internal/config/color.go
Normal file
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user