make progress output optional
This commit is contained in:
parent
3e765a7943
commit
13a64cecfe
1 changed files with 30 additions and 18 deletions
|
|
@ -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()
|
||||
fi, err := sf.Stat()
|
||||
if err != nil {
|
||||
return
|
||||
if progress {
|
||||
fi, err := sf.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r := &progressReader{
|
||||
f: sf,
|
||||
done: make(chan struct{}),
|
||||
size: fi.Size(),
|
||||
}
|
||||
go r.Progress()
|
||||
_, err = io.Copy(df, r)
|
||||
close(r.done)
|
||||
r.Print()
|
||||
fmt.Println()
|
||||
} else {
|
||||
_, err = io.Copy(df, sf)
|
||||
}
|
||||
r := &progressReader{
|
||||
f: sf,
|
||||
done: make(chan struct{}),
|
||||
size: fi.Size(),
|
||||
}
|
||||
go r.PrintProgress()
|
||||
_, err = io.Copy(df, r)
|
||||
close(r.done)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -214,19 +224,21 @@ func (p *progressReader) ReadAt(b []byte, off int64) (n int, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func (p *progressReader) PrintProgress() {
|
||||
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:
|
||||
fmt.Println()
|
||||
return
|
||||
case <-time.After(time.Millisecond * 500):
|
||||
}
|
||||
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)
|
||||
p.Print()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func rm(args []string) (err error) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue