diff options
-rw-r--r-- | main.go | 52 |
1 files changed, 49 insertions, 3 deletions
@@ -73,6 +73,39 @@ func dbCollectStrings(query string) ([]string, error) { return result, nil } +// --- Progress bar ------------------------------------------------------------ + +type progressBar struct { + mutex sync.Mutex + current int + target int +} + +func newProgressBar(target int) *progressBar { + pb := &progressBar{current: 0, target: target} + pb.update() + return pb +} + +func (pb *progressBar) Stop() { + // The minimum thing that works: just print a newline. + os.Stdout.WriteString("\n") +} + +func (pb *progressBar) update() { + target := fmt.Sprintf("%d", pb.target) + fmt.Printf("\r%*d/%s (%2d%%)", len(target), pb.current, target, + int(float32(pb.current)/float32(pb.target)*100)) +} + +func (pb *progressBar) Step() { + pb.mutex.Lock() + defer pb.mutex.Unlock() + + pb.current++ + pb.update() +} + // --- Initialization ---------------------------------------------------------- // cmdInit initializes a "gallery directory" that contains gallery.sqlite, @@ -445,7 +478,13 @@ func cmdImport(args []string) error { return err } - // TODO: Show progress in some manner. Perhaps port my propeller code. + // 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) + defer pb.Stop() + ctx, cancel := context.WithCancelCause(context.Background()) i := importer{} wg := sync.WaitGroup{} @@ -466,8 +505,11 @@ func cmdImport(args []string) error { return nil } if err := filepath.WalkDir(name, cb); err != nil { - return err + cancel(err) + break } + + pb.Step() } wg.Wait() if ctx.Err() != nil { @@ -585,7 +627,9 @@ func cmdThumbnail(args []string) error { } } - // TODO: Show progress in some manner. Perhaps port my propeller code. + pb := newProgressBar(len(hexSHA1)) + defer pb.Stop() + ctx, cancel := context.WithCancelCause(context.Background()) wg := sync.WaitGroup{} for _, sha1 := range hexSHA1 { @@ -600,6 +644,8 @@ func cmdThumbnail(args []string) error { if err := makeThumbnailFor(sha1); err != nil { cancel(err) } + + pb.Step() }(sha1) } wg.Wait() |