Skip to content
Myr

Extend --nth option to take ranges

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

As discussed in #55

authored by Junegunn Choi · committed by Junegunn Choi · parent c3a4e4c · Browse files · 4 files+80 added−39 removed
This commit

State

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

Diff stats

4 files+80 added−39 removed

  • tests:+50 added−21 removed
  • other:+30 added−18 removed

4 files+80 added−39 removed

modified:test/test_fzf.rb+50 added-21 removed
58 unmodified lines
59 assert_equal false, fzf.mouse60 assert_equal true, fzf.select161 assert_equal true, fzf.exit062 assert_equal true, fzf.reverse63 assert_equal [3, -1, 2], fzf.nth63 assert_equal [2..2, -1..-1, 1..1], fzf.nth64 end6566 def test_option_parser67 # Long opts
11 unmodified lines
79 assert_equal true, fzf.select180 assert_equal true, fzf.exit081 assert_equal 'howdy', fzf.filter82 assert_equal :exact, fzf.extended83 assert_equal [1], fzf.nth83 assert_equal [0..0], fzf.nth84 assert_equal true, fzf.reverse8586 # Long opts (left-to-right)87 fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query=hello
12 unmodified lines
100 assert_equal 'hello', fzf.query.get101 assert_equal false, fzf.select1102 assert_equal false, fzf.exit0103 assert_equal nil, fzf.extended104 assert_equal [-2], fzf.nth104 assert_equal [-2..-2], fzf.nth105 assert_equal false, fzf.reverse106107 # Short opts108 fzf = FZF.new %w[-s2000 +c -m +i -qhello -x -fhowdy +2 -n3 -1 -0]
4 unmodified lines
113 assert_equal 0, fzf.rxflag114 assert_equal 'hello', fzf.query.get115 assert_equal 'howdy', fzf.filter116 assert_equal :fuzzy, fzf.extended117 assert_equal [3], fzf.nth117 assert_equal [2..2], fzf.nth118 assert_equal true, fzf.select1119 assert_equal true, fzf.exit0120121 # Left-to-right
11 unmodified lines
133 assert_equal false, fzf.select1134 assert_equal false, fzf.exit0135 assert_equal 'world', fzf.filter136 assert_equal nil, fzf.extended137 assert_equal [4, 5], fzf.nth137 assert_equal [3..3, 4..4], fzf.nth138 rescue SystemExit => e139 assert false, "Exited"140 end141142 def test_invalid_option143 [%w[--unknown], %w[yo dawg]].each do |argv|143 [144 %w[--unknown],145 %w[yo dawg],146 %w[--nth=0],147 %w[-n 0],148 %w[-n 1..2..3],149 %w[-n 1....],150 %w[-n ....3],151 %w[-n 1....3],152 %w[-n 1..0],153 %w[--nth ..0],154 ].each do |argv|155 assert_raises(SystemExit) do156 fzf = FZF.new argv157 end158 end148 assert_raises(SystemExit) do149 fzf = FZF.new %w[--nth=0]150 end151 assert_raises(SystemExit) do152 fzf = FZF.new %w[-n 0]153 end159 end160161 # FIXME Only on 1.9 or above162 def test_width
328 unmodified lines
491 assert_equal [492 [list[0], [[2, 5]]],493 [list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')494490 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [2]495 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1..1]496 assert_equal [[list[1], [[8, 9]]]], matcher.match(list, 'f', '', '')497 assert_equal [[list[0], [[8, 9]]]], matcher.match(list, 's', '', '')498494 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [3]499 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [2..2]500 assert_equal [[list[0], [[19, 20]]]], matcher.match(list, 'r', '', '')501502 # Comma-separated498 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [3, 1]503 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [2..2, 0..0]504 assert_equal [[list[0], [[19, 20]]], [list[1], [[3, 4]]]], matcher.match(list, 'r', '', '')505506 # Ordered502 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1, 3]507 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [0..0, 2..2]508 assert_equal [[list[0], [[3, 4]]], [list[1], [[3, 4]]]], matcher.match(list, 'r', '', '')509510 regex = FZF.build_delim_regex "\t"506 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1], regex511 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [0..0], regex512 assert_equal [[list[0], [[3, 10]]]], matcher.match(list, 're', '', '')513509 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [2], regex514 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1..1], regex515 assert_equal [], matcher.match(list, 'r', '', '')516 assert_equal [[list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')517518 # Negative indexing514 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [-1], regex519 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [-1..-1], regex520 assert_equal [[list[0], [[3, 6]]]], matcher.match(list, 'rt', '', '')521 assert_equal [[list[0], [[2, 5]]], [list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')522523 # Regex delimiter524 regex = FZF.build_delim_regex "[ \t]+"520 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1], regex525 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [0..0], regex526 assert_equal [list[1]], matcher.match(list, 'f', '', '').map(&:first)527523 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [2], regex528 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1..1], regex529 assert_equal [[list[0], [[1, 2]]], [list[1], [[8, 9]]]], matcher.match(list, 'f', '', '')530 end531532 def test_nth_match_range533 list = [534 ' first second third',535 'fourth fifth sixth',536 ]537538 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1..2]539 assert_equal [[list[0], [[8, 20]]]], matcher.match(list, 'sr', '', '')540 assert_equal [], matcher.match(list, 'fo', '', '')541542 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1..-1, 0..0]543 assert_equal [[list[0], [[8, 20]]]], matcher.match(list, 'sr', '', '')544 assert_equal [[list[1], [[0, 2]]]], matcher.match(list, 'fo', '', '')545546 matcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE, :fuzzy, [0..0, 1..2]547 assert_equal [], matcher.match(list, '^t', '', '')548549 matcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE, :fuzzy, [0..1, 2..2]550 assert_equal [[list[0], [[16, 17]]]], matcher.match(list, '^t', '', '')551552 matcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE, :fuzzy, [1..-1]553 assert_equal [[list[0], [[8, 9]]]], matcher.match(list, '^s', '', '')554 end555556 def stream_for str557 StringIO.new(str).tap do |sio|558 sio.instance_eval do559 alias org_gets gets
93 unmodified lines
modified:fzf+24 added-13 removed
5 unmodified lines
6# / /_/_ / / /_7# / __/ / /_/ __/8# /_/ /___/_/ Fuzzy finder for your shell9#10# Version: 0.8.5 (Jun 12, 2014)10# Version: 0.8.5 (Jun 14, 2014)11#12# Author: Junegunn Choi13# URL: https://github.com/junegunn/fzf14# License: MIT
117 unmodified lines
132 @filter = query133 when /^-f(.*)$/, /^--filter=(.*)$/134 @filter = $1135 when '-n', '--nth'136 usage 1, 'field number required' unless nth = argv.shift137 usage 1, 'invalid field number' if nth.to_i == 0136 usage 1, 'field expression required' unless nth = argv.shift137 @nth = parse_nth nth139 when /^-n([0-9,-]+)$/, /^--nth=([0-9,-]+)$/138 when /^-n([0-9,-\.]+)$/, /^--nth=([0-9,-\.]+)$/139 @nth = parse_nth $1140 when '-d', '--delimiter'141 usage 1, 'delimiter required' unless delim = argv.shift142 @delim = FZF.build_delim_regex delim
34 unmodified lines
177 end178 end179180 def parse_nth nth182 nth.split(',').map { |n|183 ni = n.to_i184 usage 1, "invalid field number: #{n}" if ni == 0185 ni181 nth.split(',').map { |expr|182 x = proc { usage 1, "invalid field expression: #{expr}" }183 first, second = expr.split('..', 2)184 x.call if !first.empty? && first.to_i == 0 ||185 second && !second.empty? && (second.to_i == 0 || second.include?('.'))186187 first = first.empty? ? 1 : first.to_i188 second = case second189 when nil then first190 when '' then -1191 else second.to_i192 end193194 Range.new(*[first, second].map { |e| e > 0 ? e - 1 : e })195 }196 end197198 def FZF.build_delim_regex delim
90 unmodified lines
289 -x, --extended Extended-search mode290 -e, --extended-exact Extended-search mode (exact match)291 -i Case-insensitive match (default: smart-case match)292 +i Case-sensitive match284 -n, --nth=[-]N[,..] Comma-separated list of field indexes for limiting285 search scope (positive or negative integers)293 -n, --nth=N[,..] Comma-separated list of field index expressions294 for limiting search scope. Each can be a non-zero295 integer or a range expression ([BEGIN]..[END])296 -d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)297298 Search result299 -s, --sort=MAX Maximum number of matched items to sort (default: 1000)
720 unmodified lines
1020 end1021 end10221023 def initialize nth, delim1014 @nth = nth && nth.map { |n| n > 0 ? n - 1 : n }1024 @nth = nth1025 @delim = delim1026 @tokens_cache = {}1027 end1028
13 unmodified lines
1042 if @nth1043 prefix_length, tokens = tokenize str10441045 @nth.each do |n|1046 if (range = tokens[n]) && (token = range.join) &&1036 if (token = tokens[n]) && (md = token.match(pat) rescue nil)1037 prefix_length += (tokens[0...n] || []).join.length1047 (md = token.sub(/\s+$/, '').match(pat) rescue nil)1048 prefix_length += (tokens[0...(n.begin)] || []).join.length1049 offset = md.offset(0).map { |o| o + prefix_length }1050 return MatchData.new(offset)1051 end1052 end
139 unmodified lines
modified:install+3 added-3 removed
138 unmodified lines
139 bind '"\C-t": " \C-u \C-a\C-k$(__fsel)\e\C-e\C-y\C-a\C-y\ey\C-h\C-e\er \C-h"'140 fi141142 # CTRL-R - Paste the selected command from history into the command line143 bind '"\C-r": " \C-e\C-u$(HISTTIMEFORMAT= history | fzf +s | sed \"s/ *[0-9]* *//\")\e\C-e\er"'143 bind '"\C-r": " \C-e\C-u$(HISTTIMEFORMAT= history | fzf +s +m -n..,1,2.. | sed \"s/ *[0-9]* *//\")\e\C-e\er"'144145 # ALT-C - cd into the selected directory146 bind '"\ec": " \C-e\C-u$(__fcd)\e\C-e\er\C-m"'147else
8 unmodified lines
156 bind '"\C-t": "\e$a \eddi$(__fsel)\C-x\C-e\e0Px$a \C-x\C-r\exa "'157 fi158159 # CTRL-R - Paste the selected command from history into the command line160 bind '"\C-r": "\eddi$(HISTTIMEFORMAT= history | fzf +s | sed \"s/ *[0-9]* *//\")\C-x\C-e\e$a\C-x\C-r"'160 bind '"\C-r": "\eddi$(HISTTIMEFORMAT= history | fzf +s +m -n..,1,2.. | sed \"s/ *[0-9]* *//\")\C-x\C-e\e$a\C-x\C-r"'161162 # ALT-C - cd into the selected directory163 bind '"\ec": "\eddi$(__fcd)\C-x\C-e\C-x\C-r\C-m"'164fi
50 unmodified lines
215bindkey '\ec' fzf-cd-widget216217# CTRL-R - Paste the selected command from history into the command line218fzf-history-widget() {219 LBUFFER=$(fc -l 1 | fzf +s | sed "s/ *[0-9*]* *//")219 LBUFFER=$(fc -l 1 | fzf +s +m -n..,1,2.. | sed "s/ *[0-9*]* *//")220 zle redisplay221}222zle -N fzf-history-widget223bindkey '^R' fzf-history-widget
129 unmodified lines
modified:README.md+3 added-2 removed
63 unmodified lines
64 -x, --extended Extended-search mode65 -e, --extended-exact Extended-search mode (exact match)66 -i Case-insensitive match (default: smart-case match)67 +i Case-sensitive match68 -n, --nth=[-]N[,..] Comma-separated list of field indexes for limiting69 search scope (positive or negative integers)68 -n, --nth=N[,..] Comma-separated list of field index expressions69 for limiting search scope. Each can be a non-zero70 integer or a range expression ([BEGIN]..[END])71 -d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)7273 Search result74 -s, --sort=MAX Maximum number of matched items to sort (default: 1000)
464 unmodified lines