Files
acp/progress_bar.go
2022-12-07 19:50:48 +08:00

50 lines
1.1 KiB
Go

package acp
import (
"fmt"
"os"
"time"
"github.com/schollz/progressbar/v3"
)
func NewProgressBar() EventHandler {
// progressBar := progressbar.DefaultBytes(1, "[0/0] indexing...")
bar := progressbar.NewOptions64(
1,
progressbar.OptionSetDescription("[0/0] indexing..."),
progressbar.OptionSetWriter(os.Stderr),
progressbar.OptionShowBytes(true),
progressbar.OptionSetWidth(10),
progressbar.OptionThrottle(65*time.Millisecond),
progressbar.OptionShowCount(),
progressbar.OptionOnCompletion(func() {}),
progressbar.OptionSpinnerType(14),
progressbar.OptionFullWidth(),
progressbar.OptionSetRenderBlankState(true),
)
var totalFiles int64
return func(ev Event) {
switch e := ev.(type) {
case *EventUpdateCount:
totalFiles = e.Files
bar.Describe(fmt.Sprintf("[0/%d] indexing...", totalFiles))
bar.ChangeMax64(e.Bytes)
return
case *EventUpdateProgress:
bar.Set64(e.Bytes)
if !e.Finished {
bar.Describe(fmt.Sprintf("[%d/%d] copying...", e.Files, totalFiles))
return
}
bar.Describe(fmt.Sprintf("[%d/%d] finishing...", e.Files, totalFiles))
return
default:
return
}
}
}