aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorPřemysl Eric Janouch <p@janouch.name>2023-12-10 08:58:52 +0100
committerPřemysl Eric Janouch <p@janouch.name>2023-12-10 08:58:52 +0100
commit32a4282a018be501628781088a1d7d2405e58770 (patch)
treefdcd03b0f896497bc6ca35a3bea703a90aa4e500 /main.go
parent95c8722dd862e03e3a8bf3e8ca080aab9d8c8e95 (diff)
downloadgallery-32a4282a018be501628781088a1d7d2405e58770.tar.gz
gallery-32a4282a018be501628781088a1d7d2405e58770.tar.xz
gallery-32a4282a018be501628781088a1d7d2405e58770.zip
Add progress bars
Diffstat (limited to 'main.go')
-rw-r--r--main.go52
1 files changed, 49 insertions, 3 deletions
diff --git a/main.go b/main.go
index 1ef03ab..9f93d49 100644
--- a/main.go
+++ b/main.go
@@ -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()