@@ -3,34 +3,79 @@ package tui
33import (
44 "github.com/charmbracelet/bubbles/key"
55 tea "github.com/charmbracelet/bubbletea"
6+ "github.com/charmbracelet/lipgloss"
67)
78
9+ // keys is a package-level variable that holds the application's keybindings.
810var keys = DefaultKeyMap ()
911
10- // Update handles all incoming messages and updates the model accordingly.
12+ // Update is the central message handler for the application. It's called by the
13+ // Bubble Tea runtime when a message is received. It's responsible for updating
14+ // the model's state based on the message and returning any commands to execute.
1115func (m Model ) Update (msg tea.Msg ) (tea.Model , tea.Cmd ) {
16+ var cmd tea.Cmd
17+ var cmds []tea.Cmd
18+
1219 switch msg := msg .(type ) {
13- // tea.WindowSizeMsg is sent when the terminal window is resized .
20+ // Handle terminal window resize events .
1421 case tea.WindowSizeMsg :
1522 m .width = msg .Width
1623 m .height = msg .Height
1724 m .help .Width = msg .Width
25+ // Recalculate the dimensions of the help viewport.
26+ m .helpViewport .Width = int (float64 (m .width ) * 0.5 )
27+ m .helpViewport .Height = int (float64 (m .height ) * 0.75 )
1828
19- // tea.KeyMsg is sent when a key is pressed .
29+ // Handle keyboard input .
2030 case tea.KeyMsg :
31+ // If the help view is currently visible, handle its specific keybindings.
32+ if m .showHelp {
33+ // Allow the viewport to handle scrolling with arrow keys.
34+ m .helpViewport , cmd = m .helpViewport .Update (msg )
35+ cmds = append (cmds , cmd )
36+
37+ // Check for keys that close the help view.
38+ switch {
39+ case key .Matches (msg , keys .Quit ), key .Matches (msg , keys .ToggleHelp ), key .Matches (msg , keys .Escape ):
40+ m .showHelp = false
41+ }
42+ return m , tea .Batch (cmds ... )
43+ }
44+
45+ // Handle keybindings for the main application view.
2146 switch {
2247 case key .Matches (msg , keys .Quit ):
2348 return m , tea .Quit
2449
50+ case key .Matches (msg , keys .ToggleHelp ):
51+ m .showHelp = true
52+ // Generate and style the help content when the view is opened.
53+ m .helpContent = m .generateHelpContent ()
54+ m .helpViewport .SetContent (m .helpContent )
55+ m .helpViewport .Style = lipgloss .NewStyle ().
56+ Border (lipgloss .RoundedBorder ()).
57+ BorderForeground (m .theme .ActivePanel .GetBorderTopForeground ()).
58+ Padding (1 , 2 )
59+ m .helpViewport .GotoTop ()
60+
2561 case key .Matches (msg , keys .SwitchTheme ):
2662 m .nextTheme ()
63+ // Regenerate help content to apply new theme colors.
64+ m .helpContent = m .generateHelpContent ()
65+ m .helpViewport .SetContent (m .helpContent )
66+ m .helpViewport .Style = lipgloss .NewStyle ().
67+ Border (lipgloss .RoundedBorder ()).
68+ BorderForeground (m .theme .ActivePanel .GetBorderTopForeground ()).
69+ Padding (1 , 2 )
2770
71+ // Handle panel focus navigation.
2872 case key .Matches (msg , keys .FocusNext ):
2973 m .nextPanel ()
3074
3175 case key .Matches (msg , keys .FocusPrev ):
3276 m .prevPanel ()
3377
78+ // Handle direct panel focus via number keys.
3479 case key .Matches (msg , keys .FocusZero ):
3580 m .focusedPanel = MainPanel
3681
@@ -51,7 +96,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
5196 }
5297 // Return the updated model to the Bubble Tea runtime.
5398 return m , nil
54-
5599 }
56- return m , nil
100+
101+ // Batch and return any commands that were generated.
102+ return m , tea .Batch (cmds ... )
57103}
0 commit comments