From bd889a9e060b041fb622760345bf2a431441e97b Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Wed, 6 Feb 2019 15:56:41 +0300 Subject: [PATCH] make 'delete' command prompting Related #113 --- eval.go | 40 +++++++++++++++++-------- nav.go | 93 ++++++++++++++++++++++++--------------------------------- 2 files changed, 67 insertions(+), 66 deletions(-) diff --git a/eval.go b/eval.go index 776d6ca..83ebf8c 100644 --- a/eval.go +++ b/eval.go @@ -431,6 +431,22 @@ func insert(app *app, arg string) { app.ui.cmdAccLeft = nil app.ui.cmdAccRight = nil app.ui.cmdPrefix = "" + case app.ui.cmdPrefix == "delete?[y/N]: ": + app.ui.menuBuf = nil + app.ui.cmdAccLeft = nil + app.ui.cmdAccRight = nil + app.ui.cmdPrefix = "" + + if arg == "y" { + if err := app.nav.del(); err != nil { + app.ui.printf("delete: %s", err) + return + } + app.nav.unselect() + if err := sendRemote("send load"); err != nil { + app.ui.printf("delete: %s", err) + } + } case app.ui.cmdPrefix == "mark-save: ": app.ui.menuBuf = nil app.ui.cmdAccLeft = nil @@ -542,11 +558,8 @@ func (e *callExpr) eval(app *app, args []string) { defer out.Close() var path string - if len(app.nav.selections) != 0 { - selections := app.nav.currSelections() - path = strings.Join(selections, "\n") - } else if curr, err := app.nav.currFile(); err == nil { - path = curr.path + if list, err := app.nav.currFileOrSelections(); err == nil { + path = strings.Join(list, "\n") } else { return } @@ -618,13 +631,16 @@ func (e *callExpr) eval(app *app, args []string) { case "delete": if cmd, ok := gOpts.cmds["delete"]; ok { cmd.eval(app, e.args) - } else if err := app.nav.deleteFiles(); err != nil { - app.ui.printf("delete: %s", err) - return - } - app.nav.unselect() - if err := sendRemote("send load"); err != nil { - app.ui.printf("delete: %s", err) + app.nav.unselect() + if err := sendRemote("send load"); err != nil { + app.ui.printf("delete: %s", err) + } + } else { + if _, err := app.nav.currFileOrSelections(); err != nil { + app.ui.printf("delete: %s", err) + return + } + app.ui.cmdPrefix = "delete?[y/N]: " } case "clear": if err := saveFiles(nil, false); err != nil { diff --git a/nav.go b/nav.go index 693da39..70c7a13 100644 --- a/nav.go +++ b/nav.go @@ -550,46 +550,19 @@ func (nav *nav) unselect() { nav.selectionInd = 0 } -// effectiveSelection is a pure function that returns the selected files's paths. -// In case the user has not selected a file it returns the file on the user's cursor. -// If the function can't return a selection it returns an error. -func (nav *nav) effectiveSelection() (list []string, err error) { - if len(nav.selections) == 0 { - curr, err := nav.currFile() - if err != nil { - return nil, errors.New("no file selected") - } - - return []string{curr.path}, nil +func (nav *nav) save(cp bool) error { + list, err := nav.currFileOrSelections() + if err != nil { + return err } - return nav.currSelections(), nil -} + if err := saveFiles(list, cp); err != nil { + return err + } -func (nav *nav) save(cp bool) error { - if len(nav.selections) == 0 { - curr, err := nav.currFile() - if err != nil { - return errors.New("no file selected") - } - - if err := saveFiles([]string{curr.path}, cp); err != nil { - return err - } - - nav.saves = make(map[string]bool) - nav.saves[curr.path] = cp - } else { - selections := nav.currSelections() - - if err := saveFiles(selections, cp); err != nil { - return err - } - - nav.saves = make(map[string]bool) - for f := range nav.selections { - nav.saves[f] = cp - } + nav.saves = make(map[string]bool) + for _, f := range list { + nav.saves[f] = cp } return nil @@ -620,10 +593,8 @@ func (nav *nav) paste() error { return nil } -// deleteFiles deletes the user's selected files -// it returns an error if no files are selected or if the OS fails to delete a file -func (nav *nav) deleteFiles() error { - list, err := nav.effectiveSelection() +func (nav *nav) del() error { + list, err := nav.currFileOrSelections() if err != nil { return err @@ -850,19 +821,6 @@ func (nav *nav) searchPrev() error { return nil } -func (nav *nav) currDir() *dir { - return nav.dirs[len(nav.dirs)-1] -} - -func (nav *nav) currFile() (*file, error) { - last := nav.dirs[len(nav.dirs)-1] - - if len(last.files) == 0 { - return nil, fmt.Errorf("empty directory") - } - return last.files[last.ind], nil -} - func (nav *nav) readMarks() error { f, err := os.Open(gMarksPath) if os.IsNotExist(err) { @@ -923,6 +881,19 @@ func (nav *nav) writeMarks() error { return nil } +func (nav *nav) currDir() *dir { + return nav.dirs[len(nav.dirs)-1] +} + +func (nav *nav) currFile() (*file, error) { + last := nav.dirs[len(nav.dirs)-1] + + if len(last.files) == 0 { + return nil, fmt.Errorf("empty directory") + } + return last.files[last.ind], nil +} + type indexedSelections struct { paths []string indices []int @@ -947,3 +918,17 @@ func (nav *nav) currSelections() []string { sort.Sort(indexedSelections{paths: paths, indices: indices}) return paths } + +func (nav *nav) currFileOrSelections() (list []string, err error) { + if len(nav.selections) == 0 { + curr, err := nav.currFile() + if err != nil { + return nil, errors.New("no file selected") + } + + return []string{curr.path}, nil + } + + return nav.currSelections(), nil +} +