-
Notifications
You must be signed in to change notification settings - Fork 0
/
ansiterm.go
executable file
·146 lines (129 loc) · 2.77 KB
/
ansiterm.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
// +build linux,386
package colorterm
import (
"fmt"
"os"
"strconv"
)
const (
COLOR_BLACK = 0
COLOR_RED = 1
COLOR_GREEN = 2
COLOR_BROWN = 3
COLOR_BLUE = 4
COLOR_PURPLE = 5
COLOR_CYAN = 6
COLOR_LGREY = 7
COLOR_DGRAY = 8
COLOR_LRED = 9
COLOR_LGREEN = 10
COLOR_YELLOW = 11
COLOR_LBLUE = 12
COLOR_LPURPLE = 13
COLOR_LCYAN = 14
COLOR_WHITE = 15
COLOR_MAX = COLOR_WHITE + 1
)
var _Code = map[string]string{
"escape": "\033[",
"reset": "\033[0m",
"bold": "\033[01m",
"clear": "\033[2J",
"clear_eol": "\033[K",
"gotoxy": "\033[%d;%dH",
"move_up": "\033[%dA",
"move_down": "\033[%dB",
"move_right": "\033[%dC",
"move_left": "\033[%dD",
"save": "\033[s",
"restore": "\033[u",
"title": "\033]1;\007\033]2;%s\007",
}
var _ColorFg = map[int]string{
0: "30m",
1: "31m",
2: "32m",
3: "33m",
4: "34m",
5: "35m",
6: "36m",
7: "37m",
8: "1;30m",
9: "1;31m",
10: "1;32m",
11: "1;33m",
12: "1;34m",
13: "1;35m",
14: "1;36m",
15: "1;37m",
}
var _ColorBg = map[int]string{
0: "40m",
1: "41m",
2: "42m",
3: "43m",
4: "44m",
5: "45m",
6: "46m",
7: "47m",
}
type ColorTerm struct {
stdout *os.File
stderr *os.File
}
func NewColorTerminal() *ColorTerm {
term := new(ColorTerm)
term.stdout = os.Stdout
term.stderr = os.Stderr
return term
}
func (term *ColorTerm) Reset() {
term.stdout.WriteString(_Code["reset"])
}
func (term *ColorTerm) ClearScreen() {
term.stdout.WriteString(_Code["clear"])
term.stdout.Sync()
}
func (term *ColorTerm) Columns() uint16 {
var cols uint16 = 0
env := os.Getenv("COLUMNS")
if env != "" {
c, _ := strconv.ParseUint(env, 10, 64)
cols = uint16(c)
}
return cols
}
func (term *ColorTerm) Lines() uint16 {
var cols uint16 = 0
env := os.Getenv("LINES")
if env != "" {
c, _ := strconv.ParseUint(env, 10, 64)
cols = uint16(c)
}
return cols
}
func (term *ColorTerm) SetTitle(title string) {
t := os.Getenv("TERM")
if t == "xterm" || t == "Eterm" || t == "aterm" || t == "rxvt" || t == "xterm-color" {
term.stderr.WriteString(fmt.Sprintf(_Code["title"], title))
}
}
func (term *ColorTerm) SetPosition(x, y uint16) {
term.stdout.WriteString(fmt.Sprintf(_Code["gotoxy"], x, y))
}
func (term *ColorTerm) SetColor(fg, bg uint16) {
term.SetBgColor(bg)
term.SetTextColor(fg)
}
func (term *ColorTerm) SetBgColor(bg uint16) {
if bg < COLOR_MAX {
term.stdout.WriteString(_Code["escape"] + _ColorBg[int(bg)%len(_ColorBg)])
term.stdout.Sync()
}
}
func (term *ColorTerm) SetTextColor(fg uint16) {
if fg < COLOR_MAX {
term.stdout.WriteString(_Code["escape"] + _ColorFg[int(fg)])
term.stdout.Sync()
}
}