From f7e778591a3ad1fa68f8442cb254a99fd06f002a Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Fri, 23 Dec 2016 18:58:24 +0300 Subject: [PATCH] implement search/search-back and search-next/prev Closes #38. --- comp.go | 2 ++ doc.go | 2 ++ docstring.go | 2 ++ eval.go | 12 ++++++++---- nav.go | 21 +++++++++++++++++++++ opts.go | 2 ++ 6 files changed, 37 insertions(+), 4 deletions(-) diff --git a/comp.go b/comp.go index df196b8..a96ff06 100644 --- a/comp.go +++ b/comp.go @@ -31,6 +31,8 @@ var ( "read-shell-async", "search", "search-back", + "search-next", + "search-prev", "toggle", "invert", "yank", diff --git a/doc.go b/doc.go index ce22821..dc6c9d8 100644 --- a/doc.go +++ b/doc.go @@ -29,6 +29,8 @@ The following commands are provided by lf with default keybindings: read-shell-async (default "&") search (default "/") search-back (default "?") + search-next (default "n") + search-prev (default "N") toggle (default "") invert (default "v") yank (default "y") diff --git a/docstring.go b/docstring.go index cc43345..977a02f 100644 --- a/docstring.go +++ b/docstring.go @@ -33,6 +33,8 @@ The following commands are provided by lf with default keybindings: read-shell-async (default "&") search (default "/") search-back (default "?") + search-next (default "n") + search-prev (default "N") toggle (default "") invert (default "v") yank (default "y") diff --git a/eval.go b/eval.go index e7cc337..709d2e1 100644 --- a/eval.go +++ b/eval.go @@ -262,6 +262,10 @@ func (e *callExpr) eval(app *app, args []string) { app.ui.cmdpref = "/" case "search-back": app.ui.cmdpref = "?" + case "search-next": + app.nav.searchNext() + case "search-prev": + app.nav.searchPrev() case "toggle": app.nav.toggle() case "invert": @@ -405,12 +409,12 @@ func (e *callExpr) eval(app *app, args []string) { app.runShell(s, nil, false, true) case "/": log.Printf("search: %s", s) - app.ui.message = "sorry, search is not implemented yet!" - // TODO: implement + app.nav.search = s + app.nav.searchNext() case "?": log.Printf("search-back: %s", s) - app.ui.message = "sorry, search-back is not implemented yet!" - // TODO: implement + app.nav.search = s + app.nav.searchPrev() default: log.Printf("entering unknown execution prefix: %q", app.ui.cmdpref) } diff --git a/nav.go b/nav.go index 88c02b1..863d499 100644 --- a/nav.go +++ b/nav.go @@ -180,6 +180,7 @@ type nav struct { marks map[string]bool saves map[string]bool height int + search string } func getDirs(wd string, height int) []*dir { @@ -352,6 +353,26 @@ func (nav *nav) cd(wd string) error { return nil } +func (nav *nav) searchNext() { + last := nav.currDir() + for i := last.ind + 1; i < len(last.fi); i++ { + if strings.Contains(last.fi[i].Name(), nav.search) { + nav.down(i - last.ind) + return + } + } +} + +func (nav *nav) searchPrev() { + last := nav.currDir() + for i := last.ind - 1; i > 0; i-- { + if strings.Contains(last.fi[i].Name(), nav.search) { + nav.up(last.ind - i) + return + } + } +} + func (nav *nav) toggleMark(path string) { if nav.marks[path] { delete(nav.marks, path) diff --git a/opts.go b/opts.go index ee546d5..0ef5840 100644 --- a/opts.go +++ b/opts.go @@ -54,6 +54,8 @@ func init() { gOpts.keys["&"] = &callExpr{"read-shell-async", nil} gOpts.keys["/"] = &callExpr{"search", nil} gOpts.keys["?"] = &callExpr{"search-back", nil} + gOpts.keys["n"] = &callExpr{"search-next", nil} + gOpts.keys["N"] = &callExpr{"search-prev", nil} gOpts.keys[""] = &callExpr{"toggle", nil} gOpts.keys["v"] = &callExpr{"invert", nil} gOpts.keys["y"] = &callExpr{"yank", nil}