Text input value getter/setter + cursor movement methods
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 826420a · Browse files · 3 files·+161 added−117 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+161 added−117 removed
- other:+161 added−117 removed
Provenance
- Commit
6ceafa0- Tree
7b01777- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
826420a
3 files changed+161 added−117 removed
deleted:examples/input/main.go-88 removed
1-package main2-3-import (4- "errors"5- "fmt"6- "log"7-8- "github.com/charmbracelet/boba"9- input "github.com/charmbracelet/boba/textinput"10-)11-12-type Model struct {13- textInput input.Model14- err error15-}16-17-type tickMsg struct{}18-type errMsg error19-20-func main() {21- p := boba.NewProgram(22- initialize,23- update,24- view,25- )26-27- if err := p.Start(); err != nil {28- log.Fatal(err)29- }30-}31-32-func initialize() (boba.Model, boba.Cmd) {33- inputModel := input.NewModel()34- inputModel.Placeholder = "Pikachu"35- inputModel.Focus()36-37- return Model{38- textInput: inputModel,39- err: nil,40- }, input.Blink(inputModel)41-}42-43-func update(msg boba.Msg, model boba.Model) (boba.Model, boba.Cmd) {44- var cmd boba.Cmd45- m, ok := model.(Model)46- if !ok {47- // When we encounter errors in Update we simply add the error to the48- // model so we can handle it in the view. We could also return a command49- // that does something else with the error, like logs it via IO.50- return Model{51- err: errors.New("could not perform assertion on model in update"),52- }, nil53- }54-55- switch msg := msg.(type) {56- case boba.KeyMsg:57- switch msg.Type {58- case boba.KeyCtrlC:59- fallthrough60- case boba.KeyEsc:61- fallthrough62- case boba.KeyEnter:63- return m, boba.Quit64- }65-66- // We handle errors just like any other message67- case errMsg:68- m.err = msg69- return m, nil70- }71-72- m.textInput, cmd = input.Update(msg, m.textInput)73- return m, cmd74-}75-76-func view(model boba.Model) string {77- m, ok := model.(Model)78- if !ok {79- return "Oh no: could not perform assertion on model."80- } else if m.err != nil {81- return fmt.Sprintf("Uh oh: %s", m.err)82- }83- return fmt.Sprintf(84- "What’s your favorite Pokémon?\n\n%s\n\n%s",85- input.View(m.textInput),86- "(esc to quit)",87- ) + "\n"88-}added:examples/textinput/main.go+88 added
1+package main2+3+import (4+ "errors"5+ "fmt"6+ "log"7+8+ "github.com/charmbracelet/boba"9+ input "github.com/charmbracelet/boba/textinput"10+)11+12+type Model struct {13+ textInput input.Model14+ err error15+}16+17+type tickMsg struct{}18+type errMsg error19+20+func main() {21+ p := boba.NewProgram(22+ initialize,23+ update,24+ view,25+ )26+27+ if err := p.Start(); err != nil {28+ log.Fatal(err)29+ }30+}31+32+func initialize() (boba.Model, boba.Cmd) {33+ inputModel := input.NewModel()34+ inputModel.Placeholder = "Pikachu"35+ inputModel.Focus()36+37+ return Model{38+ textInput: inputModel,39+ err: nil,40+ }, input.Blink(inputModel)41+}42+43+func update(msg boba.Msg, model boba.Model) (boba.Model, boba.Cmd) {44+ var cmd boba.Cmd45+ m, ok := model.(Model)46+ if !ok {47+ // When we encounter errors in Update we simply add the error to the48+ // model so we can handle it in the view. We could also return a command49+ // that does something else with the error, like logs it via IO.50+ return Model{51+ err: errors.New("could not perform assertion on model in update"),52+ }, nil53+ }54+55+ switch msg := msg.(type) {56+ case boba.KeyMsg:57+ switch msg.Type {58+ case boba.KeyCtrlC:59+ fallthrough60+ case boba.KeyEsc:61+ fallthrough62+ case boba.KeyEnter:63+ return m, boba.Quit64+ }65+66+ // We handle errors just like any other message67+ case errMsg:68+ m.err = msg69+ return m, nil70+ }71+72+ m.textInput, cmd = input.Update(msg, m.textInput)73+ return m, cmd74+}75+76+func view(model boba.Model) string {77+ m, ok := model.(Model)78+ if !ok {79+ return "Oh no: could not perform assertion on model."80+ } else if m.err != nil {81+ return fmt.Sprintf("Uh oh: %s", m.err)82+ }83+ return fmt.Sprintf(84+ "What’s your favorite Pokémon?\n\n%s\n\n%s",85+ input.View(m.textInput),86+ "(esc to quit)",87+ ) + "\n"88+}modified:textinput/textinput.go+73 added-29 removed
20 unmodified lines21 // Model is the Boba model for this text input element22 type Model struct {23 Err error24 Prompt string25- Value string25 Cursor string26 BlinkSpeed time.Duration27 Placeholder string28 TextColor string9 unmodified lines38 // It essentially treats the text field like a horizontally scrolling39 // viewport. If 0 or less this setting is ignored.40 Width int41 42+ // Underlying text value43+ value string44+45 // Focus indicates whether user input focus should be on this input46 // component. When false, don't blink and ignore keyboard input.47 focus bool48 7 unmodified lines56 // overflowing57 offset int58 }59 60+// SetValue sets the value of the text input61+func (m *Model) SetValue(s string) {62+ if m.CharLimit > 0 && len(s) > m.CharLimit {63+ m.value = s[:m.CharLimit]64+ } else {65+ m.value = s66+ }67+ if m.pos > len(m.value) {68+ m.pos = len(m.value)69+ }70+ m.handleOverflow()71+}72+73+// Value returns the value of the text input74+func (m Model) Value() string {75+ return m.value76+}77+78+// Cursor start moves the cursor to the given position. If the position is out79+// of bounds the cursor will be moved to the start or end accordingly.80+func (m *Model) SetCursor(pos int) {81+ m.pos = max(0, min(len(m.value), pos))82+ m.handleOverflow()83+}84+85+// CursorStart moves the cursor to the start of the field86+func (m *Model) CursorStart() {87+ m.pos = 088+ m.handleOverflow()89+}90+91+// CursorEnd moves the cursor to the end of the field92+func (m *Model) CursorEnd() {93+ m.pos = len(m.value)94+ m.handleOverflow()95+}96+97 // Focused returns the focus state on the model98 func (m Model) Focused() bool {99 return m.focus100 }11 unmodified lines112 }113 114 // Reset sets the input to its default state with no input.115 func (m *Model) Reset() {77- m.Value = ""116+ m.value = ""117 m.offset = 0118 m.pos = 0119 }120 121+// If a max width is defined, perform some logic to treat the visible area122+// as a horizontally scrolling viewport.123+func (m *Model) handleOverflow() {124+ if m.Width > 0 {125+ overflow := max(0, len(m.value)-m.Width)126+127+ if overflow > 0 && m.pos < m.offset {128+ m.offset = max(0, min(len(m.value), m.pos))129+ } else if overflow > 0 && m.pos >= m.offset+m.Width {130+ m.offset = max(0, m.pos-m.Width)131+ }132+ }133+}134+135 // colorText colorizes a given string according to the TextColor value of the136 // model137 func (m *Model) colorText(s string) string {138 return termenv.19 unmodified lines158 // NewModel creates a new model with default settings159 func NewModel() Model {160 return Model{161 Prompt: "> ",109- Value: "",162 BlinkSpeed: time.Millisecond * 600,163 Placeholder: "",164 TextColor: "",165 PlaceholderColor: "240",166 CursorColor: "",167 CharLimit: 0,168 169+ value: "",170 focus: false,171 blink: true,172 pos: 0,173 }12 unmodified lines186 switch msg.Type {187 case boba.KeyBackspace:188 fallthrough189 case boba.KeyDelete:137- if len(m.Value) > 0 {138- m.Value = m.Value[:m.pos-1] + m.Value[m.pos:]190+ if len(m.value) > 0 {191+ m.value = m.value[:m.pos-1] + m.value[m.pos:]192 m.pos--193 }194 case boba.KeyLeft:195 if m.pos > 0 {196 m.pos--197 }198 case boba.KeyRight:146- if m.pos < len(m.Value) {199+ if m.pos < len(m.value) {200 m.pos++201 }202 case boba.KeyCtrlF: // ^F, forward one character203 fallthrough204 case boba.KeyCtrlB: // ^B, back one charcter205 fallthrough206 case boba.KeyCtrlA: // ^A, go to beginning154- m.pos = 0207+ m.CursorStart()208 case boba.KeyCtrlD: // ^D, delete char under cursor156- if len(m.Value) > 0 && m.pos < len(m.Value) {157- m.Value = m.Value[:m.pos] + m.Value[m.pos+1:]209+ if len(m.value) > 0 && m.pos < len(m.value) {210+ m.value = m.value[:m.pos] + m.value[m.pos+1:]211 }212 case boba.KeyCtrlE: // ^E, go to end160- m.pos = len(m.Value)213+ m.CursorEnd()214 case boba.KeyCtrlK: // ^K, kill text after cursor162- m.Value = m.Value[:m.pos]163- m.pos = len(m.Value)215+ m.value = m.value[:m.pos]216+ m.pos = len(m.value)217 case boba.KeyCtrlU: // ^U, kill text before cursor165- m.Value = m.Value[m.pos:]218+ m.value = m.value[m.pos:]219 m.pos = 0220 m.offset = 0221 case boba.KeyRune: // input a regular character169- if m.CharLimit <= 0 || len(m.Value) < m.CharLimit {170- m.Value = m.Value[:m.pos] + string(msg.Rune) + m.Value[m.pos:]222+ if m.CharLimit <= 0 || len(m.value) < m.CharLimit {223+ m.value = m.value[:m.pos] + string(msg.Rune) + m.value[m.pos:]224 m.pos++225 }226 }227 4 unmodified lines232 m.blink = !m.blink233 return m, Blink(m)234 }235 183- // If a max width is defined, perform some logic to treat the visible area184- // as a horizontally scrolling mini viewport.185- if m.Width > 0 {186- overflow := max(0, len(m.Value)-m.Width)187- if overflow > 0 && m.pos < m.offset {188- m.offset = max(0, min(len(m.Value), m.pos))189- } else if overflow > 0 && m.pos >= m.offset+m.Width {190- m.offset = max(0, m.pos-m.Width)191- }192- }236+ m.handleOverflow()237 238 return m, nil239 }240 4 unmodified lines245 return "could not perform assertion on model"246 }247 248 // Placeholder text205- if m.Value == "" && m.Placeholder != "" {249+ if m.value == "" && m.Placeholder != "" {250 return placeholderView(m)251 }252 253 left := m.offset254 right := 0255 if m.Width > 0 {212- right = min(len(m.Value), m.offset+m.Width+1)256+ right = min(len(m.value), m.offset+m.Width+1)257 } else {214- right = len(m.Value)258+ right = len(m.value)259 }216- value := m.Value[left:right]260+ value := m.value[left:right]261 pos := m.pos - m.offset262 263 v := m.colorText(value[:pos])264 85 unmodified lines