select new file after rename

Related #197
This commit is contained in:
Gokcehan 2020-07-17 00:28:32 +03:00
parent e7fb56391f
commit 2f056ad359
2 changed files with 32 additions and 12 deletions

31
eval.go
View File

@ -492,8 +492,7 @@ func insert(app *app, arg string) {
app.ui.loadFile(app.nav)
app.ui.loadFileInfo(app.nav)
}
case strings.HasPrefix(app.ui.cmdPrefix, "replace") ||
strings.HasPrefix(app.ui.cmdPrefix, "create path"):
case strings.HasPrefix(app.ui.cmdPrefix, "replace"):
normal(app)
if arg == "y" {
@ -508,6 +507,25 @@ func insert(app *app, arg string) {
app.ui.loadFile(app.nav)
app.ui.loadFileInfo(app.nav)
}
case strings.HasPrefix(app.ui.cmdPrefix, "create path"):
normal(app)
if arg == "y" {
if err := os.MkdirAll(filepath.Dir(app.nav.renameNewPath), os.ModePerm); err != nil {
app.ui.echoerrf("rename: %s", err)
return
}
if err := app.nav.rename(); 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)
}
case app.ui.cmdPrefix == "mark-save: ":
normal(app)
@ -1117,11 +1135,10 @@ func (e *callExpr) eval(app *app, args []string) {
app.nav.renameOldPath = oldPath
app.nav.renameNewPath = newPath
if dir, _ := filepath.Split(s); dir != "" {
if _, err := os.Stat(filepath.Join(wd, dir)); err != nil {
app.ui.cmdPrefix = "create path " + dir + "?[y/N]"
return
}
newDir := filepath.Dir(newPath)
if _, err := os.Stat(newDir); os.IsNotExist(err) {
app.ui.cmdPrefix = "create path " + newDir + "?[y/N]"
return
}
oldStat, err := os.Stat(oldPath)

13
nav.go
View File

@ -854,18 +854,21 @@ func (nav *nav) rename() error {
oldPath := nav.renameOldPath
newPath := nav.renameNewPath
dir, _ := filepath.Split(newPath)
os.MkdirAll(dir, os.ModePerm)
if err := os.Rename(oldPath, newPath); err != nil {
return err
}
// TODO: change selection
if err := nav.sel(newPath); err != nil {
lstat, err := os.Lstat(newPath)
if err != nil {
return err
}
dir := nav.loadDir(filepath.Dir(newPath))
dir.files = append(dir.files, &file{FileInfo: lstat})
dir.sel(lstat.Name(), nav.height)
nav.checkDir(dir)
return nil
}