Skip to content
Myr

Allow --nth option to take multiple indexes (comma-separated)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Junegunn Choi · committed by Junegunn Choi · parent 608ec2b · Browse files · 3 files+52 added−28 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+52 added−28 removed

  • tests:+23 added−15 removed
  • other:+29 added−13 removed

3 files+52 added−28 removed

modified:test/test_fzf.rb+23 added-15 removed
28 unmodified lines
29 assert_equal 20000, fzf.sort30 assert_equal nil, fzf.nth3132 ENV['FZF_DEFAULT_OPTS'] =33 '-x -m -s 10000 -q " hello world " +c +2 --no-mouse -f "goodbye world" --black --nth=3'33 '-x -m -s 10000 -q " hello world " +c +2 --no-mouse -f "goodbye world" --black --nth=3,-1,2'34 fzf = FZF.new []35 assert_equal 10000, fzf.sort36 assert_equal ' hello world ',37 fzf.query.get
4 unmodified lines
42 assert_equal false, fzf.color43 assert_equal false, fzf.ansi25644 assert_equal true, fzf.black45 assert_equal false, fzf.mouse46 assert_equal 3, fzf.nth46 assert_equal [3, -1, 2], fzf.nth47 end4849 def test_option_parser50 # Long opts
8 unmodified lines
59 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.nth63 assert_equal [1], fzf.nth6465 fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello66 --filter a --filter b --no-256 --black --nth 266 --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.color
3 unmodified lines
74 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.nth78 assert_equal [-2], fzf.nth7980 # Short opts81 fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x -fhowdy +2 -n3]82 assert_equal 2000, fzf.sort
3 unmodified lines
86 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.nth90 assert_equal [3], fzf.nth9192 # Left-to-right93 fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x -fgoodbye +2 -n3 -n493 fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x -fgoodbye +2 -n3 -n4,594 -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.color
2 unmodified lines
100 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.nth104 assert_equal [4, 5], fzf.nth105106 fzf = FZF.new %w[--query hello +s -s 2000 --query=world]107 assert_equal 2000, fzf.sort108 assert_equal 'world', fzf.query.get
392 unmodified lines
501 assert_equal [502 [list[0], [[2, 5]]],503 [list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')504505 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, 2505 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [2]506 assert_equal [[list[1], [[8, 9]]]], matcher.match(list, 'f', '', '')507 assert_equal [[list[0], [[8, 9]]]], matcher.match(list, 's', '', '')508509 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, 3509 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [3]510 assert_equal [[list[0], [[19, 20]]]], matcher.match(list, 'r', '', '')511512 # Comma-separated513 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [3, 1]514 assert_equal [[list[0], [[19, 20]]], [list[1], [[3, 4]]]], matcher.match(list, 'r', '', '')515516 # Ordered517 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1, 3]518 assert_equal [[list[0], [[3, 4]]], [list[1], [[3, 4]]]], matcher.match(list, 'r', '', '')519520 regex = FZF.build_delim_regex "\t"513 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, 1, regex521 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1], regex522 assert_equal [[list[0], [[3, 10]]]], matcher.match(list, 're', '', '')523516 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, 2, regex524 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [2], regex525 assert_equal [], matcher.match(list, 'r', '', '')526 assert_equal [[list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')527528 # Negative indexing521 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, -1, regex529 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [-1], regex530 assert_equal [[list[0], [[3, 6]]]], matcher.match(list, 'rt', '', '')531 assert_equal [[list[0], [[2, 5]]], [list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')532533 # Regex delimiter534 regex = FZF.build_delim_regex "[ \t]+"527 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, 1, regex535 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1], regex536 assert_equal [list[1]], matcher.match(list, 'f', '', '').map(&:first)537530 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, 2, regex538 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [2], regex539 assert_equal [[list[0], [[1, 2]]], [list[1], [[8, 9]]]], matcher.match(list, 'f', '', '')540 end541end542
modified:fzf+22 added-10 removed
5 unmodified lines
6# / /_/_ / / /_7# / __/ / /_/ __/8# /_/ /___/_/ Fuzzy finder for your shell9#10# Version: 0.8.2 (March 30, 2014)10# Version: 0.8.3 (April 2, 2014)11#12# Author: Junegunn Choi13# URL: https://github.com/junegunn/fzf14# License: MIT
110 unmodified lines
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_i129 @nth = parse_nth nth130 when /^-n([0-9,-]+)$/, /^--nth=([0-9,-]+)$/131 @nth = parse_nth $1132 when '-d', '--delimiter'133 usage 1, 'delimiter required' unless delim = argv.shift134 @delim = FZF.build_delim_regex delim135 when /^-d(.+)$/, /^--delimiter=(.+)$/
32 unmodified lines
168 @plcount = 0169 end170 end171172 def parse_nth nth173 nth.split(',').map { |n|174 ni = n.to_i175 usage 1, "invalid field number: #{n}" if ni == 0176 ni177 }178 end179180 def FZF.build_delim_regex delim181 Regexp.compile(delim) rescue (delim = Regexp.escape(delim))182 Regexp.compile "(?:.*?#{delim})|(?:.+?$)"183 end
50 unmodified lines
234 -x, --extended Extended-search mode235 -e, --extended-exact Extended-search mode (exact match)236 -q, --query=STR Initial query237 -f, --filter=STR Filter mode. Do not start interactive finder.230 -n, --nth=[-]N Match only in the N-th token of the item238 -n, --nth=[-]N[,..] Comma-separated list of field indexes for limiting239 search scope (positive or negative integers)240 -d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)241 -s, --sort=MAX Maximum number of matched items to sort (default: 1000)242 +s, --no-sort Do not sort the result. Keep the sequence unchanged.243 -i Case-insensitive match (default: smart-case match)
823 unmodified lines
1067 end1068 end10691070 def initialize nth, delim1062 @nth = nth && (nth > 0 ? nth - 1 : nth)1071 @nth = nth && nth.map { |n| n > 0 ? n - 1 : n }1072 @delim = delim1073 @tokens_cache = {}1074 end1075
12 unmodified lines
1088 def do_match str, pat1089 if @nth1090 prefix_length, tokens = tokenize str10911092 @nth.each do |n|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 offset1093 if (token = tokens[n]) && (md = token.match(pat) rescue nil)1094 prefix_length += (tokens[0...n] || []).join.length1095 offset = md.offset(0).map { |o| o + prefix_length }1096 return MatchData.new offset1097 end1098 end1099 nil1100 else1101 str.match(pat) rescue nil1102 end1103 end
134 unmodified lines
modified:README.md+7 added-3 removed
54 unmodified lines
55 -x, --extended Extended-search mode56 -e, --extended-exact Extended-search mode (exact match)57 -q, --query=STR Initial query58 -f, --filter=STR Filter mode. Do not start interactive finder.59 -n, --nth=[-]N Match only in the N-th token of the item59 -n, --nth=[-]N[,..] Comma-separated list of field indexes for limiting60 search scope (positive or negative integers)61 -d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)62 -s, --sort=MAX Maximum number of matched items to sort (default: 1000)63 +s, --no-sort Do not sort the result. Keep the sequence unchanged.64 -i Case-insensitive match (default: smart-case match)
121 unmodified lines
186# ftags - search ctags187ftags() {188 local line189 [ -e tags ] &&189 line=$(grep -v "^!" tags | cut -f1-3 | cut -c1-80 | fzf --nth=1) &&190 $EDITOR $(cut -f2 <<< "$line")190 line=$(191 awk 'BEGIN { FS="\t" } !/^!/ {print toupper($4)"\t"$1"\t"$2"\t"$3}' tags |192 cut -c1-80 | fzf --nth=1,2193 ) && $EDITOR $(cut -f3 <<< "$line") -c "set nocst" \194 -c "silent tag $(cut -f2 <<< "$line")"195}196197# fq1 [QUERY]198# - Immediately select the file when there's only one match.
252 unmodified lines