show copy progress in the ruler

This commit is contained in:
Gokcehan 2019-02-28 21:04:38 +03:00
parent 599ac11f26
commit 6ec2924b6b
3 changed files with 60 additions and 40 deletions

13
app.go
View File

@ -189,6 +189,19 @@ func (app *app) loop() {
}
return
case n := <-app.nav.copyBytesChan:
// n is usually 1024B so update roughly per 1024B x 1024 = 1MB copied
if app.nav.copyUpdate++; app.nav.copyUpdate >= 1024 {
app.nav.copyUpdate = 0
app.ui.draw(app.nav)
}
app.nav.copyBytes += n
case n := <-app.nav.copyTotalChan:
app.nav.copyTotal += n
if n < 0 {
app.nav.copyBytes += n
}
app.ui.draw(app.nav)
case d := <-app.nav.dirChan:
prev, ok := app.nav.dirCache[d.path]
if ok {

29
nav.go
View File

@ -189,6 +189,11 @@ func (dir *dir) sel(name string, height int) {
type nav struct {
dirs []*dir
copyBytes int64
copyTotal int64
copyUpdate int
copyBytesChan chan int64
copyTotalChan chan int64
dirChan chan *dir
regChan chan *reg
dirCache map[string]*dir
@ -271,6 +276,8 @@ func newNav(height int) *nav {
}
nav := &nav{
copyBytesChan: make(chan int64, 1024),
copyTotalChan: make(chan int64, 1024),
dirChan: make(chan *dir),
regChan: make(chan *reg),
dirCache: make(map[string]*dir),
@ -568,7 +575,7 @@ func (nav *nav) save(cp bool) error {
return nil
}
func copyAsync(ui *ui, srcs []string, dstDir string) {
func (nav *nav) copyAsync(ui *ui, srcs []string, dstDir string) {
echo := &callExpr{"echo", []string{""}, 1}
_, err := os.Stat(dstDir)
@ -585,25 +592,19 @@ func copyAsync(ui *ui, srcs []string, dstDir string) {
return
}
nav.copyTotalChan <- total
nums, errs := copyAll(srcs, dstDir)
curr := int64(0)
lastUpdate := 0
errCount := 0
loop:
for {
select {
case n := <-nums:
curr += n
// n is usually 1024B so update roughly per 1024B x 1024 = 1MB copied
if lastUpdate++; lastUpdate >= 1024 {
lastUpdate = 0
percentage := int((100 * float64(curr)) / float64(total))
echo.args[0] = fmt.Sprintf("%d%%", percentage)
ui.exprChan <- echo
}
nav.copyBytesChan <- n
case err, ok := <-errs:
if !ok {
nav.copyTotalChan <- -total
break loop
}
errCount++
@ -619,7 +620,7 @@ loop:
}
}
func moveAsync(ui *ui, srcs []string, dstDir string) {
func (nav *nav) moveAsync(ui *ui, srcs []string, dstDir string) {
echo := &callExpr{"echo", []string{""}, 1}
_, err := os.Stat(dstDir)
@ -683,9 +684,9 @@ func (nav *nav) paste(ui *ui) error {
dstDir := nav.currDir().path
if cp {
go copyAsync(ui, srcs, dstDir)
go nav.copyAsync(ui, srcs, dstDir)
} else {
go moveAsync(ui, srcs, dstDir)
go nav.moveAsync(ui, srcs, dstDir)
}
if err := saveFiles(nil, false); err != nil {

8
ui.go
View File

@ -576,7 +576,13 @@ func (ui *ui) drawStatLine(nav *nav) {
ind := min(currDir.ind+1, tot)
acc := string(ui.keyCount) + string(ui.keyAcc)
ruler := fmt.Sprintf("%s %d/%d", acc, ind, tot)
var progress string
if nav.copyTotal > 0 {
percentage := int((100 * float64(nav.copyBytes)) / float64(nav.copyTotal))
progress = fmt.Sprintf(" [%d%%]", percentage)
}
ruler := fmt.Sprintf("%s%s %d/%d", acc, progress, ind, tot)
ui.msgWin.printRight(0, fg, bg, ruler)
}