Cycle through cursor modes in textinputs example per Bubbles v0.8.0
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Christian Rocha · committed by Christian Rocha · parent 035d2ab · Browse files · 3 files·+102 added−101 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+102 added−101 removed
- other:+102 added−101 removed
Provenance
- Commit
ec2fbf0- Tree
047ae00- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
035d2ab
3 files changed+102 added−101 removed
modified:examples/textinputs/main.go+99 added-98 removed
4 unmodified lines5 6 import (7 "fmt"8 "os"9+ "strings"10 11 "github.com/charmbracelet/bubbles/textinput"12 tea "github.com/charmbracelet/bubbletea"13 "github.com/charmbracelet/lipgloss"14 )15 16 var (16- focusedStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))17- blurredButtonStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("240"))18- noStyle = lipgloss.NewStyle()17+ focusedStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))18+ blurredStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("240"))19+ cursorStyle = focusedStyle.Copy()20+ noStyle = lipgloss.NewStyle()21+ helpStyle = blurredStyle.Copy()22+ cursorModeHelpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("244"))23 20- focusedSubmitButton = "[ " + focusedStyle.Render("Submit") + " ]"21- blurredSubmitButton = "[ " + blurredButtonStyle.Render("Submit") + " ]"24+ focusedButton = focusedStyle.Copy().Render("[ Submit ]")25+ blurredButton = fmt.Sprintf("[ %s ]", blurredStyle.Render("Submit"))26 )27 24-func main() {25- if err := tea.NewProgram(initialModel()).Start(); err != nil {26- fmt.Printf("could not start program: %s\n", err)27- os.Exit(1)28- }29-}30-28 type model struct {32- index int33- nameInput textinput.Model34- emailInput textinput.Model35- passwordInput textinput.Model36- submitButton string29+ focusIndex int30+ inputs []textinput.Model31+ cursorMode textinput.CursorMode32 }33 34 func initialModel() model {40- name := textinput.NewModel()41- name.Placeholder = "Nickname"42- name.Focus()43- name.PromptStyle = focusedStyle44- name.TextStyle = focusedStyle45- name.CharLimit = 3235+ m := model{36+ inputs: make([]textinput.Model, 3),37+ }38 47- email := textinput.NewModel()48- email.Placeholder = "Email"49- email.CharLimit = 6439+ var t textinput.Model40+ for i := range m.inputs {41+ t = textinput.NewModel()42+ t.CursorStyle = cursorStyle43+ t.CharLimit = 3244 51- password := textinput.NewModel()52- password.Placeholder = "Password"53- password.EchoMode = textinput.EchoPassword54- password.EchoCharacter = '•'55- password.CharLimit = 3245+ switch i {46+ case 0:47+ t.Placeholder = "Nickname"48+ t.Focus()49+ t.PromptStyle = focusedStyle50+ t.TextStyle = focusedStyle51+ case 1:52+ t.Placeholder = "Email"53+ t.CharLimit = 6454+ case 2:55+ t.Placeholder = "Password"56+ t.EchoMode = textinput.EchoPassword57+ t.EchoCharacter = '•'58+ }59 57- return model{0, name, email, password, blurredSubmitButton}60+ m.inputs[i] = t61+ }62 63+ return m64 }65 func (m model) Init() tea.Cmd {66 return textinput.Blink67 }68 69 func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {65- var cmd tea.Cmd66-70 switch msg := msg.(type) {71 case tea.KeyMsg:72 switch msg.String() {73 case "ctrl+c", "esc":74 return m, tea.Quit75 73- // Cycle between inputs74- case "tab", "shift+tab", "enter", "up", "down":75-76- inputs := []textinput.Model{77- m.nameInput,78- m.emailInput,79- m.passwordInput,76+ // Change cursor mode77+ case "ctrl+r":78+ m.cursorMode++79+ if m.cursorMode > textinput.CursorHide {80+ m.cursorMode = textinput.CursorBlink81 }82+ cmds := make([]tea.Cmd, len(m.inputs))83+ for i := range m.inputs {84+ cmds[i] = m.inputs[i].SetCursorMode(m.cursorMode)85+ }86+ return m, tea.Batch(cmds...)87 88+ // Set focus to next input89+ case "tab", "shift+tab", "enter", "up", "down":90 s := msg.String()91 92 // Did the user press enter while the submit button was focused?93 // If so, exit.86- if s == "enter" && m.index == len(inputs) {94+ if s == "enter" && m.focusIndex == len(m.inputs) {95 return m, tea.Quit96 }97 98 // Cycle indexes99 if s == "up" || s == "shift+tab" {92- m.index--100+ m.focusIndex--101 } else {94- m.index++102+ m.focusIndex++103 }104 97- if m.index > len(inputs) {98- m.index = 099- } else if m.index < 0 {100- m.index = len(inputs)105+ if m.focusIndex > len(m.inputs) {106+ m.focusIndex = 0107+ } else if m.focusIndex < 0 {108+ m.focusIndex = len(m.inputs)109 }110 111+ cmds := make([]tea.Cmd, len(m.inputs))103- for i := 0; i <= len(inputs)-1; i++ {104- if i == m.index {112+ for i := 0; i <= len(m.inputs)-1; i++ {113+ if i == m.focusIndex {114 // Set focused state106- inputs[i].Focus()107- inputs[i].PromptStyle = focusedStyle108- inputs[i].TextStyle = focusedStyle115+ cmds[i] = m.inputs[i].Focus()116+ m.inputs[i].PromptStyle = focusedStyle117+ m.inputs[i].TextStyle = focusedStyle118 continue119 }120 // Remove focused state112- inputs[i].Blur()113- inputs[i].PromptStyle = noStyle114- inputs[i].TextStyle = noStyle121+ m.inputs[i].Blur()122+ m.inputs[i].PromptStyle = noStyle123+ m.inputs[i].TextStyle = noStyle124 }125 117- m.nameInput = inputs[0]118- m.emailInput = inputs[1]119- m.passwordInput = inputs[2]120-121- if m.index == len(inputs) {122- m.submitButton = focusedSubmitButton123- } else {124- m.submitButton = blurredSubmitButton125- }126-127- return m, nil126+ return m, tea.Batch(cmds...)127 }128 }129 131- // Handle character input and blinks132- m, cmd = updateInputs(msg, m)130+ // Handle character input and blinking131+ cmd := m.updateInputs(msg)132+133 return m, cmd134 }135 136-// Pass messages and models through to text input components. Only text inputs137-// with Focus() set will respond, so it's safe to simply update all of them138-// here without any further logic.139-func updateInputs(msg tea.Msg, m model) (model, tea.Cmd) {140- var (141- cmd tea.Cmd142- cmds []tea.Cmd143- )136+func (m *model) updateInputs(msg tea.Msg) tea.Cmd {137+ var cmds = make([]tea.Cmd, len(m.inputs))138 145- m.nameInput, cmd = m.nameInput.Update(msg)146- cmds = append(cmds, cmd)139+ // Only text inputs with Focus() set will respond, so it's safe to simply140+ // update all of them here without any further logic.141+ for i := range m.inputs {142+ m.inputs[i], cmds[i] = m.inputs[i].Update(msg)143+ }144 148- m.emailInput, cmd = m.emailInput.Update(msg)149- cmds = append(cmds, cmd)150-151- m.passwordInput, cmd = m.passwordInput.Update(msg)152- cmds = append(cmds, cmd)153-154- return m, tea.Batch(cmds...)145+ return tea.Batch(cmds...)146 }147 148 func (m model) View() string {158- s := "\n"149+ var b strings.Builder150 160- inputs := []string{161- m.nameInput.View(),162- m.emailInput.View(),163- m.passwordInput.View(),151+ for i := range m.inputs {152+ b.WriteString(m.inputs[i].View())153+ if i < len(m.inputs)-1 {154+ b.WriteRune('\n')155+ }156 }157 166- for i := 0; i < len(inputs); i++ {167- s += inputs[i]168- if i < len(inputs)-1 {169- s += "\n"170- }158+ button := &blurredButton159+ if m.focusIndex == len(m.inputs) {160+ button = &focusedButton161 }162+ fmt.Fprintf(&b, "\n\n%s\n\n", *button)163 173- s += "\n\n" + m.submitButton + "\n"174- return s164+ b.WriteString(helpStyle.Render("cursor mode is "))165+ b.WriteString(cursorModeHelpStyle.Render(m.cursorMode.String()))166+ b.WriteString(helpStyle.Render(" (ctrl+r to change style)"))167+168+ return b.String()169 }170+171+func main() {172+ if err := tea.NewProgram(initialModel()).Start(); err != nil {173+ fmt.Printf("could not start program: %s\n", err)174+ os.Exit(1)175+ }176+}modified:examples/go.mod+1 added-1 removed
1 unmodified line2 3 go 1.134 5 require (6- github.com/charmbracelet/bubbles v0.7.76+ github.com/charmbracelet/bubbles v0.8.07 github.com/charmbracelet/bubbletea v0.13.18 github.com/charmbracelet/glamour v0.2.09 github.com/charmbracelet/lipgloss v0.1.210 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf17768 unmodified linesmodified:examples/go.sum+2 added-2 removed
7 unmodified lines8 github.com/alecthomas/repr v0.0.0-20180818092828-117648cd9897 h1:p9Sln00KOTlrYkxI1zYWl1QLnEqAqEARBEYa8FQnQcY=9 github.com/alecthomas/repr v0.0.0-20180818092828-117648cd9897/go.mod h1:xTS7Pm1pD1mvyM075QCDSRqH6qRLXylzS24ZTpRiSzQ=10 github.com/atotto/clipboard v0.1.2 h1:YZCtFu5Ie8qX2VmVTBnrqLSiU9XOWwqNRmdT3gIQzbY=11 github.com/atotto/clipboard v0.1.2/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=12-github.com/charmbracelet/bubbles v0.7.7 h1:eVsAYiAJskifXaqHpPzbOZS1srMaTnuiTazC6uukBbA=13-github.com/charmbracelet/bubbles v0.7.7/go.mod h1:5WX1sSSjNCgCrzvRMN/z23HxvWaa+AI16Ch0KPZPeDs=12+github.com/charmbracelet/bubbles v0.8.0 h1:+l2op90Ag37Vn+30O1hbg/0wBl+e+sxHhgY1F/rvdHs=13+github.com/charmbracelet/bubbles v0.8.0/go.mod h1:5WX1sSSjNCgCrzvRMN/z23HxvWaa+AI16Ch0KPZPeDs=14 github.com/charmbracelet/glamour v0.2.0 h1:mTgaiNiumpqTZp3qVM6DH9UB0NlbY17wejoMf1kM8Pg=15 github.com/charmbracelet/glamour v0.2.0/go.mod h1:UA27Kwj3QHialP74iU6C+Gpc8Y7IOAKupeKMLLBURWM=16 github.com/charmbracelet/lipgloss v0.1.2 h1:D+LUMg34W7n2pkuMrevKVxT7HXqnoRHm7IoomkX3/ZU=17 github.com/charmbracelet/lipgloss v0.1.2/go.mod h1:5D8zradw52m7QmxRF6QgwbwJi9je84g8MkWiGN07uKg=66 unmodified lines