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.
This commit is contained in:
Christopher Allen Lane 2022-08-05 06:38:08 -04:00
parent 85f5ae8ec7
commit f4e6c76e58
5 changed files with 17 additions and 38 deletions

View File

@ -59,9 +59,9 @@ func cmdSearch(opts map[string]interface{}, conf config.Config) {
os.Exit(1) os.Exit(1)
} }
// `Search` will return text entries that match the search terms. We're // `Search` will return text entries that match the search terms.
// using it here to overwrite the prior cheatsheet Text, filtering it to // We're using it here to overwrite the prior cheatsheet Text,
// only what is relevant // filtering it to only what is relevant.
sheet.Text = sheet.Search(reg) sheet.Text = sheet.Search(reg)
// if the sheet did not match the search, ignore it and move on // 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) sheet.Colorize(conf)
} }
// display the cheatsheet title and path // display the cheatsheet body
out += fmt.Sprintf("%s %s\n", out += fmt.Sprintf(
display.Underline(sheet.Title), "%s %s\n%s\n",
// append the cheatsheet title
sheet.Title,
// append the cheatsheet path
display.Faint(fmt.Sprintf("(%s)", sheet.CheatPath), conf), 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) out = strings.TrimSpace(out)
// display the output // display the output
// NB: resist the temptation to call `display.Display` multiple times in // NB: resist the temptation to call `display.Write` multiple times in the
// the loop above. That will not play nicely with the paginator. // loop above. That will not play nicely with the paginator.
display.Write(out, conf) display.Write(out, conf)
} }

View File

@ -41,7 +41,7 @@ func cmdView(opts map[string]interface{}, conf config.Config) {
// identify the matching cheatsheet // identify the matching cheatsheet
out += fmt.Sprintf("%s %s\n", out += fmt.Sprintf("%s %s\n",
display.Underline(sheet.Title), sheet.Title,
display.Faint(fmt.Sprintf("(%s)", sheet.CheatPath), conf), display.Faint(fmt.Sprintf("(%s)", sheet.CheatPath), conf),
) )

View File

@ -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)
}

View File

@ -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)
}
}

View File

@ -23,14 +23,13 @@ func Write(out string, conf config.Config) {
pager := parts[0] pager := parts[0]
args := parts[1:] args := parts[1:]
// run the pager // configure the pager
cmd := exec.Command(pager, args...) cmd := exec.Command(pager, args...)
cmd.Stdin = strings.NewReader(out) cmd.Stdin = strings.NewReader(out)
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
// handle errors // run the pager and handle errors
err := cmd.Run() if err := cmd.Run(); err != nil {
if err != nil {
fmt.Fprintf(os.Stderr, "failed to write to pager: %v\n", err) fmt.Fprintf(os.Stderr, "failed to write to pager: %v\n", err)
os.Exit(1) os.Exit(1)
} }