diff --git a/complete.go b/complete.go index a3073d97..00ed30c4 100644 --- a/complete.go +++ b/complete.go @@ -48,6 +48,8 @@ var ( "search-back", "search-next", "search-prev", + "filter", + "setfilter", "mark-save", "mark-remove", "mark-load", @@ -106,6 +108,9 @@ var ( "incsearch", "noincsearch", "incsearch!", + "incfilter", + "noincfilter", + "incfilter!", "mouse", "nomouse", "mouse!", diff --git a/doc.go b/doc.go index a791a92c..028bd7e4 100644 --- a/doc.go +++ b/doc.go @@ -62,6 +62,8 @@ The following commands are provided by lf: search-back (modal) (default '?') search-next (default 'n') search-prev (default 'N') + filter (modal) + setfilter mark-save (modal) (default 'm') mark-load (modal) (default "'") mark-remove (modal) (default `"`) @@ -114,6 +116,7 @@ The following options can be used to customize the behavior of lf: ifs string (default '') ignorecase bool (default on) ignoredia bool (default on) + incfilter bool (default off) incsearch bool (default off) info []string (default '') mouse bool (default off) @@ -390,6 +393,15 @@ Read a pattern to search for a file name match in the forward/backward direction (See also 'globsearch', 'incsearch', 'wrapscan', 'ignorecase', 'smartcase', 'ignoredia', and 'smartdia' options and 'Searching Files' section) + filter (modal) + setfilter + +Read a pattern to filter out and only view files matching the pattern. +setfilter does the same but uses an argument to set the filter immediatly. +You can supply an argument to filter, in order to use that as the starting prompt. + +(See also 'globsearch', 'incfilter', 'ignorecase', 'smartcase', 'ignoredia', and 'smartdia' options) + mark-save (modal) (default 'm') Save the current directory as a bookmark assigned to the given key. @@ -587,6 +599,10 @@ Ignore diacritics in sorting and search patterns. Jump to the first match after each keystroke during searching. + incfilter bool (default off) + +Apply filter pattern after each keystroke during filtering. + info []string (default '') List of information shown for directory items at the right side of pane. diff --git a/docstring.go b/docstring.go index 1bd4b968..18efdeff 100644 --- a/docstring.go +++ b/docstring.go @@ -66,6 +66,8 @@ The following commands are provided by lf: search-back (modal) (default '?') search-next (default 'n') search-prev (default 'N') + filter (modal) + setfilter mark-save (modal) (default 'm') mark-load (modal) (default "'") mark-remove (modal) (default '"') @@ -118,6 +120,7 @@ The following options can be used to customize the behavior of lf: ifs string (default '') ignorecase bool (default on) ignoredia bool (default on) + incfilter bool (default off) incsearch bool (default off) info []string (default '') mouse bool (default off) @@ -410,6 +413,17 @@ direction and jump to the next/previous match. (See also 'globsearch', 'incsearch', 'wrapscan', 'ignorecase', 'smartcase', 'ignoredia', and 'smartdia' options and 'Searching Files' section) + filter (modal) + setfilter + +Read a pattern to filter out and only view files matching the pattern. +setfilter does the same but uses an argument to set the filter immediatly. +You can supply an argument to filter, in order to use that as the starting +prompt. + +(See also 'globsearch', 'incfilter', 'ignorecase', 'smartcase', 'ignoredia', +and 'smartdia' options) + mark-save (modal) (default 'm') Save the current directory as a bookmark assigned to the given key. @@ -618,6 +632,10 @@ Ignore diacritics in sorting and search patterns. Jump to the first match after each keystroke during searching. + incfilter bool (default off) + +Apply filter pattern after each keystroke during filtering. + info []string (default '') List of information shown for directory items at the right side of pane. diff --git a/eval.go b/eval.go index 5e58021f..0d55bfd3 100644 --- a/eval.go +++ b/eval.go @@ -94,10 +94,19 @@ func (e *setExpr) eval(app *app, args []string) { app.ui.loadFile(app.nav, true) case "globsearch": gOpts.globsearch = true + app.nav.sort() + app.ui.sort() + app.ui.loadFile(app.nav, true) case "noglobsearch": gOpts.globsearch = false + app.nav.sort() + app.ui.sort() + app.ui.loadFile(app.nav, true) case "globsearch!": gOpts.globsearch = !gOpts.globsearch + app.nav.sort() + app.ui.sort() + app.ui.loadFile(app.nav, true) case "hidden": gOpts.sortType.option |= hiddenSort app.nav.sort() @@ -126,14 +135,17 @@ func (e *setExpr) eval(app *app, args []string) { gOpts.ignorecase = true app.nav.sort() app.ui.sort() + app.ui.loadFile(app.nav, true) case "noignorecase": gOpts.ignorecase = false app.nav.sort() app.ui.sort() + app.ui.loadFile(app.nav, true) case "ignorecase!": gOpts.ignorecase = !gOpts.ignorecase app.nav.sort() app.ui.sort() + app.ui.loadFile(app.nav, true) case "ignoredia": gOpts.ignoredia = true app.nav.sort() @@ -146,6 +158,12 @@ func (e *setExpr) eval(app *app, args []string) { gOpts.ignoredia = !gOpts.ignoredia app.nav.sort() app.ui.sort() + case "incfilter": + gOpts.incfilter = true + case "noincfilter": + gOpts.incfilter = false + case "incfilter!": + gOpts.incfilter = !gOpts.incfilter case "incsearch": gOpts.incsearch = true case "noincsearch": @@ -210,10 +228,19 @@ func (e *setExpr) eval(app *app, args []string) { app.ui.sort() case "smartcase": gOpts.smartcase = true + app.nav.sort() + app.ui.sort() + app.ui.loadFile(app.nav, true) case "nosmartcase": gOpts.smartcase = false + app.nav.sort() + app.ui.sort() + app.ui.loadFile(app.nav, true) case "smartcase!": gOpts.smartcase = !gOpts.smartcase + app.nav.sort() + app.ui.sort() + app.ui.loadFile(app.nav, true) case "smartdia": gOpts.smartdia = true case "nosmartdia": @@ -485,6 +512,17 @@ func update(app *app) { app.ui.loadFile(app.nav, true) app.ui.loadFileInfo(app.nav) } + case gOpts.incfilter && app.ui.cmdPrefix == "filter: ": + filter := string(app.ui.cmdAccLeft) + string(app.ui.cmdAccRight) + dir := app.nav.currDir() + old := dir.ind + + if err := app.nav.setFilter(strings.Split(filter, " ")); err != nil { + app.ui.echoerrf("filter: %s", err) + } else if old != dir.ind { + app.ui.loadFile(app.nav, true) + app.ui.loadFileInfo(app.nav) + } } } @@ -503,6 +541,9 @@ func insert(app *app, arg string) { case gOpts.incsearch && (app.ui.cmdPrefix == "/" || app.ui.cmdPrefix == "?"): app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(arg)...) update(app) + case gOpts.incfilter && app.ui.cmdPrefix == "filter: ": + app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(arg)...) + update(app) case app.ui.cmdPrefix == "find: ": app.nav.find = string(app.ui.cmdAccLeft) + arg + string(app.ui.cmdAccRight) @@ -1050,6 +1091,23 @@ func (e *callExpr) eval(app *app, args []string) { } } } + case "filter": + app.ui.cmdPrefix = "filter: " + dir := app.nav.currDir() + app.nav.prevFilter = dir.filter + if len(e.args) == 0 { + app.ui.cmdAccLeft = []rune(strings.Join(dir.filter, " ")) + } else { + app.ui.cmdAccLeft = []rune(strings.Join(e.args, " ")) + } + app.ui.loadFileInfo(app.nav) + case "setfilter": + log.Printf("filter: %s", e.args) + if err := app.nav.setFilter(e.args); err != nil { + app.ui.echoerrf("filter: %s", err) + } + app.ui.loadFile(app.nav, true) + app.ui.loadFileInfo(app.nav) case "mark-save": app.ui.cmdPrefix = "mark-save: " case "mark-load": @@ -1210,6 +1268,15 @@ func (e *callExpr) eval(app *app, args []string) { app.ui.loadFileInfo(app.nav) } } + if gOpts.incfilter && app.ui.cmdPrefix == "filter: " { + dir := app.nav.currDir() + old := dir.ind + app.nav.setFilter(app.nav.prevFilter) + if old != dir.ind { + app.ui.loadFile(app.nav, true) + app.ui.loadFileInfo(app.nav) + } + } normal(app) case "cmd-complete": var matches []string @@ -1326,7 +1393,7 @@ func (e *callExpr) eval(app *app, args []string) { } case "cmd-enter": s := string(append(app.ui.cmdAccLeft, app.ui.cmdAccRight...)) - if len(s) == 0 { + if len(s) == 0 && app.ui.cmdPrefix != "filter: " { return } @@ -1403,6 +1470,14 @@ func (e *callExpr) eval(app *app, args []string) { app.ui.loadFile(app.nav, true) app.ui.loadFileInfo(app.nav) } + case "filter: ": + log.Printf("filter: %s", s) + app.ui.cmdPrefix = "" + if err := app.nav.setFilter(strings.Split(s, " ")); err != nil { + app.ui.echoerrf("filter: %s", err) + } + app.ui.loadFile(app.nav, true) + app.ui.loadFileInfo(app.nav) case "find: ": app.ui.cmdPrefix = "" if moved, found := app.nav.findNext(); !found { diff --git a/lf.1 b/lf.1 index 58d78b95..c1d67398 100644 --- a/lf.1 +++ b/lf.1 @@ -77,6 +77,8 @@ The following commands are provided by lf: search-back (modal) (default '?') search-next (default 'n') search-prev (default 'N') + filter (modal) + setfilter mark-save (modal) (default 'm') mark-load (modal) (default "'") mark-remove (modal) (default `"`) @@ -133,6 +135,7 @@ The following options can be used to customize the behavior of lf: ifs string (default '') ignorecase bool (default on) ignoredia bool (default on) + incfilter bool (default off) incsearch bool (default off) info []string (default '') mouse bool (default off) @@ -481,6 +484,15 @@ Read a pattern to search for a file name match in the forward/backward direction .PP (See also 'globsearch', 'incsearch', 'wrapscan', 'ignorecase', 'smartcase', 'ignoredia', and 'smartdia' options and 'Searching Files' section) .PP +.EX + filter (modal) + setfilter +.EE +.PP +Read a pattern to filter out and only view files matching the pattern. setfilter does the same but uses an argument to set the filter immediatly. You can supply an argument to filter, in order to use that as the starting prompt. +.PP +(See also 'globsearch', 'incfilter', 'ignorecase', 'smartcase', 'ignoredia', and 'smartdia' options) +.PP .EX mark-save (modal) (default 'm') .EE @@ -722,6 +734,12 @@ Ignore diacritics in sorting and search patterns. .PP Jump to the first match after each keystroke during searching. .PP +.EX + incfilter bool (default off) +.EE +.PP +Apply filter pattern after each keystroke during filtering. +.PP .EX info []string (default '') .EE diff --git a/nav.go b/nav.go index 4fa6ca2c..ffa21d65 100644 --- a/nav.go +++ b/nav.go @@ -115,6 +115,7 @@ type dir struct { sortType sortType // sort method and options from last sort dironly bool // dironly value from last sort hiddenfiles []string // hiddenfiles value from last sort + filter []string // last filter for this directory ignorecase bool // ignorecase value from last sort ignoredia bool // ignoredia value from last sort noPerm bool // whether lf has no permission to open the directory @@ -254,10 +255,30 @@ func (dir *dir) sort() { for i, f := range dir.files { if !isHidden(f, dir.path, dir.hiddenfiles) { dir.files = dir.files[i:] - return + break } } - dir.files = dir.files[len(dir.files):] + if len(dir.files) > 0 && isHidden(dir.files[len(dir.files)-1], dir.path, dir.hiddenfiles) { + dir.files = dir.files[len(dir.files):] + } + } + + if len(dir.filter) != 0 { + sort.SliceStable(dir.files, func(i, j int) bool { + if isFiltered(dir.files[i], dir.filter) && isFiltered(dir.files[j], dir.filter) { + return i < j + } + return isFiltered(dir.files[i], dir.filter) + }) + for i, f := range dir.files { + if !isFiltered(f, dir.filter) { + dir.files = dir.files[i:] + break + } + } + if len(dir.files) > 0 && isFiltered(dir.files[len(dir.files)-1], dir.filter) { + dir.files = dir.files[len(dir.files):] + } } } @@ -329,6 +350,7 @@ type nav struct { searchBack bool searchInd int searchPos int + prevFilter []string volatilePreview bool } @@ -389,6 +411,7 @@ func (nav *nav) checkDir(dir *dir) { dir.loadTime = now go func() { nd := newDir(dir.path) + nd.filter = dir.filter nd.sort() nav.dirChan <- nd }() @@ -633,6 +656,27 @@ func (nav *nav) sort() { } } +func (nav *nav) setFilter(filter []string) error { + newfilter := []string{} + for _, tok := range filter { + _, err := filepath.Match(tok, "a") + if err != nil { + return err + } + if tok != "" { + newfilter = append(newfilter, tok) + } + } + dir := nav.currDir() + dir.filter = newfilter + + // Apply filter, by sorting current dir (see nav.sort()) + name := dir.name() + dir.sort() + dir.sel(name, nav.height) + return nil +} + func (nav *nav) up(dist int) bool { dir := nav.currDir() @@ -1283,6 +1327,22 @@ func (nav *nav) searchPrev() (bool, error) { return false, nil } +func isFiltered(f os.FileInfo, filter []string) bool { + for _, pattern := range filter { + matched, err := searchMatch(f.Name(), strings.TrimPrefix(pattern, "!")) + if err != nil { + log.Printf("Filter Error: %s", err) + return false + } + if strings.HasPrefix(pattern, "!") && matched { + return true + } else if !strings.HasPrefix(pattern, "!") && !matched { + return true + } + } + return false +} + func (nav *nav) removeMark(mark string) error { if _, ok := nav.marks[mark]; ok { delete(nav.marks, mark) diff --git a/opts.go b/opts.go index fbb54174..724c93d3 100644 --- a/opts.go +++ b/opts.go @@ -38,6 +38,7 @@ var gOpts struct { icons bool ignorecase bool ignoredia bool + incfilter bool incsearch bool mouse bool number bool @@ -83,6 +84,7 @@ func init() { gOpts.icons = false gOpts.ignorecase = true gOpts.ignoredia = true + gOpts.incfilter = false gOpts.incsearch = false gOpts.mouse = false gOpts.number = false diff --git a/ui.go b/ui.go index 79120853..26cc27fb 100644 --- a/ui.go +++ b/ui.go @@ -676,7 +676,8 @@ func (ui *ui) loadFileInfo(nav *nav) { func (ui *ui) drawPromptLine(nav *nav) { st := tcell.StyleDefault - pwd := nav.currDir().path + dir := nav.currDir() + pwd := dir.path if strings.HasPrefix(pwd, gUser.HomeDir) { pwd = filepath.Join("~", strings.TrimPrefix(pwd, gUser.HomeDir)) @@ -717,6 +718,12 @@ func (ui *ui) drawPromptLine(nav *nav) { } prompt = strings.Replace(prompt, "%d", pwd, -1) + if len(dir.filter) != 0 { + prompt = strings.Replace(prompt, "%F", fmt.Sprint(dir.filter), -1) + } else { + prompt = strings.Replace(prompt, "%F", "", -1) + } + ui.promptWin.print(ui.screen, 0, 0, st, prompt) } @@ -755,6 +762,10 @@ func (ui *ui) drawStatLine(nav *nav) { selection += fmt.Sprintf(" \033[35;7m %d \033[0m", len(nav.selections)) } + if len(dir.filter) != 0 { + selection += " \033[34;7m F \033[0m" + } + var progress string if nav.copyTotal > 0 {