chore: cleanup autocomplete example (#892)

This commit is contained in:
Maas Lalani 2024-01-08 12:05:38 -05:00 committed by GitHub
parent b6695477b4
commit 705292761d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 5 deletions

View File

@ -7,6 +7,8 @@ import (
"log" "log"
"net/http" "net/http"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textinput" "github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
@ -60,18 +62,40 @@ func getRepos() tea.Msg {
type model struct { type model struct {
textInput textinput.Model textInput textinput.Model
help help.Model
keymap keymap
}
type keymap struct{}
func (k keymap) ShortHelp() []key.Binding {
return []key.Binding{
key.NewBinding(key.WithKeys("tab"), key.WithHelp("tab", "complete")),
key.NewBinding(key.WithKeys("ctrl+n"), key.WithHelp("ctrl+n", "next")),
key.NewBinding(key.WithKeys("ctrl+p"), key.WithHelp("ctrl+p", "prev")),
key.NewBinding(key.WithKeys("esc"), key.WithHelp("esc", "quit")),
}
}
func (k keymap) FullHelp() [][]key.Binding {
return [][]key.Binding{k.ShortHelp()}
} }
func initialModel() model { func initialModel() model {
ti := textinput.New() ti := textinput.New()
ti.Placeholder = "repository"
ti.Prompt = "charmbracelet/" ti.Prompt = "charmbracelet/"
ti.Placeholder = "repo..." ti.PromptStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("63"))
ti.Cursor.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("63")) ti.Cursor.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("63"))
ti.Focus() ti.Focus()
ti.CharLimit = 50 ti.CharLimit = 50
ti.Width = 20 ti.Width = 20
ti.ShowSuggestions = true ti.ShowSuggestions = true
return model{textInput: ti}
h := help.New()
km := keymap{}
return model{textInput: ti, help: h, keymap: km}
} }
func (m model) Init() tea.Cmd { func (m model) Init() tea.Cmd {
@ -100,8 +124,8 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m model) View() string { func (m model) View() string {
return fmt.Sprintf( return fmt.Sprintf(
"Whats your favorite Charm repository?\n\n%s\n\n%s\n", "Pick a Charm™ repo:\n\n %s\n\n%s\n\n",
m.textInput.View(), m.textInput.View(),
"(tab to complete, ctrl+n/ctrl+p to cycle through suggestions, esc to quit)", m.help.View(m.keymap),
) )
} }

View File

@ -4,7 +4,7 @@ go 1.17
require ( require (
github.com/charmbracelet/bubbles v0.17.0 github.com/charmbracelet/bubbles v0.17.0
github.com/charmbracelet/bubbletea v0.24.2 github.com/charmbracelet/bubbletea v0.25.0
github.com/charmbracelet/glamour v0.6.0 github.com/charmbracelet/glamour v0.6.0
github.com/charmbracelet/harmonica v0.2.0 github.com/charmbracelet/harmonica v0.2.0
github.com/charmbracelet/lipgloss v0.9.1 github.com/charmbracelet/lipgloss v0.9.1