cmd/meta4ra/check.go | 7 +++++++ cmd/meta4ra/progress.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ internal/common.go | 2 +- diff --git a/cmd/meta4ra/check.go b/cmd/meta4ra/check.go index 6d1b11b2d62d31e9cce4fcf3098d615fa79c426b899b8152507f8f24d44b3c08..c2959e39553c4001ff81bf24c30222f1b229f2de9e8219c8ea6764afd4fda895 100644 --- a/cmd/meta4ra/check.go +++ b/cmd/meta4ra/check.go @@ -27,12 +27,14 @@ "net/http" "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[,...]") @@ -207,6 +209,11 @@ if *pipe { w = io.MultiWriter(os.Stdout, hasher) } 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 { diff --git a/cmd/meta4ra/progress.go b/cmd/meta4ra/progress.go new file mode 100644 index 0000000000000000000000000000000000000000..58a2265d6e0ee3809732f6c62ea0ab04897f2d2326407900286981c429118da6 --- /dev/null +++ b/cmd/meta4ra/progress.go @@ -0,0 +1,47 @@ +// meta4ra -- Metalink 4.0 utilities +// Copyright (C) 2021-2024 Sergey Matveev +// +// 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 . + +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 11397aa620c927fab4874ca59fbc0e6ea184c05a1331ad4a4fbe622dc287caab..505b89440bef50d2c745deb3c7ff9a001ae474ee7a4898f949d74207e08d47f8 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ lukechampine.com/blake3 v1.3.0 ) 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 4f1a551dabaf10bb5d46caf1da7fd6031d6d82b8d019a533256cab5bc3590acd..e77482571a7b88628e3e8bdc0f1a3e3c49a71d939576e67b43d142f759ea875c 100644 --- 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= diff --git a/internal/common.go b/internal/common.go index 3d00f2c16f91c863c98c5a86908a585d55441a780bfaaf3f0cd31ce5be1f0cc1..d60e52d8b74a69b4f77bdb56979b7eeef441fc3e870082613704a1c9348ac539 100644 --- a/internal/common.go +++ b/internal/common.go @@ -21,7 +21,7 @@ "runtime" ) const ( - Generator = "meta4ra/0.9.1" + Generator = "meta4ra/0.10.0" SigMediaTypePGP = "application/pgp-signature" SigMediaTypeSSH = "application/ssh-signature" BufLen = 1 << 20