-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterminal.go
More file actions
139 lines (110 loc) · 2.41 KB
/
terminal.go
File metadata and controls
139 lines (110 loc) · 2.41 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"fmt"
"os"
"os/exec"
"strconv"
"strings"
)
var TCONF_RAW = []string{
"-F", "/dev/tty", "-icanon", "-echo",
"min", "0", "time", "0",
"-isig", "-ixon",
}
var TCONF_NORMAL = []string{
"-F", "/dev/tty", "icanon", "echo",
}
type Terminal struct {
width, height int
size Vector2d
}
func NewTerminal() *Terminal {
return &Terminal{}
}
// GLOBAL METHODS
func CommandOutput(name string, arg ...string) string {
cmd := exec.Command(name, arg...)
cmd.Stdin = os.Stdin
out, err := cmd.Output()
if err != nil {
panic(err)
}
return string(out)
}
func GetTerminalSize() (int, int) {
output := CommandOutput("stty", "size")
output = strings.TrimSpace(output)
size := strings.Split(output, " ")
rows, _ := strconv.Atoi(size[0])
cols, _ := strconv.Atoi(size[1])
return cols, rows
}
func ReadRaw() []byte {
var b []byte = make([]byte, 4096)
os.Stdin.Sync()
n, err := os.Stdin.Read(b)
if err != nil {
return []byte{}
}
return b[:n]
}
// SETTINGS
func (t *Terminal) CursorVisibility(b bool) {
if b {
fmt.Print(CURSOR_SHOW)
} else {
fmt.Print(CURSOR_HIDE)
}
}
func (t *Terminal) LineWrap(b bool) {
if b {
fmt.Print(LINE_WRAP)
} else {
fmt.Print(NO_LINE_WRAP)
}
}
// CONFIGURE
func (t *Terminal) init() {
width, height := GetTerminalSize()
t.width, t.height = width, height
t.size = Vector2d{width, height}
}
func (t *Terminal) configure() {
exec.Command("stty", TCONF_RAW...).Run()
t.CursorVisibility(false)
}
func (t *Terminal) restore() {
exec.Command("stty", TCONF_NORMAL...).Run()
t.CursorVisibility(true)
}
// PRIMITIVES
func (t *Terminal) color(r, g, b int) string {
return NUMS[r] + SEP + NUMS[g] + SEP + NUMS[b]
}
func (t *Terminal) foreground(r, g, b int) string {
return FOREGROUND + t.color(r, g, b) + SEP + "m"
}
func (t *Terminal) background(r, g, b int) string {
return BACKGROUND + t.color(r, g, b) + SEP + "m"
}
func (t *Terminal) reset() string {
return RESET
}
func (t *Terminal) pos(x, y int) string {
return CSI + NUMS[y] + SEP + NUMS[x] + "H"
}
func (t *Terminal) pos2d(p Vector2d) string {
return CSI + NUMS[p.y] + SEP + NUMS[p.x] + "H"
}
func (t *Terminal) cursorUp(n int) string {
return CSI + NUMS[n] + "A"
}
func (t *Terminal) cursorDown(n int) string {
return CSI + NUMS[n] + "B"
}
func (t *Terminal) cursorForward(n int) string {
return CSI + NUMS[n] + "C"
}
func (t *Terminal) cursorBackward(n int) string {
return CSI + NUMS[n] + "D"
}