Skip to content

Commit 83bc52b

Browse files
feat: render help component
1 parent 6c46a17 commit 83bc52b

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

keys.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,31 @@ func DefaultKeyMap() *KeyMap {
2525
HardDrop: key.NewBinding(key.WithKeys("up", "w", "i"), key.WithHelp("w, i, up", "hard drop")),
2626
}
2727
}
28+
29+
func (k *KeyMap) ShortHelp() []key.Binding {
30+
return []key.Binding{
31+
k.Quit,
32+
k.Help,
33+
}
34+
}
35+
36+
func (k *KeyMap) FullHelp() [][]key.Binding {
37+
return [][]key.Binding{
38+
{
39+
k.Quit,
40+
k.Help,
41+
},
42+
{
43+
k.Left,
44+
k.Right,
45+
},
46+
{
47+
k.Clockwise,
48+
k.CounterClockwise,
49+
},
50+
{
51+
k.SoftDrop,
52+
k.HardDrop,
53+
},
54+
}
55+
}

model.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
package main
22

33
import (
4+
"github.com/charmbracelet/bubbles/help"
45
"github.com/charmbracelet/bubbles/key"
56
tea "github.com/charmbracelet/bubbletea"
67
)
78

9+
type Piece struct {
10+
}
11+
812
type Model struct {
913
playfield [40][10]byte
1014
styles *Styles
15+
help help.Model
1116
keys *KeyMap
1217
}
1318

1419
func InitialModel() *Model {
1520
return &Model{
16-
playfield: [40][10]byte{},
17-
styles: DefaultStyles(),
18-
keys: DefaultKeyMap(),
21+
playfield: [40][10]byte{
22+
{0, 'I', 'O', 'T', 'S', 'Z', 'J', 'L', 'G'},
23+
},
24+
styles: DefaultStyles(),
25+
help: help.New(),
26+
keys: DefaultKeyMap(),
1927
}
2028
}
2129

@@ -29,6 +37,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
2937
switch {
3038
case key.Matches(msg, m.keys.Quit):
3139
return m, tea.Quit
40+
case key.Matches(msg, m.keys.Help):
41+
m.help.ShowAll = !m.help.ShowAll
3242
}
3343
}
3444

@@ -59,5 +69,5 @@ func (m Model) View() string {
5969
}
6070
}
6171

62-
return m.styles.Program.Render(output)
72+
return m.styles.Program.Render(output) + "\n" + m.help.View(m.keys)
6373
}

0 commit comments

Comments
 (0)