-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.go
62 lines (48 loc) · 1.03 KB
/
display.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
package main
import (
"fmt"
"termsnake/term"
)
const ESC = "\x1b"
type Display struct {
term.Terminal
}
func (d *Display) Size() (width, height int, err error) {
width, height, err = d.Terminal.Size()
if err != nil {
return
}
width = width / 2
return
}
func (d *Display) MoveTo(x, y int) error {
x = (x-1)*2 + 1
return d.PrintfCode("[%v;%vH", y, x)
}
func (d *Display) Clear() error {
defer d.Flush()
return d.PrintfCode("[2J")
}
func (d *Display) EnableAlternativeBuffer() error {
defer d.Flush()
return d.PrintfCode("[?1049h")
}
func (d *Display) DisableAlternativeBuffer() error {
defer d.Flush()
return d.PrintfCode("[?1049l")
}
func (d *Display) HideCursor() error {
defer d.Flush()
return d.PrintfCode("[?25l")
}
func (t *Display) ShowCursor() error {
defer t.Flush()
return t.PrintfCode("[?25h")
}
func (t *Display) PrintfCode(format string, a ...any) error {
return t.Printf(ESC+format, a...)
}
func (t Display) Printf(format string, a ...any) error {
_, err := fmt.Fprintf(t, format, a...)
return err
}