From: Sergey Matveev Date: Thu, 25 Jul 2024 10:01:56 +0000 (+0300) Subject: Simple progress bar X-Git-Tag: v0.10.0 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=4ae5df5b038d5d5a80268d4deddf4b56bfc8df0d;p=meta4ra.git Simple progress bar --- diff --git a/cmd/meta4ra/check.go b/cmd/meta4ra/check.go index 95511c0..88ab9cf 100644 --- a/cmd/meta4ra/check.go +++ b/cmd/meta4ra/check.go @@ -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 index 0000000..d34d98e --- /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 52edb75..6fdba3a 100644 --- 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 6e0b2e0..1db1cee 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 8df7d70..92919d2 100644 --- a/internal/common.go +++ b/internal/common.go @@ -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