-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(progress): Make progress bars more responsive
- Loading branch information
Showing
5 changed files
with
131 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package progressbar | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
) | ||
|
||
func NewBarSafeLogger(w io.Writer, bar *ProgressBar) *BarSafeLogger { | ||
return &BarSafeLogger{ | ||
out: w, | ||
bar: bar, | ||
} | ||
} | ||
|
||
type BarSafeLogger struct { | ||
out io.Writer | ||
bar *ProgressBar | ||
buf bytes.Buffer | ||
} | ||
|
||
func (l *BarSafeLogger) Write(p []byte) (int, error) { | ||
if l.bar.IsFinished() { | ||
return l.out.Write(p) | ||
} | ||
|
||
l.buf.Write([]byte("\r\x1B[K")) | ||
l.buf.Write(p) | ||
if p[len(p)-1] == '\n' { | ||
l.buf.WriteString(l.bar.String()) | ||
} | ||
l.bar.mu.Lock() | ||
defer l.bar.mu.Unlock() | ||
if n, err := io.Copy(l.out, &l.buf); err != nil { | ||
return int(n), err | ||
} | ||
return len(p), nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package progressbar | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"sync" | ||
"time" | ||
|
||
"github.com/clevyr/kubedb/internal/config/flags" | ||
"github.com/gabe565/go-spinners" | ||
"github.com/mattn/go-isatty" | ||
"github.com/schollz/progressbar/v3" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func New(max int64, label string, spinnerKey string) *ProgressBar { | ||
s, ok := spinner.Map[spinnerKey] | ||
if !ok { | ||
log.WithField("spinner", spinnerKey).Warn("invalid spinner") | ||
s = spinner.Map[flags.DefaultSpinner] | ||
} | ||
|
||
options := []progressbar.Option{ | ||
progressbar.OptionSetDescription(label), | ||
progressbar.OptionSetWriter(io.Discard), | ||
progressbar.OptionShowBytes(true), | ||
progressbar.OptionSetWidth(10), | ||
progressbar.OptionShowCount(), | ||
progressbar.OptionSpinnerCustom(s.Frames), | ||
progressbar.OptionFullWidth(), | ||
} | ||
|
||
var throttle time.Duration | ||
if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) { | ||
throttle = 65 * time.Millisecond | ||
options = append(options, | ||
progressbar.OptionSetRenderBlankState(true), | ||
progressbar.OptionOnCompletion(func() { | ||
_, _ = fmt.Fprint(os.Stderr, "\r\x1B[K") | ||
}), | ||
) | ||
} else { | ||
throttle = 2 * time.Second | ||
} | ||
options = append(options, | ||
progressbar.OptionThrottle(throttle), | ||
) | ||
|
||
cancelChan := make(chan struct{}) | ||
bar := &ProgressBar{ | ||
ProgressBar: progressbar.NewOptions64(max, options...), | ||
cancelChan: cancelChan, | ||
} | ||
go func() { | ||
for { | ||
select { | ||
case <-cancelChan: | ||
return | ||
case <-time.After(throttle): | ||
if bar.IsFinished() { | ||
return | ||
} | ||
if bar.mu.TryLock() { | ||
_ = bar.RenderBlank() | ||
_, _ = os.Stderr.Write([]byte(bar.String())) | ||
bar.mu.Unlock() | ||
} | ||
} | ||
} | ||
}() | ||
|
||
return bar | ||
} | ||
|
||
type ProgressBar struct { | ||
*progressbar.ProgressBar | ||
mu sync.Mutex | ||
cancelChan chan struct{} | ||
cancelOnce sync.Once | ||
} | ||
|
||
func (p *ProgressBar) Finish() error { | ||
p.Close() | ||
return p.ProgressBar.Finish() | ||
} | ||
|
||
func (p *ProgressBar) Close() { | ||
p.cancelOnce.Do(func() { | ||
close(p.cancelChan) | ||
}) | ||
} |