From c035f87b30b658f2ddd852ec9f782e702037d526 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Thu, 5 Dec 2024 16:08:03 -0300 Subject: [PATCH] fix(input): help --- input/command.go | 3 +++ input/input.go | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/input/command.go b/input/command.go index e14bff011..d055d7229 100644 --- a/input/command.go +++ b/input/command.go @@ -4,6 +4,7 @@ import ( "fmt" "os" + "github.com/charmbracelet/bubbles/help" "github.com/charmbracelet/bubbles/textinput" tea "github.com/charmbracelet/bubbletea" @@ -50,6 +51,8 @@ func (o Options) Run() error { timeout: o.Timeout, hasTimeout: o.Timeout > 0, autoWidth: o.Width < 1, + showHelp: o.ShowHelp, + help: help.New(), }, tea.WithOutput(os.Stderr)) tm, err := p.Run() if err != nil { diff --git a/input/input.go b/input/input.go index eb8f73c2f..f08065294 100644 --- a/input/input.go +++ b/input/input.go @@ -10,12 +10,36 @@ package input import ( "time" + "github.com/charmbracelet/bubbles/help" + "github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/textinput" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/gum/timeout" "github.com/charmbracelet/lipgloss" ) +type keymap textinput.KeyMap + +func defaultKeymap() help.KeyMap { + k := textinput.DefaultKeyMap + return keymap(k) +} + +// FullHelp implements help.KeyMap. +func (k keymap) FullHelp() [][]key.Binding { + return [][]key.Binding{k.ShortHelp()} +} + +// ShortHelp implements help.KeyMap. +func (k keymap) ShortHelp() []key.Binding { + return []key.Binding{ + key.NewBinding( + key.WithKeys("enter"), + key.WithHelp("enter", "submit"), + ), + } +} + type model struct { autoWidth bool header string @@ -26,6 +50,8 @@ type model struct { aborted bool timeout time.Duration hasTimeout bool + showHelp bool + help help.Model } func (m model) Init() tea.Cmd { @@ -44,7 +70,15 @@ func (m model) View() string { return lipgloss.JoinVertical(lipgloss.Left, header, m.textinput.View()) } - return m.textinput.View() + if !m.showHelp { + return m.textinput.View() + } + return lipgloss.JoinVertical( + lipgloss.Top, + m.textinput.View(), + "", + m.help.View(defaultKeymap()), + ) } func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {