add throttling and interface

This commit is contained in:
Celogeek 2023-09-25 09:10:46 +02:00
parent 77d6600a36
commit 4a3fc60732
Signed by: celogeek
SSH Key Fingerprint: SHA256:DEDfxIK2nUWXbslbRkww3zsauDjhWHlTXar+ak4lDJ4

View File

@ -6,6 +6,7 @@ package epubprogress
import ( import (
"fmt" "fmt"
"os" "os"
"time"
"github.com/schollz/progressbar/v3" "github.com/schollz/progressbar/v3"
) )
@ -18,7 +19,12 @@ type Options struct {
TotalJob int TotalJob int
} }
func New(o Options) *progressbar.ProgressBar { type EpubProgress interface {
Add(num int) error
Close() (err error)
}
func New(o Options) EpubProgress {
if o.Quiet { if o.Quiet {
return progressbar.DefaultSilent(int64(o.Max)) return progressbar.DefaultSilent(int64(o.Max))
} }
@ -26,6 +32,7 @@ func New(o Options) *progressbar.ProgressBar {
fmtDesc := fmt.Sprintf("[%s/%s] %%-15s", fmtJob, fmtJob) fmtDesc := fmt.Sprintf("[%s/%s] %%-15s", fmtJob, fmtJob)
return progressbar.NewOptions(o.Max, return progressbar.NewOptions(o.Max,
progressbar.OptionSetWriter(os.Stderr), progressbar.OptionSetWriter(os.Stderr),
progressbar.OptionThrottle(65*time.Millisecond),
progressbar.OptionOnCompletion(func() { progressbar.OptionOnCompletion(func() {
fmt.Fprint(os.Stderr, "\n") fmt.Fprint(os.Stderr, "\n")
}), }),