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
This commit is contained in:
Kallinteris Andreas authored and gokcehan committed 2018-12-29 20:14:20 +03:00
1 parent 3efdfd3bca
commit 7e3f92f049
6 files changed
+49

No files matched your search

+1
View File
@@ -31,6 +31,7 @@ var (
"copy",
"cut",
"paste",
"delete",
"clear",
"draw",
"redraw",
+1
View File
@@ -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:
+1
View File
@@ -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:
+11
View File
@@ -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)
+1
View File
@@ -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:
+34
View File
@@ -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 {