From 089e6dc7053c61919d03351b8281d8ab7ae71c8d Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Fri, 11 Mar 2022 15:21:27 +0300 Subject: [PATCH] init navigation after evaluation of config file cc #748 #759 --- app.go | 35 +++++++++++------- eval.go | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- nav.go | 15 +++----- ui.go | 8 ++++ 4 files changed, 147 insertions(+), 23 deletions(-) diff --git a/app.go b/app.go index ceeaac2..5ef53f3 100644 --- a/app.go +++ b/app.go @@ -245,19 +245,6 @@ func (app *app) loop() { app.ui.readExpr() - if gSelect != "" { - go func() { - lstat, err := os.Lstat(gSelect) - if err != nil { - app.ui.exprChan <- &callExpr{"echoerr", []string{err.Error()}, 1} - } else if lstat.IsDir() { - app.ui.exprChan <- &callExpr{"cd", []string{gSelect}, 1} - } else { - app.ui.exprChan <- &callExpr{"select", []string{gSelect}, 1} - } - }() - } - if gConfigPath != "" { if _, err := os.Stat(gConfigPath); !os.IsNotExist(err) { app.readFile(gConfigPath) @@ -284,6 +271,28 @@ func (app *app) loop() { } } + wd, err := os.Getwd() + if err != nil { + log.Printf("getting current directory: %s", err) + } + + app.nav.getDirs(wd) + app.nav.addJumpList() + app.nav.init = true + + if gSelect != "" { + go func() { + lstat, err := os.Lstat(gSelect) + if err != nil { + app.ui.exprChan <- &callExpr{"echoerr", []string{err.Error()}, 1} + } else if lstat.IsDir() { + app.ui.exprChan <- &callExpr{"cd", []string{gSelect}, 1} + } else { + app.ui.exprChan <- &callExpr{"select", []string{gSelect}, 1} + } + }() + } + for { select { case <-app.quitChan: diff --git a/eval.go b/eval.go index bb9fe62..09dcb92 100644 --- a/eval.go +++ b/eval.go @@ -793,46 +793,73 @@ func insert(app *app, arg string) { func (e *callExpr) eval(app *app, args []string) { switch e.name { case "up": + if (!app.nav.init) { + return + } if app.nav.up(e.count) { app.ui.loadFile(app.nav, true) app.ui.loadFileInfo(app.nav) } case "half-up": + if (!app.nav.init) { + return + } if app.nav.up(e.count * app.nav.height / 2) { app.ui.loadFile(app.nav, true) app.ui.loadFileInfo(app.nav) } case "page-up": + if (!app.nav.init) { + return + } if app.nav.up(e.count * app.nav.height) { app.ui.loadFile(app.nav, true) app.ui.loadFileInfo(app.nav) } case "scrollup": + if (!app.nav.init) { + return + } if app.nav.scrollup(e.count) { app.ui.loadFile(app.nav, true) app.ui.loadFileInfo(app.nav) } case "down": + if (!app.nav.init) { + return + } if app.nav.down(e.count) { app.ui.loadFile(app.nav, true) app.ui.loadFileInfo(app.nav) } case "half-down": + if (!app.nav.init) { + return + } if app.nav.down(e.count * app.nav.height / 2) { app.ui.loadFile(app.nav, true) app.ui.loadFileInfo(app.nav) } case "page-down": + if (!app.nav.init) { + return + } if app.nav.down(e.count * app.nav.height) { app.ui.loadFile(app.nav, true) app.ui.loadFileInfo(app.nav) } case "scrolldown": + if (!app.nav.init) { + return + } if app.nav.scrolldown(e.count) { app.ui.loadFile(app.nav, true) app.ui.loadFileInfo(app.nav) } case "updir": + if (!app.nav.init) { + return + } resetIncCmd(app) preChdir(app) for i := 0; i < e.count; i++ { @@ -846,6 +873,9 @@ func (e *callExpr) eval(app *app, args []string) { restartIncCmd(app) onChdir(app) case "open": + if (!app.nav.init) { + return + } curr, err := app.nav.currFile() if err != nil { app.ui.echoerrf("opening: %s", err) @@ -918,16 +948,25 @@ func (e *callExpr) eval(app *app, args []string) { case "quit": app.quitChan <- struct{}{} case "top": + if (!app.nav.init) { + return + } if app.nav.top() { app.ui.loadFile(app.nav, true) app.ui.loadFileInfo(app.nav) } case "bottom": + if (!app.nav.init) { + return + } if app.nav.bottom() { app.ui.loadFile(app.nav, true) app.ui.loadFileInfo(app.nav) } case "toggle": + if (!app.nav.init) { + return + } if len(e.args) == 0 { app.nav.toggle() } else { @@ -945,11 +984,17 @@ func (e *callExpr) eval(app *app, args []string) { } } case "invert": + if (!app.nav.init) { + return + } app.nav.invert() case "unselect": app.nav.unselect() case "calcdirsize": - err := app.nav.getDirSize() + if (!app.nav.init) { + return + } + err := app.nav.calcDirSize() if err != nil { app.ui.echoerrf("calcdirsize: %s", err) return @@ -958,6 +1003,10 @@ func (e *callExpr) eval(app *app, args []string) { app.nav.sort() app.ui.sort() case "copy": + if (!app.nav.init) { + return + } + if err := app.nav.save(true); err != nil { app.ui.echoerrf("copy: %s", err) return @@ -976,6 +1025,10 @@ func (e *callExpr) eval(app *app, args []string) { } app.ui.loadFileInfo(app.nav) case "cut": + if (!app.nav.init) { + return + } + if err := app.nav.save(false); err != nil { app.ui.echoerrf("cut: %s", err) return @@ -994,6 +1047,10 @@ func (e *callExpr) eval(app *app, args []string) { } app.ui.loadFileInfo(app.nav) case "paste": + if (!app.nav.init) { + return + } + if cmd, ok := gOpts.cmds["paste"]; ok { cmd.eval(app, e.args) } else if err := app.nav.paste(app.ui); err != nil { @@ -1003,6 +1060,10 @@ func (e *callExpr) eval(app *app, args []string) { app.ui.loadFile(app.nav, true) app.ui.loadFileInfo(app.nav) case "delete": + if (!app.nav.init) { + return + } + if cmd, ok := gOpts.cmds["delete"]; ok { cmd.eval(app, e.args) app.nav.unselect() @@ -1035,6 +1096,9 @@ func (e *callExpr) eval(app *app, args []string) { app.ui.loadFile(app.nav, true) app.ui.loadFileInfo(app.nav) case "clear": + if (!app.nav.init) { + return + } if err := saveFiles(nil, false); err != nil { app.ui.echoerrf("clear: %s", err) return @@ -1053,6 +1117,9 @@ func (e *callExpr) eval(app *app, args []string) { app.ui.loadFileInfo(app.nav) case "draw": case "redraw": + if (!app.nav.init) { + return + } app.ui.renew() app.ui.screen.Sync() if app.nav.height != app.ui.wins[0].h { @@ -1061,9 +1128,15 @@ func (e *callExpr) eval(app *app, args []string) { } app.ui.loadFile(app.nav, true) case "load": + if (!app.nav.init) { + return + } app.nav.renew() app.ui.loadFile(app.nav, true) case "reload": + if (!app.nav.init) { + return + } if err := app.nav.reload(); err != nil { app.ui.echoerrf("reload: %s", err) } @@ -1121,6 +1194,9 @@ func (e *callExpr) eval(app *app, args []string) { app.nav.findBack = true app.ui.loadFileInfo(app.nav) case "find-next": + if (!app.nav.init) { + return + } dir := app.nav.currDir() old := dir.ind for i := 0; i < e.count; i++ { @@ -1135,6 +1211,9 @@ func (e *callExpr) eval(app *app, args []string) { app.ui.loadFileInfo(app.nav) } case "find-prev": + if (!app.nav.init) { + return + } dir := app.nav.currDir() old := dir.ind for i := 0; i < e.count; i++ { @@ -1149,6 +1228,9 @@ func (e *callExpr) eval(app *app, args []string) { app.ui.loadFileInfo(app.nav) } case "search": + if (!app.nav.init) { + return + } if app.ui.cmdPrefix == ">" { return } @@ -1160,6 +1242,9 @@ func (e *callExpr) eval(app *app, args []string) { app.nav.searchBack = false app.ui.loadFileInfo(app.nav) case "search-back": + if (!app.nav.init) { + return + } if app.ui.cmdPrefix == ">" { return } @@ -1171,6 +1256,9 @@ func (e *callExpr) eval(app *app, args []string) { app.nav.searchBack = true app.ui.loadFileInfo(app.nav) case "search-next": + if (!app.nav.init) { + return + } for i := 0; i < e.count; i++ { if app.nav.searchBack { if moved, err := app.nav.searchPrev(); err != nil { @@ -1189,6 +1277,9 @@ func (e *callExpr) eval(app *app, args []string) { } } case "search-prev": + if (!app.nav.init) { + return + } for i := 0; i < e.count; i++ { if app.nav.searchBack { if moved, err := app.nav.searchNext(); err != nil { @@ -1207,6 +1298,9 @@ func (e *callExpr) eval(app *app, args []string) { } } case "filter": + if (!app.nav.init) { + return + } if app.ui.cmdPrefix == ">" { return } @@ -1221,6 +1315,9 @@ func (e *callExpr) eval(app *app, args []string) { } app.ui.loadFileInfo(app.nav) case "setfilter": + if (!app.nav.init) { + return + } log.Printf("filter: %s", e.args) if err := app.nav.setFilter(e.args); err != nil { app.ui.echoerrf("filter: %s", err) @@ -1248,6 +1345,9 @@ func (e *callExpr) eval(app *app, args []string) { app.ui.menuBuf = listMarks(app.nav.marks) app.ui.cmdPrefix = "mark-remove: " case "rename": + if (!app.nav.init) { + return + } if cmd, ok := gOpts.cmds["rename"]; ok { cmd.eval(app, e.args) if gSingleMode { @@ -1321,6 +1421,10 @@ func (e *callExpr) eval(app *app, args []string) { onChdir(app) } case "select": + if (!app.nav.init) { + return + } + if len(e.args) != 1 { app.ui.echoerr("select: requires an argument") return @@ -1357,6 +1461,9 @@ func (e *callExpr) eval(app *app, args []string) { onChdir(app) } case "glob-select": + if (!app.nav.init) { + return + } if len(e.args) != 1 { app.ui.echoerr("glob-select: requires a pattern to match") return @@ -1366,6 +1473,9 @@ func (e *callExpr) eval(app *app, args []string) { return } case "glob-unselect": + if (!app.nav.init) { + return + } if len(e.args) != 1 { app.ui.echoerr("glob-unselect: requires a pattern to match") return diff --git a/nav.go b/nav.go index 12c7568..3b82359 100644 --- a/nav.go +++ b/nav.go @@ -328,6 +328,7 @@ func (dir *dir) sel(name string, height int) { } type nav struct { + init bool dirs []*dir copyBytes int64 copyTotal int64 @@ -460,11 +461,6 @@ func (nav *nav) getDirs(wd string) { } func newNav(height int) *nav { - wd, err := os.Getwd() - if err != nil { - log.Printf("getting current directory: %s", err) - } - nav := &nav{ copyBytesChan: make(chan int64, 1024), copyTotalChan: make(chan int64, 1024), @@ -486,9 +482,6 @@ func newNav(height int) *nav { jumpListInd: -1, } - nav.getDirs(wd) - nav.addJumpList() - return nav } @@ -558,6 +551,10 @@ func (nav *nav) reload() error { } func (nav *nav) position() { + if (!nav.init) { + return + } + path := nav.currDir().path for i := len(nav.dirs) - 2; i >= 0; i-- { nav.dirs[i].sel(filepath.Base(path), nav.height) @@ -1593,7 +1590,7 @@ func (nav *nav) currFileOrSelections() (list []string, err error) { return nav.currSelections(), nil } -func (nav *nav) getDirSize() error { +func (nav *nav) calcDirSize() error { calc := func(f *file) error { if f.IsDir() { diff --git a/ui.go b/ui.go index 7b28489..0f57542 100644 --- a/ui.go +++ b/ui.go @@ -641,6 +641,10 @@ type reg struct { } func (ui *ui) loadFile(nav *nav, volatile bool) { + if (!nav.init) { + return + } + curr, err := nav.currFile() if err != nil { return @@ -662,6 +666,10 @@ func (ui *ui) loadFile(nav *nav, volatile bool) { } func (ui *ui) loadFileInfo(nav *nav) { + if (!nav.init) { + return + } + curr, err := nav.currFile() if err != nil { return