refactor: remove init flag (#2313)
* refactor: remove init flag * fix bugs * cleanup of `addcustominfo` * use `getDir` for accessing directories from cache * Avoid trimming `addcustominfo` value
This commit is contained in:
1 parent
57f6ade1b7
commit
733e8d6b55
4 files changed
+213
-451
No files matched your search
@@ -296,14 +296,8 @@ func (app *app) loop() {
|
||||
}
|
||||
}
|
||||
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
log.Printf("getting current directory: %s", err)
|
||||
}
|
||||
|
||||
app.nav.updateDirs(wd)
|
||||
app.nav.loadDirs()
|
||||
app.nav.addJumpList()
|
||||
app.nav.init = true
|
||||
|
||||
if gSelect != "" {
|
||||
go func() {
|
||||
@@ -393,6 +387,11 @@ func (app *app) loop() {
|
||||
}
|
||||
app.ui.draw(app.nav)
|
||||
case d := <-app.nav.dirChan:
|
||||
var oldCurrPath string
|
||||
if curr := app.nav.currFile(); curr != nil {
|
||||
oldCurrPath = curr.path
|
||||
}
|
||||
|
||||
prev, ok := app.nav.dirCache[d.path]
|
||||
if ok {
|
||||
d.ind = prev.ind
|
||||
@@ -405,21 +404,9 @@ func (app *app) loop() {
|
||||
}
|
||||
app.nav.dirCache[d.path] = d
|
||||
|
||||
var oldCurrPath string
|
||||
if curr, err := app.nav.currFile(); err == nil {
|
||||
oldCurrPath = curr.path
|
||||
}
|
||||
|
||||
for i := range app.nav.dirs {
|
||||
if app.nav.dirs[i].path == d.path {
|
||||
app.nav.dirs[i] = d
|
||||
}
|
||||
}
|
||||
|
||||
app.nav.position()
|
||||
|
||||
curr, err := app.nav.currFile()
|
||||
if err == nil {
|
||||
if curr := app.nav.currFile(); curr != nil {
|
||||
if curr.path != oldCurrPath {
|
||||
app.ui.loadFile(app, true)
|
||||
}
|
||||
@@ -441,8 +428,7 @@ func (app *app) loop() {
|
||||
case r := <-app.nav.regChan:
|
||||
app.nav.regCache[r.path] = r
|
||||
|
||||
curr, err := app.nav.currFile()
|
||||
if err == nil {
|
||||
if curr := app.nav.currFile(); curr != nil {
|
||||
if r.path == curr.path {
|
||||
app.ui.sxScreen.forceClear = true
|
||||
if gOpts.preload && r.volatile {
|
||||
@@ -485,9 +471,7 @@ func (app *app) loop() {
|
||||
deletePathRecursive(app.nav.dirCache, path)
|
||||
currPath := app.nav.currDir().path
|
||||
if currPath == path || strings.HasPrefix(currPath, path+string(filepath.Separator)) {
|
||||
if wd, err := os.Getwd(); err == nil {
|
||||
app.nav.updateDirs(wd)
|
||||
}
|
||||
app.nav.loadDirs()
|
||||
}
|
||||
case ev := <-app.ui.evChan:
|
||||
e := app.ui.readEvent(ev, app.nav)
|
||||
|
||||
@@ -887,32 +887,14 @@ func insert(app *app, arg string) {
|
||||
case app.ui.cmdPrefix == "mark-load: ":
|
||||
normal(app)
|
||||
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
log.Printf("getting current directory: %s", err)
|
||||
}
|
||||
|
||||
path, ok := app.nav.marks[arg]
|
||||
if !ok {
|
||||
app.ui.echoerr("mark-load: no such mark")
|
||||
return
|
||||
}
|
||||
|
||||
if wd != path {
|
||||
resetIncCmd(app)
|
||||
preChdir(app)
|
||||
}
|
||||
|
||||
if err := app.nav.cd(path); err != nil {
|
||||
app.ui.echoerrf("%s", err)
|
||||
return
|
||||
}
|
||||
app.ui.loadFile(app, true)
|
||||
|
||||
if wd != path {
|
||||
app.nav.marks["'"] = wd
|
||||
restartIncCmd(app)
|
||||
onChdir(app)
|
||||
if err := cd(app, path); err != nil {
|
||||
app.ui.echoerrf("mark-load: %s", err)
|
||||
}
|
||||
case app.ui.cmdPrefix == "mark-remove: ":
|
||||
normal(app)
|
||||
@@ -951,6 +933,37 @@ func insert(app *app, arg string) {
|
||||
}
|
||||
}
|
||||
|
||||
func cd(app *app, path string) error {
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return fmt.Errorf("getting current directory: %w", err)
|
||||
}
|
||||
|
||||
path, err = filepath.Abs(replaceTilde(path))
|
||||
if err != nil {
|
||||
return fmt.Errorf("getting absolute path: %w", err)
|
||||
}
|
||||
|
||||
if path == wd {
|
||||
return nil
|
||||
}
|
||||
|
||||
resetIncCmd(app)
|
||||
preChdir(app)
|
||||
|
||||
if err := app.nav.cd(path); err != nil {
|
||||
return fmt.Errorf("changing directory: %w", err)
|
||||
}
|
||||
|
||||
app.ui.loadFile(app, true)
|
||||
|
||||
app.nav.marks["'"] = wd
|
||||
restartIncCmd(app)
|
||||
onChdir(app)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func exitCompMenu(app *app) {
|
||||
app.ui.menu = ""
|
||||
app.ui.menuSelect = nil
|
||||
@@ -982,65 +995,38 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
case "quit":
|
||||
app.quitChan <- struct{}{}
|
||||
case "up":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
if app.nav.up(e.count) {
|
||||
app.ui.loadFile(app, true)
|
||||
}
|
||||
case "half-up":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
if app.nav.up(e.count * app.nav.height / 2) {
|
||||
app.ui.loadFile(app, true)
|
||||
}
|
||||
case "page-up":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
if app.nav.up(e.count * app.nav.height) {
|
||||
app.ui.loadFile(app, true)
|
||||
}
|
||||
case "scroll-up":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
if app.nav.scrollUp(e.count) {
|
||||
app.ui.loadFile(app, true)
|
||||
}
|
||||
case "down":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
if app.nav.down(e.count) {
|
||||
app.ui.loadFile(app, true)
|
||||
}
|
||||
case "half-down":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
if app.nav.down(e.count * app.nav.height / 2) {
|
||||
app.ui.loadFile(app, true)
|
||||
}
|
||||
case "page-down":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
if app.nav.down(e.count * app.nav.height) {
|
||||
app.ui.loadFile(app, true)
|
||||
}
|
||||
case "scroll-down":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
if app.nav.scrollDown(e.count) {
|
||||
app.ui.loadFile(app, true)
|
||||
}
|
||||
case "updir":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
resetIncCmd(app)
|
||||
preChdir(app)
|
||||
for range e.count {
|
||||
@@ -1053,12 +1039,8 @@ func (e *callExpr) eval(app *app, _ []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)
|
||||
curr := app.nav.currFile()
|
||||
if curr == nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1103,9 +1085,6 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
restartIncCmd(app)
|
||||
onChdir(app)
|
||||
case "top":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
var moved bool
|
||||
if e.count == 1 {
|
||||
moved = app.nav.top()
|
||||
@@ -1116,9 +1095,6 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
app.ui.loadFile(app, true)
|
||||
}
|
||||
case "bottom":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
var moved bool
|
||||
if e.count == 1 {
|
||||
// Different from Vim, which would treat a count of 1 as meaning to
|
||||
@@ -1131,57 +1107,41 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
app.ui.loadFile(app, true)
|
||||
}
|
||||
case "high":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
if app.nav.high() {
|
||||
app.ui.loadFile(app, true)
|
||||
}
|
||||
case "middle":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
if app.nav.middle() {
|
||||
app.ui.loadFile(app, true)
|
||||
}
|
||||
case "low":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
if app.nav.low() {
|
||||
app.ui.loadFile(app, true)
|
||||
}
|
||||
case "toggle":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
if len(e.args) == 0 {
|
||||
app.nav.toggle()
|
||||
} else {
|
||||
dir := app.nav.currDir()
|
||||
for _, path := range e.args {
|
||||
path = replaceTilde(path)
|
||||
if !filepath.IsAbs(path) {
|
||||
path = filepath.Join(dir.path, path)
|
||||
}
|
||||
if _, err := os.Lstat(path); !os.IsNotExist(err) {
|
||||
app.nav.toggleSelection(path)
|
||||
} else {
|
||||
path, err := filepath.Abs(replaceTilde(path))
|
||||
if err != nil {
|
||||
app.ui.echoerrf("toggle: %s", err)
|
||||
continue
|
||||
}
|
||||
|
||||
if _, err := os.Lstat(path); os.IsNotExist(err) {
|
||||
app.ui.echoerrf("toggle: %s", err)
|
||||
continue
|
||||
}
|
||||
|
||||
app.nav.toggleSelection(path)
|
||||
}
|
||||
}
|
||||
case "invert":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
app.nav.invert()
|
||||
case "unselect":
|
||||
app.nav.unselect()
|
||||
case "glob-select":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
if len(e.args) != 1 {
|
||||
app.ui.echoerr("glob-select: requires a pattern to match")
|
||||
return
|
||||
@@ -1191,9 +1151,6 @@ func (e *callExpr) eval(app *app, _ []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
|
||||
@@ -1203,10 +1160,6 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
return
|
||||
}
|
||||
case "copy":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
|
||||
if err := app.nav.save(clipboardCopy); err != nil {
|
||||
app.ui.echoerrf("copy: %s", err)
|
||||
return
|
||||
@@ -1224,10 +1177,6 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
}
|
||||
}
|
||||
case "cut":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
|
||||
if err := app.nav.save(clipboardCut); err != nil {
|
||||
app.ui.echoerrf("cut: %s", err)
|
||||
return
|
||||
@@ -1245,10 +1194,6 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
}
|
||||
}
|
||||
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); err != nil {
|
||||
@@ -1257,9 +1202,6 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
}
|
||||
app.ui.loadFile(app, true)
|
||||
case "clear":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
if err := saveFiles(clipboard{nil, clipboardCut}); err != nil {
|
||||
app.ui.echoerrf("clear: %s", err)
|
||||
return
|
||||
@@ -1281,9 +1223,6 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
}
|
||||
case "draw":
|
||||
case "redraw":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
app.ui.screen.Sync()
|
||||
app.ui.renew()
|
||||
app.nav.resize(app.ui)
|
||||
@@ -1291,22 +1230,15 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
app.ui.loadFile(app, true)
|
||||
onRedraw(app)
|
||||
case "load":
|
||||
if !app.nav.init || gOpts.watch {
|
||||
if gOpts.watch {
|
||||
return
|
||||
}
|
||||
app.nav.renew()
|
||||
app.ui.loadFile(app, false)
|
||||
case "reload":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
app.nav.reload()
|
||||
app.ui.loadFile(app, true)
|
||||
case "delete":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
|
||||
if cmd, ok := gOpts.cmds["delete"]; ok {
|
||||
cmd.eval(app, e.args)
|
||||
app.nav.unselect()
|
||||
@@ -1337,9 +1269,6 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
}
|
||||
}
|
||||
case "rename":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
if cmd, ok := gOpts.cmds["rename"]; ok {
|
||||
cmd.eval(app, e.args)
|
||||
if gSingleMode {
|
||||
@@ -1352,9 +1281,9 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
curr, err := app.nav.currFile()
|
||||
if err != nil {
|
||||
app.ui.echoerrf("rename: %s:", err)
|
||||
curr := app.nav.currFile()
|
||||
if curr == nil {
|
||||
app.ui.echoerr("rename: empty directory")
|
||||
return
|
||||
}
|
||||
if app.ui.cmdPrefix == ">" {
|
||||
@@ -1416,9 +1345,6 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
app.ui.cmdPrefix = "find-back: "
|
||||
app.nav.findBack = true
|
||||
case "find-next":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
dir := app.nav.currDir()
|
||||
old := dir.ind
|
||||
for range e.count {
|
||||
@@ -1432,9 +1358,6 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
app.ui.loadFile(app, true)
|
||||
}
|
||||
case "find-prev":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
dir := app.nav.currDir()
|
||||
old := dir.ind
|
||||
for range e.count {
|
||||
@@ -1448,9 +1371,6 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
app.ui.loadFile(app, true)
|
||||
}
|
||||
case "search":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
if app.ui.cmdPrefix == ">" {
|
||||
return
|
||||
}
|
||||
@@ -1461,9 +1381,6 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
app.nav.searchPos = dir.pos
|
||||
app.nav.searchBack = false
|
||||
case "search-back":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
if app.ui.cmdPrefix == ">" {
|
||||
return
|
||||
}
|
||||
@@ -1474,9 +1391,6 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
app.nav.searchPos = dir.pos
|
||||
app.nav.searchBack = true
|
||||
case "search-next":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
for range e.count {
|
||||
if app.nav.searchBack {
|
||||
if moved, err := app.nav.searchPrev(); err != nil {
|
||||
@@ -1493,9 +1407,6 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
}
|
||||
}
|
||||
case "search-prev":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
for range e.count {
|
||||
if app.nav.searchBack {
|
||||
if moved, err := app.nav.searchNext(); err != nil {
|
||||
@@ -1512,9 +1423,6 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
}
|
||||
}
|
||||
case "filter":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
if app.ui.cmdPrefix == ">" {
|
||||
return
|
||||
}
|
||||
@@ -1528,9 +1436,6 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
app.ui.cmdAccLeft = []rune(strings.Join(e.args, " "))
|
||||
}
|
||||
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)
|
||||
@@ -1557,10 +1462,6 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
app.ui.menu = listMarks(app.nav.marks)
|
||||
app.ui.cmdPrefix = "mark-remove: "
|
||||
case "tag":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
|
||||
tag := "*"
|
||||
if len(e.args) != 0 {
|
||||
tag = e.args[0]
|
||||
@@ -1582,10 +1483,6 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
}
|
||||
}
|
||||
case "tag-toggle":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
|
||||
tag := "*"
|
||||
if len(e.args) != 0 {
|
||||
tag = e.args[0]
|
||||
@@ -1618,73 +1515,38 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
path = e.args[0]
|
||||
}
|
||||
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
log.Printf("getting current directory: %s", err)
|
||||
}
|
||||
|
||||
path = replaceTilde(path)
|
||||
if !filepath.IsAbs(path) {
|
||||
path = filepath.Join(wd, path)
|
||||
} else {
|
||||
path = filepath.Clean(path)
|
||||
}
|
||||
|
||||
if wd != path {
|
||||
resetIncCmd(app)
|
||||
preChdir(app)
|
||||
}
|
||||
|
||||
if err := app.nav.cd(path); err != nil {
|
||||
app.ui.echoerrf("%s", err)
|
||||
return
|
||||
}
|
||||
|
||||
app.ui.loadFile(app, true)
|
||||
|
||||
if wd != path {
|
||||
app.nav.marks["'"] = wd
|
||||
restartIncCmd(app)
|
||||
onChdir(app)
|
||||
if err := cd(app, path); err != nil {
|
||||
app.ui.echoerrf("cd: %s", err)
|
||||
}
|
||||
case "select":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
|
||||
if len(e.args) != 1 {
|
||||
app.ui.echoerr("select: requires an argument")
|
||||
return
|
||||
}
|
||||
|
||||
wd, err := os.Getwd()
|
||||
path, err := filepath.Abs(replaceTilde(e.args[0]))
|
||||
if err != nil {
|
||||
log.Printf("getting current directory: %s", err)
|
||||
}
|
||||
|
||||
path := filepath.Dir(replaceTilde(e.args[0]))
|
||||
if !filepath.IsAbs(path) {
|
||||
path = filepath.Join(wd, path)
|
||||
} else {
|
||||
path = filepath.Clean(path)
|
||||
}
|
||||
|
||||
if wd != path {
|
||||
resetIncCmd(app)
|
||||
preChdir(app)
|
||||
}
|
||||
|
||||
if err := app.nav.sel(e.args[0]); err != nil {
|
||||
app.ui.echoerrf("%s", err)
|
||||
app.ui.echoerrf("select: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
app.ui.loadFile(app, true)
|
||||
lstat, err := os.Lstat(path)
|
||||
if err != nil {
|
||||
app.ui.echoerrf("select: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
if wd != path {
|
||||
app.nav.marks["'"] = wd
|
||||
restartIncCmd(app)
|
||||
onChdir(app)
|
||||
if err := cd(app, filepath.Dir(path)); err != nil {
|
||||
app.ui.echoerrf("select: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
dir := app.nav.currDir()
|
||||
if dir.loading {
|
||||
dir.files = append(dir.files, &file{FileInfo: lstat})
|
||||
} else {
|
||||
app.nav.currDir().sel(filepath.Base(path), app.nav.height)
|
||||
app.ui.loadFile(app, true)
|
||||
}
|
||||
case "source":
|
||||
if len(e.args) != 1 {
|
||||
@@ -1719,12 +1581,7 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
return
|
||||
}
|
||||
|
||||
dir := filepath.Dir(path)
|
||||
d, ok := app.nav.dirCache[dir]
|
||||
if !ok {
|
||||
app.ui.echoerrf("addcustominfo: dir not loaded: %s", dir)
|
||||
return
|
||||
}
|
||||
d := app.nav.getDir(filepath.Dir(path))
|
||||
|
||||
var f *file
|
||||
for _, file := range d.allFiles {
|
||||
@@ -1738,20 +1595,14 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
return
|
||||
}
|
||||
|
||||
if len(strings.Trim(v, " ")) == 0 {
|
||||
v = ""
|
||||
}
|
||||
if f.customInfo != v {
|
||||
f.customInfo = v
|
||||
// only sort when order changes
|
||||
if getSortBy(dir) == customSort {
|
||||
if getSortBy(d.path) == customSort {
|
||||
d.sort()
|
||||
}
|
||||
}
|
||||
case "calcdirsize":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
err := app.nav.calcDirSize()
|
||||
if err != nil {
|
||||
app.ui.echoerrf("calcdirsize: %s", err)
|
||||
@@ -1778,16 +1629,10 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
|
||||
tty.Write([]byte(e.args[0]))
|
||||
case "visual":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
dir := app.nav.currDir()
|
||||
dir.visualAnchor = dir.ind
|
||||
dir.visualWrap = 0
|
||||
case "visual-accept":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
dir := app.nav.currDir()
|
||||
for _, path := range dir.visualSelections() {
|
||||
if _, ok := app.nav.selections[path]; !ok {
|
||||
@@ -1800,9 +1645,6 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
dir.visualAnchor = -1
|
||||
normal(app)
|
||||
case "visual-unselect":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
dir := app.nav.currDir()
|
||||
for _, path := range dir.visualSelections() {
|
||||
delete(app.nav.selections, path)
|
||||
@@ -1813,9 +1655,6 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
dir.visualAnchor = -1
|
||||
normal(app)
|
||||
case "visual-discard":
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
dir := app.nav.currDir()
|
||||
dir.visualAnchor = -1
|
||||
normal(app)
|
||||
@@ -1951,9 +1790,9 @@ func (e *callExpr) eval(app *app, _ []string) {
|
||||
case "rename: ":
|
||||
app.ui.cmdPrefix = ""
|
||||
|
||||
curr, err := app.nav.currFile()
|
||||
if err != nil {
|
||||
app.ui.echoerrf("rename: %s", err)
|
||||
curr := app.nav.currFile()
|
||||
if curr == nil {
|
||||
app.ui.echoerr("rename: empty directory")
|
||||
return
|
||||
}
|
||||
wd, err := os.Getwd()
|
||||
|
||||
@@ -361,7 +361,7 @@ func (dir *dir) name() string {
|
||||
}
|
||||
|
||||
func (nav *nav) isVisualMode() bool {
|
||||
return nav.init && nav.currDir().visualAnchor != -1
|
||||
return nav.currDir().visualAnchor != -1
|
||||
}
|
||||
|
||||
func (dir *dir) visualSelections() []string {
|
||||
@@ -444,8 +444,7 @@ type clipboard struct {
|
||||
}
|
||||
|
||||
type nav struct {
|
||||
init bool
|
||||
dirs []*dir
|
||||
dirPaths []string
|
||||
copyJobs int
|
||||
copyBytes int64
|
||||
copyTotal int64
|
||||
@@ -495,32 +494,30 @@ type nav struct {
|
||||
jumpListInd int
|
||||
}
|
||||
|
||||
func (nav *nav) loadDir(path string) *dir {
|
||||
d, ok := nav.dirCache[path]
|
||||
if !ok {
|
||||
go func() {
|
||||
nav.dirChan <- newDir(path)
|
||||
}()
|
||||
|
||||
d := &dir{
|
||||
loading: true,
|
||||
path: path,
|
||||
sortby: getSortBy(path),
|
||||
dircounts: getDirCounts(path),
|
||||
dirfirst: getDirFirst(path),
|
||||
dironly: getDirOnly(path),
|
||||
hidden: getHidden(path),
|
||||
reverse: getReverse(path),
|
||||
visualAnchor: -1,
|
||||
hiddenfiles: gOpts.hiddenfiles,
|
||||
ignorecase: gOpts.ignorecase,
|
||||
ignoredia: gOpts.ignoredia,
|
||||
}
|
||||
nav.dirCache[path] = d
|
||||
func (nav *nav) getDir(path string) *dir {
|
||||
if d, ok := nav.dirCache[path]; ok {
|
||||
return d
|
||||
}
|
||||
|
||||
nav.checkDir(d)
|
||||
go func() {
|
||||
nav.dirChan <- newDir(path)
|
||||
}()
|
||||
|
||||
d := &dir{
|
||||
loading: true,
|
||||
path: path,
|
||||
sortby: getSortBy(path),
|
||||
dircounts: getDirCounts(path),
|
||||
dirfirst: getDirFirst(path),
|
||||
dironly: getDirOnly(path),
|
||||
hidden: getHidden(path),
|
||||
reverse: getReverse(path),
|
||||
visualAnchor: -1,
|
||||
hiddenfiles: gOpts.hiddenfiles,
|
||||
ignorecase: gOpts.ignorecase,
|
||||
ignoredia: gOpts.ignoredia,
|
||||
}
|
||||
nav.dirCache[path] = d
|
||||
return d
|
||||
}
|
||||
|
||||
@@ -573,21 +570,26 @@ func (nav *nav) checkDir(dir *dir) {
|
||||
}
|
||||
}
|
||||
|
||||
func (nav *nav) updateDirs(wd string) {
|
||||
var dirs []*dir
|
||||
func (nav *nav) loadDirs() {
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
log.Printf("getting current directory: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
wd = filepath.Clean(wd)
|
||||
var dirPaths []string
|
||||
|
||||
for curr, base := wd, ""; !isRoot(base); curr, base = filepath.Dir(curr), filepath.Base(curr) {
|
||||
dir := nav.loadDir(curr)
|
||||
dirPaths = append(dirPaths, curr)
|
||||
|
||||
dir := nav.getDir(curr)
|
||||
if base != "" {
|
||||
dir.sel(base, nav.height)
|
||||
}
|
||||
dirs = append(dirs, dir)
|
||||
}
|
||||
|
||||
slices.Reverse(dirs)
|
||||
nav.dirs = dirs
|
||||
slices.Reverse(dirPaths)
|
||||
nav.dirPaths = dirPaths
|
||||
}
|
||||
|
||||
func newNav(ui *ui) *nav {
|
||||
@@ -655,8 +657,9 @@ func (nav *nav) cdJumpListNext() {
|
||||
}
|
||||
|
||||
func (nav *nav) renew() {
|
||||
for _, d := range nav.dirs {
|
||||
nav.checkDir(d)
|
||||
for _, path := range nav.dirPaths {
|
||||
dir := nav.getDir(path)
|
||||
nav.checkDir(dir)
|
||||
}
|
||||
|
||||
for m := range nav.selections {
|
||||
@@ -671,14 +674,16 @@ func (nav *nav) renew() {
|
||||
}
|
||||
|
||||
func (nav *nav) reload() {
|
||||
curr := nav.currFile()
|
||||
|
||||
clear(nav.dirCache)
|
||||
clear(nav.regCache)
|
||||
|
||||
curr, err := nav.currFile()
|
||||
nav.updateDirs(nav.currDir().path)
|
||||
if err == nil {
|
||||
last := nav.dirs[len(nav.dirs)-1]
|
||||
last.files = append(last.files, curr)
|
||||
nav.loadDirs()
|
||||
|
||||
if curr != nil {
|
||||
dir := nav.currDir()
|
||||
dir.files = append(dir.files, curr)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -691,8 +696,8 @@ func (nav *nav) resize(ui *ui) {
|
||||
nav.height = previewWin.h
|
||||
nav.previewWidth = previewWin.w
|
||||
|
||||
for _, dir := range nav.dirs {
|
||||
dir.boundPos(nav.height)
|
||||
for _, path := range nav.dirPaths {
|
||||
nav.getDir(path).boundPos(nav.height)
|
||||
}
|
||||
|
||||
clear(nav.regCache)
|
||||
@@ -700,24 +705,22 @@ func (nav *nav) resize(ui *ui) {
|
||||
}
|
||||
|
||||
func (nav *nav) position() {
|
||||
if !nav.init {
|
||||
return
|
||||
}
|
||||
var path string
|
||||
var base string
|
||||
|
||||
path := nav.currDir().path
|
||||
for i := len(nav.dirs) - 2; i >= 0; i-- {
|
||||
nav.dirs[i].sel(filepath.Base(path), nav.height)
|
||||
path = filepath.Dir(path)
|
||||
for i := len(nav.dirPaths) - 1; i > 0; i-- {
|
||||
path = nav.dirPaths[i]
|
||||
if i < len(nav.dirPaths)-1 {
|
||||
nav.getDir(path).sel(base, nav.height)
|
||||
}
|
||||
|
||||
base = filepath.Base(path)
|
||||
}
|
||||
}
|
||||
|
||||
func (nav *nav) exportFiles() {
|
||||
if !nav.init {
|
||||
return
|
||||
}
|
||||
|
||||
var currFile string
|
||||
if curr, err := nav.currFile(); err == nil {
|
||||
if curr := nav.currFile(); curr != nil {
|
||||
currFile = quoteString(curr.path)
|
||||
}
|
||||
|
||||
@@ -836,7 +839,7 @@ func matchPattern(pattern, name, path string) bool {
|
||||
}
|
||||
|
||||
func (nav *nav) preload() {
|
||||
if !nav.init || !gOpts.preview || !gOpts.preload {
|
||||
if !gOpts.preview || !gOpts.preload {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1008,22 +1011,18 @@ func (nav *nav) startPreview() {
|
||||
}
|
||||
|
||||
func (nav *nav) sort() {
|
||||
if !nav.init {
|
||||
return
|
||||
for _, path := range nav.dirPaths {
|
||||
dir := nav.getDir(path)
|
||||
name := dir.name()
|
||||
dir.sort()
|
||||
dir.sel(name, nav.height)
|
||||
}
|
||||
|
||||
for _, d := range nav.dirs {
|
||||
name := d.name()
|
||||
d.sort()
|
||||
d.sel(name, nav.height)
|
||||
}
|
||||
|
||||
if curr, err := nav.currFile(); err == nil {
|
||||
if d, ok := nav.dirCache[curr.path]; ok {
|
||||
name := d.name()
|
||||
d.sort()
|
||||
d.sel(name, nav.height)
|
||||
}
|
||||
if curr := nav.currFile(); curr != nil && curr.IsDir() {
|
||||
dir := nav.getDir(curr.path)
|
||||
name := dir.name()
|
||||
dir.sort()
|
||||
dir.sel(name, nav.height)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1129,37 +1128,29 @@ func (nav *nav) scrollDown(dist int) bool {
|
||||
}
|
||||
|
||||
func (nav *nav) updir() error {
|
||||
if len(nav.dirs) <= 1 {
|
||||
if len(nav.dirPaths) < 2 {
|
||||
return nil
|
||||
}
|
||||
|
||||
dir := nav.currDir()
|
||||
|
||||
nav.dirs = nav.dirs[:len(nav.dirs)-1]
|
||||
|
||||
if err := os.Chdir(filepath.Dir(dir.path)); err != nil {
|
||||
if err := os.Chdir(nav.dirPaths[len(nav.dirPaths)-2]); err != nil {
|
||||
return fmt.Errorf("updir: %w", err)
|
||||
}
|
||||
|
||||
nav.dirPaths = nav.dirPaths[:len(nav.dirPaths)-1]
|
||||
return nil
|
||||
}
|
||||
|
||||
func (nav *nav) open() error {
|
||||
curr, err := nav.currFile()
|
||||
if err != nil {
|
||||
return fmt.Errorf("open: %w", err)
|
||||
}
|
||||
|
||||
path := curr.path
|
||||
|
||||
dir := nav.loadDir(path)
|
||||
|
||||
nav.dirs = append(nav.dirs, dir)
|
||||
|
||||
if err := os.Chdir(path); err != nil {
|
||||
curr := nav.currFile()
|
||||
if curr == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := os.Chdir(curr.path); err != nil {
|
||||
return fmt.Errorf("open: %w", err)
|
||||
}
|
||||
|
||||
nav.dirPaths = append(nav.dirPaths, curr.path)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1265,12 +1256,9 @@ func (nav *nav) toggleSelection(path string) {
|
||||
}
|
||||
|
||||
func (nav *nav) toggle() {
|
||||
curr, err := nav.currFile()
|
||||
if err != nil {
|
||||
return
|
||||
if curr := nav.currFile(); curr != nil {
|
||||
nav.toggleSelection(curr.path)
|
||||
}
|
||||
|
||||
nav.toggleSelection(curr.path)
|
||||
}
|
||||
|
||||
func (nav *nav) tagToggleSelection(path, tag string) {
|
||||
@@ -1316,10 +1304,8 @@ func (nav *nav) tag(tag string) error {
|
||||
}
|
||||
|
||||
func (nav *nav) invert() {
|
||||
dir := nav.currDir()
|
||||
for _, f := range dir.files {
|
||||
path := filepath.Join(dir.path, f.Name())
|
||||
nav.toggleSelection(path)
|
||||
for _, file := range nav.currDir().files {
|
||||
nav.toggleSelection(file.path)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1589,7 +1575,8 @@ func (nav *nav) rename() error {
|
||||
// this clears only the current instance of lf, and not any other instances.
|
||||
deletePathRecursive(nav.regCache, newPath)
|
||||
deletePathRecursive(nav.dirCache, newPath)
|
||||
dir := nav.loadDir(filepath.Dir(newPath))
|
||||
dir := nav.getDir(filepath.Dir(newPath))
|
||||
nav.checkDir(dir)
|
||||
|
||||
if dir.loading {
|
||||
for i := range dir.allFiles {
|
||||
@@ -1632,56 +1619,16 @@ func (nav *nav) sync() error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (nav *nav) cd(wd string) error {
|
||||
wd = replaceTilde(wd)
|
||||
wd = filepath.Clean(wd)
|
||||
|
||||
if !filepath.IsAbs(wd) {
|
||||
wd = filepath.Join(nav.currDir().path, wd)
|
||||
func (nav *nav) cd(path string) error {
|
||||
if err := os.Chdir(path); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := os.Chdir(wd); err != nil {
|
||||
return fmt.Errorf("cd: %w", err)
|
||||
}
|
||||
|
||||
nav.updateDirs(wd)
|
||||
nav.loadDirs()
|
||||
nav.addJumpList()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (nav *nav) sel(path string) error {
|
||||
path = replaceTilde(path)
|
||||
path = filepath.Clean(path)
|
||||
|
||||
lstat, err := os.Lstat(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("select: %w", err)
|
||||
}
|
||||
|
||||
dir := filepath.Dir(path)
|
||||
|
||||
if err := nav.cd(dir); err != nil {
|
||||
return fmt.Errorf("select: %w", err)
|
||||
}
|
||||
|
||||
base := filepath.Base(path)
|
||||
|
||||
last := nav.currDir()
|
||||
|
||||
if last.loading {
|
||||
last.files = append(last.files, &file{FileInfo: lstat})
|
||||
}
|
||||
|
||||
for i, f := range last.files {
|
||||
if f.Name() == base {
|
||||
nav.move(i)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (nav *nav) globSel(pattern string, invert bool) error {
|
||||
dir := nav.currDir()
|
||||
anyMatched := false
|
||||
@@ -2002,17 +1949,22 @@ func (nav *nav) writeTags() error {
|
||||
}
|
||||
|
||||
func (nav *nav) currDir() *dir {
|
||||
return nav.dirs[len(nav.dirs)-1]
|
||||
}
|
||||
|
||||
func (nav *nav) currFile() (*file, error) {
|
||||
dir := nav.dirs[len(nav.dirs)-1]
|
||||
|
||||
if len(dir.files) == 0 {
|
||||
return nil, errors.New("empty directory")
|
||||
if len(nav.dirPaths) == 0 {
|
||||
nav.loadDirs()
|
||||
}
|
||||
|
||||
return dir.files[dir.ind], nil
|
||||
path := nav.dirPaths[len(nav.dirPaths)-1]
|
||||
return nav.getDir(path)
|
||||
}
|
||||
|
||||
func (nav *nav) currFile() *file {
|
||||
dir := nav.currDir()
|
||||
|
||||
if len(dir.files) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return dir.files[dir.ind]
|
||||
}
|
||||
|
||||
type indexedSelections struct {
|
||||
@@ -2050,16 +2002,15 @@ func (nav *nav) currSelections() []string {
|
||||
}
|
||||
|
||||
func (nav *nav) currFileOrSelections() (list []string, err error) {
|
||||
sel := nav.currSelections()
|
||||
if sel := nav.currSelections(); len(sel) > 0 {
|
||||
return sel, nil
|
||||
}
|
||||
|
||||
if len(sel) == 0 {
|
||||
curr, err := nav.currFile()
|
||||
if err != nil {
|
||||
return nil, errors.New("no file selected")
|
||||
}
|
||||
if curr := nav.currFile(); curr != nil {
|
||||
return []string{curr.path}, nil
|
||||
}
|
||||
return sel, nil
|
||||
|
||||
return nil, errors.New("no file selected")
|
||||
}
|
||||
|
||||
func (nav *nav) calcDirSize() error {
|
||||
@@ -2075,8 +2026,8 @@ func (nav *nav) calcDirSize() error {
|
||||
}
|
||||
|
||||
if len(nav.selections) == 0 {
|
||||
curr, err := nav.currFile()
|
||||
if err != nil {
|
||||
curr := nav.currFile()
|
||||
if curr == nil {
|
||||
return errors.New("no file selected")
|
||||
}
|
||||
return calc(curr)
|
||||
@@ -2088,7 +2039,7 @@ func (nav *nav) calcDirSize() error {
|
||||
continue
|
||||
}
|
||||
path, name := filepath.Dir(sel), filepath.Base(sel)
|
||||
dir := nav.loadDir(path)
|
||||
dir := nav.getDir(path)
|
||||
|
||||
for _, f := range dir.files {
|
||||
if f.Name() == name {
|
||||
|
||||
@@ -764,12 +764,8 @@ type reg struct {
|
||||
}
|
||||
|
||||
func (ui *ui) loadFile(app *app, volatile bool) {
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
|
||||
curr, err := app.nav.currFile()
|
||||
if err != nil {
|
||||
curr := app.nav.currFile()
|
||||
if curr == nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -789,7 +785,8 @@ func (ui *ui) loadFile(app *app, volatile bool) {
|
||||
if curr.isPreviewable() {
|
||||
app.nav.loadReg(curr.path, volatile)
|
||||
} else if curr.IsDir() {
|
||||
app.nav.loadDir(curr.path)
|
||||
dir := app.nav.getDir(curr.path)
|
||||
app.nav.checkDir(dir)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -806,8 +803,7 @@ func (ui *ui) drawPromptLine(nav *nav) {
|
||||
sep := string(filepath.Separator)
|
||||
|
||||
var fname string
|
||||
curr, err := nav.currFile()
|
||||
if err == nil {
|
||||
if curr := nav.currFile(); curr != nil {
|
||||
fname = filepath.Base(curr.path)
|
||||
}
|
||||
|
||||
@@ -872,8 +868,8 @@ func (ui *ui) drawStat(nav *nav) {
|
||||
return
|
||||
}
|
||||
|
||||
curr, err := nav.currFile()
|
||||
if err != nil {
|
||||
curr := nav.currFile()
|
||||
if curr == nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1026,8 +1022,8 @@ func (ui *ui) drawRulerFile(nav *nav) {
|
||||
}
|
||||
|
||||
var stat *statData
|
||||
curr, err := nav.currFile()
|
||||
if err == nil {
|
||||
curr := nav.currFile()
|
||||
if curr != nil {
|
||||
if curr.err == nil {
|
||||
stat = &statData{
|
||||
Path: curr.path,
|
||||
@@ -1150,8 +1146,8 @@ func (ui *ui) drawRulerFile(nav *nav) {
|
||||
}
|
||||
|
||||
func (ui *ui) drawPreview(nav *nav, context *dirContext) {
|
||||
curr, err := nav.currFile()
|
||||
if err != nil {
|
||||
curr := nav.currFile()
|
||||
if curr == nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1165,10 +1161,9 @@ func (ui *ui) drawPreview(nav *nav, context *dirContext) {
|
||||
}
|
||||
} else if curr.IsDir() {
|
||||
ui.sxScreen.lastFile = ""
|
||||
if dir, ok := nav.dirCache[curr.path]; ok {
|
||||
dirStyle := &dirStyle{colors: ui.styles, icons: ui.icons, role: Preview}
|
||||
win.printDir(ui, dir, context, dirStyle)
|
||||
}
|
||||
dir := nav.getDir(curr.path)
|
||||
dirStyle := &dirStyle{colors: ui.styles, icons: ui.icons, role: Preview}
|
||||
win.printDir(ui, dir, context, dirStyle)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1248,11 +1243,11 @@ func (ui *ui) dirOfWin(nav *nav, wind int) *dir {
|
||||
if gOpts.preview {
|
||||
wins--
|
||||
}
|
||||
ind := len(nav.dirs) - wins + wind
|
||||
ind := len(nav.dirPaths) - wins + wind
|
||||
if ind < 0 {
|
||||
return nil
|
||||
}
|
||||
return nav.dirs[ind]
|
||||
return nav.getDir(nav.dirPaths[ind])
|
||||
}
|
||||
|
||||
func (ui *ui) draw(nav *nav) {
|
||||
@@ -1475,9 +1470,6 @@ func listMarks(marks map[string]string) string {
|
||||
}
|
||||
|
||||
func listFilesInCurrDir(nav *nav) string {
|
||||
if !nav.init {
|
||||
return ""
|
||||
}
|
||||
dir := nav.currDir()
|
||||
if dir.loading {
|
||||
log.Printf("listFilesInCurrDir(): %s is still loading, `files` isn't ready for remote query", dir.path)
|
||||
@@ -1704,8 +1696,8 @@ func (ui *ui) readNormalEvent(ev tcell.Event, nav *nav) expr {
|
||||
|
||||
var dir *dir
|
||||
if gOpts.preview && wind == len(ui.wins)-1 {
|
||||
curr, err := nav.currFile()
|
||||
if err != nil {
|
||||
curr := nav.currFile()
|
||||
if curr == nil {
|
||||
return nil
|
||||
} else if !curr.IsDir() || gOpts.dirpreviews {
|
||||
if tev.Buttons() != tcell.Button2 {
|
||||
@@ -1714,11 +1706,7 @@ func (ui *ui) readNormalEvent(ev tcell.Event, nav *nav) expr {
|
||||
return &callExpr{"open", nil, 1}
|
||||
}
|
||||
|
||||
var ok bool
|
||||
dir, ok = nav.dirCache[curr.path]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
dir = nav.getDir(curr.path)
|
||||
} else {
|
||||
dir = ui.dirOfWin(nav, wind)
|
||||
if dir == nil {
|
||||
|
||||
Reference in new issue
Block a user