-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_cursor.go
More file actions
37 lines (35 loc) · 775 Bytes
/
input_cursor.go
File metadata and controls
37 lines (35 loc) · 775 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main
// wrappedCursorPos converts a plain text cursor position to the equivalent
// index in a wrapped string that may contain newline characters.
func wrappedCursorPos(text string, plain int) int {
rs := []rune(text)
plainCount := 0
for i, r := range rs {
if r == '\n' {
continue
}
if plainCount == plain {
return i
}
plainCount++
}
return len(rs)
}
// plainCursorPos converts a cursor index in a wrapped string back to the
// position in the underlying plain text (with newlines removed).
func plainCursorPos(text string, wrapped int) int {
rs := []rune(text)
if wrapped < 0 {
wrapped = 0
}
if wrapped > len(rs) {
wrapped = len(rs)
}
plain := 0
for i := 0; i < wrapped; i++ {
if rs[i] != '\n' {
plain++
}
}
return plain
}