-
Notifications
You must be signed in to change notification settings - Fork 0
/
vt-clear.go
86 lines (69 loc) · 1.75 KB
/
vt-clear.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
package termios
import (
"strconv"
"strings"
)
func (vt *vt) clearScreen(c ClearType) error {
var err error
switch c {
case ClearToEnd:
_, err = vt.term.WriteString("\x1b[0J")
case ClearToStart:
_, err = vt.term.WriteString("\x1b[1J")
case ClearCompletely:
_, err = vt.term.WriteString("\x1b[2J")
default:
err = &InvalidClearTypeError{c}
}
if err != nil {
return &IOError{"clearing screen", err}
}
return nil
}
func (vt *vt) clearLine(c ClearType) error {
var err error
switch c {
case ClearToEnd:
_, err = vt.term.WriteString("\x1b[0K")
case ClearToStart:
_, err = vt.term.WriteString("\x1b[1K")
case ClearCompletely:
_, err = vt.term.WriteString("\x1b[2K")
default:
err = &InvalidClearTypeError{c}
}
if err != nil {
return &IOError{"clearing screen", err}
}
return nil
}
func (vt *vt) getPosition() (*Position, error) {
_, err := vt.term.Write([]byte{0x1b, 0x5b, 0x36, 0x6e, 0x0b})
if err != nil {
return nil, &IOError{"", err}
}
p := make([]byte, 32)
n, err := vt.term.readback(p)
if err != nil { //nolint:wsl // conflicts with gofumpt
return nil, &IOError{"reading current position", err}
}
if n < 6 {
return nil, &InvalidResponseError{"reading position", string(p[:n])}
}
if p[0] != 0x1b || p[1] != 0x5b || p[n-1] != 0x52 {
return nil, &InvalidResponseError{"reading position", string(p[:n])}
}
pos := strings.Split(string(p[2:n-1]), ";")
if len(pos) != 2 {
return nil, &InvalidResponseError{"reading position", string(p[:n])}
}
x, err := strconv.Atoi(pos[0])
if err != nil {
return nil, &InvalidResponseError{"reading position", string(p[:n])}
}
y, err := strconv.Atoi(pos[1])
if err != nil {
return nil, &InvalidResponseError{"reading position", string(p[:n])}
}
return &Position{x, y}, nil
}