2020-01-17 22:21:51 -05:00
|
|
|
|
package main
|
|
|
|
|
|
2020-10-14 11:51:04 -04:00
|
|
|
|
// A simple program demonstrating the text input component from the Bubbles
|
|
|
|
|
// component library.
|
|
|
|
|
|
2020-01-17 22:21:51 -05:00
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
|
2020-10-26 21:20:28 -04:00
|
|
|
|
"github.com/charmbracelet/bubbles/textinput"
|
2020-05-25 19:26:40 -04:00
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2020-01-17 22:21:51 -05:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
2020-10-15 19:48:42 -04:00
|
|
|
|
p := tea.NewProgram(initialModel())
|
2020-01-17 22:21:51 -05:00
|
|
|
|
|
|
|
|
|
if err := p.Start(); err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-15 19:48:42 -04:00
|
|
|
|
type tickMsg struct{}
|
|
|
|
|
type errMsg error
|
|
|
|
|
|
|
|
|
|
type model struct {
|
2021-02-06 14:14:38 -05:00
|
|
|
|
textInput textinput.Model
|
2020-10-15 19:48:42 -04:00
|
|
|
|
err error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func initialModel() model {
|
2022-01-10 20:42:10 -05:00
|
|
|
|
ti := textinput.New()
|
2021-02-06 14:14:38 -05:00
|
|
|
|
ti.Placeholder = "Pikachu"
|
|
|
|
|
ti.Focus()
|
|
|
|
|
ti.CharLimit = 156
|
|
|
|
|
ti.Width = 20
|
2020-02-01 22:28:31 -05:00
|
|
|
|
|
2020-10-15 19:48:42 -04:00
|
|
|
|
return model{
|
2021-02-06 14:14:38 -05:00
|
|
|
|
textInput: ti,
|
2020-04-22 11:00:30 -04:00
|
|
|
|
err: nil,
|
2020-10-15 19:48:42 -04:00
|
|
|
|
}
|
2020-01-18 22:18:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-15 19:48:42 -04:00
|
|
|
|
func (m model) Init() tea.Cmd {
|
2020-10-26 21:20:28 -04:00
|
|
|
|
return textinput.Blink
|
2020-10-15 19:48:42 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
2020-05-25 19:26:40 -04:00
|
|
|
|
var cmd tea.Cmd
|
2020-01-17 22:21:51 -05:00
|
|
|
|
|
|
|
|
|
switch msg := msg.(type) {
|
2020-05-25 19:26:40 -04:00
|
|
|
|
case tea.KeyMsg:
|
2020-01-26 16:46:30 -05:00
|
|
|
|
switch msg.Type {
|
2021-05-01 09:28:58 -04:00
|
|
|
|
case tea.KeyEnter, tea.KeyCtrlC, tea.KeyEsc:
|
2020-05-25 19:26:40 -04:00
|
|
|
|
return m, tea.Quit
|
2020-01-17 22:21:51 -05:00
|
|
|
|
}
|
2020-01-18 10:13:44 -05:00
|
|
|
|
|
|
|
|
|
// We handle errors just like any other message
|
2020-04-22 11:00:30 -04:00
|
|
|
|
case errMsg:
|
|
|
|
|
m.err = msg
|
2020-01-18 10:13:44 -05:00
|
|
|
|
return m, nil
|
2020-01-17 22:21:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-26 21:20:28 -04:00
|
|
|
|
m.textInput, cmd = m.textInput.Update(msg)
|
2020-01-17 22:21:51 -05:00
|
|
|
|
return m, cmd
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-15 19:48:42 -04:00
|
|
|
|
func (m model) View() string {
|
2020-01-18 10:13:44 -05:00
|
|
|
|
return fmt.Sprintf(
|
|
|
|
|
"What’s your favorite Pokémon?\n\n%s\n\n%s",
|
2020-10-26 21:20:28 -04:00
|
|
|
|
m.textInput.View(),
|
2020-01-18 10:13:44 -05:00
|
|
|
|
"(esc to quit)",
|
2020-05-12 17:05:16 -04:00
|
|
|
|
) + "\n"
|
2020-01-17 22:21:51 -05:00
|
|
|
|
}
|