-
Notifications
You must be signed in to change notification settings - Fork 1
/
cursor.js
74 lines (69 loc) · 1.42 KB
/
cursor.js
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
module.exports = {
updateCursor ({ x, y }, { cols, rows }, seq) {
const { code, text, params = [1] } = seq
// TODO: Test that default count is 1
const count = params[0] || 1
const tabWidth = 8
if (text) {
return { x: x + text.length, y }
}
switch (code) {
case 'HVP':
case 'CUP':
return {
y: (params[0] || 1) - 1,
x: (params[1] || 1) - 1
}
case 'CR':
return {
y,
x: 0
}
case 'HTS':
return {
y,
x: tabWidth + x - (x % tabWidth)
}
case 'CNL':
return {
y: y + count,
x: 0
}
case 'CHA':
return {
y,
x: params[0] - 1
}
case 'NL':
return {
y: y + 1,
x
}
case 'RI':
return {
y: y - 1,
x
}
case 'BS':
return {
y,
x: x - 1
}
}
if (code === 'CUU' || code === 'CUD' || code === 'CUF' || code === 'CUB') {
let dx = 0
let dy = 0
switch (code) {
case 'CUU': dy = -count; break
case 'CUD': dy = count; break
case 'CUF': dx = count; break
case 'CUB': dx = -count; break
}
return {
x: Math.max(Math.min(x + dx, cols), 0),
y: Math.max(Math.min(y + dy, rows), 0)
}
}
return { y, x }
}
}