-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcursor.go
80 lines (75 loc) · 1.69 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
package main
import (
"fmt"
"github.com/charmbracelet/x/ansi"
)
//nolint:mnd
func handleCursor(p *ansi.Parser) (string, error) {
count := 1
if n, ok := p.Param(0, 1); ok && n > 0 {
count = n
}
cmd := p.Cmd()
isPrivate := cmd.Marker() == '?'
switch cmd.Command() {
case 'A':
// CUU - Cursor Up
return fmt.Sprintf("Cursor up %d", default1(count)), nil
case 'B':
// CUD - Cursor Down
return fmt.Sprintf("Cursor down %d", default1(count)), nil
case 'C':
// CUF - Cursor Forward
return fmt.Sprintf("Cursor right %d", default1(count)), nil
case 'D':
// CUB - Cursor Back
return fmt.Sprintf("Cursor left %d", default1(count)), nil
case 'E':
return fmt.Sprintf("Cursor next line %d", default1(count)), nil
case 'F':
return fmt.Sprintf("Cursor previous line %d", default1(count)), nil
case 'H':
row, col := 1, 1
if n, ok := p.Param(0, 1); ok && n > 0 {
row = n
}
if n, ok := p.Param(1, 1); ok && n > 0 {
col = n
}
return fmt.Sprintf("Set cursor position row=%[1]d col=%[2]d", row, col), nil
case 'n':
if count != 6 {
return "", errInvalid
}
if isPrivate {
return "Request extended cursor position", nil
}
return "Request cursor position", nil
case 's':
return "Save cursor position", nil
case 'u':
return "Restore cursor position", nil
case 'q':
return fmt.Sprintf("Set cursor style %s", descCursorStyle(count)), nil
}
return "", errUnhandled
}
//nolint:mnd
func descCursorStyle(i int) string {
switch i {
case 0, 1:
return "Blinking block"
case 2:
return "Steady block"
case 3:
return "Blinking underline"
case 4:
return "Steady underline"
case 5:
return "Blinking bar"
case 6:
return "Steady bar"
default:
return "Unknown"
}
}