make progress output optional

This commit is contained in:
ston1th 2022-04-08 19:55:44 +02:00
commit 13a64cecfe

View file

@ -19,7 +19,7 @@ import (
)
var (
version string
progress bool
)
const gib = 1024 * 1024 * 1024
@ -72,9 +72,13 @@ remove file (uses `+srcKey+`):
rm file:///mnt/nfs /buzz.txt
options
-p show copy progress
`)
os.Exit(1)
}
flag.BoolVar(&progress, "p", false, "show copy progress")
flag.Parse()
args := flag.Args()
@ -180,18 +184,24 @@ func cp(args []string) (err error) {
return
}
defer df.Close()
if progress {
fi, err := sf.Stat()
if err != nil {
return
return err
}
r := &progressReader{
f: sf,
done: make(chan struct{}),
size: fi.Size(),
}
go r.PrintProgress()
go r.Progress()
_, err = io.Copy(df, r)
close(r.done)
r.Print()
fmt.Println()
} else {
_, err = io.Copy(df, sf)
}
return
}
@ -214,19 +224,21 @@ func (p *progressReader) ReadAt(b []byte, off int64) (n int, err error) {
return
}
func (p *progressReader) PrintProgress() {
for {
select {
case <-p.done:
fmt.Println()
return
case <-time.After(time.Millisecond * 500):
}
func (p *progressReader) Print() {
n := atomic.LoadInt64(&p.n)
percent := int(math.Round(float64(n) / float64(p.size) * 100))
fmt.Printf("progress: %d / %d %d%%\r", n, p.size, percent)
}
func (p *progressReader) Progress() {
for {
select {
case <-p.done:
return
case <-time.After(time.Millisecond * 500):
}
p.Print()
}
}
func rm(args []string) (err error) {