fix(view): whitespace corrections

- Fix bug whereby `--all` flag would conflict with pager

- Fix whitespace inconsistencies among view and search outputs
This commit is contained in:
Chris Lane 2020-11-28 11:18:16 -05:00
parent ecc96c64f9
commit 4f2a57fce8
4 changed files with 16 additions and 9 deletions

View File

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

View File

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

View File

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

View File

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