Skip to content

Commit 3a4922d

Browse files
committed
feat(tui): integrate command palette with slide navigation
- add `Command` key binding to keymap for palette activation - integrate command palette state management in main model - add command palette handling in `Update()` method with slide selection - modify `View()` to overlay command palette when active - store root slide reference for command palette initialization
1 parent 2bbc049 commit 3a4922d

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

internal/tui/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func NewCommand(rootSlide *Slide) Command {
108108
key.WithHelp("f", "filter"),
109109
)
110110

111-
return Command{list: l}
111+
return Command{list: l, showing: false}
112112
}
113113

114114
func (m Command) Init() tea.Cmd {

internal/tui/tui.go

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ import (
1111
)
1212

1313
type keyMap struct {
14-
Quit key.Binding
15-
Next key.Binding
16-
Prev key.Binding
17-
Top key.Binding
18-
Bottom key.Binding
14+
Quit key.Binding
15+
Next key.Binding
16+
Prev key.Binding
17+
Top key.Binding
18+
Bottom key.Binding
19+
Command key.Binding
1920
}
2021

2122
func (k keyMap) ShortHelp() []key.Binding {
@@ -47,6 +48,10 @@ var keys = keyMap{
4748
key.WithKeys("end", "shift+down", "$"),
4849
key.WithHelp("end, shift+down, $", "bottom"),
4950
),
51+
Command: key.NewBinding(
52+
key.WithKeys("?", "p"),
53+
key.WithHelp("ctrl+p, p", "command palette"),
54+
),
5055
}
5156

5257
func style(width, height int, styleConfig config.StyleConfig) config.SlideStyle {
@@ -57,16 +62,19 @@ type model struct {
5762
width int
5863
height int
5964

60-
slide *Slide
61-
keys keyMap
62-
help help.Model
65+
slide *Slide
66+
keys keyMap
67+
help help.Model
68+
command *Command
69+
rootSlide *Slide
6370
}
6471

6572
func New(rootSlide *Slide) model {
6673
return model{
67-
slide: rootSlide,
68-
keys: keys,
69-
help: help.New(),
74+
slide: rootSlide,
75+
keys: keys,
76+
help: help.New(),
77+
rootSlide: rootSlide,
7078
}
7179
}
7280

@@ -75,6 +83,20 @@ func (m model) Init() tea.Cmd {
7583
}
7684

7785
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
86+
if m.command != nil && m.command.IsShowing() {
87+
command, cmd := m.command.Update(msg)
88+
m.command = &command
89+
90+
if command.quitting || command.Choice() != nil {
91+
if command.Choice() != nil {
92+
m.slide = command.Choice()
93+
}
94+
m.command = nil
95+
return m, nil
96+
}
97+
return m, cmd
98+
}
99+
78100
switch msg := msg.(type) {
79101
case UpdateSlidesMsg:
80102
// Find current position in the slide list
@@ -85,6 +107,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
85107

86108
// Update root and navigate to the same position in the new list
87109
m.slide = msg.NewRoot
110+
m.rootSlide = msg.NewRoot
88111
for i := 0; i < currentPosition && m.slide != nil; i++ {
89112
m.slide = m.slide.Next
90113
}
@@ -106,6 +129,11 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
106129
case tea.KeyMsg:
107130
if key.Matches(msg, m.keys.Quit) {
108131
return m, tea.Quit
132+
} else if key.Matches(msg, m.keys.Command) {
133+
palette := NewCommand(m.rootSlide)
134+
palette = palette.SetShowing(true)
135+
m.command = &palette
136+
return m, nil
109137
} else if key.Matches(msg, m.keys.Next) {
110138
if m.slide.Next == nil || m.slide.ActiveTransition != nil && m.slide.ActiveTransition.Animating() {
111139
return m, nil
@@ -145,11 +173,17 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
145173
func (m model) View() string {
146174
m.slide.Style = style(m.width, m.height, m.slide.Properties.Style)
147175

148-
return lipgloss.Place(
176+
slideView := lipgloss.Place(
149177
m.width,
150178
m.height,
151179
lipgloss.Center,
152180
lipgloss.Center,
153181
m.slide.View(),
154182
)
183+
184+
if m.command != nil && m.command.IsShowing() {
185+
return m.command.Show(slideView, m.width, m.height)
186+
}
187+
188+
return slideView
155189
}

0 commit comments

Comments
 (0)