Skip to content

Commit

Permalink
Improve word boundary algorithm to ignore previous spaces so that con…
Browse files Browse the repository at this point in the history
…trol+arrow-keys will skip over repeated spaces
  • Loading branch information
ddworken committed Feb 22, 2024
1 parent c3f9ba3 commit 67126d8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
12 changes: 10 additions & 2 deletions client/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
16 changes: 16 additions & 0 deletions client/tui/tui_test.go
Original file line number Diff line number Diff line change
@@ -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 "))
}

0 comments on commit 67126d8

Please sign in to comment.