mirror of
https://github.com/ddworken/hishtory.git
synced 2025-06-20 03:47:54 +02:00
Improve word boundary algorithm to ignore previous spaces so that control+arrow-keys will skip over repeated spaces
This commit is contained in:
parent
a91f1ca793
commit
f138f4cf9c
@ -454,12 +454,20 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
func calculateWordBoundaries(input string) []int {
|
func calculateWordBoundaries(input string) []int {
|
||||||
ret := make([]int, 0)
|
ret := make([]int, 0)
|
||||||
ret = append(ret, 0)
|
ret = append(ret, 0)
|
||||||
|
prevWasBreaking := false
|
||||||
for idx, char := range input {
|
for idx, char := range input {
|
||||||
if char == ' ' || char == '-' {
|
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
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
16
client/tui/tui_test.go
Normal file
16
client/tui/tui_test.go
Normal 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 "))
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user