make 'delete' command prompting

Related #113
This commit is contained in:
Gokcehan 2019-02-06 15:56:41 +03:00
parent 951529ebc0
commit bd889a9e06
2 changed files with 67 additions and 66 deletions

40
eval.go
View File

@ -431,6 +431,22 @@ func insert(app *app, arg string) {
app.ui.cmdAccLeft = nil app.ui.cmdAccLeft = nil
app.ui.cmdAccRight = nil app.ui.cmdAccRight = nil
app.ui.cmdPrefix = "" 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: ": case app.ui.cmdPrefix == "mark-save: ":
app.ui.menuBuf = nil app.ui.menuBuf = nil
app.ui.cmdAccLeft = nil app.ui.cmdAccLeft = nil
@ -542,11 +558,8 @@ func (e *callExpr) eval(app *app, args []string) {
defer out.Close() defer out.Close()
var path string var path string
if len(app.nav.selections) != 0 { if list, err := app.nav.currFileOrSelections(); err == nil {
selections := app.nav.currSelections() path = strings.Join(list, "\n")
path = strings.Join(selections, "\n")
} else if curr, err := app.nav.currFile(); err == nil {
path = curr.path
} else { } else {
return return
} }
@ -618,13 +631,16 @@ func (e *callExpr) eval(app *app, args []string) {
case "delete": case "delete":
if cmd, ok := gOpts.cmds["delete"]; ok { if cmd, ok := gOpts.cmds["delete"]; ok {
cmd.eval(app, e.args) cmd.eval(app, e.args)
} else if err := app.nav.deleteFiles(); err != nil { app.nav.unselect()
app.ui.printf("delete: %s", err) if err := sendRemote("send load"); err != nil {
return app.ui.printf("delete: %s", err)
} }
app.nav.unselect() } else {
if err := sendRemote("send load"); err != nil { if _, err := app.nav.currFileOrSelections(); err != nil {
app.ui.printf("delete: %s", err) app.ui.printf("delete: %s", err)
return
}
app.ui.cmdPrefix = "delete?[y/N]: "
} }
case "clear": case "clear":
if err := saveFiles(nil, false); err != nil { if err := saveFiles(nil, false); err != nil {

93
nav.go
View File

@ -550,46 +550,19 @@ func (nav *nav) unselect() {
nav.selectionInd = 0 nav.selectionInd = 0
} }
// effectiveSelection is a pure function that returns the selected files's paths. func (nav *nav) save(cp bool) error {
// In case the user has not selected a file it returns the file on the user's cursor. list, err := nav.currFileOrSelections()
// If the function can't return a selection it returns an error. if err != nil {
func (nav *nav) effectiveSelection() (list []string, err error) { return err
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 if err := saveFiles(list, cp); err != nil {
} return err
}
func (nav *nav) save(cp bool) error { nav.saves = make(map[string]bool)
if len(nav.selections) == 0 { for _, f := range list {
curr, err := nav.currFile() nav.saves[f] = cp
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
}
} }
return nil return nil
@ -620,10 +593,8 @@ func (nav *nav) paste() error {
return nil return nil
} }
// deleteFiles deletes the user's selected files func (nav *nav) del() error {
// it returns an error if no files are selected or if the OS fails to delete a file list, err := nav.currFileOrSelections()
func (nav *nav) deleteFiles() error {
list, err := nav.effectiveSelection()
if err != nil { if err != nil {
return err return err
@ -850,19 +821,6 @@ func (nav *nav) searchPrev() error {
return nil 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 { func (nav *nav) readMarks() error {
f, err := os.Open(gMarksPath) f, err := os.Open(gMarksPath)
if os.IsNotExist(err) { if os.IsNotExist(err) {
@ -923,6 +881,19 @@ func (nav *nav) writeMarks() error {
return nil 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 { type indexedSelections struct {
paths []string paths []string
indices []int indices []int
@ -947,3 +918,17 @@ func (nav *nav) currSelections() []string {
sort.Sort(indexedSelections{paths: paths, indices: indices}) sort.Sort(indexedSelections{paths: paths, indices: indices})
return paths 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
}