From 1a7b5c612770ac2a8770e6ad52a2a9d9030fac5e Mon Sep 17 00:00:00 2001 From: Chris Lane Date: Fri, 27 Nov 2020 22:50:55 -0500 Subject: [PATCH] feat(display): make `Faint` respect `Colorize` Make `display.Faint` respect the `Colorize` config value. --- cmd/cheat/cmd_search.go | 2 +- cmd/cheat/cmd_view.go | 2 +- internal/display/faint.go | 16 +++++++++++++--- internal/display/faint_test.go | 15 ++++++++++++++- 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/cmd/cheat/cmd_search.go b/cmd/cheat/cmd_search.go index 2219d4e..2fe59d7 100644 --- a/cmd/cheat/cmd_search.go +++ b/cmd/cheat/cmd_search.go @@ -77,7 +77,7 @@ func cmdSearch(opts map[string]interface{}, conf config.Config) { // display the cheatsheet title and path out += fmt.Sprintf("\n%s %s\n", display.Underline(sheet.Title), - display.Faint(fmt.Sprintf("(%s)", sheet.CheatPath)), + display.Faint(fmt.Sprintf("(%s)", sheet.CheatPath), conf), ) // indent each line of content diff --git a/cmd/cheat/cmd_view.go b/cmd/cheat/cmd_view.go index 643950a..8234f3c 100644 --- a/cmd/cheat/cmd_view.go +++ b/cmd/cheat/cmd_view.go @@ -41,7 +41,7 @@ func cmdView(opts map[string]interface{}, conf config.Config) { // identify the matching cheatsheet fmt.Println(fmt.Sprintf("%s %s", display.Underline(sheet.Title), - display.Faint(fmt.Sprintf("(%s)", sheet.CheatPath)), + display.Faint(fmt.Sprintf("(%s)", sheet.CheatPath), conf), )) // apply colorization if requested diff --git a/internal/display/faint.go b/internal/display/faint.go index af0c040..aafdd27 100644 --- a/internal/display/faint.go +++ b/internal/display/faint.go @@ -1,8 +1,18 @@ package display -import "fmt" +import ( + "fmt" + + "github.com/cheat/cheat/internal/config" +) // Faint returns an faint string -func Faint(str string) string { - return fmt.Sprintf(fmt.Sprintf("\033[2m%s\033[0m", str)) +func Faint(str string, conf config.Config) string { + // make `str` faint only if colorization has been requested + if conf.Colorize { + return fmt.Sprintf(fmt.Sprintf("\033[2m%s\033[0m", str)) + } + + // otherwise, return the string unmodified + return str } diff --git a/internal/display/faint_test.go b/internal/display/faint_test.go index 6591bb4..f590236 100644 --- a/internal/display/faint_test.go +++ b/internal/display/faint_test.go @@ -2,12 +2,25 @@ package display import ( "testing" + + "github.com/cheat/cheat/internal/config" ) // TestFaint asserts that Faint applies faint formatting func TestFaint(t *testing.T) { + + // case: apply colorization + conf := config.Config{Colorize: true} want := "\033[2mfoo\033[0m" - got := Faint("foo") + got := Faint("foo", conf) + if want != got { + t.Errorf("failed to faint: want: %s, got: %s", want, got) + } + + // case: do not apply colorization + conf.Colorize = false + want = "foo" + got = Faint("foo", conf) if want != got { t.Errorf("failed to faint: want: %s, got: %s", want, got) }