Skip to content
Myr

Prototype implementation of extended mode (#1)

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 90ad6d5 · Browse files · 1 file+49 added−8 removed
This commit

State

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

Diff stats

1 file+49 added−8 removed

  • other:+49 added−8 removed

1 file+49 added−8 removed

modified:fzf+49 added-8 removed
68 unmodified lines
69 usage 0 unless (%w[--help -h] & argv).empty?70 @rxflag = argv.delete('+i') ? 0 : Regexp::IGNORECASE71 @sort = %w[+s --no-sort].map { |e| argv.delete e }.compact.empty? ?72 ENV.fetch('FZF_DEFAULT_SORT', 500).to_i : nil73 @color = %w[+c --no-color].map { |e| argv.delete e }.compact.empty?74 @multi = !%w[-m --multi].map { |e| argv.delete e }.compact.empty?73 @color = %w[+c --no-color].map { |e| argv.delete e }.compact.empty?74 @multi = !%w[-m --multi].map { |e| argv.delete e }.compact.empty?75 @xmode = !%w[-x --extended].map { |e| argv.delete e }.compact.empty?76 rest = argv.join ' '77 if sort = rest.match(/(-s|--sort=?) ?([0-9]+)/)78 usage 1 unless @sort79 @sort = sort[2].to_i
29 unmodified lines
109 def usage x110 $stderr.puts %[usage: fzf [options]111112 -m, --multi Enable multi-select113 -x, --extended Extended mode114 -s, --sort=MAX Maximum number of matched items to sort. Default: 500.115 +s, --no-sort Do not sort the result. Keep the sequence unchanged.116 +i Case-sensitive match117 +c, --no-color Disable colors]
334 unmodified lines
452 end453454 def start_search455 main = Thread.current454 matcher = FuzzyMatcher.new @rxflag456 matcher = (@xmode ? XFuzzyMatcher : FuzzyMatcher).new @rxflag457 searcher = Thread.new {458 lists = []459 events = {}460 fcache = {}
139 unmodified lines
600 @vcursor.set { |v| [0, v + (o == :stab ? 1 : -1)].max }601 end602 } if @multi603 },602 :left => proc { cursor = [0, cursor - 1].max },604 :left => proc { cursor = [0, cursor - 1].max },605 :right => proc { cursor = [input.length, cursor + 1].min },606 }607 actions[ctrl(:b)] = actions[:left]608 actions[ctrl(:f)] = actions[:right]
49 unmodified lines
658 end659 end660661 class FuzzyMatcher < Matcher660 attr_reader :cache662 attr_reader :cache, :rxflag663664 def initialize rxflag665 @cache = Hash.new { |h, k| h[k] = {} }666 @regexp = {}667 @rxflag = rxflag668 end669668 def match list, q, prefix, suffix669 regexp = @regexp[q] ||= begin670 def fuzzy_regex q671 @regexp[q] ||= begin672 q = q.downcase if @rxflag != 0673 Regexp.new(convert_query(q).inject('') { |sum, e|674 e = Regexp.escape e675 sum << "#{e}[^#{e}]*?"676 }, @rxflag)677 end678 end679677 cache = @cache[list.object_id]680 def match list, q, prefix, suffix681 regexp = fuzzy_regex q682683 cache = @cache[list.object_id]684 prefix_cache = nil685 (prefix.length - 1).downto(1) do |len|686 break if prefix_cache = cache[prefix[0, len]]687 end
12 unmodified lines
700 md && [line, [md.offset(0)]]701 }.compact702 end703 end704705 class XFuzzyMatcher < FuzzyMatcher706 def match list, q, prefix, suffix707 regexps = q.strip.split(/\s+/).map { |w|708 invert =709 if w =~ /^!/710 w = w[1..-1]711 true712 end713714 [ case w715 when ''716 nil717 when /^\^/718 w.length > 1 ? Regexp.new('^' << w[1..-1], rxflag) : nil719 when /\$$/720 w.length > 1 ? Regexp.new(w[0..-2] << '$', rxflag) : nil721 else722 fuzzy_regex w723 end, invert ]724 }.select { |pair| pair.first }725726 list.map { |line|727 offsets = []728 regexps.all? { |pair|729 regexp, invert = pair730 md = line.match(regexp) rescue nil731 if md && !invert732 offsets << md.offset(0)733 elsif !md && invert734 true735 end736 } && [line, offsets]737 }.select { |e| e }738 end739 end740end#FZF741742FZF.new(ARGV, $stdin).start if $0 == __FILE__743