-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutput_console.go
More file actions
229 lines (182 loc) · 4.63 KB
/
output_console.go
File metadata and controls
229 lines (182 loc) · 4.63 KB
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
package log
import (
"fmt"
"os"
"strings"
"time"
"github.com/fatih/color"
"github.com/nathan-fiscaletti/consolesize-go"
)
type ConsoleOutput struct {
l *Logger
// causes the progress bars to refresh
ticker *time.Ticker
// causes the output handler to shutdown
cancel chan bool
// causes the progress bars to refresh
refresh chan bool
// causes line buffers to appear in-order
msg chan *Line
// allows progressbars to be added
addPbChan chan *ProgressBar
// allows progressbars to be removed
rmPbChan chan *ProgressBar
// list of active progressbars
progressbars []*ProgressBar
// the number of progressbars currently displayed in console (alternatively: how many lines to remove!)
displayedProgressBarCount int
}
func (w *ConsoleOutput) eraseLines(lines int) {
for i := 0; i < lines; i++ {
eraseLine()
}
}
func (w *ConsoleOutput) getConsoleWidth() int {
cols, _ := consolesize.GetConsoleSize()
return cols
}
func formatDur(d time.Duration) string {
if d < 1*time.Minute {
return fmt.Sprintf("%ds", d/time.Second)
}
if d < 1*time.Hour {
return fmt.Sprintf("%dm%ds", d/time.Minute, d/time.Second)
}
return d.String()
}
func (w *ConsoleOutput) renderProgressBar(pb *ProgressBar) {
width := w.getConsoleWidth()
titleCharacters := []rune(pb.Title)
pbAvailableChars := width
pbAvailableChars -= len(titleCharacters)
pbAvailableChars -= 1 // space
mn, mx := pb.Range.MinMax()
progressText := fmt.Sprintf(" %s %s/%s (%.2f%%) %s", pb.Title, mn, mx, pb.Range.PercentValue(), formatDur(time.Since(pb.start)))
pbAvailableChars -= len([]rune(progressText))
bar := renderBarInWidth(&w.l.StyleSheet, pb.oscillationIndex, pbAvailableChars, pb.Range.PercentValue())
if time.Since(pb.lastOscillation) > w.l.StyleSheet.HeadOscillation {
pb.oscillationIndex++
if pb.oscillationIndex >= len(w.l.StyleSheet.BarHead) {
pb.oscillationIndex = 0
}
pb.lastOscillation = time.Now()
}
fmt.Printf("%s%s\n", bar, progressText)
}
func (w *ConsoleOutput) eraseProgressBars() {
linesToErase := w.displayedProgressBarCount
w.eraseLines(linesToErase)
w.displayedProgressBarCount = 0
}
func (w *ConsoleOutput) renderProgressBars() {
for _, pb := range w.progressbars {
w.renderProgressBar(pb)
}
w.displayedProgressBarCount = len(w.progressbars)
}
func (w *ConsoleOutput) refreshProgressBars() {
w.eraseProgressBars()
w.renderProgressBars()
}
func (w *ConsoleOutput) print(ln *Line) {
w.eraseProgressBars()
color.Set(color.Attribute(w.l.StyleSheet.TimeColor))
timestamp := printTime(ln.Time)
fmt.Printf("[%s] ", timestamp)
timestampWidth := len(timestamp) + 3
color.Unset()
cl, setColor := w.l.StyleSheet.Colors[ln.Category]
if setColor {
color.Set(color.Attribute(cl))
}
fmt.Printf("[%s] ", ln.Category)
categoryWidth := len(ln.Category) + 3
if setColor {
color.Unset()
}
prefixWidth := categoryWidth + timestampWidth
for i, lineText := range strings.Split(ln.Text, "\n") {
if i > 0 {
for c := 0; c < prefixWidth; c++ {
fmt.Printf(" ")
}
}
fmt.Printf("%s\r\n", lineText)
}
if ln.Fatal {
os.Exit(1)
}
w.refreshProgressBars()
}
func (w *ConsoleOutput) appendProgressBar(pb *ProgressBar) {
w.progressbars = append(w.progressbars, pb)
}
func (w *ConsoleOutput) removeProgressBar(pb *ProgressBar) {
index := -1
for i, curPb := range w.progressbars {
if curPb == pb {
index = i
break
}
}
if index >= 0 {
w.progressbars = append(w.progressbars[:index], w.progressbars[index+1:]...)
}
}
func (w *ConsoleOutput) begin() {
for {
select {
case <-w.cancel:
return
case pb := <-w.addPbChan:
w.appendProgressBar(pb)
case pb := <-w.rmPbChan:
w.removeProgressBar(pb)
case <-w.refresh:
w.refreshProgressBars()
case msg := <-w.msg:
w.print(msg)
case <-w.ticker.C:
w.refreshProgressBars()
}
}
}
func (w *ConsoleOutput) Begin(l *Logger) error {
w.l = l
w.cancel = make(chan bool)
w.msg = make(chan *Line)
w.refresh = make(chan bool)
w.addPbChan = make(chan *ProgressBar)
w.rmPbChan = make(chan *ProgressBar)
w.ticker = time.NewTicker(200 * time.Millisecond)
go w.begin()
return nil
}
func (w *ConsoleOutput) End() error {
w.cancel <- true
close(w.cancel)
close(w.refresh)
close(w.addPbChan)
close(w.rmPbChan)
w.ticker.Stop()
return nil
}
func (w *ConsoleOutput) AddLine(l *Line) error {
w.msg <- l
return nil
}
func (w *ConsoleOutput) AddProgressBar(pb *ProgressBar) error {
pb.update = w.refresh
w.addPbChan <- pb
return nil
}
func (w *ConsoleOutput) RemoveProgressBar(pb *ProgressBar) error {
w.rmPbChan <- pb
return nil
}
func (w *ConsoleOutput) Exclude(cat Category) error {
return nil
}
func (w *ConsoleOutput) Include(cat Category) error {
return nil
}