-
Notifications
You must be signed in to change notification settings - Fork 0
/
printer.go
290 lines (252 loc) · 9.33 KB
/
printer.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
package termtools
import (
"errors"
"fmt"
"io"
)
var (
ErrFailedToSetColor = errors.New("error: failed to set printer color")
ErrFailedToSetBackground = errors.New("error: failed to set printer background")
)
// Printer holds color and style settings and implements most methods as in fmt module like Print,
// Println, Sprint etc. adding color and styles to the input values.
type Printer struct {
color string
background string
prefix string
suffix string
bold bool
underline bool
reversed bool
blinking bool
name string
}
// PrinterConfig describes configuration of Printer.
type PrinterConfig struct {
// Name identifies configuration when used
// with PrintSuite type. Name field is mandatory when passing PrinterConfig
// to PrintSuite Configure method!
//
// When setting up a Printer instance with NewPrinter() Name field may be omitted.
Name string
// Color and Backgrount fields may hold color name (as string) or color ID in range
// [0;255]. See package docs for list of available color names.
Color interface{}
Background interface{}
// Bold, Underline, Reversed, Blinking switch relevant printer modes on if set to true.
Bold bool
Underline bool
Reversed bool
Blinking bool
// Prefix and suffix are added to output if they are not empty strings.
Prefix string
Suffix string
}
// NewPrinter takes PrinterConfig and returns pointer to Printer.
// If invalid color(s) identifier is encountered the function returns
// instance of printer with color(s) not set. Otherwise the printer is functional.
// Check for errors to make sure that returned Printer is fully configured.
func NewPrinter(conf PrinterConfig) (p *Printer, err error) {
p = &Printer{}
// TODO: Probably rewrite two blocks below using unexported funcs.
p.bold, p.underline, p.reversed, p.blinking = conf.Bold, conf.Underline, conf.Reversed, conf.Blinking
p.prefix, p.suffix = conf.Prefix, conf.Suffix
if conf.Color != nil {
if err = p.SetColor(conf.Color); err != nil {
return
}
}
if conf.Background != nil {
if err = p.SetBackground(conf.Background); err != nil {
return
}
}
err = nil
return
}
// Printing methods
// Errorf formats according to a format specifier and returns the string as a value that satisfies error.
func (p *Printer) Errorf(format string, a ...interface{}) error {
out := p.processString(format)
return fmt.Errorf(out, a...)
}
// Fprint formats using the default formats for its operands and writes to w. Spaces are added between
// operands when neither is a string. It returns the number of bytes written and any write error encountered.
func (p *Printer) Fprint(w io.Writer, a ...interface{}) (n int, err error) {
out := p.processString(a...)
return fmt.Fprint(w, out)
}
// Fprintf formats according to a format specifier and writes to w. It returns the number of bytes written
// and any write error encountered.
func (p *Printer) Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) {
out := p.processString(format)
return fmt.Fprintf(w, out, a...)
}
// Fprintln formats using the default formats for its operands and writes to w.
// Spaces are always added between operands and a newline is appended. It returns the number of bytes written and any write error encountered.
func (p *Printer) Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
out := p.processString(a...)
return fmt.Fprintln(w, out)
}
// Print formats using the default formats for its operands and writes to standard output.
// Spaces are added between operands when neither is a string. It returns the number of bytes written and any write error encountered.
func (p *Printer) Print(a ...interface{}) (n int, err error) {
out := p.processString(a...)
return fmt.Print(out)
}
// Printf formats according to a format specifier and writes to standard output.
// It returns the number of bytes written and any write error encountered.
func (p *Printer) Printf(format string, a ...interface{}) (n int, err error) {
out := p.processString(format)
return fmt.Printf(out, a...)
}
// Println formats using the default formats for its operands and writes to standard output.
// Spaces are always added between operands and a newline is appended. It returns the number of bytes written and any write error encountered.
func (p *Printer) Println(a ...interface{}) (n int, err error) {
out := p.processString(a...)
return fmt.Println(out)
}
// Sprint formats using the default formats for its operands and returns the resulting string.
// Spaces are added between operands when neither is a string.
func (p *Printer) Sprint(a ...interface{}) string {
out := p.processString(a...)
return fmt.Sprint(out)
}
// Sprintf formats according to a format specifier and returns the resulting string.
func (p *Printer) Sprintf(format string, a ...interface{}) string {
out := p.processString(format)
return fmt.Sprintf(out, a...)
}
// Sprintln formats using the default formats for its operands and returns the resulting string.
// Spaces are always added between operands and a newline is appended.
func (p *Printer) Sprintln(a ...interface{}) string {
out := p.processString(a...)
return fmt.Sprintln(out)
}
// Color methods
// SetColor sets color of printer. Argument "color" must be either string or int.
// Valid color names are: "black", "blue", "cyan", "green", "magenta",
// "red", "white", "yellow", "brightblack", "brightblue", "brightcyan",
// "brightgreen", "brightmagenta", "brightred", "brightwhite", "brightyellow".
// Valid numbers are from 0 to 255 inclusive.
// If color is not known, is empty, or int is out of range the method
// will return an error and currently set Printer color will not be changed.
func (p *Printer) SetColor(color interface{}) error {
if code, err := getColorCode(color); err == nil {
p.color = code
return nil
}
return ErrFailedToSetColor
}
// SetBackground sets background color of printer. Argument color is the same as in SetColor method.
// will return an error and currently set Printer color will not be changed.
func (p *Printer) SetBackground(color interface{}) error {
if code, err := getBackgroundCode(color); err == nil {
p.background = code
return nil
}
return ErrFailedToSetBackground
}
// SetPrefixSuffix configures Printer to always preceed output with prefix
// and end output with suffix. Printer color and style settings apply to prefix and suffix.
func (p *Printer) SetPrefixSuffix(prefix, suffix string) {
p.prefix, p.suffix = prefix, suffix
}
// Modes Methods
// ToggleBold toggles bold mode of Printer
func (p *Printer) ToggleBold() {
p.bold = !p.bold
}
// ToggleUnderline toggles underline mode of Printer
func (p *Printer) ToggleUnderline() {
p.underline = !p.underline
}
// ToggleReversed toggles reverse mode of Printer
func (p *Printer) ToggleReversed() {
p.reversed = !p.reversed
}
// ToggleBlinking toggles blinking mode of Printer
func (p *Printer) ToggleBlinking() {
p.blinking = !p.blinking
}
// Reset resets printer state to initial state (no color, no background, bold, underline and reversed modes turned off).
func (p *Printer) Reset() {
p.color = ""
p.background = ""
p.bold = false
p.underline = false
p.reversed = false
p.blinking = false
p.prefix = ""
p.suffix = ""
}
// Methods implementing cursor movement
// PrintAtPosition moves cursor to specified column and row and issues Print
// It does not return to the initial position. It returns the number of bytes written and any write error encountered.
// See also PrintAtPositionAndReturn method.
func (p *Printer) PrintAtPosition(column, row int, a ...interface{}) (n int, err error) {
out := p.processString(a...)
moveCursorTo(column, row)
return fmt.Print(out)
}
// PrintAtPositionAndReturn moves cursor to specified column and row and issues Print
// then moves cursor to initial position when method was called. It returns the number of bytes written and any write error encountered.
func (p *Printer) PrintAtPositionAndReturn(column, row int, a ...interface{}) (n int, err error) {
out := p.processString(a...)
saveCursorPosition()
moveCursorTo(column, row)
defer restoreCursorPosition()
return fmt.Print(out)
}
// MoveTo places cursor at the specified column and row.
func (p *Printer) MoveTo(column, row int) {
moveCursorTo(column, row)
}
// MoveHome places cursor at the top left corner of the screen.
func (p *Printer) MoveHome() {
moveCursorHome()
}
// MoveUp moves cursor up specified number of rows.
func (p *Printer) MoveUp(rows int) {
moveCursorUp(rows)
}
// MoveDown moves cursor down specified number of rows.
func (p *Printer) MoveDown(rows int) {
moveCursorDown(rows)
}
// MoveLeft moves cursor left specified number of columns.
func (p *Printer) MoveLeft(columns int) {
moveCursorLeft(columns)
}
// MoveRight moves cursor right specified number of columns.
func (p *Printer) MoveRight(columns int) {
moveCursorRight(columns)
}
// MoveToNextRow moves cursor to the next row..
func (p *Printer) MoveToNextRow() {
moveCursorToNextRow()
}
// MoveToRow moves cursor to the specified row
func (p *Printer) MoveToRow(row int) {
moveCursorToRow(row)
}
func (p *Printer) processString(a ...interface{}) string {
out := p.color + p.background
if p.bold {
out += Bold
}
if p.underline {
out += Underline
}
if p.reversed {
out += Reversed
}
if p.blinking {
out += Blinking
}
if out == "" {
return p.prefix + fmt.Sprint(a...) + p.suffix
}
out += p.prefix + fmt.Sprint(a...) + p.suffix + Reset
return out
}