From 9f661344edf8752b6906c4368f7f30d3af27ea3b Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Sun, 21 Jul 2019 20:40:57 +0300 Subject: [PATCH] allow defining a custom rename command --- etc/lfrc.example | 6 +++--- eval.go | 35 ++++++++++++++++++++++++++++++----- nav.go | 6 +++--- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/etc/lfrc.example b/etc/lfrc.example index 103b41b..41a957b 100644 --- a/etc/lfrc.example +++ b/etc/lfrc.example @@ -42,9 +42,9 @@ cmd open ${{ esac }} -# rename current file without overwrite -cmd rename %[ -e $1 ] && printf "file exists" || mv $f $1 -map r push :rename +# define a custom 'rename' command without prompt for overwrite +# cmd rename %[ -e $1 ] && printf "file exists" || mv $f $1 +# map r push :rename # make sure trash folder exists # %mkdir -p ~/.trash diff --git a/eval.go b/eval.go index 253d7b7..fb657d7 100644 --- a/eval.go +++ b/eval.go @@ -470,7 +470,10 @@ func insert(app *app, arg string) { app.nav.unselect() if err := remote("send load"); err != nil { app.ui.echoerrf("delete: %s", err) + return } + app.ui.loadFile(app.nav) + app.ui.loadFileInfo(app.nav) } case strings.HasPrefix(app.ui.cmdPrefix, "replace") || strings.HasPrefix(app.ui.cmdPrefix, "create path"): @@ -481,8 +484,13 @@ func insert(app *app, arg string) { app.ui.echoerrf("rename: %s", err) return } + if err := remote("send load"); err != nil { + app.ui.echoerrf("rename: %s", err) + return + } + app.ui.loadFile(app.nav) + app.ui.loadFileInfo(app.nav) } - case app.ui.cmdPrefix == "mark-save: ": normal(app) @@ -829,14 +837,23 @@ func (e *callExpr) eval(app *app, args []string) { app.ui.menuBuf = listMarks(app.nav.marks) app.ui.cmdPrefix = "mark-remove: " case "rename": - if curr, err := app.nav.currFile(); err != nil { - app.ui.echoerrf("rename: %s:", err) - return + if cmd, ok := gOpts.cmds["rename"]; ok { + cmd.eval(app, e.args) + if err := remote("send load"); err != nil { + app.ui.echoerrf("rename: %s", err) + return + } } else { + curr, err := app.nav.currFile() + if err != nil { + app.ui.echoerrf("rename: %s:", err) + return + } app.ui.cmdPrefix = "rename: " app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(curr.Name())...) } - + app.ui.loadFile(app.nav) + app.ui.loadFileInfo(app.nav) case "sync": if err := app.nav.sync(); err != nil { app.ui.echoerrf("sync: %s", err) @@ -1076,8 +1093,16 @@ func (e *callExpr) eval(app *app, args []string) { if err := app.nav.rename(app.ui); err != nil { app.ui.echoerrf("rename: %s", err) + return } + if err := remote("send load"); err != nil { + app.ui.echoerrf("rename: %s", err) + return + } + + app.ui.loadFile(app.nav) + app.ui.loadFileInfo(app.nav) } default: log.Printf("entering unknown execution prefix: %q", app.ui.cmdPrefix) diff --git a/nav.go b/nav.go index fa6bc37..2fb48a3 100644 --- a/nav.go +++ b/nav.go @@ -751,16 +751,16 @@ func (nav *nav) rename(ui *ui) error { return err } } + if err := os.Rename(oldPath, newPath); err != nil { return err } - nav.renew() + // TODO: change selection if err := nav.sel(newPath); err != nil { return err } - ui.loadFile(nav) - ui.loadFileInfo(nav) + return nil }