Skip to content

Commit

Permalink
fix(input): help
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Dec 5, 2024
1 parent e6f2b60 commit c035f87
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
3 changes: 3 additions & 0 deletions input/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"

"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"

Expand Down Expand Up @@ -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 {
Expand Down
36 changes: 35 additions & 1 deletion input/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down

0 comments on commit c035f87

Please sign in to comment.