Merge pull request #32 from junegunn/nth
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Add --nth and --delimiter option
authored by Junegunn Choi · committed by Junegunn Choi · parents cf8dbf8, ec4b8a5 · Browse files · 3 files·+144 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+144 added−14 removed
- tests:+59 added−5 removed
- other:+85 added−9 removed
Provenance
- Commit
52b74ab- Tree
48b6e21- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent 1
cf8dbf8- Parent 2
ec4b8a5
3 files changed+144 added−14 removed
modified:test/test_fzf.rb+59 added-5 removed
25 unmodified lines26 # Deprecated27 ENV['FZF_DEFAULT_SORT'] = '20000'28 fzf = FZF.new []29 assert_equal 20000, fzf.sort30+ assert_equal nil, fzf.nth31 31- ENV['FZF_DEFAULT_OPTS'] = '-x -m -s 10000 -q " hello world " +c +2 --no-mouse -f "goodbye world" --black'32+ ENV['FZF_DEFAULT_OPTS'] =33+ '-x -m -s 10000 -q " hello world " +c +2 --no-mouse -f "goodbye world" --black --nth=3'34 fzf = FZF.new []35 assert_equal 10000, fzf.sort36 assert_equal ' hello world ',37 fzf.query.get4 unmodified lines42 assert_equal false, fzf.color43 assert_equal false, fzf.ansi25644 assert_equal true, fzf.black45 assert_equal false, fzf.mouse46+ assert_equal 3, fzf.nth47 end48 49 def test_option_parser50 # Long opts51 fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello49- --filter=howdy --extended-exact --no-mouse --no-256]52+ --filter=howdy --extended-exact --no-mouse --no-256 --nth=1]53 assert_equal 2000, fzf.sort54 assert_equal true, fzf.multi55 assert_equal false, fzf.color56 assert_equal false, fzf.ansi2562 unmodified lines59 assert_equal 0, fzf.rxflag60 assert_equal 'hello', fzf.query.get61 assert_equal 'howdy', fzf.filter62 assert_equal :exact, fzf.extended63+ assert_equal 1, fzf.nth64 65 fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello62- --filter a --filter b --no-256 --black66+ --filter a --filter b --no-256 --black --nth 267 --no-sort -i --color --no-multi --256]68 assert_equal nil, fzf.sort69 assert_equal false, fzf.multi70 assert_equal true, fzf.color3 unmodified lines74 assert_equal 1, fzf.rxflag75 assert_equal 'b', fzf.filter76 assert_equal 'hello', fzf.query.get77 assert_equal nil, fzf.extended78+ assert_equal 2, fzf.nth79 80 # Short opts76- fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x -fhowdy +2]81+ fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x -fhowdy +2 -n3]82 assert_equal 2000, fzf.sort83 assert_equal true, fzf.multi84 assert_equal false, fzf.color85 assert_equal false, fzf.ansi25686 assert_equal 0, fzf.rxflag87 assert_equal 'hello', fzf.query.get88 assert_equal 'howdy', fzf.filter89 assert_equal :fuzzy, fzf.extended90+ assert_equal 3, fzf.nth91 92 # Left-to-right87- fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x -fgoodbye +293+ fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x -fgoodbye +2 -n3 -n494 -s 3000 -c +m -i -q world +x -fworld -2 --black --no-black]95 assert_equal 3000, fzf.sort96 assert_equal false, fzf.multi97 assert_equal true, fzf.color2 unmodified lines100 assert_equal 1, fzf.rxflag101 assert_equal 'world', fzf.query.get102 assert_equal 'world', fzf.filter103 assert_equal nil, fzf.extended104+ assert_equal 4, fzf.nth105 106 fzf = FZF.new %w[--query hello +s -s 2000 --query=world]107 assert_equal 2000, fzf.sort108 assert_equal 'world', fzf.query.get6 unmodified lines115 assert_raises(SystemExit) do116 fzf = FZF.new argv117 end118 end119+ assert_raises(SystemExit) do120+ fzf = FZF.new %w[--nth=0]121+ end122+ assert_raises(SystemExit) do123+ fzf = FZF.new %w[-n 0]124+ end125 end126 127 # FIXME Only on 1.9 or above128 def test_width359 unmodified lines488 assert_equal true, me.double?(20)489 sleep interval490 assert_equal false, me.double?(20)491 end492+493+ def test_nth_match494+ list = [495+ ' first second third',496+ 'fourth fifth sixth',497+ ]498+499+ matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE500+ assert_equal list, matcher.match(list, 'f', '', '').map(&:first)501+ assert_equal [502+ [list[0], [[2, 5]]],503+ [list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')504+505+ matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, 2506+ assert_equal [[list[1], [[8, 9]]]], matcher.match(list, 'f', '', '')507+ assert_equal [[list[0], [[8, 9]]]], matcher.match(list, 's', '', '')508+509+ matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, 3510+ assert_equal [[list[0], [[19, 20]]]], matcher.match(list, 'r', '', '')511+512+ regex = FZF.build_delim_regex "\t"513+ matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, 1, regex514+ assert_equal [[list[0], [[3, 10]]]], matcher.match(list, 're', '', '')515+516+ matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, 2, regex517+ assert_equal [], matcher.match(list, 'r', '', '')518+ assert_equal [[list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')519+520+ # Negative indexing521+ matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, -1, regex522+ assert_equal [[list[0], [[3, 6]]]], matcher.match(list, 'rt', '', '')523+ assert_equal [[list[0], [[2, 5]]], [list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')524+525+ # Regex delimiter526+ regex = FZF.build_delim_regex "[ \t]+"527+ matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, 1, regex528+ assert_equal [list[1]], matcher.match(list, 'f', '', '').map(&:first)529+530+ matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, 2, regex531+ assert_equal [[list[0], [[1, 2]]], [list[1], [[8, 9]]]], matcher.match(list, 'f', '', '')532+ end533 end534 modified:fzf+75 added-9 removed
5 unmodified lines6 # / /_/_ / / /_7 # / __/ / /_/ __/8 # /_/ /___/_/ Fuzzy finder for your shell9 #10-# Version: 0.8.2 (March 15, 2014)10+# Version: 0.8.2 (March 30, 2014)11 #12 # Author: Junegunn Choi13 # URL: https://github.com/junegunn/fzf14 # License: MIT34 unmodified lines49 end50 51 class FZF52 C = Curses53- attr_reader :rxflag, :sort, :color, :black, :ansi256, :mouse, :multi, :query, :filter, :extended53+ attr_reader :rxflag, :sort, :nth, :color, :black, :ansi256,54+ :mouse, :multi, :query, :filter, :extended55 56 class AtomicVar57 def initialize value58 @value = value24 unmodified lines83 @multi = false84 @mouse = true85 @extended = nil86 @filter = nil87+ @nth = nil88+ @delim = nil89 90 argv =91 if opts = ENV['FZF_DEFAULT_OPTS']92 require 'shellwords'29 unmodified lines122 usage 1, 'query string required' unless query = argv.shift123 @filter = query124 when /^-f(.*)$/, /^--filter=(.*)$/125 @filter = $1126+ when '-n', '--nth'127+ usage 1, 'field number required' unless nth = argv.shift128+ usage 1, 'invalid field number' if nth.to_i == 0129+ @nth = nth.to_i130+ when /^-n(-?[1-9][0-9]*)$/, /^--nth=(-?[1-9][0-9]*)$/131+ @nth = $1.to_i132+ when '-d', '--delimiter'133+ usage 1, 'delimiter required' unless delim = argv.shift134+ @delim = FZF.build_delim_regex delim135+ when /^-d(.+)$/, /^--delimiter=(.+)$/136+ @delim = FZF.build_delim_regex $1137 when '-s', '--sort'138 usage 1, 'sort size required' unless sort = argv.shift139 usage 1, 'invalid sort size' unless sort =~ /^[0-9]+$/140 @sort = sort.to_i27 unmodified lines168 @plcount = 0169 end170 end171 172+ def FZF.build_delim_regex delim173+ Regexp.compile(delim) rescue (delim = Regexp.escape(delim))174+ Regexp.compile "(?:.*?#{delim})|(?:.+?$)"175+ end176+177 def start178 if @filter179 start_reader(false).join180 filter_list @new18 unmodified lines199 end200 201 def get_matcher202 if @extended184- ExtendedFuzzyMatcher.new @rxflag, @extended203+ ExtendedFuzzyMatcher.new @rxflag, @extended, @nth, @delim204 else186- FuzzyMatcher.new @rxflag205+ FuzzyMatcher.new @rxflag, @nth, @delim206 end207 end208 209 def version16 unmodified lines226 -x, --extended Extended-search mode227 -e, --extended-exact Extended-search mode (exact match)228 -q, --query=STR Initial query229 -f, --filter=STR Filter mode. Do not start interactive finder.230+ -n, --nth=[-]N Match only in the N-th token of the item231+ -d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)232 -s, --sort=MAX Maximum number of matched items to sort (default: 1000)233 +s, --no-sort Do not sort the result. Keep the sequence unchanged.234 -i Case-insensitive match (default: smart-case match)235 +i Case-sensitive match810 unmodified lines1046 end1047 end1048 end1049 1050+ class Matcher1051+ class MatchData1052+ def initialize n1053+ @n = n1054+ end1055+1056+ def offset _1057+ @n1058+ end1059+ end1060+1061+ def initialize nth, delim1062+ @nth = nth && (nth > 0 ? nth - 1 : nth)1063+ @delim = delim1064+ @tokens_cache = {}1065+ end1066+1067+ def tokenize str1068+ @tokens_cache[str] ||=1069+ unless @delim1070+ # AWK default1071+ prefix_length = str[/^\s+/].length rescue 01072+ [prefix_length, (str.strip.scan(/\S+\s*/) rescue [])]1073+ else1074+ prefix_length = 01075+ [prefix_length, (str.scan(@delim) rescue [])]1076+ end1077+ end1078+1079+ def do_match str, pat1080+ if @nth1081+ prefix_length, tokens = tokenize str1082+1083+ if (token = tokens[@nth]) && (md = token.match(pat) rescue nil)1084+ prefix_length += (tokens[0...@nth] || []).join.length1085+ offset = md.offset(0).map { |o| o + prefix_length }1086+ MatchData.new offset1087+ end1088+ else1089+ str.match(pat) rescue nil1090+ end1091+ end1092+ end1093+1094 class FuzzyMatcher < Matcher1095 attr_reader :caches, :rxflag1096 1032- def initialize rxflag1097+ def initialize rxflag, nth = nil, delim = nil1098+ super nth, delim1099 @caches = Hash.new { |h, k| h[k] = {} }1100 @regexp = {}1101 @rxflag = rxflag1102 end35 unmodified lines1138 suffix_cache].compact.sort_by { |e| e.length }.first1139 cache[q] ||= (partial_cache ?1140 partial_cache.map { |e| e.first } : list).map { |line|1141 # Ignore errors: e.g. invalid byte sequence in UTF-81076- md = line.match(regexp) rescue nil1142+ md = do_match(line, regexp)1143 md && [line, [md.offset(0)]]1144 }.compact1145 end1146 end1147 1148 class ExtendedFuzzyMatcher < FuzzyMatcher1083- def initialize rxflag, mode = :fuzzy1084- super rxflag1149+ def initialize rxflag, mode = :fuzzy, nth = nil, delim = nil1150+ super rxflag, nth, delim1151 @regexps = {}1152 @mode = mode1153 end1154 53 unmodified lines1208 list).map { |line|1209 offsets = []1210 regexps.all? { |pair|1211 regexp, invert = pair1146- md = line.match(regexp) rescue nil1212+ md = do_match(line, regexp)1213 if md && !invert1214 offsets << md.offset(0)1215 elsif !md && invert1216 true9 unmodified linesmodified:README.md+10 added
51 unmodified lines52 -x, --extended Extended-search mode53 -e, --extended-exact Extended-search mode (exact match)54 -q, --query=STR Initial query55 -f, --filter=STR Filter mode. Do not start interactive finder.56+ -n, --nth=[-]N Match only in the N-th token of the item57+ -d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)58 -s, --sort=MAX Maximum number of matched items to sort (default: 1000)59 +s, --no-sort Do not sort the result. Keep the sequence unchanged.60 -i Case-insensitive match (default: smart-case match)61 +i Case-sensitive match116 unmodified lines178 commit=$(echo "$commits" | fzf +s +m -e) &&179 git checkout $(echo "$commit" | sed "s/ .*//")180 }181 182+# ftags - search ctags183+ftags() {184+ local line185+ [ -e tags ] &&186+ line=$(grep -v "^!" tags | cut -f1-3 | cut -c1-80 | fzf --nth=1) &&187+ $EDITOR $(cut -f2 <<< "$line")188+}189+190 # fq1 [QUERY]191 # - Immediately select the file when there's only one match.192 # If not, start the fuzzy finder as usual.193 fq1() {250 unmodified lines