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"
|
2021-06-01 15:17:50 -04:00
|
|
|
"strings"
|
2020-05-12 17:05:16 -04:00
|
|
|
|
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-06-01 15:17:50 -04:00
|
|
|
focusedStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
|
|
|
|
blurredStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("240"))
|
|
|
|
cursorStyle = focusedStyle.Copy()
|
|
|
|
noStyle = lipgloss.NewStyle()
|
|
|
|
helpStyle = blurredStyle.Copy()
|
|
|
|
cursorModeHelpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("244"))
|
|
|
|
|
|
|
|
focusedButton = focusedStyle.Copy().Render("[ Submit ]")
|
|
|
|
blurredButton = fmt.Sprintf("[ %s ]", blurredStyle.Render("Submit"))
|
2020-05-12 17:05:16 -04:00
|
|
|
)
|
|
|
|
|
2020-07-30 12:40:37 -04:00
|
|
|
type model struct {
|
2021-06-01 15:17:50 -04:00
|
|
|
focusIndex int
|
|
|
|
inputs []textinput.Model
|
|
|
|
cursorMode textinput.CursorMode
|
2020-05-12 17:05:16 -04:00
|
|
|
}
|
|
|
|
|
2020-10-15 19:48:42 -04:00
|
|
|
func initialModel() model {
|
2021-06-01 15:17:50 -04:00
|
|
|
m := model{
|
|
|
|
inputs: make([]textinput.Model, 3),
|
|
|
|
}
|
2020-05-12 17:05:16 -04:00
|
|
|
|
2021-06-01 15:17:50 -04:00
|
|
|
var t textinput.Model
|
|
|
|
for i := range m.inputs {
|
|
|
|
t = textinput.NewModel()
|
|
|
|
t.CursorStyle = cursorStyle
|
|
|
|
t.CharLimit = 32
|
|
|
|
|
|
|
|
switch i {
|
|
|
|
case 0:
|
|
|
|
t.Placeholder = "Nickname"
|
|
|
|
t.Focus()
|
|
|
|
t.PromptStyle = focusedStyle
|
|
|
|
t.TextStyle = focusedStyle
|
|
|
|
case 1:
|
|
|
|
t.Placeholder = "Email"
|
|
|
|
t.CharLimit = 64
|
|
|
|
case 2:
|
|
|
|
t.Placeholder = "Password"
|
|
|
|
t.EchoMode = textinput.EchoPassword
|
|
|
|
t.EchoCharacter = '•'
|
|
|
|
}
|
2020-10-26 21:20:28 -04:00
|
|
|
|
2021-06-01 15:17:50 -04:00
|
|
|
m.inputs[i] = t
|
|
|
|
}
|
2020-05-12 17:56:30 -04:00
|
|
|
|
2021-06-01 15:17:50 -04:00
|
|
|
return m
|
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-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
|
|
|
|
2021-06-01 15:17:50 -04:00
|
|
|
// Change cursor mode
|
|
|
|
case "ctrl+r":
|
|
|
|
m.cursorMode++
|
|
|
|
if m.cursorMode > textinput.CursorHide {
|
|
|
|
m.cursorMode = textinput.CursorBlink
|
|
|
|
}
|
|
|
|
cmds := make([]tea.Cmd, len(m.inputs))
|
|
|
|
for i := range m.inputs {
|
|
|
|
cmds[i] = m.inputs[i].SetCursorMode(m.cursorMode)
|
2020-05-12 17:05:16 -04:00
|
|
|
}
|
2021-06-01 15:17:50 -04:00
|
|
|
return m, tea.Batch(cmds...)
|
2020-05-12 17:05:16 -04:00
|
|
|
|
2021-06-01 15:17:50 -04:00
|
|
|
// Set focus to next input
|
|
|
|
case "tab", "shift+tab", "enter", "up", "down":
|
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.
|
2021-06-01 15:17:50 -04:00
|
|
|
if s == "enter" && m.focusIndex == len(m.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" {
|
2021-06-01 15:17:50 -04:00
|
|
|
m.focusIndex--
|
2020-05-12 17:05:16 -04:00
|
|
|
} else {
|
2021-06-01 15:17:50 -04:00
|
|
|
m.focusIndex++
|
2020-05-12 17:05:16 -04:00
|
|
|
}
|
|
|
|
|
2021-06-01 15:17:50 -04:00
|
|
|
if m.focusIndex > len(m.inputs) {
|
|
|
|
m.focusIndex = 0
|
|
|
|
} else if m.focusIndex < 0 {
|
|
|
|
m.focusIndex = len(m.inputs)
|
2020-05-12 17:05:16 -04:00
|
|
|
}
|
|
|
|
|
2021-06-01 15:17:50 -04:00
|
|
|
cmds := make([]tea.Cmd, len(m.inputs))
|
|
|
|
for i := 0; i <= len(m.inputs)-1; i++ {
|
|
|
|
if i == m.focusIndex {
|
2020-07-30 12:40:37 -04:00
|
|
|
// Set focused state
|
2021-06-01 15:17:50 -04:00
|
|
|
cmds[i] = m.inputs[i].Focus()
|
|
|
|
m.inputs[i].PromptStyle = focusedStyle
|
|
|
|
m.inputs[i].TextStyle = focusedStyle
|
2020-05-12 17:05:16 -04:00
|
|
|
continue
|
|
|
|
}
|
2020-07-30 12:40:37 -04:00
|
|
|
// Remove focused state
|
2021-06-01 15:17:50 -04:00
|
|
|
m.inputs[i].Blur()
|
|
|
|
m.inputs[i].PromptStyle = noStyle
|
|
|
|
m.inputs[i].TextStyle = noStyle
|
2020-05-12 17:05:16 -04:00
|
|
|
}
|
|
|
|
|
2021-06-01 15:17:50 -04:00
|
|
|
return m, tea.Batch(cmds...)
|
2020-05-12 17:05:16 -04:00
|
|
|
}
|
|
|
|
}
|
2020-07-30 12:40:37 -04:00
|
|
|
|
2021-06-01 15:17:50 -04:00
|
|
|
// Handle character input and blinking
|
|
|
|
cmd := m.updateInputs(msg)
|
|
|
|
|
2020-07-30 12:40:37 -04:00
|
|
|
return m, cmd
|
2020-05-12 17:05:16 -04:00
|
|
|
}
|
|
|
|
|
2021-06-01 15:17:50 -04:00
|
|
|
func (m *model) updateInputs(msg tea.Msg) tea.Cmd {
|
|
|
|
var cmds = make([]tea.Cmd, len(m.inputs))
|
2020-07-30 12:40:37 -04:00
|
|
|
|
2021-06-01 15:17:50 -04:00
|
|
|
// Only text inputs with Focus() set will respond, so it's safe to simply
|
|
|
|
// update all of them here without any further logic.
|
|
|
|
for i := range m.inputs {
|
|
|
|
m.inputs[i], cmds[i] = m.inputs[i].Update(msg)
|
|
|
|
}
|
2020-07-30 12:40:37 -04:00
|
|
|
|
2021-06-01 15:17:50 -04:00
|
|
|
return tea.Batch(cmds...)
|
2020-05-12 17:05:16 -04:00
|
|
|
}
|
|
|
|
|
2020-10-15 19:48:42 -04:00
|
|
|
func (m model) View() string {
|
2021-06-01 15:17:50 -04:00
|
|
|
var b strings.Builder
|
2020-05-12 17:05:16 -04:00
|
|
|
|
2021-06-01 15:17:50 -04:00
|
|
|
for i := range m.inputs {
|
|
|
|
b.WriteString(m.inputs[i].View())
|
|
|
|
if i < len(m.inputs)-1 {
|
|
|
|
b.WriteRune('\n')
|
|
|
|
}
|
2020-05-12 17:05:16 -04:00
|
|
|
}
|
|
|
|
|
2021-06-01 15:17:50 -04:00
|
|
|
button := &blurredButton
|
|
|
|
if m.focusIndex == len(m.inputs) {
|
|
|
|
button = &focusedButton
|
2020-05-12 17:05:16 -04:00
|
|
|
}
|
2021-06-01 15:17:50 -04:00
|
|
|
fmt.Fprintf(&b, "\n\n%s\n\n", *button)
|
2020-05-12 17:05:16 -04:00
|
|
|
|
2021-06-01 15:17:50 -04:00
|
|
|
b.WriteString(helpStyle.Render("cursor mode is "))
|
|
|
|
b.WriteString(cursorModeHelpStyle.Render(m.cursorMode.String()))
|
|
|
|
b.WriteString(helpStyle.Render(" (ctrl+r to change style)"))
|
|
|
|
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if err := tea.NewProgram(initialModel()).Start(); err != nil {
|
|
|
|
fmt.Printf("could not start program: %s\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-05-12 17:05:16 -04:00
|
|
|
}
|