-
Notifications
You must be signed in to change notification settings - Fork 6
/
opts.go
155 lines (139 loc) · 3.68 KB
/
opts.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
package bar
// this package supports the functional opts style for incrementally
// adding customization options when creating a new instance - you can
// read more about this here: https://halls-of-valhalla.org/beta/articles/functional-options-pattern-in-go,54/
import (
"fmt"
"time"
)
type barOpts struct {
total, width int
start, end string
complete, head, incomplete string
formatString string
callback func()
output Output
context Context
debug bool
}
type augment func(*barOpts)
// NewWithFormat creates a new instance of bar.Bar with the given total
// and format and returns a reference to it
func NewWithFormat(t int, f string) *Bar {
return &Bar{
progress: 0,
total: t,
width: 20,
start: "(",
complete: "█",
head: "█",
incomplete: " ",
end: ")",
closed: false,
startedAt: time.Now(),
rate: 0,
formatString: f,
format: tokenize(f, nil),
callback: noop,
output: initializeStdout(),
}
}
// NewWithOpts creates a new instance of bar.Bar with the provided options
// and returns a reference to it
func NewWithOpts(opts ...func(o *barOpts)) *Bar {
o := &barOpts{
start: "(",
complete: "█",
head: "█",
incomplete: " ",
end: ")",
formatString: defaultFormat,
callback: noop,
output: initializeStdout(),
}
for _, aug := range opts {
aug(o)
}
if o.width <= 0 {
panic(fmt.Sprintf("a bar may not have a zero or negative width (received: %d)", o.width))
}
return &Bar{
progress: 0,
total: o.total,
width: o.width,
start: o.start,
complete: o.complete,
head: o.head,
incomplete: o.incomplete,
end: o.end,
closed: false,
startedAt: time.Now(),
rate: 0,
formatString: o.formatString,
format: tokenize(o.formatString, o.context.customVerbs()),
callback: o.callback,
output: o.output,
context: o.context,
debug: o.debug,
}
}
// WithDisplay augments an options constructor by customizing terminal
// output characters
//
// [ xxx > ]
// | | | | |- end
// | | | |- incomplete
// | | |- head
// | |- complete
// |- start
func WithDisplay(start, complete, head, incomplete, end string) augment {
return func(o *barOpts) {
o.start = start
o.complete = complete
o.head = head
o.incomplete = incomplete
o.end = end
}
}
// WithDimensions augments an options constructor by customizing the
// bar's width and total
func WithDimensions(total, width int) augment {
return func(o *barOpts) {
o.total = total
o.width = width
}
}
// WithFormat augments an options constructor by customizing the bar's
// output format
func WithFormat(f string) augment {
return func(o *barOpts) {
o.formatString = f
}
}
// WithCallback augments an options constructor by setting a callback
func WithCallback(cb func()) augment {
return func(o *barOpts) {
o.callback = cb
}
}
// WithOutput augments an options constructor by setting the output stream
func WithOutput(out Output) augment {
return func(o *barOpts) {
o.output = out
}
}
// WithContext augments an options constructor by setting the initial values
// for the bar's context
func WithContext(ctx Context) augment {
return func(o *barOpts) {
o.context = ctx
}
}
// WithDebug augments an options constructor by setting the internal
// debug flag to true; this will display the list of internal tokens recognized
// on each Tick/Update in place of the standard output
func WithDebug() augment {
return func(o *barOpts) {
o.debug = true
}
}