2
0
mirror of https://github.com/junegunn/fzf synced 2024-11-16 12:12:48 +00:00

Stop searching if query string has changed

This commit is contained in:
Junegunn Choi 2013-10-26 02:24:38 +09:00
parent 5b3af8ec1e
commit 7cd97cc763

34
fzf
View File

@ -73,7 +73,6 @@ require 'curses'
@cursor_x = 0 @cursor_x = 0
@vcursor = 0 @vcursor = 0
@events = {} @events = {}
@stat = { :hit => 0, :partial_hit => 0, :prefix_hit => 0, :search => 0 }
def emit event def emit event
@mtx.synchronize do @mtx.synchronize do
@ -232,33 +231,28 @@ searcher = Thread.new {
sum << "#{e}[^#{e}]*?" sum << "#{e}[^#{e}]*?"
}, @rxflag) }, @rxflag)
matches = matches = fcache[q] ||=
if fcache.has_key?(q) begin
@stat[:hit] += 1
fcache[q]
else
@smtx.synchronize do @smtx.synchronize do
print_info true, ' ..' print_info true, ' ..'
refresh refresh
end unless q.empty? end unless q.empty?
found = @lists.map { |pair| found = []
list, cache = pair skip = false
@lists.each do |pair|
@mtx.synchronize { skip = @events[:key] }
break if skip
if cache[q] list, cache = pair
@stat[:partial_hit] += 1 found.concat(cache[q] ||= begin
cache[q]
else
prefix_cache = nil prefix_cache = nil
(q.length - 1).downto(1) do |len| (q.length - 1).downto(1) do |len|
prefix = q[0, len] prefix = q[0, len]
if prefix_cache = cache[prefix] break if prefix_cache = cache[prefix]
@stat[:prefix_hit] += 1
break
end
end end
cache[q] ||= (prefix_cache ? prefix_cache.map { |e| e.first } : list).map { |line| (prefix_cache ? prefix_cache.map { |e| e.first } : list).map { |line|
if regexp if regexp
# Ignore errors: e.g. invalid byte sequence in UTF-8 # Ignore errors: e.g. invalid byte sequence in UTF-8
md = line.match(regexp) rescue nil md = line.match(regexp) rescue nil
@ -267,11 +261,11 @@ searcher = Thread.new {
[line, zz] [line, zz]
end end
}.compact }.compact
end)
end end
}.inject([]) { |all, e| all.concat e } next if skip
fcache[q] = @sort ? found : found.reverse @sort ? found : found.reverse
end end
@stat[:search] += 1
mcount = matches.length mcount = matches.length
if @sort && mcount <= @sort if @sort && mcount <= @sort