textinput.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package textinput
  2. import (
  3. "fmt"
  4. "github.com/charmbracelet/bubbles/textinput"
  5. tea "github.com/charmbracelet/bubbletea"
  6. )
  7. type (
  8. errMsg error
  9. )
  10. type model struct {
  11. textInput textinput.Model
  12. err error
  13. }
  14. func NewTextInput(placeholder string) *model {
  15. ti := textinput.New()
  16. ti.Placeholder = placeholder
  17. ti.Focus()
  18. ti.CharLimit = 156
  19. ti.Width = 20
  20. return &model{
  21. textInput: ti,
  22. err: nil,
  23. }
  24. }
  25. func (m *model) Value() string {
  26. return m.textInput.Value()
  27. }
  28. func (m *model) Init() tea.Cmd {
  29. return textinput.Blink
  30. }
  31. func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  32. var cmd tea.Cmd
  33. switch msg := msg.(type) {
  34. case tea.KeyMsg:
  35. switch msg.Type {
  36. case tea.KeyEnter, tea.KeyCtrlC, tea.KeyEsc:
  37. return m, tea.Quit
  38. }
  39. // We handle errors just like any other message
  40. case errMsg:
  41. m.err = msg
  42. return m, nil
  43. }
  44. m.textInput, cmd = m.textInput.Update(msg)
  45. return m, cmd
  46. }
  47. func (m *model) View() string {
  48. return fmt.Sprintf(
  49. "\nPlease insert the name of the session\n\n%s\n\n%s",
  50. m.textInput.View(),
  51. "(esc to quit)",
  52. ) + "\n"
  53. }