diff --git a/cmd/cheat/cmd_search.go b/cmd/cheat/cmd_search.go index 2fe59d7..ed11954 100644 --- a/cmd/cheat/cmd_search.go +++ b/cmd/cheat/cmd_search.go @@ -75,7 +75,7 @@ func cmdSearch(opts map[string]interface{}, conf config.Config) { } // display the cheatsheet title and path - out += fmt.Sprintf("\n%s %s\n", + out += fmt.Sprintf("%s %s\n", display.Underline(sheet.Title), display.Faint(fmt.Sprintf("(%s)", sheet.CheatPath), conf), ) @@ -85,8 +85,8 @@ func cmdSearch(opts map[string]interface{}, conf config.Config) { } } - // trim the leading newline - out = strings.TrimPrefix(out, "\n") + // trim superfluous newlines + out = strings.TrimSpace(out) // display the output // NB: resist the temptation to call `display.Display` multiple times in diff --git a/cmd/cheat/cmd_view.go b/cmd/cheat/cmd_view.go index 8234f3c..13b3c93 100644 --- a/cmd/cheat/cmd_view.go +++ b/cmd/cheat/cmd_view.go @@ -33,16 +33,17 @@ func cmdView(opts map[string]interface{}, conf config.Config) { // if --all was passed, display cheatsheets from all cheatpaths if opts["--all"].(bool) { // iterate over the cheatpaths + out := "" for _, cheatpath := range cheatsheets { // if the cheatpath contains the specified cheatsheet, display it if sheet, ok := cheatpath[cheatsheet]; ok { // identify the matching cheatsheet - fmt.Println(fmt.Sprintf("%s %s", + out += fmt.Sprintf("%s %s\n", display.Underline(sheet.Title), display.Faint(fmt.Sprintf("(%s)", sheet.CheatPath), conf), - )) + ) // apply colorization if requested if conf.Color(opts) { @@ -50,11 +51,12 @@ func cmdView(opts map[string]interface{}, conf config.Config) { } // display the cheatsheet - display.Write(display.Indent(sheet.Text), conf) + out += display.Indent(sheet.Text) + "\n" } } - // exit early + // display and exit + display.Write(strings.TrimSuffix(out, "\n"), conf) os.Exit(0) } diff --git a/internal/display/indent.go b/internal/display/indent.go index 3b40556..9399879 100644 --- a/internal/display/indent.go +++ b/internal/display/indent.go @@ -7,10 +7,15 @@ import ( // Indent prepends each line of a string with a tab func Indent(str string) string { + + // trim superfluous whitespace + str = strings.TrimSpace(str) + + // prepend each line with a tab character out := "" for _, line := range strings.Split(str, "\n") { out += fmt.Sprintf("\t%s\n", line) } - return strings.TrimSuffix(out, "\n") + return out } diff --git a/internal/display/indent_test.go b/internal/display/indent_test.go index be50b2d..94b1ac9 100644 --- a/internal/display/indent_test.go +++ b/internal/display/indent_test.go @@ -5,7 +5,7 @@ import "testing" // TestIndent asserts that Indent prepends a tab to each line func TestIndent(t *testing.T) { got := Indent("foo\nbar\nbaz") - want := "\tfoo\n\tbar\n\tbaz" + want := "\tfoo\n\tbar\n\tbaz\n" if got != want { t.Errorf("failed to indent: want: %s, got: %s", want, got) }