Cleanup textinputs example
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 43bcbfc · Browse files · 1 file·+29 added−32 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+29 added−32 removed
- other:+29 added−32 removed
Provenance
- Commit
e202d49- Tree
2e736af- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
43bcbfc
1 file changed+29 added−32 removed
modified:examples/textinputs/main.go+29 added-32 removed
7 unmodified lines8 tea "github.com/charmbracelet/bubbletea"9 te "github.com/muesli/termenv"10 )11 12+const (13+ focusedTextColor = "205"14+)15+16 var (17 color = te.ColorProfile().Color14- focusedText = "205"18 focusedPrompt = te.String("> ").Foreground(color("205")).String()19 blurredPrompt = "> "20 focusedSubmitButton = "[ " + te.String("Submit").Foreground(color("205")).String() + " ]"21 blurredSubmitButton = "[ " + te.String("Submit").Foreground(color("240")).String() + " ]"9 unmodified lines31 os.Exit(1)32 }33 }34 32-type Model struct {35+type model struct {36 index int37 nameInput input.Model38 nickNameInput input.Model39 emailInput input.Model4 unmodified lines44 name := input.NewModel()45 name.Placeholder = "Name"46 name.Focus()47 name.Prompt = focusedPrompt45- name.TextColor = focusedText48+ name.TextColor = focusedTextColor49 50 nickName := input.NewModel()51 nickName.Placeholder = "Nickname"52 nickName.Prompt = blurredPrompt1 unmodified line54 email := input.NewModel()55 email.Placeholder = "Email"56 email.Prompt = blurredPrompt57 55- return Model{0, name, nickName, email, blurredSubmitButton},58+ return model{0, name, nickName, email, blurredSubmitButton},59 tea.Batch(60 input.Blink(name),61 input.Blink(nickName),62 input.Blink(email),63 )64 65 }66 64-func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {65- m, ok := model.(Model)67+func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {68+ m, ok := mdl.(model)69 if !ok {70 panic("could not perform assertion on model")71 }72 7 unmodified lines80 case "ctrl+c":81 return m, tea.Quit82 83 // Cycle between inputs81- case "tab":82- fallthrough83- case "shift+tab":84- fallthrough85- case "enter":86- fallthrough87- case "up":88- fallthrough89- case "down":84+ case "tab", "shift+tab", "enter", "up", "down":85+86 inputs := []input.Model{87 m.nameInput,88 m.nickNameInput,89 m.emailInput,21 unmodified lines111 }112 113 for i := 0; i <= len(inputs)-1; i++ {114 if i == m.index {119- // Focused input115+ // Set focused state116 inputs[i].Focus()117 inputs[i].Prompt = focusedPrompt122- inputs[i].TextColor = focusedText118+ inputs[i].TextColor = focusedTextColor119 continue120 }125- // Blurred input121+ // Remove focused state122 inputs[i].Blur()123 inputs[i].Prompt = blurredPrompt124 inputs[i].TextColor = ""125 }8 unmodified lines134 m.submitButton = blurredSubmitButton135 }136 137 return m, nil142-143- default:144- // Handle character input145- m, cmd = updateInputs(msg, m)146- return m, cmd138 }148-149- default:150- // Handle blinks151- m, cmd = updateInputs(msg, m)152- return m, cmd139 }140+141+ // Handle character input and blinks142+ m, cmd = updateInputs(msg, m)143+ return m, cmd144 }145 146+// Pass messages and models through to text input components. Only text inputs147+// with Focus() set will respond, so it's safe to simply update all of them148+// here without any further logic.156-func updateInputs(msg tea.Msg, m Model) (Model, tea.Cmd) {149+func updateInputs(msg tea.Msg, m model) (model, tea.Cmd) {150 var (151 cmd tea.Cmd152 cmds []tea.Cmd153 )154+155 m.nameInput, cmd = input.Update(msg, m.nameInput)156 cmds = append(cmds, cmd)157+158 m.nickNameInput, cmd = input.Update(msg, m.nickNameInput)159 cmds = append(cmds, cmd)160+161 m.emailInput, cmd = input.Update(msg, m.emailInput)162 cmds = append(cmds, cmd)163+164 return m, tea.Batch(cmds...)165 }166 170-func view(model tea.Model) string {171- m, ok := model.(Model)167+func view(mdl tea.Model) string {168+ m, ok := mdl.(model)169 if !ok {173- return "[error] could not perform assertion on model"170+ return "could not perform assertion on model"171 }172 173 s := "\n"174 17 unmodified lines