From eb615c377a7bc4d9cdd8bfae05d9ad2b62b81b18 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Fri, 30 Dec 2022 13:17:47 +0100 Subject: [PATCH] improve progressbar --- internal/epub/core.go | 3 +-- internal/epub/image_processing.go | 3 +-- internal/epub/progress.go | 31 +++++++++++++++++++++++++++++++ internal/image-converter/core.go | 18 ------------------ 4 files changed, 33 insertions(+), 22 deletions(-) create mode 100644 internal/epub/progress.go diff --git a/internal/epub/core.go b/internal/epub/core.go index d894445..8e1429e 100644 --- a/internal/epub/core.go +++ b/internal/epub/core.go @@ -8,7 +8,6 @@ import ( "time" "github.com/gofrs/uuid" - "github.com/schollz/progressbar/v3" ) type ImageOptions struct { @@ -139,7 +138,7 @@ func (e *ePub) Write() error { } totalParts := len(epubParts) - bar := progressbar.Default(int64(totalParts), "Writing Part") + bar := NewBar(totalParts, "Writing Part", 2, 2) for i, part := range epubParts { ext := filepath.Ext(e.Output) suffix := "" diff --git a/internal/epub/image_processing.go b/internal/epub/image_processing.go index a4e4ccd..5f83246 100644 --- a/internal/epub/image_processing.go +++ b/internal/epub/image_processing.go @@ -15,7 +15,6 @@ import ( "sync" "github.com/nwaples/rardecode" - "github.com/schollz/progressbar/v3" ) type Image struct { @@ -65,7 +64,7 @@ func LoadImages(path string, options *ImageOptions) ([]*Image, error) { // processing wg := &sync.WaitGroup{} - bar := progressbar.Default(int64(imageCount), "Processing") + bar := NewBar(imageCount, "Processing", 1, 2) for i := 0; i < runtime.NumCPU(); i++ { wg.Add(1) go func() { diff --git a/internal/epub/progress.go b/internal/epub/progress.go new file mode 100644 index 0000000..f188d90 --- /dev/null +++ b/internal/epub/progress.go @@ -0,0 +1,31 @@ +package epub + +import ( + "fmt" + "os" + + "github.com/schollz/progressbar/v3" +) + +func NewBar(max int, description string, currentJob, totalJob int) *progressbar.ProgressBar { + fmtJob := fmt.Sprintf("%%0%dd", len(fmt.Sprint(totalJob))) + fmtDesc := fmt.Sprintf("[%s/%s] %%-15s", fmtJob, fmtJob) + return progressbar.NewOptions(max, + progressbar.OptionSetWriter(os.Stderr), + progressbar.OptionOnCompletion(func() { + fmt.Fprint(os.Stderr, "\n") + }), + progressbar.OptionSetDescription(fmt.Sprintf(fmtDesc, currentJob, totalJob, description)), + progressbar.OptionSetWidth(60), + progressbar.OptionShowCount(), + progressbar.OptionSetRenderBlankState(true), + progressbar.OptionEnableColorCodes(true), + progressbar.OptionSetTheme(progressbar.Theme{ + Saucer: "[green]=[reset]", + SaucerHead: "[green]>[reset]", + SaucerPadding: " ", + BarStart: "[", + BarEnd: "]", + }), + ) +} diff --git a/internal/image-converter/core.go b/internal/image-converter/core.go index a59b749..1f29388 100644 --- a/internal/image-converter/core.go +++ b/internal/image-converter/core.go @@ -6,7 +6,6 @@ import ( "image/color" "image/jpeg" "io" - "os" "golang.org/x/image/draw" ) @@ -121,23 +120,6 @@ func Get(img *image.Gray, quality int) []byte { return b.Bytes() } -func Save(img *image.Gray, output string, quality int) { - o, err := os.Create(output) - if err != nil { - panic(err) - } - defer o.Close() - - if quality == 0 { - quality = 75 - } - - err = jpeg.Encode(o, img, &jpeg.Options{Quality: quality}) - if err != nil { - panic(err) - } -} - func Convert(reader io.ReadCloser, crop bool, w, h int, quality int) ([]byte, int, int) { img := Load(reader) if crop {