Help screen: wrap descriptions under themselves, size to the terminal
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
||||
func TestRenderHelpFitsAndAligns(t *testing.T) {
|
||||
for _, width := range []int{40, 60, 76, 96} {
|
||||
text := renderHelp(width)
|
||||
var inItem bool
|
||||
for i, line := range strings.Split(text, "\n") {
|
||||
if w := tview.TaggedStringWidth(line); w > width {
|
||||
t.Errorf("width %d, line %d is %d columns wide: %q", width, i, w, line)
|
||||
}
|
||||
plain := plainText(line)
|
||||
// Descriptions wrap under themselves: a continuation line starts with
|
||||
// spaces up to the description column, never at the left edge.
|
||||
switch {
|
||||
case plain == "":
|
||||
inItem = false
|
||||
case !strings.HasPrefix(plain, " "):
|
||||
inItem = false // a section title
|
||||
case strings.HasPrefix(plain, strings.Repeat(" ", 2+helpKeyWidth)) && strings.TrimSpace(plain) != "":
|
||||
if !inItem {
|
||||
t.Errorf("width %d: continuation without an item before it: %q", width, plain)
|
||||
}
|
||||
default:
|
||||
inItem = true
|
||||
}
|
||||
}
|
||||
// Nothing is lost.
|
||||
for _, sec := range helpSections {
|
||||
if !strings.Contains(text, sec.title) {
|
||||
t.Errorf("width %d: section %q missing", width, sec.title)
|
||||
}
|
||||
for _, it := range sec.items {
|
||||
first := strings.Fields(it.desc)[0]
|
||||
if !strings.Contains(plainText(text), first) {
|
||||
t.Errorf("width %d: description of %q lost", width, it.keys)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderHelpKeepsEveryWord(t *testing.T) {
|
||||
desc := "Changes apply live behind the dialog, also while you move through an open list"
|
||||
words := strings.Fields(desc)
|
||||
text := plainText(renderHelp(50))
|
||||
// Join the wrapped lines of the long Library "F" description and look for every word in order.
|
||||
pos := 0
|
||||
for _, w := range words {
|
||||
i := strings.Index(text[pos:], w)
|
||||
if i < 0 {
|
||||
t.Fatalf("word %q missing or out of order", w)
|
||||
}
|
||||
pos += i + len(w)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user