feat(app): help overlay on '?', esc closes zoom
A quiet centered panel listing the keybindings, btop-style. The overlay is modal — any key dismisses it (quit still works) — and esc now closes the zoomed panel, matching what fingers expect.
This commit is contained in:
1 parent
7d8a1b3b17
commit
34c363cd91
3 files changed
+127
No files matched your search
@@ -0,0 +1,57 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
|
||||
"github.com/qf-studio/grot/pkg/tui/render"
|
||||
"github.com/qf-studio/grot/pkg/tui/theme"
|
||||
)
|
||||
|
||||
// helpBindings lists the dashboard keybindings shown by the ? overlay.
|
||||
var helpBindings = [][2]string{
|
||||
{"hjkl / arrows", "move focus"},
|
||||
{"enter", "zoom the focused panel"},
|
||||
{"esc", "close zoom (or this help)"},
|
||||
{"+ / -", "widen / narrow the time range"},
|
||||
{"r", "refresh now"},
|
||||
{"t", "cycle theme"},
|
||||
{"?", "toggle this help"},
|
||||
{"q / ctrl-c", "quit"},
|
||||
}
|
||||
|
||||
// helpView renders the keybinding overlay as a quiet centered panel filling a
|
||||
// w×h viewport. It replaces the grid while shown (a cell grid has no
|
||||
// z-compositing); any key press dismisses it.
|
||||
func helpView(th theme.Theme, w, h int) string {
|
||||
keyW, descW := 0, 0
|
||||
for _, b := range helpBindings {
|
||||
if kw := lipgloss.Width(b[0]); kw > keyW {
|
||||
keyW = kw
|
||||
}
|
||||
if dw := lipgloss.Width(b[1]); dw > descW {
|
||||
descW = dw
|
||||
}
|
||||
}
|
||||
|
||||
lines := make([]string, 0, len(helpBindings)+2)
|
||||
lines = append(lines, "")
|
||||
for _, b := range helpBindings {
|
||||
key := th.LabelStyle().Render(render.PadOrTruncate(b[0], keyW))
|
||||
lines = append(lines, key+" "+th.DimStyle().Render(b[1]))
|
||||
}
|
||||
lines = append(lines, "")
|
||||
|
||||
pw := keyW + 3 + descW + 4 // columns + gap + panel chrome
|
||||
ph := len(lines) + 2
|
||||
if pw > w {
|
||||
pw = w
|
||||
}
|
||||
if ph > h {
|
||||
ph = h
|
||||
}
|
||||
ps := render.PanelStyle{Border: th.BorderStyle(), Title: th.TitleStyle()}
|
||||
panel := render.Panel("keys", strings.Join(lines, "\n"), pw, ph, ps)
|
||||
return lipgloss.Place(w, h, lipgloss.Center, lipgloss.Center, panel)
|
||||
}
|
||||
@@ -48,6 +48,7 @@ type Model struct {
|
||||
rects []Rect
|
||||
focus int
|
||||
zoomed bool
|
||||
helpShown bool
|
||||
scroll int
|
||||
ready bool
|
||||
|
||||
@@ -131,6 +132,9 @@ func (m Model) View() string {
|
||||
return ""
|
||||
}
|
||||
header := headerLine(coalesce(m.dash.Title, "grot"), m.th, formatRange(m.rng), m.zoomed, m.staleNames(), m.width)
|
||||
if m.helpShown {
|
||||
return header + "\n" + helpView(m.th, m.width, m.contentHeight())
|
||||
}
|
||||
if m.zoomed && len(m.widgets) > 0 {
|
||||
r := m.fetchRect(m.focus)
|
||||
return header + "\n" + safeRender(m.widgets[m.focus], r.W, r.H, m.th, true)
|
||||
@@ -140,6 +144,15 @@ func (m Model) View() string {
|
||||
}
|
||||
|
||||
func (m Model) handleKey(k tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
// The help overlay is modal: any key dismisses it, except quit.
|
||||
if m.helpShown {
|
||||
switch k.String() {
|
||||
case "q", "ctrl+c":
|
||||
return m, tea.Quit
|
||||
}
|
||||
m.helpShown = false
|
||||
return m, nil
|
||||
}
|
||||
switch k.String() {
|
||||
case "q", "ctrl+c":
|
||||
return m, tea.Quit
|
||||
@@ -157,6 +170,10 @@ func (m Model) handleKey(k tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
m.ensureVisible()
|
||||
case "enter":
|
||||
m.zoomed = !m.zoomed
|
||||
case "esc":
|
||||
m.zoomed = false
|
||||
case "?":
|
||||
m.helpShown = true
|
||||
case "+", "=":
|
||||
if m.rangeIdx < len(rangePresets)-1 {
|
||||
m.rangeIdx++
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"time"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
|
||||
"github.com/qf-studio/grot/internal/config"
|
||||
"github.com/qf-studio/grot/internal/datasource"
|
||||
@@ -109,6 +110,58 @@ func TestModelZoomToggle(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestModelEscClosesZoom(t *testing.T) {
|
||||
m, _ := newModel(t)
|
||||
m = drive(m, tea.WindowSizeMsg{Width: 100, Height: 40})
|
||||
m = drive(m, tea.KeyMsg{Type: tea.KeyEnter})
|
||||
if !m.zoomed {
|
||||
t.Fatal("Enter should zoom")
|
||||
}
|
||||
m = drive(m, tea.KeyMsg{Type: tea.KeyEscape})
|
||||
if m.zoomed {
|
||||
t.Error("esc should close zoom")
|
||||
}
|
||||
}
|
||||
|
||||
func TestModelHelpOverlay(t *testing.T) {
|
||||
m, _ := newModel(t)
|
||||
m = drive(m, tea.WindowSizeMsg{Width: 100, Height: 40})
|
||||
|
||||
m = drive(m, runeKey('?'))
|
||||
if !m.helpShown {
|
||||
t.Fatal("'?' should show help")
|
||||
}
|
||||
v := m.View()
|
||||
if !strings.Contains(v, "cycle theme") {
|
||||
t.Errorf("help view missing bindings; got:\n%s", v)
|
||||
}
|
||||
// Overlay lines must not exceed the viewport width (wrap corruption).
|
||||
for i, line := range strings.Split(v, "\n") {
|
||||
if got := lipgloss.Width(line); got > 100 {
|
||||
t.Errorf("help line %d wider than viewport: %d", i, got)
|
||||
}
|
||||
}
|
||||
|
||||
// Any key dismisses the overlay without acting on the grid.
|
||||
m = drive(m, runeKey('j'))
|
||||
if m.helpShown {
|
||||
t.Error("any key should dismiss help")
|
||||
}
|
||||
if m.focus != 0 {
|
||||
t.Errorf("dismissing key must not move focus; focus = %d", m.focus)
|
||||
}
|
||||
|
||||
// Quit still works while help is open.
|
||||
m = drive(m, runeKey('?'))
|
||||
_, cmd := m.Update(runeKey('q'))
|
||||
if cmd == nil {
|
||||
t.Fatal("'q' from help should return a command")
|
||||
}
|
||||
if _, ok := cmd().(tea.QuitMsg); !ok {
|
||||
t.Error("'q' from help should quit")
|
||||
}
|
||||
}
|
||||
|
||||
func TestModelRangePresets(t *testing.T) {
|
||||
m, _ := newModel(t)
|
||||
m = drive(m, tea.WindowSizeMsg{Width: 100, Height: 40})
|
||||
|
||||
Reference in new issue
Block a user