From 011cca76d16456eebbf36a36b687afaea04b606e Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Wed, 22 Aug 2018 20:05:22 +0300 Subject: [PATCH] add find commands to find as you type Related #68 --- app.go | 2 +- complete.go | 4 +++ doc.go | 4 +++ docstring.go | 4 +++ eval.go | 52 ++++++++++++++++++++++++++++++++++- lf.1 | 4 +++ nav.go | 76 +++++++++++++++++++++++++++++++++++++++++++--------- opts.go | 4 +++ ui.go | 2 +- 9 files changed, 136 insertions(+), 16 deletions(-) diff --git a/app.go b/app.go index cb2d834a..522d3174 100644 --- a/app.go +++ b/app.go @@ -168,7 +168,7 @@ func (app *app) loop() { case d := <-app.nav.dirChan: prev, ok := app.nav.dirCache[d.path] if ok { - d.find(prev.name(), app.nav.height) + d.sel(prev.name(), app.nav.height) } app.nav.dirCache[d.path] = d diff --git a/complete.go b/complete.go index 5ec75368..812fa529 100644 --- a/complete.go +++ b/complete.go @@ -41,6 +41,10 @@ var ( "shell-pipe", "shell-wait", "shell-async", + "find", + "find-back", + "find-next", + "find-prev", "search", "search-back", "search-next", diff --git a/doc.go b/doc.go index 81d7e6a7..89eefeb8 100644 --- a/doc.go +++ b/doc.go @@ -41,6 +41,10 @@ The following commands are provided by lf with default keybindings: shell-pipe (default '%') shell-wait (default '!') shell-async (default '&') + find (default 'f') + find-back (default 'F') + find-next (default ';') + find-prev (default ',') search (default '/') search-back (default '?') search-next (default 'n') diff --git a/docstring.go b/docstring.go index 7fc20f3f..2a4479ed 100644 --- a/docstring.go +++ b/docstring.go @@ -44,6 +44,10 @@ The following commands are provided by lf with default keybindings: shell-pipe (default '%') shell-wait (default '!') shell-async (default '&') + find (default 'f') + find-back (default 'F') + find-next (default ';') + find-prev (default ',') search (default '/') search-back (default '?') search-next (default 'n') diff --git a/eval.go b/eval.go index 51666756..95074e95 100644 --- a/eval.go +++ b/eval.go @@ -417,6 +417,22 @@ func (e *callExpr) eval(app *app, args []string) { app.ui.cmdPrefix = "!" case "shell-async": app.ui.cmdPrefix = "&" + case "find": + app.ui.cmdPrefix = "find: " + case "find-back": + app.ui.cmdPrefix = "find-back: " + case "find-next": + for i := 0; i < e.count; i++ { + app.nav.findNext() + } + app.ui.loadFile(app.nav) + app.ui.loadFileInfo(app.nav) + case "find-prev": + for i := 0; i < e.count; i++ { + app.nav.findPrev() + } + app.ui.loadFile(app.nav) + app.ui.loadFileInfo(app.nav) case "search": app.ui.cmdPrefix = "/" case "search-back": @@ -489,7 +505,7 @@ func (e *callExpr) eval(app *app, args []string) { log.Printf("getting current directory: %s", err) } - if err := app.nav.find(e.args[0]); err != nil { + if err := app.nav.sel(e.args[0]); err != nil { app.ui.printf("%s", err) return } @@ -527,6 +543,40 @@ func (e *callExpr) eval(app *app, args []string) { return } switch app.ui.cmdPrefix { + case "find: ": + app.ui.menuBuf = nil + app.ui.cmdAccLeft = nil + app.ui.cmdAccRight = nil + app.ui.cmdPrefix = "" + + app.nav.find = e.args[0] + + if !app.nav.findNext() { + app.ui.print("pattern not found") + return + } + + app.ui.loadFile(app.nav) + app.ui.loadFileInfo(app.nav) + + return + case "find-back: ": + app.ui.menuBuf = nil + app.ui.cmdAccLeft = nil + app.ui.cmdAccRight = nil + app.ui.cmdPrefix = "" + + app.nav.find = e.args[0] + + if !app.nav.findPrev() { + app.ui.print("pattern not found") + return + } + + app.ui.loadFile(app.nav) + app.ui.loadFileInfo(app.nav) + + return case "mark-save: ": app.ui.menuBuf = nil app.ui.cmdAccLeft = nil diff --git a/lf.1 b/lf.1 index 0c707f46..66c66ee0 100644 --- a/lf.1 +++ b/lf.1 @@ -51,6 +51,10 @@ The following commands are provided by lf with default keybindings: shell-pipe (default '%') shell-wait (default '!') shell-async (default '&') + find (default 'f') + find-back (default 'F') + find-next (default ';') + find-prev (default ',') search (default '/') search-back (default '?') search-next (default 'n') diff --git a/nav.go b/nav.go index 73534bef..684647b9 100644 --- a/nav.go +++ b/nav.go @@ -166,7 +166,7 @@ func (dir *dir) name() string { return dir.files[dir.ind].Name() } -func (dir *dir) find(name string, height int) { +func (dir *dir) sel(name string, height int) { if len(dir.files) == 0 { dir.ind, dir.pos = 0, 0 return @@ -198,6 +198,7 @@ type nav struct { selections map[string]int selectionInd int height int + find string search string } @@ -226,7 +227,7 @@ func (nav *nav) loadDir(path string) *dir { d.loadTime = time.Now() nd := newDir(path) nd.sort() - nd.find(d.name(), nav.height) + nd.sel(d.name(), nav.height) nav.dirChan <- nd }() case d.sortType != gOpts.sortType: @@ -234,7 +235,7 @@ func (nav *nav) loadDir(path string) *dir { d.loading = true name := d.name() d.sort() - d.find(name, nav.height) + d.sel(name, nav.height) d.loading = false nav.dirChan <- d }() @@ -248,7 +249,7 @@ func (nav *nav) getDirs(wd string) { for curr, base := wd, ""; !isRoot(base); curr, base = filepath.Dir(curr), filepath.Base(curr) { dir := nav.loadDir(curr) - dir.find(base, nav.height) + dir.sel(base, nav.height) dirs = append(dirs, dir) } @@ -332,7 +333,7 @@ func (nav *nav) reload() error { func (nav *nav) position() { path := nav.currDir().path for i := len(nav.dirs) - 2; i >= 0; i-- { - nav.dirs[i].find(filepath.Base(path), nav.height) + nav.dirs[i].sel(filepath.Base(path), nav.height) path = filepath.Dir(path) } } @@ -418,7 +419,7 @@ func (nav *nav) sort() { for _, d := range nav.dirs { name := d.name() d.sort() - d.find(name, nav.height) + d.sel(name, nav.height) } } @@ -630,7 +631,7 @@ func (nav *nav) cd(wd string) error { return nil } -func (nav *nav) find(path string) error { +func (nav *nav) sel(path string) error { lstat, err := os.Stat(path) if err != nil { return fmt.Errorf("select: %s", err) @@ -648,13 +649,62 @@ func (nav *nav) find(path string) error { if last.loading { last.files = append(last.files, &file{FileInfo: lstat}) } else { - last.find(base, nav.height) + last.sel(base, nav.height) } return nil } -func match(pattern, name string) (matched bool, err error) { +func findMatch(name, pattern string) bool { + if gOpts.ignorecase { + lpattern := strings.ToLower(pattern) + if !gOpts.smartcase || lpattern == pattern { + pattern = lpattern + name = strings.ToLower(name) + } + } + return strings.HasPrefix(name, pattern) +} + +func (nav *nav) findNext() bool { + last := nav.currDir() + for i := last.ind + 1; i < len(last.files); i++ { + if findMatch(last.files[i].Name(), nav.find) { + nav.down(i - last.ind) + return true + } + } + if gOpts.wrapscan { + for i := 0; i < last.ind; i++ { + if findMatch(last.files[i].Name(), nav.find) { + nav.down(i - last.ind) + return true + } + } + } + return false +} + +func (nav *nav) findPrev() bool { + last := nav.currDir() + for i := last.ind - 1; i >= 0; i-- { + if findMatch(last.files[i].Name(), nav.find) { + nav.down(i - last.ind) + return true + } + } + if gOpts.wrapscan { + for i := len(last.files) - 1; i > last.ind; i-- { + if findMatch(last.files[i].Name(), nav.find) { + nav.down(i - last.ind) + return true + } + } + } + return false +} + +func searchMatch(name, pattern string) (matched bool, err error) { if gOpts.ignorecase { lpattern := strings.ToLower(pattern) if !gOpts.smartcase || lpattern == pattern { @@ -671,7 +721,7 @@ func match(pattern, name string) (matched bool, err error) { func (nav *nav) searchNext() error { last := nav.currDir() for i := last.ind + 1; i < len(last.files); i++ { - matched, err := match(nav.search, last.files[i].Name()) + matched, err := searchMatch(last.files[i].Name(), nav.search) if err != nil { return err } @@ -682,7 +732,7 @@ func (nav *nav) searchNext() error { } if gOpts.wrapscan { for i := 0; i < last.ind; i++ { - matched, err := match(nav.search, last.files[i].Name()) + matched, err := searchMatch(last.files[i].Name(), nav.search) if err != nil { return err } @@ -698,7 +748,7 @@ func (nav *nav) searchNext() error { func (nav *nav) searchPrev() error { last := nav.currDir() for i := last.ind - 1; i >= 0; i-- { - matched, err := match(nav.search, last.files[i].Name()) + matched, err := searchMatch(last.files[i].Name(), nav.search) if err != nil { return err } @@ -709,7 +759,7 @@ func (nav *nav) searchPrev() error { } if gOpts.wrapscan { for i := len(last.files) - 1; i > last.ind; i-- { - matched, err := match(nav.search, last.files[i].Name()) + matched, err := searchMatch(last.files[i].Name(), nav.search) if err != nil { return err } diff --git a/opts.go b/opts.go index 3187b9c2..e5820abf 100644 --- a/opts.go +++ b/opts.go @@ -105,6 +105,10 @@ func init() { gOpts.keys["%"] = &callExpr{"shell-pipe", nil, 1} gOpts.keys["!"] = &callExpr{"shell-wait", nil, 1} gOpts.keys["&"] = &callExpr{"shell-async", nil, 1} + gOpts.keys["f"] = &callExpr{"find", nil, 1} + gOpts.keys["F"] = &callExpr{"find-back", nil, 1} + gOpts.keys[";"] = &callExpr{"find-next", nil, 1} + gOpts.keys[","] = &callExpr{"find-prev", nil, 1} gOpts.keys["/"] = &callExpr{"search", nil, 1} gOpts.keys["?"] = &callExpr{"search-back", nil, 1} gOpts.keys["n"] = &callExpr{"search-next", nil, 1} diff --git a/ui.go b/ui.go index 9e95088a..bfe8db38 100644 --- a/ui.go +++ b/ui.go @@ -455,7 +455,7 @@ func (ui *ui) sort() { } name := ui.dirPrev.name() ui.dirPrev.sort() - ui.dirPrev.find(name, ui.wins[0].h) + ui.dirPrev.sel(name, ui.wins[0].h) } func (ui *ui) print(msg string) {