Skip to content
Myr

Add alt+left/right and alt+b/f to jump between words 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 bb85a88 · Browse files · 1 file+74 added−0 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+74 added−0 removed

  • other:+74 added−0 removed

1 file+74 added−0 removed

modified:textinput/textinput.go+74 added
1 unmodified line
23import (4 "strings"5 "time"6 "unicode"78 "github.com/charmbracelet/boba"9 "github.com/muesli/termenv"10)
146 unmodified lines
157 Background(color(m.BackgroundColor)).158 String()159}160161func (m *Model) wordLeft() {162 if m.pos == 0 || len(m.value) == 0 {163 return164 }165166 i := m.pos - 1167168 for i >= 0 {169 if unicode.IsSpace(rune(m.value[i])) {170 m.pos--171 i--172 } else {173 break174 }175 }176177 for i >= 0 {178 if !unicode.IsSpace(rune(m.value[i])) {179 m.pos--180 i--181 } else {182 break183 }184 }185}186187func (m *Model) wordRight() {188 if m.pos >= len(m.value) || len(m.value) == 0 {189 return190 }191192 i := m.pos193194 for i < len(m.value) {195 if unicode.IsSpace(rune(m.value[i])) {196 m.pos++197 i++198 } else {199 break200 }201 }202203 for i < len(m.value) {204 if !unicode.IsSpace(rune(m.value[i])) {205 m.pos++206 i++207 } else {208 break209 }210 }211}212213// BlinkMsg is sent when the cursor should alternate it's blinking state.214type BlinkMsg struct{}215216// NewModel creates a new model with default settings.
31 unmodified lines
248 m.value = m.value[:m.pos-1] + m.value[m.pos:]249 m.pos--250 }251 case boba.KeyLeft:252 if msg.Alt { // alt+left arrow, back one word253 m.wordLeft()254 break255 }256 if m.pos > 0 {257 m.pos--258 }259 case boba.KeyRight:260 if msg.Alt { // alt+right arrow, forward one word261 m.wordRight()262 break263 }264 if m.pos < len(m.value) {265 m.pos++266 }267 case boba.KeyCtrlF: // ^F, forward one character
15 unmodified lines
283 m.value = m.value[m.pos:]284 m.pos = 0285 m.offset = 0286 case boba.KeyRune: // input a regular character287288 if msg.Alt {289 if msg.Rune == 'b' { // alt+b, back one word290 m.wordLeft()291 break292 }293 if msg.Rune == 'f' { // alt+f, forward one word294 m.wordRight()295 break296 }297 }298299 // Input a regular character300 if m.CharLimit <= 0 || len(m.value) < m.CharLimit {301 m.value = m.value[:m.pos] + string(msg.Rune) + m.value[m.pos:]302 m.pos++303 }
124 unmodified lines