-
Notifications
You must be signed in to change notification settings - Fork 2
/
cursor.go
113 lines (88 loc) · 2 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// +build linux darwin
package goTerminal
import (
"fmt"
"os"
"os/exec"
"strconv"
"strings"
)
// A moves cursor up by n
func CursorUp(n int) {
fmt.Fprintf(Output, "%s[%dA", escape, n)
}
// moves cursor down by n
func CursorDown(n int) {
fmt.Fprintf(Output, "%s[%dB", escape, n)
}
// moves cursor right by n
func CursorRight(n int) {
fmt.Fprintf(Output, "%s[%dC", escape, n)
}
// moves cursor left by n
func CursorLeft(n int) {
fmt.Fprintf(Output, "%s[%dD", escape, n)
}
// Save cursor position and attributes
func SaveCursorAttrs() {
fmt.Fprintf(Output, "%s7", escape)
}
// Restore cursor position and attributes
func RestoreCursorAttrs() {
fmt.Fprintf(Output, "%s8", escape)
}
// Save cursor position
func SaveCursor() {
fmt.Fprintf(Output, "%s[s", escape)
}
// Restore cursor position
func RestoreCursor() {
fmt.Fprintf(Output, "%s[u", escape)
}
// Move to next line
func CursorNextLine() {
fmt.Fprintf(Output, "%sE", escape)
}
// moves cursor to column n
func CursorColumn(n int) {
fmt.Fprintf(Output, "%s[%dG", escape, n)
}
// Move cursor to screen location v,h
func CursorLineColumn(Line, Column int) {
fmt.Fprintf(Output, "%s[%d;%dH", escape, Line, Column)
}
// Move cursor to upper left corner
func CursorHome() {
fmt.Fprintf(Output, "%s[H", escape)
}
// Move cursor to upper left corner hvhome
func CursorLeftHome() {
fmt.Fprintf(Output, "%s[f", escape)
}
// CursorShow shows the cursor.
func CursorShow() {
fmt.Fprintf(Output, "%s[?25h", escape)
}
// CursorHide hide the cursor.
func CursorHide() {
fmt.Fprintf(Output, "%s[?25l", escape)
}
// Size returns the height and width of the terminal.
func Size() (*Coord, error) {
cmd := exec.Command("stty", "size")
cmd.Stdin = os.Stdin
out, err := cmd.Output()
if err != nil {
return nil, err
}
parts := strings.Split(string(out), " ")
x, err := strconv.Atoi(parts[0])
if err != nil {
return nil, err
}
y, err := strconv.Atoi(strings.Replace(parts[1], "\n", "", 1))
if err != nil {
return nil, err
}
return &Coord{Y: y, X: x}, nil
}