Avoid redundantly loading previews for search/find (#569)

* Avoid redundantly loading previews for find

* Avoid redundantly loading previews for search
This commit is contained in:
neeshy 2021-01-29 15:27:27 +00:00 committed by GitHub
parent c6cf0e61ba
commit 1163efb7aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 83 additions and 84 deletions

95
eval.go
View File

@ -393,31 +393,31 @@ func update(app *app) {
app.nav.search = string(app.ui.cmdAccLeft) + string(app.ui.cmdAccRight) app.nav.search = string(app.ui.cmdAccLeft) + string(app.ui.cmdAccRight)
dir := app.nav.currDir() dir := app.nav.currDir()
old := dir.ind
dir.ind = app.nav.searchInd dir.ind = app.nav.searchInd
dir.pos = app.nav.searchPos dir.pos = app.nav.searchPos
if err := app.nav.searchNext(); err != nil { if _, err := app.nav.searchNext(); err != nil {
app.ui.echoerrf("search: %s: %s", err, app.nav.search) app.ui.echoerrf("search: %s: %s", err, app.nav.search)
return } else if old != dir.ind {
}
app.ui.loadFile(app.nav, true) app.ui.loadFile(app.nav, true)
app.ui.loadFileInfo(app.nav) app.ui.loadFileInfo(app.nav)
}
case gOpts.incsearch && app.ui.cmdPrefix == "?": case gOpts.incsearch && app.ui.cmdPrefix == "?":
app.nav.search = string(app.ui.cmdAccLeft) + string(app.ui.cmdAccRight) app.nav.search = string(app.ui.cmdAccLeft) + string(app.ui.cmdAccRight)
dir := app.nav.currDir() dir := app.nav.currDir()
old := dir.ind
dir.ind = app.nav.searchInd dir.ind = app.nav.searchInd
dir.pos = app.nav.searchPos dir.pos = app.nav.searchPos
if err := app.nav.searchPrev(); err != nil { if _, err := app.nav.searchPrev(); err != nil {
app.ui.echoerrf("search: %s: %s", err, app.nav.search) app.ui.echoerrf("search: %s: %s", err, app.nav.search)
return } else if old != dir.ind {
}
app.ui.loadFile(app.nav, true) app.ui.loadFile(app.nav, true)
app.ui.loadFileInfo(app.nav) app.ui.loadFileInfo(app.nav)
} }
}
} }
func normal(app *app) { func normal(app *app) {
@ -455,9 +455,9 @@ func insert(app *app, arg string) {
return return
} }
if !app.nav.findNext() { if moved, found := app.nav.findNext(); !found {
app.ui.echoerrf("find: pattern not found: %s", app.nav.find) app.ui.echoerrf("find: pattern not found: %s", app.nav.find)
} else { } else if moved {
app.ui.loadFile(app.nav, true) app.ui.loadFile(app.nav, true)
app.ui.loadFileInfo(app.nav) app.ui.loadFileInfo(app.nav)
} }
@ -484,9 +484,9 @@ func insert(app *app, arg string) {
return return
} }
if !app.nav.findPrev() { if moved, found := app.nav.findPrev(); !found {
app.ui.echoerrf("find-back: pattern not found: %s", app.nav.find) app.ui.echoerrf("find-back: pattern not found: %s", app.nav.find)
} else { } else if moved {
app.ui.loadFile(app.nav, true) app.ui.loadFile(app.nav, true)
app.ui.loadFileInfo(app.nav) app.ui.loadFileInfo(app.nav)
} }
@ -854,6 +854,8 @@ func (e *callExpr) eval(app *app, args []string) {
app.nav.findBack = true app.nav.findBack = true
app.ui.loadFileInfo(app.nav) app.ui.loadFileInfo(app.nav)
case "find-next": case "find-next":
dir := app.nav.currDir()
old := dir.ind
for i := 0; i < e.count; i++ { for i := 0; i < e.count; i++ {
if app.nav.findBack { if app.nav.findBack {
app.nav.findPrev() app.nav.findPrev()
@ -861,9 +863,13 @@ func (e *callExpr) eval(app *app, args []string) {
app.nav.findNext() app.nav.findNext()
} }
} }
if old != dir.ind {
app.ui.loadFile(app.nav, true) app.ui.loadFile(app.nav, true)
app.ui.loadFileInfo(app.nav) app.ui.loadFileInfo(app.nav)
}
case "find-prev": case "find-prev":
dir := app.nav.currDir()
old := dir.ind
for i := 0; i < e.count; i++ { for i := 0; i < e.count; i++ {
if app.nav.findBack { if app.nav.findBack {
app.nav.findNext() app.nav.findNext()
@ -871,8 +877,10 @@ func (e *callExpr) eval(app *app, args []string) {
app.nav.findPrev() app.nav.findPrev()
} }
} }
if old != dir.ind {
app.ui.loadFile(app.nav, true) app.ui.loadFile(app.nav, true)
app.ui.loadFileInfo(app.nav) app.ui.loadFileInfo(app.nav)
}
case "search": case "search":
app.ui.cmdPrefix = "/" app.ui.cmdPrefix = "/"
dir := app.nav.currDir() dir := app.nav.currDir()
@ -890,35 +898,39 @@ func (e *callExpr) eval(app *app, args []string) {
case "search-next": case "search-next":
for i := 0; i < e.count; i++ { for i := 0; i < e.count; i++ {
if app.nav.searchBack { if app.nav.searchBack {
if err := app.nav.searchPrev(); err != nil { if moved, err := app.nav.searchPrev(); err != nil {
app.ui.echoerrf("search-back: %s: %s", err, app.nav.search) app.ui.echoerrf("search-back: %s: %s", err, app.nav.search)
return } else if moved {
}
} else {
if err := app.nav.searchNext(); err != nil {
app.ui.echoerrf("search: %s: %s", err, app.nav.search)
return
}
}
}
app.ui.loadFile(app.nav, true) app.ui.loadFile(app.nav, true)
app.ui.loadFileInfo(app.nav) app.ui.loadFileInfo(app.nav)
}
} else {
if moved, err := app.nav.searchNext(); err != nil {
app.ui.echoerrf("search: %s: %s", err, app.nav.search)
} else if moved {
app.ui.loadFile(app.nav, true)
app.ui.loadFileInfo(app.nav)
}
}
}
case "search-prev": case "search-prev":
for i := 0; i < e.count; i++ { for i := 0; i < e.count; i++ {
if app.nav.searchBack { if app.nav.searchBack {
if err := app.nav.searchNext(); err != nil { if moved, err := app.nav.searchNext(); err != nil {
app.ui.echoerrf("search-back: %s: %s", err, app.nav.search) app.ui.echoerrf("search-back: %s: %s", err, app.nav.search)
return } else if moved {
}
} else {
if err := app.nav.searchPrev(); err != nil {
app.ui.echoerrf("search: %s: %s", err, app.nav.search)
return
}
}
}
app.ui.loadFile(app.nav, true) app.ui.loadFile(app.nav, true)
app.ui.loadFileInfo(app.nav) app.ui.loadFileInfo(app.nav)
}
} else {
if moved, err := app.nav.searchPrev(); err != nil {
app.ui.echoerrf("search: %s: %s", err, app.nav.search)
} else if moved {
app.ui.loadFile(app.nav, true)
app.ui.loadFileInfo(app.nav)
}
}
}
case "mark-save": case "mark-save":
app.ui.cmdPrefix = "mark-save: " app.ui.cmdPrefix = "mark-save: "
case "mark-load": case "mark-load":
@ -1067,12 +1079,13 @@ func (e *callExpr) eval(app *app, args []string) {
} }
if gOpts.incsearch && (app.ui.cmdPrefix == "/" || app.ui.cmdPrefix == "?") { if gOpts.incsearch && (app.ui.cmdPrefix == "/" || app.ui.cmdPrefix == "?") {
dir := app.nav.currDir() dir := app.nav.currDir()
dir.ind = app.nav.searchInd
dir.pos = app.nav.searchPos dir.pos = app.nav.searchPos
if dir.ind != app.nav.searchInd {
dir.ind = app.nav.searchInd
app.ui.loadFile(app.nav, true) app.ui.loadFile(app.nav, true)
app.ui.loadFileInfo(app.nav) app.ui.loadFileInfo(app.nav)
} }
}
normal(app) normal(app)
case "cmd-complete": case "cmd-complete":
var matches []string var matches []string
@ -1235,32 +1248,34 @@ func (e *callExpr) eval(app *app, args []string) {
app.cmdHistory = append(app.cmdHistory, cmdItem{"&", s}) app.cmdHistory = append(app.cmdHistory, cmdItem{"&", s})
app.runShell(s, nil, "&") app.runShell(s, nil, "&")
case "/": case "/":
if gOpts.incsearch {
dir := app.nav.currDir() dir := app.nav.currDir()
old := dir.ind
if gOpts.incsearch {
dir.ind = app.nav.searchInd dir.ind = app.nav.searchInd
dir.pos = app.nav.searchPos dir.pos = app.nav.searchPos
} }
log.Printf("search: %s", s) log.Printf("search: %s", s)
app.ui.cmdPrefix = "" app.ui.cmdPrefix = ""
app.nav.search = s app.nav.search = s
if err := app.nav.searchNext(); err != nil { if _, err := app.nav.searchNext(); err != nil {
app.ui.echoerrf("search: %s: %s", err, app.nav.search) app.ui.echoerrf("search: %s: %s", err, app.nav.search)
} else { } else if old != dir.ind {
app.ui.loadFile(app.nav, true) app.ui.loadFile(app.nav, true)
app.ui.loadFileInfo(app.nav) app.ui.loadFileInfo(app.nav)
} }
case "?": case "?":
if gOpts.incsearch {
dir := app.nav.currDir() dir := app.nav.currDir()
old := dir.ind
if gOpts.incsearch {
dir.ind = app.nav.searchInd dir.ind = app.nav.searchInd
dir.pos = app.nav.searchPos dir.pos = app.nav.searchPos
} }
log.Printf("search-back: %s", s) log.Printf("search-back: %s", s)
app.ui.cmdPrefix = "" app.ui.cmdPrefix = ""
app.nav.search = s app.nav.search = s
if err := app.nav.searchPrev(); err != nil { if _, err := app.nav.searchPrev(); err != nil {
app.ui.echoerrf("search-back: %s: %s", err, app.nav.search) app.ui.echoerrf("search-back: %s: %s", err, app.nav.search)
} else { } else if old != dir.ind {
app.ui.loadFile(app.nav, true) app.ui.loadFile(app.nav, true)
app.ui.loadFileInfo(app.nav) app.ui.loadFileInfo(app.nav)
} }

72
nav.go
View File

@ -1123,42 +1123,38 @@ func (nav *nav) findSingle() int {
return count return count
} }
func (nav *nav) findNext() bool { func (nav *nav) findNext() (bool, bool) {
dir := nav.currDir() dir := nav.currDir()
for i := dir.ind + 1; i < len(dir.files); i++ { for i := dir.ind + 1; i < len(dir.files); i++ {
if findMatch(dir.files[i].Name(), nav.find) { if findMatch(dir.files[i].Name(), nav.find) {
nav.down(i - dir.ind) return nav.down(i - dir.ind), true
return true
} }
} }
if gOpts.wrapscan { if gOpts.wrapscan {
for i := 0; i < dir.ind; i++ { for i := 0; i < dir.ind; i++ {
if findMatch(dir.files[i].Name(), nav.find) { if findMatch(dir.files[i].Name(), nav.find) {
nav.up(dir.ind - i) return nav.up(dir.ind - i), true
return true
} }
} }
} }
return false return false, false
} }
func (nav *nav) findPrev() bool { func (nav *nav) findPrev() (bool, bool) {
dir := nav.currDir() dir := nav.currDir()
for i := dir.ind - 1; i >= 0; i-- { for i := dir.ind - 1; i >= 0; i-- {
if findMatch(dir.files[i].Name(), nav.find) { if findMatch(dir.files[i].Name(), nav.find) {
nav.up(dir.ind - i) return nav.up(dir.ind - i), true
return true
} }
} }
if gOpts.wrapscan { if gOpts.wrapscan {
for i := len(dir.files) - 1; i > dir.ind; i-- { for i := len(dir.files) - 1; i > dir.ind; i-- {
if findMatch(dir.files[i].Name(), nav.find) { if findMatch(dir.files[i].Name(), nav.find) {
nav.down(i - dir.ind) return nav.down(i - dir.ind), true
return true
} }
} }
} }
return false return false, false
} }
func searchMatch(name, pattern string) (matched bool, err error) { func searchMatch(name, pattern string) (matched bool, err error) {
@ -1182,58 +1178,46 @@ func searchMatch(name, pattern string) (matched bool, err error) {
return strings.Contains(name, pattern), nil return strings.Contains(name, pattern), nil
} }
func (nav *nav) searchNext() error { func (nav *nav) searchNext() (bool, error) {
dir := nav.currDir() dir := nav.currDir()
for i := dir.ind + 1; i < len(dir.files); i++ { for i := dir.ind + 1; i < len(dir.files); i++ {
matched, err := searchMatch(dir.files[i].Name(), nav.search) if matched, err := searchMatch(dir.files[i].Name(), nav.search); err != nil {
if err != nil { return false, err
return err } else if matched {
} return nav.down(i - dir.ind), nil
if matched {
nav.down(i - dir.ind)
return nil
} }
} }
if gOpts.wrapscan { if gOpts.wrapscan {
for i := 0; i < dir.ind; i++ { for i := 0; i < dir.ind; i++ {
matched, err := searchMatch(dir.files[i].Name(), nav.search) if matched, err := searchMatch(dir.files[i].Name(), nav.search); err != nil {
if err != nil { return false, err
return err } else if matched {
} return nav.up(dir.ind - i), nil
if matched {
nav.up(dir.ind - i)
return nil
} }
} }
} }
return nil return false, nil
} }
func (nav *nav) searchPrev() error { func (nav *nav) searchPrev() (bool, error) {
dir := nav.currDir() dir := nav.currDir()
for i := dir.ind - 1; i >= 0; i-- { for i := dir.ind - 1; i >= 0; i-- {
matched, err := searchMatch(dir.files[i].Name(), nav.search) if matched, err := searchMatch(dir.files[i].Name(), nav.search); err != nil {
if err != nil { return false, err
return err } else if matched {
} return nav.up(dir.ind - i), nil
if matched {
nav.up(dir.ind - i)
return nil
} }
} }
if gOpts.wrapscan { if gOpts.wrapscan {
for i := len(dir.files) - 1; i > dir.ind; i-- { for i := len(dir.files) - 1; i > dir.ind; i-- {
matched, err := searchMatch(dir.files[i].Name(), nav.search) if matched, err := searchMatch(dir.files[i].Name(), nav.search); err != nil {
if err != nil { return false, err
return err } else if matched {
} return nav.down(i - dir.ind), nil
if matched {
nav.down(i - dir.ind)
return nil
} }
} }
} }
return nil return false, nil
} }
func (nav *nav) removeMark(mark string) error { func (nav *nav) removeMark(mark string) error {