Skip to content

Commit dd408b8

Browse files
committed
Refactor to single-source bindings (DRY principle)
Now there's only ONE place where bindings are defined: - AllBindings() - Defines ALL available key bindings (single source of truth) - Bindings() - Filters AllBindings() to return only status bar keys Benefits: - DRY: No code duplication - Maintainability: Add new binding in one place, automatically appears in help - Curated status bar: Filter decides what shows in status bar - Single source of truth for all keyboard shortcuts The filter uses a whitelist map to determine which bindings should appear in the status bar vs only in the comprehensive help dialog. Assisted-By: docker-agent
1 parent 7e0c54a commit dd408b8

1 file changed

Lines changed: 36 additions & 60 deletions

File tree

pkg/tui/tui.go

Lines changed: 36 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,66 +1553,6 @@ func (m *appModel) Help() help.KeyMap {
15531553
return core.NewSimpleHelp(m.Bindings())
15541554
}
15551555

1556-
// Bindings returns the key bindings shown in the status bar (a curated subset).
1557-
func (m *appModel) Bindings() []key.Binding {
1558-
quitBinding := key.NewBinding(
1559-
key.WithKeys("ctrl+c"),
1560-
key.WithHelp("Ctrl+c", "quit"),
1561-
)
1562-
1563-
if m.leanMode {
1564-
return []key.Binding{quitBinding}
1565-
}
1566-
1567-
tabBinding := key.NewBinding(
1568-
key.WithKeys("tab"),
1569-
key.WithHelp("Tab", "switch focus"),
1570-
)
1571-
1572-
bindings := []key.Binding{quitBinding, tabBinding}
1573-
bindings = append(bindings, m.tabBar.Bindings()...)
1574-
1575-
bindings = append(bindings, key.NewBinding(
1576-
key.WithKeys("ctrl+k"),
1577-
key.WithHelp("Ctrl+k", "commands"),
1578-
))
1579-
1580-
bindings = append(bindings, key.NewBinding(
1581-
key.WithKeys("ctrl+h"),
1582-
key.WithHelp("Ctrl+h", "help"),
1583-
))
1584-
1585-
// Show newline help based on keyboard enhancement support
1586-
if m.keyboardEnhancementsSupported {
1587-
bindings = append(bindings, key.NewBinding(
1588-
key.WithKeys("shift+enter"),
1589-
key.WithHelp("Shift+Enter", "newline"),
1590-
))
1591-
} else {
1592-
bindings = append(bindings, key.NewBinding(
1593-
key.WithKeys("ctrl+j"),
1594-
key.WithHelp("Ctrl+j", "newline"),
1595-
))
1596-
}
1597-
1598-
if m.focusedPanel == PanelContent {
1599-
bindings = append(bindings, m.chatPage.Bindings()...)
1600-
} else {
1601-
editorName := getEditorDisplayNameFromEnv(os.Getenv("VISUAL"), os.Getenv("EDITOR"))
1602-
bindings = append(bindings,
1603-
key.NewBinding(
1604-
key.WithKeys("ctrl+g"),
1605-
key.WithHelp("Ctrl+g", "edit in "+editorName),
1606-
),
1607-
key.NewBinding(
1608-
key.WithKeys("ctrl+r"),
1609-
key.WithHelp("Ctrl+r", "history search"),
1610-
),
1611-
)
1612-
}
1613-
return bindings
1614-
}
1615-
16161556
// AllBindings returns ALL available key bindings for the help dialog (comprehensive list).
16171557
func (m *appModel) AllBindings() []key.Binding {
16181558
quitBinding := key.NewBinding(
@@ -1711,6 +1651,42 @@ func (m *appModel) AllBindings() []key.Binding {
17111651
return bindings
17121652
}
17131653

1654+
// Bindings returns the key bindings shown in the status bar (a curated subset).
1655+
// This filters AllBindings() to show only the most essential commands.
1656+
func (m *appModel) Bindings() []key.Binding {
1657+
all := m.AllBindings()
1658+
1659+
// Define which keys should appear in the status bar
1660+
statusBarKeys := map[string]bool{
1661+
"ctrl+c": true, // quit
1662+
"tab": true, // switch focus
1663+
"ctrl+t": true, // new tab (from tabBar)
1664+
"ctrl+w": true, // close tab (from tabBar)
1665+
"ctrl+p": true, // prev tab (from tabBar)
1666+
"ctrl+n": true, // next tab (from tabBar)
1667+
"ctrl+k": true, // commands
1668+
"ctrl+h": true, // help
1669+
"shift+enter": true, // newline
1670+
"ctrl+j": true, // newline fallback
1671+
"ctrl+g": true, // edit in external editor (editor context)
1672+
"ctrl+r": true, // history search (editor context)
1673+
// Content panel bindings are included via chatPage.Bindings()
1674+
}
1675+
1676+
// Filter to only include status bar keys
1677+
var filtered []key.Binding
1678+
for _, binding := range all {
1679+
if len(binding.Keys()) > 0 {
1680+
key := binding.Keys()[0]
1681+
if statusBarKeys[key] {
1682+
filtered = append(filtered, binding)
1683+
}
1684+
}
1685+
}
1686+
1687+
return filtered
1688+
}
1689+
17141690
// handleKeyPress handles all keyboard input with proper priority routing.
17151691
func (m *appModel) handleKeyPress(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
17161692
// Check if we should stop transcription on Enter or Escape

0 commit comments

Comments
 (0)