Archived
Make the screen explain itself: labels, tools, help pages, hints
For developers new to submodules (notes/usability-plan.md, D32): - x and X read "restore to pin" and "restore committed pins" - problems only, filter and fetch are shown in the footer - ? shows every key in titled columns, then a legend of the status words, symbols and spine roles; esc closes it - u reads "pull (fast-forward)" on the superproject - the detail pane suggests f when the last fetch is old - a hint above the footer until the first key - the header says "staged", like the tree Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+132
-11
@@ -2,6 +2,7 @@
|
||||
package footer
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -18,18 +19,39 @@ const flashDuration = 2 * time.Second
|
||||
|
||||
type clearFlashMsg struct{ id int }
|
||||
|
||||
// Keys provides the key groups. The short bar shows Navigation (and
|
||||
// Keys provides the key groups. The short bar shows Navigation, Tools (and
|
||||
// Submodule, if requested) on the left, Global and then Highlight (on a
|
||||
// highlighted background) on the right; full help
|
||||
// shows FullHelp.
|
||||
// highlighted background) on the right. The full help has two pages: the
|
||||
// HelpGroups (the last one is always kept), then the theme's legend with
|
||||
// LegendKeys next to it.
|
||||
type Keys interface {
|
||||
help.KeyMap
|
||||
Navigation() []key.Binding
|
||||
Tools() []key.Binding
|
||||
Submodule() []key.Binding
|
||||
Global() []key.Binding
|
||||
Highlight() []key.Binding
|
||||
HelpGroups() []Group
|
||||
LegendKeys() Group
|
||||
}
|
||||
|
||||
// Group is a titled column of the full help.
|
||||
type Group struct {
|
||||
Title string
|
||||
Keys []key.Binding
|
||||
}
|
||||
|
||||
// helpPage is what the footer shows besides a flash.
|
||||
type helpPage int
|
||||
|
||||
const (
|
||||
pageBar helpPage = iota // one line of key hints
|
||||
pageKeys // every key, in titled columns
|
||||
pageLegend // what the words and symbols mean
|
||||
)
|
||||
|
||||
// columnGap separates the columns of the full help.
|
||||
const columnGap = " "
|
||||
|
||||
type Model struct {
|
||||
theme theme.Theme
|
||||
help help.Model
|
||||
@@ -37,6 +59,7 @@ type Model struct {
|
||||
width int
|
||||
flash string
|
||||
flashID int
|
||||
page helpPage
|
||||
actions bool // show the submodule bindings in the short bar
|
||||
}
|
||||
|
||||
@@ -56,8 +79,18 @@ func (m *Model) SetWidth(w int) {
|
||||
m.help.Width = w - 2
|
||||
}
|
||||
|
||||
// ToggleHelp switches between short and full key help.
|
||||
func (m *Model) ToggleHelp() { m.help.ShowAll = !m.help.ShowAll }
|
||||
// CycleHelp shows the next help page: the short bar, every key, the legend,
|
||||
// then the short bar again.
|
||||
func (m *Model) CycleHelp() { m.page = (m.page + 1) % 3 }
|
||||
|
||||
// HelpOpen reports whether a full help page is shown.
|
||||
func (m Model) HelpOpen() bool { return m.page != pageBar }
|
||||
|
||||
// CloseHelp goes back to the short bar from any help page.
|
||||
func (m *Model) CloseHelp() { m.page = pageBar }
|
||||
|
||||
// Legend reports whether the legend is shown.
|
||||
func (m Model) Legend() bool { return m.page == pageLegend }
|
||||
|
||||
// Flash shows msg for a short time instead of the key hints.
|
||||
func (m *Model) Flash(msg string) tea.Cmd {
|
||||
@@ -78,14 +111,102 @@ func (m Model) View() string {
|
||||
if m.flash != "" {
|
||||
return style.Foreground(m.theme.Warn).Render(m.flash)
|
||||
}
|
||||
if m.help.ShowAll {
|
||||
return style.Render(m.help.View(m.keys))
|
||||
switch m.page {
|
||||
case pageKeys:
|
||||
var cols []column
|
||||
for _, g := range m.keys.HelpGroups() {
|
||||
cols = append(cols, m.keyColumn(g))
|
||||
}
|
||||
return style.Render(m.columns(cols[:len(cols)-1], cols[len(cols)-1], m.width-2))
|
||||
case pageLegend:
|
||||
var cols []column
|
||||
for _, g := range m.theme.Legend() {
|
||||
c := column{title: g.Title}
|
||||
for _, r := range g.Rows {
|
||||
c.rows = append(c.rows, [2]string{r.Word, m.help.Styles.FullDesc.Render(r.Meaning)})
|
||||
}
|
||||
cols = append(cols, c)
|
||||
}
|
||||
return style.Render(m.columns(cols, m.keyColumn(m.keys.LegendKeys()), m.width-2))
|
||||
}
|
||||
return style.Render(m.shortBar(m.width - 2))
|
||||
}
|
||||
|
||||
// column is a titled column of the full help: rows of a rendered key (or
|
||||
// word) and its description.
|
||||
type column struct {
|
||||
title string
|
||||
rows [][2]string
|
||||
}
|
||||
|
||||
func (m Model) keyColumn(g Group) column {
|
||||
c := column{title: g.Title}
|
||||
for _, b := range g.Keys {
|
||||
if b.Enabled() {
|
||||
c.rows = append(c.rows, [2]string{m.help.Styles.FullKey.Render(b.Help().Key), m.help.Styles.FullDesc.Render(b.Help().Desc)})
|
||||
}
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
// columns lays cols out side by side, each under its title, and last at the
|
||||
// end. Like the global keys in the short bar, last is always kept: the
|
||||
// columns before it that don't fit into w are left out from the right,
|
||||
// marked with an ellipsis.
|
||||
func (m Model) columns(cols []column, last column, w int) string {
|
||||
tail := m.block(last)
|
||||
avail := w - lipgloss.Width(tail) - len(columnGap)
|
||||
var out []string
|
||||
width := 0
|
||||
for i, c := range cols {
|
||||
b := m.block(c)
|
||||
bw := lipgloss.Width(b) + len(columnGap)
|
||||
if i == 0 {
|
||||
bw -= len(columnGap)
|
||||
}
|
||||
if width+bw > avail {
|
||||
for len(out) > 0 && width+2 > avail { // room for the ellipsis
|
||||
width -= lipgloss.Width(out[len(out)-1]) + len(columnGap)
|
||||
out = out[:len(out)-1]
|
||||
}
|
||||
if width+2 <= avail {
|
||||
out = append(out, " "+m.help.Styles.Ellipsis.Render("…"))
|
||||
}
|
||||
break
|
||||
}
|
||||
out = append(out, b)
|
||||
width += bw
|
||||
}
|
||||
var parts []string
|
||||
for i, b := range out {
|
||||
if i > 0 && !strings.HasPrefix(b, " ") {
|
||||
parts = append(parts, columnGap)
|
||||
}
|
||||
parts = append(parts, b)
|
||||
}
|
||||
if len(parts) > 0 {
|
||||
parts = append(parts, columnGap)
|
||||
}
|
||||
parts = append(parts, tail)
|
||||
return lipgloss.NewStyle().MaxWidth(w).Render(lipgloss.JoinHorizontal(lipgloss.Top, parts...))
|
||||
}
|
||||
|
||||
// block renders a column: its title, then its rows with the descriptions
|
||||
// aligned.
|
||||
func (m Model) block(c column) string {
|
||||
left := 0
|
||||
for _, r := range c.rows {
|
||||
left = max(left, lipgloss.Width(r[0]))
|
||||
}
|
||||
lines := []string{theme.Fg(m.theme.Muted).Bold(true).Render(c.title)}
|
||||
for _, r := range c.rows {
|
||||
lines = append(lines, r[0]+strings.Repeat(" ", left-lipgloss.Width(r[0])+1)+r[1])
|
||||
}
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
// shortBar puts the global keys on the right and as many of the others on the
|
||||
// left as fit; the submodule keys are dropped first.
|
||||
// left as fit; the submodule keys are dropped first, then the tools.
|
||||
func (m Model) shortBar(w int) string {
|
||||
h := m.help
|
||||
h.Width = 0
|
||||
@@ -97,9 +218,9 @@ func (m Model) shortBar(w int) string {
|
||||
}
|
||||
right += " " + m.theme.Highlight().Render(" "+strings.Join(parts, " ")+" ")
|
||||
}
|
||||
left := m.keys.Navigation()
|
||||
left := append(slices.Clip(m.keys.Navigation()), m.keys.Tools()...)
|
||||
if m.actions {
|
||||
left = append(left[:len(left):len(left)], m.keys.Submodule()...)
|
||||
left = append(left, m.keys.Submodule()...)
|
||||
}
|
||||
for ; ; left = left[:len(left)-1] {
|
||||
l := h.ShortHelpView(left)
|
||||
|
||||
@@ -15,14 +15,25 @@ type testKeys struct{ highlight []key.Binding }
|
||||
|
||||
func bind(k, desc string) key.Binding { return key.NewBinding(key.WithKeys(k), key.WithHelp(k, desc)) }
|
||||
|
||||
func (testKeys) ShortHelp() []key.Binding { return nil }
|
||||
func (testKeys) FullHelp() [][]key.Binding { return nil }
|
||||
func (testKeys) Navigation() []key.Binding {
|
||||
return []key.Binding{bind("j", "move"), bind("h", "fold")}
|
||||
}
|
||||
func (testKeys) Submodule() []key.Binding { return nil }
|
||||
func (testKeys) Tools() []key.Binding {
|
||||
return []key.Binding{bind("!", "problems only"), bind("/", "filter"), bind("f", "fetch")}
|
||||
}
|
||||
func (testKeys) Submodule() []key.Binding { return []key.Binding{bind("x", "restore to pin")} }
|
||||
func (testKeys) Global() []key.Binding { return []key.Binding{bind("?", "help"), bind("Q", "quit")} }
|
||||
func (k testKeys) Highlight() []key.Binding { return k.highlight }
|
||||
func (testKeys) HelpGroups() []Group {
|
||||
return []Group{
|
||||
{Title: "Move", Keys: []key.Binding{bind("j", "down"), bind("k", "up")}},
|
||||
{Title: "Selected submodule", Keys: []key.Binding{bind("x", "restore to pin")}},
|
||||
{Title: "App", Keys: []key.Binding{bind("?", "what words mean"), bind("Q", "quit")}},
|
||||
}
|
||||
}
|
||||
func (testKeys) LegendKeys() Group {
|
||||
return Group{Title: "App", Keys: []key.Binding{bind("esc", "close")}}
|
||||
}
|
||||
|
||||
func bar(w int, hl ...key.Binding) string {
|
||||
m := New(theme.Default(), testKeys{hl})
|
||||
@@ -52,3 +63,77 @@ func TestNavigationGivesWayToUpdate(t *testing.T) {
|
||||
t.Errorf("the right group must be kept:\n%q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToolsFollowNavigation(t *testing.T) {
|
||||
got := bar(80)
|
||||
if !strings.Contains(got, "h fold • ! problems only • / filter • f fetch") {
|
||||
t.Errorf("tools must follow the navigation keys:\n%q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToolsGiveWayBeforeNavigation(t *testing.T) {
|
||||
got := bar(40)
|
||||
if strings.Contains(got, "f fetch") || !strings.Contains(got, "j move") {
|
||||
t.Errorf("tools must go before navigation:\n%q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubmoduleKeysGoFirst(t *testing.T) {
|
||||
m := New(theme.Default(), testKeys{})
|
||||
m.SetActions(true)
|
||||
m.SetWidth(80)
|
||||
got := ansi.Strip(m.View())
|
||||
if strings.Contains(got, "restore to pin") || !strings.Contains(got, "f fetch") {
|
||||
t.Errorf("submodule keys must go before the tools:\n%q", got)
|
||||
}
|
||||
m.SetWidth(120)
|
||||
if got := ansi.Strip(m.View()); !strings.Contains(got, "f fetch • x restore to pin") {
|
||||
t.Errorf("submodule keys follow the tools:\n%q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHelpPages(t *testing.T) {
|
||||
m := New(theme.Default(), testKeys{})
|
||||
m.SetWidth(120)
|
||||
m.CycleHelp()
|
||||
keys := ansi.Strip(m.View())
|
||||
for _, want := range []string{"Move", "Selected submodule", "App", "x restore to pin", "? what words mean"} {
|
||||
if !strings.Contains(keys, want) {
|
||||
t.Errorf("key page lacks %q:\n%s", want, keys)
|
||||
}
|
||||
}
|
||||
m.CycleHelp()
|
||||
if !m.Legend() {
|
||||
t.Fatal("second page must be the legend")
|
||||
}
|
||||
legend := ansi.Strip(m.View())
|
||||
for _, want := range []string{"Status", "off pin", "Symbols", "Commits", "esc close"} {
|
||||
if !strings.Contains(legend, want) {
|
||||
t.Errorf("legend lacks %q:\n%s", want, legend)
|
||||
}
|
||||
}
|
||||
for _, l := range strings.Split(m.View(), "\n") {
|
||||
if w := lipgloss.Width(l); w > 120 {
|
||||
t.Errorf("legend line is %d wide: %q", w, ansi.Strip(l))
|
||||
}
|
||||
}
|
||||
m.CycleHelp()
|
||||
if got := ansi.Strip(m.View()); !strings.Contains(got, "? help") {
|
||||
t.Errorf("third ? must close the help:\n%q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHelpLeavesOutColumnsThatDontFit(t *testing.T) {
|
||||
m := New(theme.Default(), testKeys{})
|
||||
m.SetWidth(30)
|
||||
m.CycleHelp()
|
||||
got := ansi.Strip(m.View())
|
||||
if !strings.Contains(got, "App") || strings.Contains(got, "Selected") || !strings.Contains(got, "…") {
|
||||
t.Errorf("want App kept, the columns before it left out with an ellipsis:\n%s", got)
|
||||
}
|
||||
for _, l := range strings.Split(m.View(), "\n") {
|
||||
if w := lipgloss.Width(l); w > 30 {
|
||||
t.Errorf("line is %d wide: %q", w, ansi.Strip(l))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user