github.com/jmigpin/editor@v1.6.0/util/testutil/sourcecursor.go (about) 1 package testutil 2 3 import ( 4 "fmt" 5 "strings" 6 ) 7 8 // Allows a src string to have multiple cursor strings to simulate cursor position. Used in testing. Useful cursor runes: "●". First n position is zero. 9 func SourceCursor(cursorStr, src string, n int) (string, int, error) { 10 // cursor positions 11 pos := []int{} 12 k := 0 13 for { 14 j := strings.Index(src[k:], cursorStr) 15 if j < 0 { 16 break 17 } 18 k += j 19 pos = append(pos, k) 20 k += len(cursorStr) 21 } 22 23 // nth position 24 if n >= len(pos) { 25 return "", 0, fmt.Errorf("nth index not found: n=%v, len=%v", n, len(pos)) 26 } 27 index := pos[n] 28 29 // adjust index with previous cursors that will be removed 30 index -= n * len(cursorStr) 31 // remove cursors 32 src2 := strings.Replace(src, cursorStr, "", -1) 33 34 return src2, index, nil 35 }