-
Notifications
You must be signed in to change notification settings - Fork 3
/
cursor.go
159 lines (127 loc) · 2.87 KB
/
cursor.go
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package readline
import (
"os"
"regexp"
"strconv"
)
func leftMost() []byte {
fd := int(os.Stdout.Fd())
w, _, err := GetSize(fd)
if err != nil {
return []byte{'\r', '\n'}
}
b := make([]byte, w+1)
for i := 0; i < w; i++ {
b[i] = ' '
}
b[w] = '\r'
return b
}
var rxRcvCursorPos = regexp.MustCompile("^\x1b([0-9]+);([0-9]+)R$")
func (rl *Instance) getCursorPos() (x int, y int) {
if !rl.EnableGetCursorPos {
return -1, -1
}
disable := func() (int, int) {
os.Stderr.WriteString("\r\ngetCursorPos() not supported by terminal emulator, disabling....\r\n")
rl.EnableGetCursorPos = false
return -1, -1
}
print(seqGetCursorPos)
b := make([]byte, 64)
i, err := os.Stdin.Read(b)
if err != nil {
return disable()
}
if !rxRcvCursorPos.Match(b[:i]) {
return disable()
}
match := rxRcvCursorPos.FindAllStringSubmatch(string(b[:i]), 1)
y, err = strconv.Atoi(match[0][1])
if err != nil {
return disable()
}
x, err = strconv.Atoi(match[0][2])
if err != nil {
return disable()
}
return x, y
}
func moveCursorUp(i int) {
if i < 1 {
return
}
printf("\x1b[%dA", i)
}
func moveCursorDown(i int) {
if i < 1 {
return
}
printf("\x1b[%dB", i)
}
func moveCursorForwards(i int) {
if i < 1 {
return
}
printf("\x1b[%dC", i)
}
func moveCursorBackwards(i int) {
if i < 1 {
return
}
printf("\x1b[%dD", i)
}
func (rl *Instance) moveCursorToStart() {
posX, posY := lineWrapPos(rl.promptLen, rl.pos, rl.termWidth)
moveCursorBackwards(posX - rl.promptLen)
moveCursorUp(posY)
}
func (rl *Instance) moveCursorFromStartToLinePos() {
posX, posY := lineWrapPos(rl.promptLen, rl.pos, rl.termWidth)
moveCursorForwards(posX)
moveCursorDown(posY)
}
func (rl *Instance) moveCursorFromEndToLinePos() {
lineX, lineY := lineWrapPos(rl.promptLen, len(rl.line), rl.termWidth)
posX, posY := lineWrapPos(rl.promptLen, rl.pos, rl.termWidth)
moveCursorBackwards(lineX - posX)
moveCursorUp(lineY - posY)
}
// moveCursorToLinePos should only be used on extreme circumstances because it
// causes the cursor to jump around quite a bit
func (rl *Instance) moveCursorFromUnknownToLinePos() {
_, lineY := lineWrapPos(rl.promptLen, len(rl.line), rl.termWidth)
posX, posY := lineWrapPos(rl.promptLen, rl.pos, rl.termWidth)
//moveCursorBackwards(lineX)
print("\r")
moveCursorForwards(posX)
moveCursorUp(lineY - posY)
}
func (rl *Instance) moveCursorByAdjust(adjust int) {
oldX, oldY := lineWrapPos(rl.promptLen, rl.pos, rl.termWidth)
rl.pos += adjust
if rl.pos < 0 {
rl.pos = 0
}
if rl.pos > len(rl.line) {
rl.pos = len(rl.line)
}
if rl.modeViMode != vimInsert && rl.pos == len(rl.line) {
rl.pos--
}
newX, newY := lineWrapPos(rl.promptLen, rl.pos, rl.termWidth)
y := newY - oldY
switch {
case y < 0:
moveCursorUp(-y)
case y > 0:
moveCursorDown(y)
}
x := newX - oldX
switch {
case x < 0:
moveCursorBackwards(-x)
case x > 0:
moveCursorForwards(x)
}
}