2020-05-12 17:05:16 -04:00
|
|
|
package main
|
|
|
|
|
2020-10-14 11:51:04 -04:00
|
|
|
// A simple example demonstrating the use of multiple text input components
|
|
|
|
// from the Bubbles component library.
|
|
|
|
|
2020-05-12 17:05:16 -04:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
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"
|
2021-04-13 21:28:39 -04:00
|
|
|
"github.com/charmbracelet/lipgloss"
|
2020-05-12 17:05:16 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-04-13 21:28:39 -04:00
|
|
|
focusedStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
|
|
|
|
blurredButtonStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("240"))
|
|
|
|
noStyle = lipgloss.NewStyle()
|
|
|
|
|
|
|
|
focusedSubmitButton = "[ " + focusedStyle.Render("Submit") + " ]"
|
|
|
|
blurredSubmitButton = "[ " + blurredButtonStyle.Render("Submit") + " ]"
|
2020-05-12 17:05:16 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2020-10-15 19:48:42 -04:00
|
|
|
if err := tea.NewProgram(initialModel()).Start(); err != nil {
|
2020-05-12 17:05:16 -04:00
|
|
|
fmt.Printf("could not start program: %s\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 12:40:37 -04:00
|
|
|
type model struct {
|
2020-05-12 17:05:16 -04:00
|
|
|
index int
|
2020-10-30 03:36:52 -04:00
|
|
|
nameInput textinput.Model
|
|
|
|
emailInput textinput.Model
|
|
|
|
passwordInput textinput.Model
|
2020-05-12 17:05:16 -04:00
|
|
|
submitButton string
|
|
|
|
}
|
|
|
|
|
2020-10-15 19:48:42 -04:00
|
|
|
func initialModel() model {
|
2020-10-30 03:36:52 -04:00
|
|
|
name := textinput.NewModel()
|
2020-10-26 21:20:28 -04:00
|
|
|
name.Placeholder = "Nickname"
|
2020-05-12 17:05:16 -04:00
|
|
|
name.Focus()
|
2021-04-13 21:28:39 -04:00
|
|
|
name.PromptStyle = focusedStyle
|
|
|
|
name.TextStyle = focusedStyle
|
2020-10-21 10:19:34 -04:00
|
|
|
name.CharLimit = 32
|
2020-05-12 17:05:16 -04:00
|
|
|
|
2020-10-30 03:36:52 -04:00
|
|
|
email := textinput.NewModel()
|
2020-05-12 17:05:16 -04:00
|
|
|
email.Placeholder = "Email"
|
2020-10-21 10:19:34 -04:00
|
|
|
email.CharLimit = 64
|
2020-05-12 17:05:16 -04:00
|
|
|
|
2020-10-30 03:36:52 -04:00
|
|
|
password := textinput.NewModel()
|
2020-10-26 21:20:28 -04:00
|
|
|
password.Placeholder = "Password"
|
|
|
|
password.EchoMode = textinput.EchoPassword
|
|
|
|
password.EchoCharacter = '•'
|
|
|
|
password.CharLimit = 32
|
|
|
|
|
|
|
|
return model{0, name, email, password, blurredSubmitButton}
|
2020-05-12 17:56:30 -04:00
|
|
|
|
2020-05-12 17:05:16 -04: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
|
|
|
}
|
2020-05-12 17:05:16 -04:00
|
|
|
|
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-05-12 17:56:30 -04:00
|
|
|
|
2020-05-12 17:05:16 -04:00
|
|
|
switch msg := msg.(type) {
|
2020-05-25 19:26:40 -04:00
|
|
|
case tea.KeyMsg:
|
2020-05-12 17:05:16 -04:00
|
|
|
switch msg.String() {
|
2021-05-01 09:28:58 -04:00
|
|
|
case "ctrl+c", "esc":
|
2020-05-25 19:26:40 -04:00
|
|
|
return m, tea.Quit
|
2020-05-12 17:05:16 -04:00
|
|
|
|
|
|
|
// Cycle between inputs
|
2020-07-30 12:40:37 -04:00
|
|
|
case "tab", "shift+tab", "enter", "up", "down":
|
|
|
|
|
2020-10-30 03:36:52 -04:00
|
|
|
inputs := []textinput.Model{
|
2020-05-12 17:05:16 -04:00
|
|
|
m.nameInput,
|
|
|
|
m.emailInput,
|
2020-10-26 21:20:28 -04:00
|
|
|
m.passwordInput,
|
2020-05-12 17:05:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
s := msg.String()
|
|
|
|
|
|
|
|
// Did the user press enter while the submit button was focused?
|
|
|
|
// If so, exit.
|
|
|
|
if s == "enter" && m.index == len(inputs) {
|
2020-05-25 19:26:40 -04:00
|
|
|
return m, tea.Quit
|
2020-05-12 17:05:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cycle indexes
|
|
|
|
if s == "up" || s == "shift+tab" {
|
|
|
|
m.index--
|
|
|
|
} else {
|
|
|
|
m.index++
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.index > len(inputs) {
|
|
|
|
m.index = 0
|
|
|
|
} else if m.index < 0 {
|
|
|
|
m.index = len(inputs)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i <= len(inputs)-1; i++ {
|
|
|
|
if i == m.index {
|
2020-07-30 12:40:37 -04:00
|
|
|
// Set focused state
|
2020-05-12 17:05:16 -04:00
|
|
|
inputs[i].Focus()
|
2021-04-13 21:28:39 -04:00
|
|
|
inputs[i].PromptStyle = focusedStyle
|
|
|
|
inputs[i].TextStyle = focusedStyle
|
2020-05-12 17:05:16 -04:00
|
|
|
continue
|
|
|
|
}
|
2020-07-30 12:40:37 -04:00
|
|
|
// Remove focused state
|
2020-05-12 17:05:16 -04:00
|
|
|
inputs[i].Blur()
|
2021-04-13 21:28:39 -04:00
|
|
|
inputs[i].PromptStyle = noStyle
|
|
|
|
inputs[i].TextStyle = noStyle
|
2020-05-12 17:05:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
m.nameInput = inputs[0]
|
2020-10-26 21:20:28 -04:00
|
|
|
m.emailInput = inputs[1]
|
|
|
|
m.passwordInput = inputs[2]
|
2020-05-12 17:05:16 -04:00
|
|
|
|
|
|
|
if m.index == len(inputs) {
|
|
|
|
m.submitButton = focusedSubmitButton
|
|
|
|
} else {
|
|
|
|
m.submitButton = blurredSubmitButton
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
}
|
2020-07-30 12:40:37 -04:00
|
|
|
|
|
|
|
// Handle character input and blinks
|
|
|
|
m, cmd = updateInputs(msg, m)
|
|
|
|
return m, cmd
|
2020-05-12 17:05:16 -04:00
|
|
|
}
|
|
|
|
|
2020-07-30 12:40:37 -04:00
|
|
|
// Pass messages and models through to text input components. Only text inputs
|
|
|
|
// with Focus() set will respond, so it's safe to simply update all of them
|
|
|
|
// here without any further logic.
|
|
|
|
func updateInputs(msg tea.Msg, m model) (model, tea.Cmd) {
|
2020-05-12 17:56:30 -04:00
|
|
|
var (
|
2020-05-25 19:26:40 -04:00
|
|
|
cmd tea.Cmd
|
|
|
|
cmds []tea.Cmd
|
2020-05-12 17:56:30 -04:00
|
|
|
)
|
2020-07-30 12:40:37 -04:00
|
|
|
|
2020-10-26 21:20:28 -04:00
|
|
|
m.nameInput, cmd = m.nameInput.Update(msg)
|
2020-05-12 17:56:30 -04:00
|
|
|
cmds = append(cmds, cmd)
|
2020-07-30 12:40:37 -04:00
|
|
|
|
2020-10-26 21:20:28 -04:00
|
|
|
m.emailInput, cmd = m.emailInput.Update(msg)
|
2020-05-12 17:56:30 -04:00
|
|
|
cmds = append(cmds, cmd)
|
2020-07-30 12:40:37 -04:00
|
|
|
|
2020-10-26 21:20:28 -04:00
|
|
|
m.passwordInput, cmd = m.passwordInput.Update(msg)
|
2020-05-12 17:56:30 -04:00
|
|
|
cmds = append(cmds, cmd)
|
2020-07-30 12:40:37 -04:00
|
|
|
|
2020-05-25 19:26:40 -04:00
|
|
|
return m, tea.Batch(cmds...)
|
2020-05-12 17:05:16 -04:00
|
|
|
}
|
|
|
|
|
2020-10-15 19:48:42 -04:00
|
|
|
func (m model) View() string {
|
2020-05-12 17:05:16 -04:00
|
|
|
s := "\n"
|
|
|
|
|
|
|
|
inputs := []string{
|
2020-10-26 21:20:28 -04:00
|
|
|
m.nameInput.View(),
|
|
|
|
m.emailInput.View(),
|
|
|
|
m.passwordInput.View(),
|
2020-05-12 17:05:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(inputs); i++ {
|
|
|
|
s += inputs[i]
|
|
|
|
if i < len(inputs)-1 {
|
|
|
|
s += "\n"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s += "\n\n" + m.submitButton + "\n"
|
|
|
|
return s
|
|
|
|
}
|