Skip to content
Myr

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

1 file+77 added−20 removed

modified:textinput/textinput.go+77 added-20 removed
1package textinput23import (4 "strings"5 "time"67 "github.com/charmbracelet/boba"8 "github.com/muesli/termenv"
25 unmodified lines
34 // CharLimit is the maximum amount of characters this input element will35 // accept. If 0 or less, there's no limit.36 CharLimit int3738 // 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 int4243 // Focus indicates whether user input focus should be on this input44 // component. When false, don't blink and ignore keyboard input.45 focus bool4647 // Cursor blink state48 blink bool4950 // Cursor position42 pos int51 pos int5253 // Used to emulate a viewport when width is set and the content is54 // overflowing55 offset int56}5758// Focused returns the focus state on the model59func (m Model) Focused() bool {
70 unmodified lines
130 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 }167168 case ErrMsg:169 m.Err = msg167 return m, nil170171 case BlinkMsg:172 m.blink = !m.blink173 return m, Blink(m)174 }175173 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 }186187 return m, nil188}189190// View renders the textinput in its current state191func View(model boba.Model) string {
6 unmodified lines
198 if m.Value == "" && m.Placeholder != "" {199 return placeholderView(m)200 }201190 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.offset211192 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])213214 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 }220221 // 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 }233234 return m.Prompt + v235}236237// placeholderView
18 unmodified lines
256257 return m.Prompt + v258}259224// cursorView style the cursor260// cursorView styles the cursor261func 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 lines
281 time.Sleep(model.BlinkSpeed)282 return BlinkMsg{}283 }284}285286func min(a, b int) int {287 if a < b {288 return a289 }290 return b291}292293func max(a, b int) int {294 if a > b {295 return a296 }297 return b298}