From f4e6c76e5897330e71dedb38c174a21ace42857a Mon Sep 17 00:00:00 2001 From: Christopher Allen Lane Date: Fri, 5 Aug 2022 06:38:08 -0400 Subject: [PATCH] fix: escape sequences in search output (#687) Fix an issue whereby ANSI escape characters could appear in search output when a pager was not configured. The root cause of the problem was code that was overzealously applying an underlying effect to search terms. This commit simply rips out underlying entirely, both as means of resolving this problem, and also simply for removing needless visual noise from search output. --- cmd/cheat/cmd_search.go | 24 +++++++++++++----------- cmd/cheat/cmd_view.go | 2 +- internal/display/underline.go | 8 -------- internal/display/underline_test.go | 14 -------------- internal/display/write.go | 7 +++---- 5 files changed, 17 insertions(+), 38 deletions(-) delete mode 100644 internal/display/underline.go delete mode 100644 internal/display/underline_test.go diff --git a/cmd/cheat/cmd_search.go b/cmd/cheat/cmd_search.go index f9da0c2..88a35e4 100644 --- a/cmd/cheat/cmd_search.go +++ b/cmd/cheat/cmd_search.go @@ -59,9 +59,9 @@ func cmdSearch(opts map[string]interface{}, conf config.Config) { os.Exit(1) } - // `Search` will return text entries that match the search terms. We're - // using it here to overwrite the prior cheatsheet Text, filtering it to - // only what is relevant + // `Search` will return text entries that match the search terms. + // We're using it here to overwrite the prior cheatsheet Text, + // filtering it to only what is relevant. sheet.Text = sheet.Search(reg) // if the sheet did not match the search, ignore it and move on @@ -74,14 +74,16 @@ func cmdSearch(opts map[string]interface{}, conf config.Config) { sheet.Colorize(conf) } - // display the cheatsheet title and path - out += fmt.Sprintf("%s %s\n", - display.Underline(sheet.Title), + // display the cheatsheet body + out += fmt.Sprintf( + "%s %s\n%s\n", + // append the cheatsheet title + sheet.Title, + // append the cheatsheet path display.Faint(fmt.Sprintf("(%s)", sheet.CheatPath), conf), + // indent each line of content + display.Indent(sheet.Text), ) - - // indent each line of content - out += display.Indent(sheet.Text) + "\n" } } @@ -89,7 +91,7 @@ func cmdSearch(opts map[string]interface{}, conf config.Config) { out = strings.TrimSpace(out) // display the output - // NB: resist the temptation to call `display.Display` multiple times in - // the loop above. That will not play nicely with the paginator. + // NB: resist the temptation to call `display.Write` multiple times in the + // loop above. That will not play nicely with the paginator. display.Write(out, conf) } diff --git a/cmd/cheat/cmd_view.go b/cmd/cheat/cmd_view.go index b745674..41a47a6 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 out += fmt.Sprintf("%s %s\n", - display.Underline(sheet.Title), + sheet.Title, display.Faint(fmt.Sprintf("(%s)", sheet.CheatPath), conf), ) diff --git a/internal/display/underline.go b/internal/display/underline.go deleted file mode 100644 index e9e5907..0000000 --- a/internal/display/underline.go +++ /dev/null @@ -1,8 +0,0 @@ -package display - -import "fmt" - -// Underline returns an underlined string -func Underline(str string) string { - return fmt.Sprintf("\033[4m%s\033[0m", str) -} diff --git a/internal/display/underline_test.go b/internal/display/underline_test.go deleted file mode 100644 index e9743ee..0000000 --- a/internal/display/underline_test.go +++ /dev/null @@ -1,14 +0,0 @@ -package display - -import ( - "testing" -) - -// TestUnderline asserts that Underline applies underline formatting -func TestUnderline(t *testing.T) { - want := "\033[4mfoo\033[0m" - got := Underline("foo") - if want != got { - t.Errorf("failed to underline: want: %s, got: %s", want, got) - } -} diff --git a/internal/display/write.go b/internal/display/write.go index 3736262..d4dcfc5 100644 --- a/internal/display/write.go +++ b/internal/display/write.go @@ -23,14 +23,13 @@ func Write(out string, conf config.Config) { pager := parts[0] args := parts[1:] - // run the pager + // configure the pager cmd := exec.Command(pager, args...) cmd.Stdin = strings.NewReader(out) cmd.Stdout = os.Stdout - // handle errors - err := cmd.Run() - if err != nil { + // run the pager and handle errors + if err := cmd.Run(); err != nil { fmt.Fprintf(os.Stderr, "failed to write to pager: %v\n", err) os.Exit(1) }