improve progressbar

This commit is contained in:
Celogeek 2022-12-30 13:17:47 +01:00
parent 110d4a2699
commit eb615c377a
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
4 changed files with 33 additions and 22 deletions

View File

@ -8,7 +8,6 @@ import (
"time" "time"
"github.com/gofrs/uuid" "github.com/gofrs/uuid"
"github.com/schollz/progressbar/v3"
) )
type ImageOptions struct { type ImageOptions struct {
@ -139,7 +138,7 @@ func (e *ePub) Write() error {
} }
totalParts := len(epubParts) totalParts := len(epubParts)
bar := progressbar.Default(int64(totalParts), "Writing Part") bar := NewBar(totalParts, "Writing Part", 2, 2)
for i, part := range epubParts { for i, part := range epubParts {
ext := filepath.Ext(e.Output) ext := filepath.Ext(e.Output)
suffix := "" suffix := ""

View File

@ -15,7 +15,6 @@ import (
"sync" "sync"
"github.com/nwaples/rardecode" "github.com/nwaples/rardecode"
"github.com/schollz/progressbar/v3"
) )
type Image struct { type Image struct {
@ -65,7 +64,7 @@ func LoadImages(path string, options *ImageOptions) ([]*Image, error) {
// processing // processing
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
bar := progressbar.Default(int64(imageCount), "Processing") bar := NewBar(imageCount, "Processing", 1, 2)
for i := 0; i < runtime.NumCPU(); i++ { for i := 0; i < runtime.NumCPU(); i++ {
wg.Add(1) wg.Add(1)
go func() { go func() {

31
internal/epub/progress.go Normal file
View File

@ -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: "]",
}),
)
}

View File

@ -6,7 +6,6 @@ import (
"image/color" "image/color"
"image/jpeg" "image/jpeg"
"io" "io"
"os"
"golang.org/x/image/draw" "golang.org/x/image/draw"
) )
@ -121,23 +120,6 @@ func Get(img *image.Gray, quality int) []byte {
return b.Bytes() 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) { func Convert(reader io.ReadCloser, crop bool, w, h int, quality int) ([]byte, int, int) {
img := Load(reader) img := Load(reader)
if crop { if crop {