Improve word boundary algorithm to ignore previous spaces so that control+arrow-keys will skip over repeated spaces

This commit is contained in:
David Dworken 2024-02-21 22:16:58 -08:00
parent a91f1ca793
commit f138f4cf9c
No known key found for this signature in database
2 changed files with 26 additions and 2 deletions

View File

@ -454,12 +454,20 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func calculateWordBoundaries(input string) []int {
ret := make([]int, 0)
ret = append(ret, 0)
prevWasBreaking := false
for idx, char := range input {
if char == ' ' || char == '-' {
ret = append(ret, idx)
if !prevWasBreaking {
ret = append(ret, idx)
}
prevWasBreaking = true
} else {
prevWasBreaking = false
}
}
ret = append(ret, len(input))
if !prevWasBreaking {
ret = append(ret, len(input))
}
return ret
}

16
client/tui/tui_test.go Normal file
View File

@ -0,0 +1,16 @@
package tui
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestCalculateWordBoundaries(t *testing.T) {
require.Equal(t, []int{0, 3}, calculateWordBoundaries("foo"))
require.Equal(t, []int{0, 3, 7}, calculateWordBoundaries("foo bar"))
require.Equal(t, []int{0, 3, 7}, calculateWordBoundaries("foo-bar"))
require.Equal(t, []int{0, 3, 7, 11}, calculateWordBoundaries("foo-bar baz"))
require.Equal(t, []int{0, 3, 10, 16}, calculateWordBoundaries("foo-- -bar - baz"))
require.Equal(t, []int{0, 3}, calculateWordBoundaries("foo "))
}