From 7e3f92f049793b5e9c6e0fe5fdc858b30aa9d6a3 Mon Sep 17 00:00:00 2001 From: Kallinteris Andreas <30759571+Kallinteris-Andreas@users.noreply.github.com> Date: Sat, 29 Dec 2018 19:14:20 +0200 Subject: [PATCH] added ':delete' builtin command (#121) * added ':delete' command * fixed doc * fixed typo * added dummy function for delete for windows * detacted the functionality from the OS * now deletion works based on selections * added effective selection * delete no longer sends 'send sync' * fixed typo * fixed typo * added DOC to effective selection * added DOC to deleteFiles() * fixed DOC typos * added ability to overwrite :delete * added ability to overwrite :delete --- complete.go | 1 + doc.go | 1 + docstring.go | 1 + eval.go | 11 +++++++++++ lf.1 | 1 + nav.go | 34 ++++++++++++++++++++++++++++++++++ 6 files changed, 49 insertions(+) diff --git a/complete.go b/complete.go index 2d203b8d..29da1fe6 100644 --- a/complete.go +++ b/complete.go @@ -31,6 +31,7 @@ var ( "copy", "cut", "paste", + "delete", "clear", "draw", "redraw", diff --git a/doc.go b/doc.go index aef762fa..5c447e37 100644 --- a/doc.go +++ b/doc.go @@ -62,6 +62,7 @@ The following commands are provided by lf without default keybindings: select changes current file selection to its argument source reads the configuration file in its argument push simulate key pushes given in its argument + delete deletes the selected files. Warning: no prompt The following command line commands are provided by lf with default keybindings: diff --git a/docstring.go b/docstring.go index 1b0ad290..511a837c 100644 --- a/docstring.go +++ b/docstring.go @@ -65,6 +65,7 @@ The following commands are provided by lf without default keybindings: select changes current file selection to its argument source reads the configuration file in its argument push simulate key pushes given in its argument + delete deletes the selected files. Warning: no prompt The following command line commands are provided by lf with default keybindings: diff --git a/eval.go b/eval.go index 78c30d02..b01fe4b0 100644 --- a/eval.go +++ b/eval.go @@ -582,6 +582,17 @@ func (e *callExpr) eval(app *app, args []string) { if err := sendRemote("send sync"); err != nil { app.ui.printf("paste: %s", err) } + 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) + } case "clear": if err := saveFiles(nil, false); err != nil { app.ui.printf("clear: %s", err) diff --git a/lf.1 b/lf.1 index 0e9b09e3..4efb79cf 100644 --- a/lf.1 +++ b/lf.1 @@ -74,6 +74,7 @@ The following commands are provided by lf without default keybindings: select changes current file selection to its argument source reads the configuration file in its argument push simulate key pushes given in its argument + delete deletes the selected files. Warning: no prompt .EE .PP The following command line commands are provided by lf with default keybindings: diff --git a/nav.go b/nav.go index 21ed90f0..693da39b 100644 --- a/nav.go +++ b/nav.go @@ -550,6 +550,22 @@ 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 + } + + return nav.currSelections(), nil +} + func (nav *nav) save(cp bool) error { if len(nav.selections) == 0 { curr, err := nav.currFile() @@ -604,6 +620,24 @@ 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() + + if err != nil { + return err + } + + for _, path := range list { + if err := os.RemoveAll(path); err != nil { + return err + } + } + + return nil +} + func (nav *nav) sync() error { list, cp, err := loadFiles() if err != nil {