]> Sergey Matveev's repositories - meta4ra.git/commitdiff
Simple progress bar v0.10.0
authorSergey Matveev <stargrave@stargrave.org>
Thu, 25 Jul 2024 10:01:56 +0000 (13:01 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Thu, 25 Jul 2024 10:17:50 +0000 (13:17 +0300)
cmd/meta4ra/check.go
cmd/meta4ra/progress.go [new file with mode: 0644]
go.mod
go.sum
internal/common.go

index 95511c088d7123ad1b30c60f3870e98f7f16734b..88ab9cfc4e4bc262a7582e7ad798a66d23ed1886 100644 (file)
@@ -27,12 +27,14 @@ import (
        "os"
        "path"
        "strings"
+       "time"
 
        meta4ra "go.stargrave.org/meta4ra/internal"
 )
 
 func runCheck() {
        pipe := flag.Bool("pipe", false, "Verify file data from stdin, copy to stdout")
+       progress := flag.Bool("progress", false, "Show progress of piping/downloading")
        allHashes := flag.Bool("all-hashes", false, "Check all hashes, not the first common one")
        hashes := flag.String("hashes", meta4ra.HashesDefault,
                "hash-name:commandline[,...]")
@@ -208,6 +210,11 @@ That can be used as a downloading utility.
                } else {
                        w = hasher
                }
+               if *progress {
+                       bar := Progress{w: w, now: time.Now(), total: f.Size}
+                       bar.next = bar.now.Add(ProgressPeriod)
+                       w = &bar
+               }
                _, err = io.Copy(w, bufio.NewReaderSize(src, meta4ra.BufLen))
                if !*pipe || *dl != -1 {
                        src.Close()
diff --git a/cmd/meta4ra/progress.go b/cmd/meta4ra/progress.go
new file mode 100644 (file)
index 0000000..d34d98e
--- /dev/null
@@ -0,0 +1,47 @@
+// meta4ra -- Metalink 4.0 utilities
+// Copyright (C) 2021-2024 Sergey Matveev <stargrave@stargrave.org>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, version 3 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+package main
+
+import (
+       "fmt"
+       "io"
+       "os"
+       "time"
+
+       "github.com/dustin/go-humanize"
+)
+
+var ProgressPeriod = time.Second
+
+type Progress struct {
+       now, next    time.Time
+       wrote, total uint64
+       w            io.Writer
+}
+
+func (p *Progress) Write(data []byte) (n int, err error) {
+       n, err = p.w.Write(data)
+       p.wrote += uint64(len(data))
+       p.now = time.Now()
+       if p.now.After(p.next) {
+               p.next = p.now.Add(ProgressPeriod)
+               fmt.Fprintf(os.Stderr, "%d%% | %s / %s\n",
+                       int(100*p.wrote/p.total),
+                       humanize.IBytes(p.wrote),
+                       humanize.IBytes(p.total))
+       }
+       return
+}
diff --git a/go.mod b/go.mod
index 52edb75e04443e0a63c52309310b063c1b7d20fa..6fdba3adfc3e86864f306cec2b69ef76ae1e1ba3 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -11,6 +11,7 @@ require (
 )
 
 require (
+       github.com/dustin/go-humanize v1.0.1 // indirect
        github.com/klauspost/cpuid/v2 v2.2.8 // indirect
        golang.org/x/sys v0.22.0 // indirect
 )
diff --git a/go.sum b/go.sum
index 6e0b2e0c1f9c15b574e653fc9d41c1aca1991acc..1db1ceee1cd94c2f32d0c9d3803b2a9159c0a5f2 100644 (file)
--- a/go.sum
+++ b/go.sum
@@ -1,5 +1,7 @@
 github.com/dchest/skein v0.0.0-20171112102903-d7f1022db390 h1:oNcAGoFeaPCgOnlARnJMQqgoq1UMlGwW7PFJddtTF2c=
 github.com/dchest/skein v0.0.0-20171112102903-d7f1022db390/go.mod h1:sh8l6PI4IHMaBmo2rlnHxnJDjXY7rxmDeaGSyupxMVM=
+github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
+github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
 github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM=
 github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
 github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
index 8df7d7014bf5bbfae34d850e11113c625724b220..92919d2b9d267ab80ce2cfb3d49f38035cc137e5 100644 (file)
@@ -21,7 +21,7 @@ import (
 )
 
 const (
-       Generator       = "meta4ra/0.9.1"
+       Generator       = "meta4ra/0.10.0"
        SigMediaTypePGP = "application/pgp-signature"
        SigMediaTypeSSH = "application/ssh-signature"
        BufLen          = 1 << 20