-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcursor.go
70 lines (56 loc) · 1.15 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
package termite
import (
"fmt"
"io"
)
// Cursor represents a terminal cursor
type Cursor interface {
Position(row, col int)
Up(l int)
Down(l int)
Forward(cols int)
Backward(cols int)
SavePosition()
RestorePosition()
Hide()
Show()
}
type cursor struct {
writer io.Writer
}
// NewCursor returns a new cursor for the specified terminal
func NewCursor(writer io.Writer) Cursor {
return cursor{
writer: writer,
}
}
func (c cursor) Position(row, col int) {
c.writeString(fmt.Sprintf("\033[%d;%dH", row, col))
}
func (c cursor) Up(lines int) {
c.writeString(fmt.Sprintf("\033[%dA", lines))
}
func (c cursor) Down(lines int) {
c.writeString(fmt.Sprintf("\033[%dB", lines))
}
func (c cursor) Forward(cols int) {
c.writeString(fmt.Sprintf("\033[%dC", cols))
}
func (c cursor) Backward(cols int) {
c.writeString(fmt.Sprintf("\033[%dD", cols))
}
func (c cursor) Hide() {
c.writeString("\033[?25l")
}
func (c cursor) Show() {
c.writeString("\033[?25h")
}
func (c cursor) SavePosition() {
c.writeString("\033[s")
}
func (c cursor) RestorePosition() {
c.writeString("\033[u")
}
func (c cursor) writeString(s string) {
io.WriteString(c.writer, s)
}