Use other progressbar

This commit is contained in:
Jan Bader 2021-08-05 23:21:46 +02:00
parent 275b63cfe8
commit 31383ad118
2 changed files with 31 additions and 11 deletions

View File

@ -8,7 +8,8 @@ import (
"path/filepath" "path/filepath"
"sync" "sync"
"github.com/cheggaaa/pb/v3" "github.com/vbauerster/mpb"
"github.com/vbauerster/mpb/decor"
) )
// FilesMap is a struct for listing files by Size and Hash to search for duplicates // FilesMap is a struct for listing files by Size and Hash to search for duplicates
@ -23,7 +24,9 @@ type FilesMap struct {
FilesHashed chan fileEntry FilesHashed chan fileEntry
progress *pb.ProgressBar progress *mpb.Progress
incomingBar *mpb.Bar
lock sync.Mutex lock sync.Mutex
} }
@ -35,17 +38,21 @@ func newFilesMap() *FilesMap {
FilesHashed: make(chan fileEntry), FilesHashed: make(chan fileEntry),
FilesIncoming: make(chan fileEntry, 100000), FilesIncoming: make(chan fileEntry, 100000),
FilesHashing: make(chan fileEntry), FilesHashing: make(chan fileEntry),
progress: pb.StartNew(0), progress: mpb.New(mpb.WithWidth(64)),
} }
} }
func (fm *FilesMap) IncomingWorker() { func (fm *FilesMap) IncomingWorker() {
for file := range fm.FilesIncoming { for file := range fm.FilesIncoming {
fm.incomingBar.Increment()
if *minSize > file.size {
continue
}
if *verbose { if *verbose {
fmt.Println("Incoming", file.path) fmt.Println("Incoming", file.path)
} }
fm.progress.Increment()
prevFile, ok := fm.FilesBySize[file.size] prevFile, ok := fm.FilesBySize[file.size]
if !ok { if !ok {
fm.FilesBySize[file.size] = file.path fm.FilesBySize[file.size] = file.path
@ -102,6 +109,16 @@ func (fm *FilesMap) HashedWorker(done chan bool) {
func (fm *FilesMap) WalkDirectories() int { func (fm *FilesMap) WalkDirectories() int {
countFiles := 0 countFiles := 0
fm.incomingBar = fm.progress.AddSpinner(0, mpb.SpinnerOnLeft,
mpb.PrependDecorators(
// display our name with one space on the right
decor.Name("Finding files"),
// replace ETA decorator with "done" message, OnComplete event
decor.OnComplete(
decor.AverageETA(decor.ET_STYLE_GO, decor.WC{W: 4}), "done",
),
),
mpb.AppendDecorators(decor.AverageSpeed(1, "%d"), decor.TotalNoUnit("%d")))
for _, path := range flag.Args() { for _, path := range flag.Args() {
filepath.Walk(path, func(path string, info os.FileInfo, err error) error { filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if info.IsDir() { if info.IsDir() {
@ -110,11 +127,19 @@ func (fm *FilesMap) WalkDirectories() int {
fm.FilesIncoming <- fileEntry{path, info.Size(), ""} fm.FilesIncoming <- fileEntry{path, info.Size(), ""}
countFiles++ countFiles++
fm.progress.SetTotal(int64(countFiles)) fm.incomingBar.SetTotal(int64(countFiles), false)
return nil return nil
}) })
} }
fm.incomingBar.SetTotal(int64(countFiles), true)
close(fm.FilesIncoming) close(fm.FilesIncoming)
return countFiles return countFiles
} }
type fileEntry struct {
path string
size int64
hash string
}

View File

@ -21,6 +21,7 @@ var toFile = flag.String("to-file", "", "Save results to <path>")
var deleteDupesIn = flag.String("delete-dupes-in", "", "Delete duplicates if they are contained in <path>") var deleteDupesIn = flag.String("delete-dupes-in", "", "Delete duplicates if they are contained in <path>")
var promptForDelete = flag.Bool("delete-prompt", false, "Ask which file to keep for each dupe-set") var promptForDelete = flag.Bool("delete-prompt", false, "Ask which file to keep for each dupe-set")
var moveToFolder = flag.String("move-files", "", "Move files to <path> instead of deleting them") var moveToFolder = flag.String("move-files", "", "Move files to <path> instead of deleting them")
var minSize = flag.Int64("min-size", -1, "Ignore all files smaller than <size> in Bytes")
var force = flag.Bool("force", false, "Actually delete files. Without this options, the files to be deleted are only printed") var force = flag.Bool("force", false, "Actually delete files. Without this options, the files to be deleted are only printed")
var verbose = flag.Bool("verbose", false, "Output additional information") var verbose = flag.Bool("verbose", false, "Output additional information")
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file") var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
@ -189,9 +190,3 @@ func printConfiguration() {
fmt.Println() fmt.Println()
fmt.Println() fmt.Println()
} }
type fileEntry struct {
path string
size int64
hash string
}