-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.go
142 lines (116 loc) · 2.72 KB
/
functions.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
package termtools
import (
"fmt"
"golang.org/x/sys/unix"
)
// Cursor position manipulations
func getTermSize() (int, int, error) {
ws, err := unix.IoctlGetWinsize(0, unix.TIOCGWINSZ)
if err != nil {
return -1, -1, ErrUnknownTermSize
}
return int(ws.Col), int(ws.Row), nil
}
func moveCursorTo(x, y int) {
maxx, maxy, _ := getTermSize()
if x <= maxx && y <= maxy {
fmt.Printf(CursorGotoTemplate, y, x)
}
}
func moveCursorHome() {
fmt.Print(CursorHome)
}
func moveCursorUp(rows int) {
fmt.Printf(CursorMoveUpTemplate, rows)
}
func moveCursorDown(rows int) {
fmt.Printf(CursorMoveDownTemplate, rows)
}
func moveCursorLeft(columns int) {
fmt.Printf(CursorMoveLeftTemplate, columns)
}
func moveCursorRight(columns int) {
fmt.Printf(CursorMoveRightTemplate, columns)
}
func moveCursorToNextRow() {
fmt.Print(CursorMoveToNextRowTemplate)
}
func moveCursorToRow(row int) {
fmt.Printf(CursorMoveToRowTemplate, row)
}
func saveCursorPosition() {
fmt.Print(CursorSave)
}
func restoreCursorPosition() {
fmt.Print(CursorRestore)
}
// Color functions
func getColorCode(a interface{}) (string, error) {
switch a.(type) {
case int:
return getColorByID(a.(int))
case string:
return getColorByName(a.(string))
default:
return "", ErrUnknownColor
}
}
func getColorByName(colorname string) (string, error) {
if code, ok := colorMap[colorname]; ok {
return code, nil
}
return "", ErrUnknownColor
}
func getColorByID(id int) (string, error) {
if id >= 0 && id < 256 {
return fmt.Sprintf(ColorIDTemplate, id), nil
}
return "", ErrUnknownColor
}
func getBackgroundCode(a interface{}) (string, error) {
switch a.(type) {
case int:
return getBackgroundByID(a.(int))
case string:
return getBackgroundByName(a.(string))
default:
return "", ErrUnknownColor
}
}
func getBackgroundByName(colorname string) (string, error) {
if code, ok := backgroundMap[colorname]; ok {
return code, nil
}
return "", ErrUnknownColor
}
func getBackgroundByID(id int) (string, error) {
if id >= 0 && id < 256 {
return fmt.Sprintf(BackgroundIDTemplate, id), nil
}
return "", ErrUnknownColor
}
// Printing functions
func colorSprint(color interface{}, a ...interface{}) string {
code, err := getColorCode(color)
if err != nil {
return fmt.Sprint(a...)
}
return code + fmt.Sprint(a...) + Reset
}
func colorSprintf(color interface{}, format string, a ...interface{}) string {
code, err := getColorCode(color)
if err != nil {
return fmt.Sprintf(format, a...)
}
return fmt.Sprintf(code+format+Reset, a...)
}
func printAtPositionAndReturn(x, y int, a ...interface{}) {
saveCursorPosition()
moveCursorTo(x, y)
fmt.Print(a...)
restoreCursorPosition()
}
func printAtPosition(x, y int, a ...interface{}) {
moveCursorTo(x, y)
fmt.Print(a...)
}