|
| 1 | +package tui |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + |
| 6 | + tea "github.com/charmbracelet/bubbletea" |
| 7 | + "github.com/charmbracelet/lipgloss" |
| 8 | + "github.com/hmans/beans/internal/ui" |
| 9 | +) |
| 10 | + |
| 11 | +// openHelpMsg requests opening the help overlay |
| 12 | +type openHelpMsg struct{} |
| 13 | + |
| 14 | +// closeHelpMsg is sent when the help overlay is closed |
| 15 | +type closeHelpMsg struct{} |
| 16 | + |
| 17 | +// helpOverlayModel displays keyboard shortcuts organized by context |
| 18 | +type helpOverlayModel struct { |
| 19 | + width int |
| 20 | + height int |
| 21 | +} |
| 22 | + |
| 23 | +func newHelpOverlayModel(width, height int) helpOverlayModel { |
| 24 | + return helpOverlayModel{ |
| 25 | + width: width, |
| 26 | + height: height, |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func (m helpOverlayModel) Init() tea.Cmd { |
| 31 | + return nil |
| 32 | +} |
| 33 | + |
| 34 | +func (m helpOverlayModel) Update(msg tea.Msg) (helpOverlayModel, tea.Cmd) { |
| 35 | + switch msg := msg.(type) { |
| 36 | + case tea.WindowSizeMsg: |
| 37 | + m.width = msg.Width |
| 38 | + m.height = msg.Height |
| 39 | + |
| 40 | + case tea.KeyMsg: |
| 41 | + switch msg.String() { |
| 42 | + case "?", "esc": |
| 43 | + return m, func() tea.Msg { |
| 44 | + return closeHelpMsg{} |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return m, nil |
| 50 | +} |
| 51 | + |
| 52 | +func (m helpOverlayModel) View() string { |
| 53 | + if m.width == 0 { |
| 54 | + return "Loading..." |
| 55 | + } |
| 56 | + |
| 57 | + // Calculate modal dimensions - make it wider than pickers |
| 58 | + modalWidth := max(70, min(90, m.width*70/100)) |
| 59 | + |
| 60 | + // Title |
| 61 | + title := lipgloss.NewStyle(). |
| 62 | + Bold(true). |
| 63 | + Foreground(ui.ColorPrimary). |
| 64 | + Render("Keyboard Shortcuts") |
| 65 | + |
| 66 | + // Helper to create a shortcut line |
| 67 | + shortcut := func(key, desc string) string { |
| 68 | + keyStyle := lipgloss.NewStyle(). |
| 69 | + Foreground(ui.ColorPrimary). |
| 70 | + Bold(true). |
| 71 | + Width(20). |
| 72 | + Render(key) |
| 73 | + descStyle := lipgloss.NewStyle(). |
| 74 | + Foreground(lipgloss.Color("#fff")). |
| 75 | + Render(desc) |
| 76 | + return keyStyle + descStyle |
| 77 | + } |
| 78 | + |
| 79 | + // Helper to create a section header |
| 80 | + sectionHeader := func(name string) string { |
| 81 | + return lipgloss.NewStyle(). |
| 82 | + Bold(true). |
| 83 | + Foreground(ui.ColorBlue). |
| 84 | + Render(name) |
| 85 | + } |
| 86 | + |
| 87 | + var content strings.Builder |
| 88 | + content.WriteString(title + "\n\n") |
| 89 | + |
| 90 | + // List view section |
| 91 | + content.WriteString(sectionHeader("List View") + "\n") |
| 92 | + content.WriteString(shortcut("j/k, ↓/↑", "Navigate up/down") + "\n") |
| 93 | + content.WriteString(shortcut("enter", "View bean details") + "\n") |
| 94 | + content.WriteString(shortcut("c", "Create new bean") + "\n") |
| 95 | + content.WriteString(shortcut("e", "Edit bean in $EDITOR") + "\n") |
| 96 | + content.WriteString(shortcut("s", "Change status") + "\n") |
| 97 | + content.WriteString(shortcut("t", "Change type") + "\n") |
| 98 | + content.WriteString(shortcut("P", "Change priority") + "\n") |
| 99 | + content.WriteString(shortcut("p", "Set parent") + "\n") |
| 100 | + content.WriteString(shortcut("b", "Manage blocking relationships") + "\n") |
| 101 | + content.WriteString(shortcut("/", "Filter list") + "\n") |
| 102 | + content.WriteString(shortcut("g t", "Go to tags (filter by tag)") + "\n") |
| 103 | + content.WriteString(shortcut("esc", "Clear filter") + "\n") |
| 104 | + content.WriteString(shortcut("q", "Quit") + "\n") |
| 105 | + content.WriteString("\n") |
| 106 | + |
| 107 | + // Detail view section |
| 108 | + content.WriteString(sectionHeader("Detail View") + "\n") |
| 109 | + content.WriteString(shortcut("j/k, ↓/↑", "Scroll up/down") + "\n") |
| 110 | + content.WriteString(shortcut("tab", "Switch focus (links/body)") + "\n") |
| 111 | + content.WriteString(shortcut("enter", "Navigate to linked bean") + "\n") |
| 112 | + content.WriteString(shortcut("e", "Edit bean in $EDITOR") + "\n") |
| 113 | + content.WriteString(shortcut("s", "Change status") + "\n") |
| 114 | + content.WriteString(shortcut("t", "Change type") + "\n") |
| 115 | + content.WriteString(shortcut("P", "Change priority") + "\n") |
| 116 | + content.WriteString(shortcut("p", "Set parent") + "\n") |
| 117 | + content.WriteString(shortcut("b", "Manage blocking relationships") + "\n") |
| 118 | + content.WriteString(shortcut("esc/backspace", "Back to list/previous bean") + "\n") |
| 119 | + content.WriteString(shortcut("q", "Quit") + "\n") |
| 120 | + content.WriteString("\n") |
| 121 | + |
| 122 | + // Picker/Dialog section |
| 123 | + content.WriteString(sectionHeader("Pickers & Dialogs") + "\n") |
| 124 | + content.WriteString(shortcut("j/k, ↓/↑", "Navigate up/down") + "\n") |
| 125 | + content.WriteString(shortcut("enter", "Select/confirm") + "\n") |
| 126 | + content.WriteString(shortcut("/", "Filter items") + "\n") |
| 127 | + content.WriteString(shortcut("esc", "Cancel") + "\n") |
| 128 | + content.WriteString("\n") |
| 129 | + |
| 130 | + // Footer |
| 131 | + footer := helpKeyStyle.Render("?/esc") + " " + helpStyle.Render("close") |
| 132 | + |
| 133 | + // Border style |
| 134 | + border := lipgloss.NewStyle(). |
| 135 | + Border(lipgloss.RoundedBorder()). |
| 136 | + BorderForeground(ui.ColorPrimary). |
| 137 | + Padding(1, 2). |
| 138 | + Width(modalWidth) |
| 139 | + |
| 140 | + return border.Render(content.String() + footer) |
| 141 | +} |
| 142 | + |
| 143 | +// ModalView returns the help overlay as a centered modal on top of the background |
| 144 | +func (m helpOverlayModel) ModalView(bgView string, fullWidth, fullHeight int) string { |
| 145 | + modal := m.View() |
| 146 | + return overlayModal(bgView, modal, fullWidth, fullHeight) |
| 147 | +} |
0 commit comments