Skip to content
Myr

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

3 files+161 added−117 removed

deleted:examples/input/main.go-88 removed
1package main23import (4 "errors"5 "fmt"6 "log"78 "github.com/charmbracelet/boba"9 input "github.com/charmbracelet/boba/textinput"10)1112type Model struct {13 textInput input.Model14 err error15}1617type tickMsg struct{}18type errMsg error1920func main() {21 p := boba.NewProgram(22 initialize,23 update,24 view,25 )2627 if err := p.Start(); err != nil {28 log.Fatal(err)29 }30}3132func initialize() (boba.Model, boba.Cmd) {33 inputModel := input.NewModel()34 inputModel.Placeholder = "Pikachu"35 inputModel.Focus()3637 return Model{38 textInput: inputModel,39 err: nil,40 }, input.Blink(inputModel)41}4243func 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 }5455 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 }6566 // We handle errors just like any other message67 case errMsg:68 m.err = msg69 return m, nil70 }7172 m.textInput, cmd = input.Update(msg, m.textInput)73 return m, cmd74}7576func 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
1package main23import (4 "errors"5 "fmt"6 "log"78 "github.com/charmbracelet/boba"9 input "github.com/charmbracelet/boba/textinput"10)1112type Model struct {13 textInput input.Model14 err error15}1617type tickMsg struct{}18type errMsg error1920func main() {21 p := boba.NewProgram(22 initialize,23 update,24 view,25 )2627 if err := p.Start(); err != nil {28 log.Fatal(err)29 }30}3132func initialize() (boba.Model, boba.Cmd) {33 inputModel := input.NewModel()34 inputModel.Placeholder = "Pikachu"35 inputModel.Focus()3637 return Model{38 textInput: inputModel,39 err: nil,40 }, input.Blink(inputModel)41}4243func 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 }5455 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 }6566 // We handle errors just like any other message67 case errMsg:68 m.err = msg69 return m, nil70 }7172 m.textInput, cmd = input.Update(msg, m.textInput)73 return m, cmd74}7576func 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 lines
21// Model is the Boba model for this text input element22type Model struct {23 Err error24 Prompt string25 Value string25 Cursor string26 BlinkSpeed time.Duration27 Placeholder string28 TextColor string
9 unmodified lines
38 // It essentially treats the text field like a horizontally scrolling39 // viewport. If 0 or less this setting is ignored.40 Width int4142 // Underlying text value43 value string4445 // 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 lines
56 // overflowing57 offset int58}5960// SetValue sets the value of the text input61func (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}7273// Value returns the value of the text input74func (m Model) Value() string {75 return m.value76}7778// 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.80func (m *Model) SetCursor(pos int) {81 m.pos = max(0, min(len(m.value), pos))82 m.handleOverflow()83}8485// CursorStart moves the cursor to the start of the field86func (m *Model) CursorStart() {87 m.pos = 088 m.handleOverflow()89}9091// CursorEnd moves the cursor to the end of the field92func (m *Model) CursorEnd() {93 m.pos = len(m.value)94 m.handleOverflow()95}9697// Focused returns the focus state on the model98func (m Model) Focused() bool {99 return m.focus100}
11 unmodified lines
112}113114// Reset sets the input to its default state with no input.115func (m *Model) Reset() {77 m.Value = ""116 m.value = ""117 m.offset = 0118 m.pos = 0119}120121// If a max width is defined, perform some logic to treat the visible area122// as a horizontally scrolling viewport.123func (m *Model) handleOverflow() {124 if m.Width > 0 {125 overflow := max(0, len(m.value)-m.Width)126127 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}134135// colorText colorizes a given string according to the TextColor value of the136// model137func (m *Model) colorText(s string) string {138 return termenv.
19 unmodified lines
158// NewModel creates a new model with default settings159func 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,168169 value: "",170 focus: false,171 blink: true,172 pos: 0,173 }
12 unmodified lines
186 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 lines
232 m.blink = !m.blink233 return m, Blink(m)234 }235183 // 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()237238 return m, nil239}240
4 unmodified lines
245 return "could not perform assertion on model"246 }247248 // Placeholder text205 if m.Value == "" && m.Placeholder != "" {249 if m.value == "" && m.Placeholder != "" {250 return placeholderView(m)251 }252253 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.offset262263 v := m.colorText(value[:pos])264
85 unmodified lines