Skip to content
Myr

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

1 file+29 added−32 removed

modified:examples/textinputs/main.go+29 added-32 removed
7 unmodified lines
8 tea "github.com/charmbracelet/bubbletea"9 te "github.com/muesli/termenv"10)1112const (13 focusedTextColor = "205"14)1516var (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 lines
31 os.Exit(1)32 }33}3432type Model struct {35type model struct {36 index int37 nameInput input.Model38 nickNameInput input.Model39 emailInput input.Model
4 unmodified lines
44 name := input.NewModel()45 name.Placeholder = "Name"46 name.Focus()47 name.Prompt = focusedPrompt45 name.TextColor = focusedText48 name.TextColor = focusedTextColor4950 nickName := input.NewModel()51 nickName.Placeholder = "Nickname"52 nickName.Prompt = blurredPrompt
1 unmodified line
54 email := input.NewModel()55 email.Placeholder = "Email"56 email.Prompt = blurredPrompt5755 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 )6465}6664func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {65 m, ok := model.(Model)67func 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 lines
80 case "ctrl+c":81 return m, tea.Quit8283 // 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":8586 inputs := []input.Model{87 m.nameInput,88 m.nickNameInput,89 m.emailInput,
21 unmodified lines
111 }112113 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 lines
134 m.submitButton = blurredSubmitButton135 }136137 return m, nil142143 default:144 // Handle character input145 m, cmd = updateInputs(msg, m)146 return m, cmd138 }148149 default:150 // Handle blinks151 m, cmd = updateInputs(msg, m)152 return m, cmd139 }140141 // Handle character input and blinks142 m, cmd = updateInputs(msg, m)143 return m, cmd144}145146// 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.156func updateInputs(msg tea.Msg, m Model) (Model, tea.Cmd) {149func updateInputs(msg tea.Msg, m model) (model, tea.Cmd) {150 var (151 cmd tea.Cmd152 cmds []tea.Cmd153 )154155 m.nameInput, cmd = input.Update(msg, m.nameInput)156 cmds = append(cmds, cmd)157158 m.nickNameInput, cmd = input.Update(msg, m.nickNameInput)159 cmds = append(cmds, cmd)160161 m.emailInput, cmd = input.Update(msg, m.emailInput)162 cmds = append(cmds, cmd)163164 return m, tea.Batch(cmds...)165}166170func view(model tea.Model) string {171 m, ok := model.(Model)167func 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 }172173 s := "\n"174
17 unmodified lines