Skip to content

Instantly share code, notes, and snippets.

@meowgorithm
Created March 10, 2021 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meowgorithm/111bedc380724b51ff5289d23342d586 to your computer and use it in GitHub Desktop.
Save meowgorithm/111bedc380724b51ff5289d23342d586 to your computer and use it in GitHub Desktop.
package prompt
import (
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
)
type State int
const (
Active State = iota
Submitted
Canceled
)
func NewModel() Model {
ti := textinput.NewModel()
ti.Focus()
return Model{
Input: ti,
}
}
type Model struct {
Input textinput.Model
State State
}
func (m Model) Init() tea.Cmd {
return nil
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyEnter:
m.State = Submitted
case tea.KeyEsc:
m.State = Canceled
default:
var cmd tea.Cmd
m.Input, cmd = m.Input.Update(msg)
return m, cmd
}
}
return m, nil
}
func (m Model) View() string {
return "What's your favorite color? " + m.Input.View()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment