2021-03-15 17:30:18 +00:00
|
|
|
package output
|
|
|
|
|
|
|
|
import (
|
2021-05-03 16:41:52 +00:00
|
|
|
"fmt"
|
2021-03-15 17:30:18 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.uber.org/atomic"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2021-06-25 14:24:22 +00:00
|
|
|
var spinner = []string{"⣷", "⣯", "⣟", "⡿", "⢿", "⣻", "⣽", "⣾"}
|
2021-03-15 17:30:18 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
progressTimeout = 10 * time.Second
|
|
|
|
progressRefreshRate = 200 * time.Millisecond
|
|
|
|
)
|
|
|
|
|
|
|
|
type Progress interface {
|
|
|
|
Start()
|
|
|
|
Stop()
|
|
|
|
Inc()
|
|
|
|
Val() uint64
|
|
|
|
}
|
|
|
|
|
2021-05-03 16:41:52 +00:00
|
|
|
type ProgressOptions struct {
|
|
|
|
LoadingText string
|
|
|
|
FinishedText string
|
|
|
|
ShowCount bool
|
|
|
|
}
|
|
|
|
|
2021-03-15 17:30:18 +00:00
|
|
|
type progress struct {
|
2021-05-03 16:41:52 +00:00
|
|
|
endChan chan struct{}
|
|
|
|
started *atomic.Bool
|
|
|
|
count *atomic.Uint64
|
|
|
|
loadingText string
|
|
|
|
finishedText string
|
|
|
|
showCount bool
|
|
|
|
highestLineLength int
|
2021-03-15 17:30:18 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 16:41:52 +00:00
|
|
|
func NewProgress(loadingText, finishedText string, showCount bool) *progress {
|
2021-03-15 17:30:18 +00:00
|
|
|
return &progress{
|
2021-03-16 17:07:22 +00:00
|
|
|
nil,
|
2021-03-15 17:30:18 +00:00
|
|
|
atomic.NewBool(false),
|
|
|
|
atomic.NewUint64(0),
|
2021-05-03 16:41:52 +00:00
|
|
|
loadingText,
|
|
|
|
finishedText,
|
|
|
|
showCount,
|
|
|
|
0,
|
2021-03-15 17:30:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *progress) Start() {
|
|
|
|
if !p.started.Swap(true) {
|
2021-03-16 17:07:22 +00:00
|
|
|
p.count.Store(0)
|
|
|
|
p.endChan = make(chan struct{})
|
2021-03-15 17:30:18 +00:00
|
|
|
go p.watch()
|
|
|
|
go p.render()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *progress) Stop() {
|
|
|
|
if p.started.Swap(false) {
|
2021-05-03 16:41:52 +00:00
|
|
|
if p.showCount {
|
|
|
|
p.printf("%s (%d)\n", p.finishedText, p.count.Load())
|
|
|
|
} else {
|
|
|
|
p.printf("%s\r", p.finishedText)
|
|
|
|
}
|
2021-03-16 17:07:22 +00:00
|
|
|
close(p.endChan)
|
2021-03-15 17:30:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *progress) Inc() {
|
2021-04-19 16:10:22 +00:00
|
|
|
if lastVal := p.count.Load(); !p.started.Load() {
|
|
|
|
logrus.Debug("Progress received a tic after stopping. Restarting...")
|
|
|
|
p.Start()
|
|
|
|
p.count.Store(lastVal)
|
2021-03-15 17:30:18 +00:00
|
|
|
}
|
2021-04-19 16:10:22 +00:00
|
|
|
p.count.Inc()
|
2021-03-15 17:30:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *progress) Val() uint64 {
|
|
|
|
return p.count.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *progress) render() {
|
|
|
|
i := -1
|
2021-05-03 16:41:52 +00:00
|
|
|
p.printf("%s\r", p.loadingText)
|
2021-03-15 17:30:18 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-p.endChan:
|
|
|
|
return
|
|
|
|
case <-time.After(progressRefreshRate):
|
|
|
|
i++
|
|
|
|
if i >= len(spinner) {
|
|
|
|
i = 0
|
|
|
|
}
|
2021-05-03 16:41:52 +00:00
|
|
|
if p.showCount {
|
|
|
|
p.printf("%s %s (%d)\r", p.loadingText, spinner[i], p.count.Load())
|
|
|
|
} else {
|
|
|
|
p.printf("%s %s\r", p.loadingText, spinner[i])
|
|
|
|
}
|
2021-03-15 17:30:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *progress) watch() {
|
|
|
|
Loop:
|
|
|
|
for {
|
2021-03-16 17:07:22 +00:00
|
|
|
lastVal := p.count.Load()
|
2021-03-15 17:30:18 +00:00
|
|
|
select {
|
|
|
|
case <-time.After(progressTimeout):
|
2021-03-16 17:07:22 +00:00
|
|
|
if p.count.Load() != lastVal {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if p.started.Swap(false) {
|
|
|
|
close(p.endChan)
|
|
|
|
break Loop
|
|
|
|
}
|
2021-03-15 17:30:18 +00:00
|
|
|
case <-p.endChan:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logrus.Debug("Progress did not receive any tic. Stopping...")
|
|
|
|
}
|
2021-05-03 16:41:52 +00:00
|
|
|
|
|
|
|
func (p *progress) flush() {
|
|
|
|
for i := 0; i < p.highestLineLength; i++ {
|
|
|
|
Printf(" ")
|
|
|
|
}
|
|
|
|
Printf("\r")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *progress) printf(format string, args ...interface{}) {
|
|
|
|
txt := fmt.Sprintf(format, args...)
|
|
|
|
length := len(txt)
|
|
|
|
if length > p.highestLineLength {
|
|
|
|
p.highestLineLength = length
|
|
|
|
}
|
|
|
|
p.flush()
|
|
|
|
Printf(txt)
|
|
|
|
}
|