-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathansiout.go
340 lines (277 loc) · 4.76 KB
/
ansiout.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**
*
* ANSI Output library.
* For creating interesting CLI apps.
*
* Author: Timothy Lorens ([email protected])
* http://www.cyberdyne.org/~icebrkr
*
*/
package ansiout
import (
"fmt"
"bufio"
"os"
"math"
"strings"
"strconv"
"time"
"github.com/tlorens/keyboard"
)
const ESC = "\033["
// Storage variables for preventing echoing of color esc
// sequences multiple times if the colors haven't changed.
var curFg int = 7 // Default forground color
var curBg int = 0 // Default background color
/**
*
* Reset text color to terminal default.
*
*/
func ColorReset() {
fmt.Print(ESC + "0m")
}
/**
*
* Private method to clear the terminal
*
* integer i
* 0 Clear from cursor to end of screen;
* 1 Clear from cursor to beginning of screen
* 2 Clear entire screen and home cursor (1,1) (ANSI.SYS)
*/
func clear(i int) {
fmt.Printf(ESC + "%dJ", i)
}
func clearline(i int) {
fmt.Printf(ESC + "%dK", i)
}
/**
*
* Clear entire screen and home cursor (1,1)
*
* Since ^[[2J doesn't work in bash/linux.
*
*/
func ClearScr() {
clear(2)
GotoXY(1,1)
}
/**
*
* Clears entire line.
*
*/
func ClearLine() {
clearline(2)
}
/**
*
* Put the cursor at X, Y coordinates
*
*/
func GotoXY(x int, y int) {
fmt.Printf(ESC + "%d;%dH", x, y)
}
/**
*
* Get / Report cursors current postition.
*
* @FIXME: Refactor this garbage.
*
*/
func CursorXY() (int, int) {
var ch int
var line string
var ret [] int
fmt.Print(ESC + "6n")
for ch != 'R' {
ch = keyboard.RawKey()
if ch != 27 && ch != '[' {
ret = append(ret, ch)
}
}
for _, value := range ret {
line += string(int(value))
}
line = strings.TrimRight(line, "R")
parts := strings.Split(line, ";")
X, err := strconv.Atoi(parts[0])
if err == nil {
}
Y, err := strconv.Atoi(parts[1])
if err == nil {
}
return X, Y
}
/**
*
* Move cursor up i number of lins
*
*/
func CursorUp(i int) {
fmt.Printf(ESC + "%dA", i)
}
/**
*
* Move cursor down i number of lines.
*
*/
func CursorDn(i int) {
fmt.Printf(ESC + "%dB", i)
}
/**
*
* Move cursor right i number of columns
*
*/
func CursorRt(i int) {
fmt.Printf(ESC + "%dC", i)
}
/**
*
* Move cursor left i number of columns
*
*/
func CursorLf(i int) {
fmt.Printf(ESC + "%dD", i)
}
/**
*
* Save cursors X,Y coordinates
*
*/
func CursorSave() {
fmt.Print(ESC + "s")
}
/**
*
* Restore / Goto saved cursor coordinates.
*
*/
func CursorRestore() {
fmt.Print(ESC + "u")
}
/**
*
* Short-cut to print a string is specified forecolor/backcolor.
*
* integer f Forground color value 0-15
* integer b background color value 0-7
* string s String to print
*
*/
func Print(f int, b int, s string) {
Color(f, b)
fmt.Printf("%s", s)
}
/**
*
* Print a string at X, Y coordinates
*
* integer x X coordinate
* integer y Y coordinate
* string s Message to print
*
*/
func PrintXY(x int, y int, s string) {
GotoXY(x,y)
fmt.Printf("%s", s)
}
/**
*
* Short-cut to print a string is specified X,Y coordinate with forecolor/backcolor.
*
* integer f Forground color value 0-15
* integer b background color value 0-7
* integer x X coordinate
* integer y Y coordinate
* string s String to print
*
*/
func PrintColorXY(f int, b int, x int, y int, s string) {
Color(f, b)
PrintXY(x, y, s)
}
/**
*
* Set forground and background color.
*
* integer f Forground color value 0-15
* integer b background color value 0-7
*
*/
func Color(f int, b int) {
var tmp string
var colors = [8]int {0,4,2,6,1,5,3,7}
// Prevent sending color codes if they haven't changed.
if curFg != f || curBg != b {
curFg = f
curBg = b
fg := colors[int(math.Mod(float64(f), 8))] + 30
bg := colors[int(math.Mod(float64(b), 8))] + 40
if b > 7 {
tmp += "5;"
} else {
tmp += "0;"
}
if f > 7 {
tmp += "1;"
}
fmt.Printf(ESC + "%s%d;%dm", tmp, fg, bg)
}
}
/**
*
* Silly whirly-cursor progress indicator.
*
*/
func Wait(i int) {
for x:=1; x < i*5; x++ {
fmt.Print("|")
time.Sleep(50 * time.Millisecond)
fmt.Print("\010/")
time.Sleep(50 * time.Millisecond)
fmt.Print("\010-")
time.Sleep(50 * time.Millisecond)
fmt.Print("\010\\")
time.Sleep(50 * time.Millisecond)
fmt.Print("\010")
}
}
/**
*
* Private method to read strings from files.
*
*/
func readln(r *bufio.Reader) (string, error) {
var (isPrefix bool = true
err error = nil
line, ln []byte
)
for isPrefix && err == nil {
line, isPrefix, err = r.ReadLine()
ln = append(ln, line...)
}
return string(ln),err
}
/**
*
* Print a file to the screen.
*
*/
func PrintFile(filename string) int {
f, err := os.Open(filename)
if err != nil {
fmt.Printf("error opening file: %v\n",err)
return -1
}
r := bufio.NewReader(f)
s, e := readln(r)
for e == nil {
fmt.Println(s)
s,e = readln(r)
}
fmt.Print("\n")
return 1
}