Created
March 10, 2021 01:55
-
-
Save meowgorithm/111bedc380724b51ff5289d23342d586 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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