From e20f93aa6714868141159d8d855c11ef704e5507 Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch Date: Sun, 10 Dec 2023 09:12:34 +0100 Subject: Improve import progress information --- main.go | 65 +++++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/main.go b/main.go index 9f93d49..91a99a8 100644 --- a/main.go +++ b/main.go @@ -386,11 +386,7 @@ type importer struct { dmMutex sync.Mutex } -func (i *importer) Import(path string, d fs.DirEntry, err error) error { - if err != nil || d.IsDir() { - return err - } - +func (i *importer) Import(path string) error { // The input may be a relative path, and we want to remember it as such, // but symlinks for the images must be absolute. absPath, err := filepath.Abs(path) @@ -478,38 +474,43 @@ func cmdImport(args []string) error { return err } - // TODO: It would be more straight-forward to collect all paths - // per argument first, then filter them through the actual import. - // We could show the actual progress, then, - // only at the cost of remembering all paths before processing. - pb := newProgressBar(len(args) - 1) + // Make the first step collecting all the paths, + // in order to show more useful progress information. + paths := []string{} + cb := func(path string, d fs.DirEntry, err error) error { + if err != nil || d.IsDir() { + return err + } + paths = append(paths, path) + return nil + } + for _, name := range args[1:] { + if err := filepath.WalkDir(name, cb); err != nil { + return err + } + } + + pb := newProgressBar(len(paths)) defer pb.Stop() - ctx, cancel := context.WithCancelCause(context.Background()) i := importer{} + ctx, cancel := context.WithCancelCause(context.Background()) wg := sync.WaitGroup{} - for _, name := range args[1:] { - cb := func(path string, d fs.DirEntry, err error) error { - if taskSemaphore.Acquire(ctx, 1) != nil { - return context.Cause(ctx) - } - - wg.Add(1) - go func() { - defer taskSemaphore.Release(1) - defer wg.Done() - if err := i.Import(path, d, err); err != nil { - cancel(err) - } - }() - return nil - } - if err := filepath.WalkDir(name, cb); err != nil { - cancel(err) + for _, path := range paths { + if taskSemaphore.Acquire(ctx, 1) != nil { break } - pb.Step() + wg.Add(1) + go func(path string) { + defer taskSemaphore.Release(1) + defer wg.Done() + if err := i.Import(path); err != nil { + cancel(err) + } else { + pb.Step() + } + }(path) } wg.Wait() if ctx.Err() != nil { @@ -643,9 +644,9 @@ func cmdThumbnail(args []string) error { defer wg.Done() if err := makeThumbnailFor(sha1); err != nil { cancel(err) + } else { + pb.Step() } - - pb.Step() }(sha1) } wg.Wait() -- cgit v1.2.3-70-g09d2