-
Notifications
You must be signed in to change notification settings - Fork 1
/
terminal.go
241 lines (203 loc) · 5.71 KB
/
terminal.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
// 2014 Iain Shigeoka - BSD license (see LICENSE)
package cli
import (
"fmt"
"os"
"reflect"
"strings"
)
func NewTerminal(program *Program) *Terminal {
return &Terminal{Program: program, IndentSize: 2}
}
type Terminal struct {
Program *Program // The program this terminal belongs to
Indent uint // Current ident level for stdout statements
IndentSize uint // Number of spaces to indent stdout statements
}
// -------------------------------------------
// Identing support for log-style output
// -------------------------------------------
// Increase the indent level
func (t *Terminal) PushIndent() *Terminal {
t.Indent++
return t
}
// Decrease the indent level
func (t *Terminal) PopIndent() *Terminal {
if t.Indent > 0 {
t.Indent--
}
return t
}
// Reduce indent level to zero
func (t *Terminal) PopIndents() *Terminal {
t.Indent = 0
return t
}
// -------------------------------------------
// Simple, log-style output
// -------------------------------------------
// Outputs the provided message only if the program is in verbose mode
func (t *Terminal) Verbose(msg string) {
t.printIndent()
fmt.Println(msg)
}
// Outputs the provided message only if the program is in verbose mode
func (t *Terminal) Verbosef(format string, data ...interface{}) {
t.printIndent()
fmt.Println(fmt.Sprintf(format, data...))
}
// Outputs the provided message
func (t *Terminal) Info(msg string) {
t.printIndent()
fmt.Println(msg)
}
// Outputs the provided message
func (t *Terminal) Infof(format string, data ...interface{}) {
t.printIndent()
fmt.Printf(format, data...)
fmt.Println()
}
// Outputs the provided error message and exits the program with an error code
func (t *Terminal) Fatal(msg string) {
// TODO pretty print the error(s) if exists
fmt.Fprintln(os.Stderr, msg)
os.Exit(1)
}
// Outputs the provided message
func (t *Terminal) Fatalf(format string, data ...interface{}) {
fmt.Fprintf(os.Stderr, format, data...)
fmt.Fprintln(os.Stderr)
os.Exit(1)
}
// Outputs the provided error message and exits the program with an error code
// only if the provided error is non-nil. If the program is in verbose mode,
// the error itself is dumped.
func (t *Terminal) Error(err error, msg string) {
if err != nil {
if !reflect.ValueOf(err).IsNil() {
if msg != "" {
fmt.Fprintln(os.Stderr, msg)
}
t.printError(err)
}
}
}
// Outputs the provided message
func (t *Terminal) Errorf(err error, format string, data ...interface{}) {
if err != nil {
if !reflect.ValueOf(err).IsNil() {
if format != "" {
fmt.Fprintf(os.Stderr, format, data...)
fmt.Fprintln(os.Stderr)
}
t.printError(err)
}
}
}
// -------------------------------------------
// Chainable i/o
// -------------------------------------------
// Prints characters to the screen (ignores indent and does not append nl)
func (t *Terminal) Print(format string, data ...interface{}) *Terminal {
if len(data) > 0 {
fmt.Printf(format, data...)
} else {
fmt.Print(format)
}
return t
}
func (t *Terminal) Nl(a ...int) *Terminal {
length := 1
if len(a) > 0 {
length = a[0]
}
for i := 0; i < length; i++ {
t.Print("\n")
}
return t
}
// -------------------------------------------
// Cursor instructions
// -------------------------------------------
// Clears the entire screen of text and sets the cursor at the top left of the screen.
func (t *Terminal) Clear() *Terminal {
return t.Print("\033[2J")
}
// Clears the current line of text.
func (t *Terminal) ClearLine() *Terminal {
return t.Print("\033[2K")
}
// Moves cursor to the absolute coordinates x, y. Values are 1-based and default to top left corner of the screen.
func (t *Terminal) Move(x, y int) *Terminal {
return t.Print("\033[%d;%dH", x, y)
}
// Moves cursor 'x' cells up. If the edge of the screen is reached, does nothing.
func (t *Terminal) Up(x int) *Terminal {
return t.Print("\033[%dA", x)
}
// Moves cursor 'x' cells dwn. If the edge of the screen is reached, does nothing.
func (t *Terminal) Down(x int) *Terminal {
return t.Print("\033[%dB", x)
}
// Moves cursor 'x' cells to the left. If the edge of the screen is reached, does nothing.
func (t *Terminal) Left(x int) *Terminal {
return t.Print("\033[%dD", x)
}
// Moves cursor 'x' cells to the right. If the edge of the screen is reached, does nothing.
func (t *Terminal) Right(x int) *Terminal {
return t.Print("\033[%dC", x)
}
// Move the cursor to the beginning of the line "x" lines down.
func (t *Terminal) NextLine(x int) *Terminal {
return t.Print("\033[%dE", x)
}
// Move the cursor to the beginning of the line "x" lines up.
func (t *Terminal) PreviousLine(x int) *Terminal {
return t.Print("\033[%dF", x)
}
// Hide the cursor
func (t *Terminal) Hide() *Terminal {
return t.Print("\033[?25h")
}
// Show the cursor
func (t *Terminal) Show() *Terminal {
return t.Print("\033[?25l")
}
// -------------------------------------------
// Color
// -------------------------------------------
const (
Black = iota
Red
Green
Yellow
Blue
Magenta
Cyan
White
)
func (t *Terminal) Color(foreground, background int) *Terminal {
return t.Print("\033[3%dm;4%dm;", foreground, background)
}
// Reset terminal attributes (including colors) to default values.
func (t *Terminal) Reset() *Terminal {
return t.Print("\033[0m")
}
// -------------------------------------------
// Helpers
// -------------------------------------------
// Prints stdout indents if necessary
func (t *Terminal) printIndent() {
if t.Indent > 0 {
fmt.Print(strings.Repeat(" ", (int)(t.Indent*t.IndentSize)))
}
}
// Pretty prints the error in error messages
func (t *Terminal) printError(err error) {
// if p.verbose {
fmt.Fprintf(os.Stderr, "\nError: %#v\n", err)
// }
// TODO if err has an error code, use that for the exit code
os.Exit(1)
}