Fix cli progressbar

This commit is contained in:
Milan Nikolic
2026-06-24 23:12:43 +02:00
parent a8b2f65ca5
commit 7f025aa9c2
+33 -16
View File
@@ -13,6 +13,7 @@ import (
"github.com/gen2brain/cbconvert" "github.com/gen2brain/cbconvert"
pb "github.com/schollz/progressbar/v3" pb "github.com/schollz/progressbar/v3"
"golang.org/x/term"
) )
var appVersion string var appVersion string
@@ -78,39 +79,55 @@ func main() {
os.Exit(1) os.Exit(1)
} }
interactive := !opts.Quiet && term.IsTerminal(int(os.Stderr.Fd()))
var bar *pb.ProgressBar var bar *pb.ProgressBar
if opts.Cover || opts.Thumbnail || opts.Meta { newBar := func(max int, description string) {
if !opts.Quiet { bar = pb.NewOptions(max,
bar = pb.NewOptions(conv.Nfiles,
pb.OptionShowCount(), pb.OptionShowCount(),
pb.OptionClearOnFinish(), pb.OptionClearOnFinish(),
pb.OptionUseANSICodes(true), pb.OptionUseANSICodes(true),
pb.OptionSetPredictTime(false), pb.OptionSetPredictTime(false),
pb.OptionSetDescription(description),
) )
} }
clearLine := func() {
fmt.Fprint(os.Stderr, "\033[2K\r")
}
cleanup := func() {
if interactive {
if bar != nil {
_ = bar.Finish()
}
clearLine()
}
}
if interactive && (opts.Cover || opts.Thumbnail || opts.Meta) {
newBar(conv.Nfiles, "")
} }
conv.OnStart = func() { conv.OnStart = func() {
if !opts.Quiet { if interactive {
bar = pb.NewOptions(conv.Ncontents, clearLine()
pb.OptionShowCount(), newBar(conv.Ncontents, fmt.Sprintf("Converting %d of %d:", conv.CurrFile, conv.Nfiles))
pb.OptionClearOnFinish(),
pb.OptionUseANSICodes(true),
pb.OptionSetDescription(fmt.Sprintf("Converting %d of %d:", conv.CurrFile, conv.Nfiles)),
pb.OptionSetPredictTime(false),
)
} }
} }
conv.OnProgress = func() { conv.OnProgress = func() {
if !opts.Quiet { if bar != nil {
_ = bar.Add(1) _ = bar.Add(1)
} }
} }
conv.OnCompress = func() { conv.OnCompress = func() {
if !opts.Quiet { if interactive {
fmt.Fprintf(os.Stderr, "Compressing %d of %d...\r", conv.CurrFile, conv.Nfiles) if bar != nil {
_ = bar.Finish()
}
fmt.Fprintf(os.Stderr, "Compressing %d of %d...", conv.CurrFile, conv.Nfiles)
} }
} }
@@ -120,7 +137,7 @@ func main() {
os.Exit(1) os.Exit(1)
} }
fmt.Fprintf(os.Stderr, "\r") cleanup()
return return
} }
@@ -163,7 +180,7 @@ func main() {
} }
} }
fmt.Fprintf(os.Stderr, "\r") cleanup()
} }
// parseFlags parses command line flags. // parseFlags parses command line flags.