@@ -3,14 +3,19 @@ package tui
33import (
44 "github.com/charmbracelet/bubbles/key"
55 tea "github.com/charmbracelet/bubbletea"
6+ "github.com/charmbracelet/lipgloss"
7+ "github.com/kujtimiihoxha/termai/internal/tui/components/core"
68 "github.com/kujtimiihoxha/termai/internal/tui/layout"
79 "github.com/kujtimiihoxha/termai/internal/tui/page"
10+ "github.com/kujtimiihoxha/termai/internal/tui/util"
811)
912
1013type keyMap struct {
11− Logs key.Binding
12− Back key.Binding
13− Quit key.Binding
14+ Logs key.Binding
15+ Return key.Binding
16+ Back key.Binding
17+ Quit key.Binding
18+ Help key.Binding
1419}
1520
1621var keys = keyMap{
@@ -18,14 +23,22 @@ var keys = keyMap{
1823 key.WithKeys("L"),
1924 key.WithHelp("L", "logs"),
2025 ),
21− Back: key.NewBinding(
26+ Return: key.NewBinding(
2227 key.WithKeys("esc"),
23− key.WithHelp("esc", "back"),
28+ key.WithHelp("esc", "close"),
29+ ),
30+ Back: key.NewBinding(
31+ key.WithKeys("backspace"),
32+ key.WithHelp("backspace", "back"),
2433 ),
2534 Quit: key.NewBinding(
2635 key.WithKeys("ctrl+c", "q"),
2736 key.WithHelp("ctrl+c/q", "quit"),
2837 ),
38+ Help: key.NewBinding(
39+ key.WithKeys("?"),
40+ key.WithHelp("?", "toggle help"),
41+ ),
2942}
3043
3144type appModel struct {
@@ -34,6 +47,9 @@ type appModel struct {
3447 previousPage page.PageID
3548 pages map[page.PageID]tea.Model
3649 loadedPages map[page.PageID]bool
50+ status tea.Model
51+ help core.HelpCmp
52+ showHelp bool
3753}
3854
3955func (a appModel) Init() tea.Cmd {
@@ -45,28 +61,61 @@ func (a appModel) Init() tea.Cmd {
4561func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
4662 switch msg := msg.(type) {
4763 case tea.WindowSizeMsg:
64+ msg.Height -= 1 // Make space for the status bar
4865 a.width, a.height = msg.Width, msg.Height
66+
67+ a.status, _ = a.status.Update(msg)
68+
69+ uh, _ := a.help.Update(msg)
70+ a.help = uh.(core.HelpCmp)
71+
72+ p, cmd := a.pages[a.currentPage].Update(msg)
73+ a.pages[a.currentPage] = p
74+ return a, cmd
75+ case util.InfoMsg:
76+ a.status, _ = a.status.Update(msg)
77+ case util.ErrorMsg:
78+ a.status, _ = a.status.Update(msg)
4979 case tea.KeyMsg:
50− if key.Matches(msg, keys.Quit) {
80+ switch {
81+ case key.Matches(msg, keys.Quit):
5182 return a, tea.Quit
52− }
53− if key.Matches(msg, keys.Back) {
83+ case key.Matches(msg, keys.Back):
5484 if a.previousPage != "" {
5585 return a, a.moveToPage(a.previousPage)
5686 }
87+ case key.Matches(msg, keys.Return):
88+ if a.showHelp {
89+ a.ToggleHelp()
90+ return a, nil
91+ }
5792 return a, nil
58− }
59− if key.Matches(msg, keys.Logs) {
93+ case key.Matches(msg, keys.Logs):
6094 return a, a.moveToPage(page.LogsPage)
95+ case key.Matches(msg, keys.Help):
96+ a.ToggleHelp()
97+ return a, nil
6198 }
6299 }
63100 p, cmd := a.pages[a.currentPage].Update(msg)
64− if p != nil {
65− a.pages[a.currentPage] = p
66− }
101+ a.pages[a.currentPage] = p
67102 return a, cmd
68103}
69104
105+func (a *appModel) ToggleHelp() {
106+ if a.showHelp {
107+ a.showHelp = false
108+ a.height += a.help.Height()
109+ } else {
110+ a.showHelp = true
111+ a.height -= a.help.Height()
112+ }
113+
114+ if sizable, ok := a.pages[a.currentPage].(layout.Sizeable); ok {
115+ sizable.SetSize(a.width, a.height)
116+ }
117+}
118+
70119func (a *appModel) moveToPage(pageID page.PageID) tea.Cmd {
71120 var cmd tea.Cmd
72121 if _, ok := a.loadedPages[pageID]; !ok {
@@ -83,13 +132,30 @@ func (a *appModel) moveToPage(pageID page.PageID) tea.Cmd {
83132}
84133
85134func (a appModel) View() string {
86− return a.pages[a.currentPage].View()
135+ components := []string{
136+ a.pages[a.currentPage].View(),
137+ }
138+
139+ if a.showHelp {
140+ bindings := layout.KeyMapToSlice(keys)
141+ if p, ok := a.pages[a.currentPage].(layout.Bindings); ok {
142+ bindings = append(bindings, p.BindingKeys()...)
143+ }
144+ a.help.SetBindings(bindings)
145+ components = append(components, a.help.View())
146+ }
147+
148+ components = append(components, a.status.View())
149+
150+ return lipgloss.JoinVertical(lipgloss.Top, components...)
87151}
88152
89153func New() tea.Model {
90154 return &appModel{
91155 currentPage: page.ReplPage,
92156 loadedPages: make(map[page.PageID]bool),
157+ status: core.NewStatusCmp(),
158+ help: core.NewHelpCmp(),
93159 pages: map[page.PageID]tea.Model{
94160 page.LogsPage: page.NewLogsPage(),
95161 page.InitPage: page.NewInitPage(),