Support bounded width in text input
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 9fc0d0e · Browse files · 1 file·+77 added−20 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+77 added−20 removed
- other:+77 added−20 removed
Provenance
- Commit
3868858- Tree
e83c397- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
9fc0d0e
1 file changed+77 added−20 removed
modified:textinput/textinput.go+77 added-20 removed
1 package textinput2 3 import (4+ "strings"5 "time"6 7 "github.com/charmbracelet/boba"8 "github.com/muesli/termenv"25 unmodified lines34 // CharLimit is the maximum amount of characters this input element will35 // accept. If 0 or less, there's no limit.36 CharLimit int37 38+ // Width is the maximum number of characters that can be displayed at once.39+ // It essentially treats the text field like a horizontally scrolling40+ // viewport. If 0 or less this setting is ignored.41+ Width int42+43 // Focus indicates whether user input focus should be on this input44 // component. When false, don't blink and ignore keyboard input.45 focus bool46 47+ // Cursor blink state48 blink bool49+50+ // Cursor position42- pos int51+ pos int52+53+ // Used to emulate a viewport when width is set and the content is54+ // overflowing55+ offset int56 }57 58 // Focused returns the focus state on the model59 func (m Model) Focused() bool {70 unmodified lines130 if len(m.Value) > 0 {131 m.Value = m.Value[:m.pos-1] + m.Value[m.pos:]132 m.pos--133 }121- return m, nil134 case boba.KeyLeft:135 if m.pos > 0 {136 m.pos--137 }126- return m, nil138 case boba.KeyRight:139 if m.pos < len(m.Value) {140 m.pos++141 }131- return m, nil142 case boba.KeyCtrlF: // ^F, forward one character143 fallthrough144 case boba.KeyCtrlB: // ^B, back one charcter145 fallthrough146 case boba.KeyCtrlA: // ^A, go to beginning147 m.pos = 0138- return m, nil148 case boba.KeyCtrlD: // ^D, delete char under cursor149 if len(m.Value) > 0 && m.pos < len(m.Value) {150 m.Value = m.Value[:m.pos] + m.Value[m.pos+1:]151 }143- return m, nil152 case boba.KeyCtrlE: // ^E, go to end153 m.pos = len(m.Value)146- return m, nil154 case boba.KeyCtrlK: // ^K, kill text after cursor155 m.Value = m.Value[:m.pos]156 m.pos = len(m.Value)150- return m, nil157 case boba.KeyCtrlU: // ^U, kill text before cursor158 m.Value = m.Value[m.pos:]159 m.pos = 0154- return m, nil160+ m.offset = 0161 case boba.KeyRune: // input a regular character162 if m.CharLimit <= 0 || len(m.Value) < m.CharLimit {163 m.Value = m.Value[:m.pos] + string(msg.Rune) + m.Value[m.pos:]164 m.pos++165 }160- return m, nil161- default:162- return m, nil166 }167 168 case ErrMsg:169 m.Err = msg167- return m, nil170 171 case BlinkMsg:172 m.blink = !m.blink173 return m, Blink(m)174+ }175 173- default:174- return m, nil176+ // If a max width is defined, perform some logic to treat the visible area177+ // as a horizontally scrolling mini viewport.178+ if m.Width > 0 {179+ overflow := max(0, len(m.Value)-m.Width)180+ if overflow > 0 && m.pos < m.offset {181+ m.offset = max(0, min(len(m.Value), m.pos))182+ } else if overflow > 0 && m.pos >= m.offset+m.Width {183+ m.offset = max(0, m.pos-m.Width)184+ }185 }186+187+ return m, nil188 }189 190 // View renders the textinput in its current state191 func View(model boba.Model) string {6 unmodified lines198 if m.Value == "" && m.Placeholder != "" {199 return placeholderView(m)200 }201 190- v := m.colorText(m.Value[:m.pos])202+ left := m.offset203+ right := 0204+ if m.Width > 0 {205+ right = min(len(m.Value), m.offset+m.Width+1)206+ } else {207+ right = len(m.Value)208+ }209+ value := m.Value[left:right]210+ pos := m.pos - m.offset211 192- if m.pos < len(m.Value) {193- v += cursorView(string(m.Value[m.pos]), m)194- v += m.colorText(m.Value[m.pos+1:])212+ v := m.colorText(value[:pos])213+214+ if pos < len(value) {215+ v += cursorView(string(value[pos]), m) // cursor and text under it216+ v += m.colorText(value[pos+1:]) // text after cursor217 } else {218 v += cursorView(" ", m)219 }220+221+ // If a max width and background color were set fill the empty spaces with222+ // the background color.223+ if m.Width > 0 && len(m.BackgroundColor) > 0 && len(value) <= m.Width {224+ padding := m.Width - len(value)225+ if len(value)+padding <= m.Width && pos < len(value) {226+ padding++227+ }228+ v += strings.Repeat(229+ termenv.String(" ").Background(color(m.BackgroundColor)).String(),230+ padding,231+ )232+ }233+234 return m.Prompt + v235 }236 237 // placeholderView18 unmodified lines256 257 return m.Prompt + v258 }259 224-// cursorView style the cursor260+// cursorView styles the cursor261 func cursorView(s string, m Model) string {262 if m.blink {263+ if m.TextColor != "" || m.BackgroundColor != "" {264+ return termenv.String(s).265+ Foreground(color(m.TextColor)).266+ Background(color(m.BackgroundColor)).267+ String()268+ }269 return s270 }271 return termenv.String(s).272 Foreground(color(m.CursorColor)).273+ Background(color(m.BackgroundColor)).274 Reverse().275 String()276 }277 3 unmodified lines281 time.Sleep(model.BlinkSpeed)282 return BlinkMsg{}283 }284 }285+286+func min(a, b int) int {287+ if a < b {288+ return a289+ }290+ return b291+}292+293+func max(a, b int) int {294+ if a > b {295+ return a296+ }297+ return b298+}