Skip to content

Commit

Permalink
allow defining a custom rename command
Browse files Browse the repository at this point in the history
  • Loading branch information
gokcehan committed Jul 21, 2019
1 parent bfd01e5 commit 9f66134
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
6 changes: 3 additions & 3 deletions etc/lfrc.example
Original file line number Diff line number Diff line change
Expand Up @@ -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<space>
# define a custom 'rename' command without prompt for overwrite
# cmd rename %[ -e $1 ] && printf "file exists" || mv $f $1
# map r push :rename<space>

# make sure trash folder exists
# %mkdir -p ~/.trash
Expand Down
35 changes: 30 additions & 5 deletions eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions nav.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 9f66134

Please sign in to comment.