From d5f989f373d7c8de0e302a9ed2d244ad8680f9e7 Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Mon, 2 Dec 2019 19:50:13 +0300 Subject: [PATCH] show progress for delete operation Related #238 --- app.go | 17 ++++++++- nav.go | 108 ++++++++++++++++++++++++++++++++------------------------- ui.go | 4 +++ 3 files changed, 80 insertions(+), 49 deletions(-) diff --git a/app.go b/app.go index 35e718e..7f79796 100644 --- a/app.go +++ b/app.go @@ -179,7 +179,7 @@ func (app *app) loop() { continue } - if app.nav.deleting { + if app.nav.deleteTotal > 0 { app.ui.echoerr("quit: delete operation in progress") continue } @@ -237,6 +237,21 @@ func (app *app) loop() { app.nav.moveUpdate = 0 } app.ui.draw(app.nav) + case n := <-app.nav.deleteCountChan: + app.nav.deleteCount += n + if app.nav.deleteUpdate++; app.nav.deleteUpdate >= 1000 { + app.nav.deleteUpdate = 0 + app.ui.draw(app.nav) + } + case n := <-app.nav.deleteTotalChan: + app.nav.deleteTotal += n + if n < 0 { + app.nav.deleteCount += n + } + if app.nav.deleteTotal == 0 { + app.nav.deleteUpdate = 0 + } + app.ui.draw(app.nav) case d := <-app.nav.dirChan: prev, ok := app.nav.dirCache[d.path] if ok { diff --git a/nav.go b/nav.go index e55c6c6..f717aee 100644 --- a/nav.go +++ b/nav.go @@ -241,34 +241,38 @@ func (dir *dir) sel(name string, height int) { } type nav struct { - dirs []*dir - copyBytes int64 - copyTotal int64 - copyUpdate int - moveCount int - moveTotal int - moveUpdate int - deleting bool - copyBytesChan chan int64 - copyTotalChan chan int64 - moveCountChan chan int - moveTotalChan chan int - dirChan chan *dir - regChan chan *reg - dirCache map[string]*dir - regCache map[string]*reg - saves map[string]bool - marks map[string]string - renameCache []string - selections map[string]int - selectionInd int - height int - find string - findBack bool - search string - searchBack bool - searchInd int - searchPos int + dirs []*dir + copyBytes int64 + copyTotal int64 + copyUpdate int + moveCount int + moveTotal int + moveUpdate int + deleteCount int + deleteTotal int + deleteUpdate int + copyBytesChan chan int64 + copyTotalChan chan int64 + moveCountChan chan int + moveTotalChan chan int + deleteCountChan chan int + deleteTotalChan chan int + dirChan chan *dir + regChan chan *reg + dirCache map[string]*dir + regCache map[string]*reg + saves map[string]bool + marks map[string]string + renameCache []string + selections map[string]int + selectionInd int + height int + find string + findBack bool + search string + searchBack bool + searchInd int + searchPos int } func (nav *nav) loadDir(path string) *dir { @@ -336,20 +340,22 @@ func newNav(height int) *nav { } nav := &nav{ - copyBytesChan: make(chan int64, 1024), - copyTotalChan: make(chan int64, 1024), - moveCountChan: make(chan int, 1024), - moveTotalChan: make(chan int, 1024), - dirChan: make(chan *dir), - regChan: make(chan *reg), - dirCache: make(map[string]*dir), - regCache: make(map[string]*reg), - saves: make(map[string]bool), - marks: make(map[string]string), - renameCache: make([]string, 2), - selections: make(map[string]int), - selectionInd: 0, - height: height, + copyBytesChan: make(chan int64, 1024), + copyTotalChan: make(chan int64, 1024), + moveCountChan: make(chan int, 1024), + moveTotalChan: make(chan int, 1024), + deleteCountChan: make(chan int, 1024), + deleteTotalChan: make(chan int, 1024), + dirChan: make(chan *dir), + regChan: make(chan *reg), + dirCache: make(map[string]*dir), + regCache: make(map[string]*reg), + saves: make(map[string]bool), + marks: make(map[string]string), + renameCache: make([]string, 2), + selections: make(map[string]int), + selectionInd: 0, + height: height, } nav.getDirs(wd) @@ -785,21 +791,27 @@ func (nav *nav) del(ui *ui) error { go func() { echo := &callExpr{"echoerr", []string{""}, 1} errCount := 0 - nav.deleting = true + + nav.deleteTotalChan <- len(list) for _, path := range list { + nav.deleteCountChan <- 1 + if err := os.RemoveAll(path); err != nil { errCount++ echo.args[0] = fmt.Sprintf("[%d] %s", errCount, err) ui.exprChan <- echo } } - nav.deleting = false - }() - if err := remote("send sync"); err != nil { - return fmt.Errorf("delete: %s", err) - } + nav.deleteTotalChan <- -len(list) + + if err := remote("send load"); err != nil { + errCount++ + echo.args[0] = fmt.Sprintf("[%d] %s", errCount, err) + ui.exprChan <- echo + } + }() return nil } diff --git a/ui.go b/ui.go index 87cabcc..c3c38bf 100644 --- a/ui.go +++ b/ui.go @@ -616,6 +616,10 @@ func (ui *ui) drawStatLine(nav *nav) { progress += fmt.Sprintf(" [%d/%d]", nav.moveCount, nav.moveTotal) } + if nav.deleteTotal > 0 { + progress += fmt.Sprintf(" [%d/%d]", nav.deleteCount, nav.deleteTotal) + } + ruler := fmt.Sprintf("%s%s %d/%d", acc, progress, ind, tot) ui.msgWin.printRight(0, fg, bg, ruler)