mirror of
https://github.com/Llewellynvdm/fzf.git
synced 2024-11-25 06:07:42 +00:00
Refactoring: separate renderer thread
This commit is contained in:
parent
f0a5757244
commit
419bc17c0c
189
fzf
189
fzf
@ -37,6 +37,7 @@
|
|||||||
|
|
||||||
require 'thread'
|
require 'thread'
|
||||||
require 'curses'
|
require 'curses'
|
||||||
|
require 'set'
|
||||||
|
|
||||||
class FZF
|
class FZF
|
||||||
C = Curses
|
C = Curses
|
||||||
@ -86,22 +87,26 @@ class FZF
|
|||||||
@cv = ConditionVariable.new
|
@cv = ConditionVariable.new
|
||||||
@events = {}
|
@events = {}
|
||||||
@new = []
|
@new = []
|
||||||
@smtx = Mutex.new
|
@queue = Queue.new
|
||||||
@cursor_x = AtomicVar.new(0)
|
@cursor_x = AtomicVar.new(0)
|
||||||
@query = AtomicVar.new('')
|
@query = AtomicVar.new('')
|
||||||
@matches = AtomicVar.new([])
|
@matches = AtomicVar.new([])
|
||||||
@count = AtomicVar.new(0)
|
@count = AtomicVar.new(0)
|
||||||
@vcursor = AtomicVar.new(0)
|
@vcursor = AtomicVar.new(0)
|
||||||
|
@vcursors = AtomicVar.new(Set.new)
|
||||||
@spinner = AtomicVar.new('-\|/-\|/'.split(//))
|
@spinner = AtomicVar.new('-\|/-\|/'.split(//))
|
||||||
@selects = AtomicVar.new({}) # ordered >= 1.9
|
@selects = AtomicVar.new({}) # ordered >= 1.9
|
||||||
|
@main = Thread.current
|
||||||
|
@stdout = $stdout.clone
|
||||||
|
@plcount = 0
|
||||||
end
|
end
|
||||||
|
|
||||||
def start
|
def start
|
||||||
@stdout = $stdout.clone
|
|
||||||
$stdout.reopen($stderr)
|
$stdout.reopen($stderr)
|
||||||
|
|
||||||
init_screen
|
init_screen
|
||||||
start_reader
|
start_reader
|
||||||
|
start_renderer
|
||||||
start_search
|
start_search
|
||||||
start_loop
|
start_loop
|
||||||
end
|
end
|
||||||
@ -256,8 +261,7 @@ class FZF
|
|||||||
C.setpos cursor_y - 1, 0
|
C.setpos cursor_y - 1, 0
|
||||||
C.clrtoeol
|
C.clrtoeol
|
||||||
prefix =
|
prefix =
|
||||||
if spinner = @spinner.shift
|
if spinner = @spinner.first
|
||||||
@spinner.push spinner
|
|
||||||
cprint spinner, color(:spinner, true)
|
cprint spinner, color(:spinner, true)
|
||||||
' '
|
' '
|
||||||
else
|
else
|
||||||
@ -481,14 +485,11 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
def start_search
|
def start_search
|
||||||
main = Thread.current
|
|
||||||
matcher = (@xmode ? ExtendedFuzzyMatcher : FuzzyMatcher).new @rxflag
|
matcher = (@xmode ? ExtendedFuzzyMatcher : FuzzyMatcher).new @rxflag
|
||||||
searcher = Thread.new {
|
searcher = Thread.new {
|
||||||
lists = []
|
lists = []
|
||||||
events = {}
|
events = {}
|
||||||
fcache = {}
|
fcache = {}
|
||||||
mcount = 0 # match count
|
|
||||||
plcount = 0 # prev list count
|
|
||||||
q = ''
|
q = ''
|
||||||
delay = -5
|
delay = -5
|
||||||
|
|
||||||
@ -509,21 +510,25 @@ class FZF
|
|||||||
if events[:new]
|
if events[:new]
|
||||||
lists << @new
|
lists << @new
|
||||||
@count.set { |c| c + @new.length }
|
@count.set { |c| c + @new.length }
|
||||||
@new = []
|
@spinner.set { |spinner|
|
||||||
fcache = {}
|
if e = spinner.shift
|
||||||
|
spinner.push e
|
||||||
|
end; spinner
|
||||||
|
}
|
||||||
|
@new = []
|
||||||
|
fcache.clear
|
||||||
end
|
end
|
||||||
end#mtx
|
end#mtx
|
||||||
|
|
||||||
new_search = events[:key] || events.delete(:new)
|
new_search = events[:key] || events.delete(:new)
|
||||||
user_input = events[:key] || events[:vcursor] || events.delete(:select)
|
user_input = events[:key]
|
||||||
progress = 0
|
progress = 0
|
||||||
started_at = Time.now
|
started_at = Time.now
|
||||||
|
|
||||||
if new_search && !lists.empty?
|
if new_search && !lists.empty?
|
||||||
q, cx = events.delete(:key) || [q, 0]
|
q, cx = events.delete(:key) || [q, 0]
|
||||||
|
empty = matcher.empty?(q)
|
||||||
plcount = [@matches.length, max_items].min
|
matches = fcache[q] ||=
|
||||||
@matches.set(fcache[q] ||=
|
|
||||||
begin
|
begin
|
||||||
found = []
|
found = []
|
||||||
skip = false
|
skip = false
|
||||||
@ -533,12 +538,8 @@ class FZF
|
|||||||
skip = @mtx.synchronize { @events[:key] }
|
skip = @mtx.synchronize { @events[:key] }
|
||||||
break if skip
|
break if skip
|
||||||
|
|
||||||
progress = (100 * cnt / @count.get)
|
if !empty && (progress = 100 * cnt / @count.get) < 100 && Time.now - started_at > 0.5
|
||||||
if progress < 100 && Time.now - started_at > 0.5 && !q.empty?
|
render { print_info " (#{progress}%)" }
|
||||||
@smtx.synchronize do
|
|
||||||
print_info " (#{progress}%)"
|
|
||||||
refresh
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
found.concat(q.empty? ? list :
|
found.concat(q.empty? ? list :
|
||||||
@ -546,53 +547,84 @@ class FZF
|
|||||||
end
|
end
|
||||||
next if skip
|
next if skip
|
||||||
@sort ? found : found.reverse
|
@sort ? found : found.reverse
|
||||||
end)
|
end
|
||||||
|
|
||||||
mcount = @matches.length
|
if !empty && @sort && matches.length <= @sort
|
||||||
if @sort && mcount <= @sort && !matcher.empty?(q)
|
matches = sort_by_rank(matches)
|
||||||
@matches.set { |m| sort_by_rank m }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Atomic update
|
||||||
|
@matches.set matches
|
||||||
end#new_search
|
end#new_search
|
||||||
|
|
||||||
# This small delay reduces the number of partial lists
|
# This small delay reduces the number of partial lists
|
||||||
sleep((delay = [20, delay + 5].min) * 0.01) unless user_input
|
sleep((delay = [20, delay + 5].min) * 0.01) unless user_input
|
||||||
|
|
||||||
if events.delete(:vcursor) || new_search
|
update_list new_search
|
||||||
@vcursor.set { |v| [0, [v, mcount - 1, max_items - 1].min].max }
|
|
||||||
end
|
|
||||||
|
|
||||||
# Output
|
|
||||||
@smtx.synchronize do
|
|
||||||
item_length = [mcount, max_items].min
|
|
||||||
if item_length < plcount
|
|
||||||
plcount.downto(item_length) do |idx|
|
|
||||||
C.setpos cursor_y - idx - 2, 0
|
|
||||||
C.clrtoeol
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
maxc = C.cols - 3
|
|
||||||
vcursor = @vcursor.get
|
|
||||||
@matches[0, max_items].each_with_index do |item, idx|
|
|
||||||
next if !new_search && !((vcursor-1)..(vcursor+1)).include?(idx)
|
|
||||||
row = cursor_y - idx - 2
|
|
||||||
chosen = idx == vcursor
|
|
||||||
selected = @selects.include?([*item][0])
|
|
||||||
line, offsets = convert_item item
|
|
||||||
tokens = format line, maxc, offsets
|
|
||||||
print_item row, tokens, chosen, selected
|
|
||||||
end
|
|
||||||
|
|
||||||
print_info if !lists.empty? || events[:loaded]
|
|
||||||
refresh
|
|
||||||
end
|
|
||||||
end#while
|
end#while
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
main.raise e
|
@main.raise e
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def pick
|
||||||
|
items = @matches[0, max_items]
|
||||||
|
curr = [0, [@vcursor.get, items.length - 1].min].max
|
||||||
|
[*items.fetch(curr, [])][0]
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_list wipe
|
||||||
|
render do
|
||||||
|
items = @matches[0, max_items]
|
||||||
|
|
||||||
|
# Wipe
|
||||||
|
if items.length < @plcount
|
||||||
|
@plcount.downto(items.length) do |idx|
|
||||||
|
C.setpos cursor_y - idx - 2, 0
|
||||||
|
C.clrtoeol
|
||||||
|
end
|
||||||
|
end
|
||||||
|
@plcount = items.length
|
||||||
|
|
||||||
|
maxc = C.cols - 3
|
||||||
|
vcursor = @vcursor.set { |v| [0, [v, items.length - 1].min].max }
|
||||||
|
cleanse = Set[vcursor]
|
||||||
|
@vcursors.set { |vs|
|
||||||
|
cleanse.merge vs
|
||||||
|
Set.new
|
||||||
|
}
|
||||||
|
items.each_with_index do |item, idx|
|
||||||
|
next unless wipe || cleanse.include?(idx)
|
||||||
|
row = cursor_y - idx - 2
|
||||||
|
chosen = idx == vcursor
|
||||||
|
selected = @selects.include?([*item][0])
|
||||||
|
line, offsets = convert_item item
|
||||||
|
tokens = format line, maxc, offsets
|
||||||
|
print_item row, tokens, chosen, selected
|
||||||
|
end
|
||||||
|
print_info
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def start_renderer
|
||||||
|
Thread.new do
|
||||||
|
begin
|
||||||
|
while blk = @queue.shift
|
||||||
|
blk.call
|
||||||
|
refresh
|
||||||
|
end
|
||||||
|
rescue Exception => e
|
||||||
|
@main.raise e
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def render &blk
|
||||||
|
@queue.push blk
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
def start_loop
|
def start_loop
|
||||||
got = nil
|
got = nil
|
||||||
begin
|
begin
|
||||||
@ -600,18 +632,18 @@ class FZF
|
|||||||
input = ''
|
input = ''
|
||||||
cursor = 0
|
cursor = 0
|
||||||
actions = {
|
actions = {
|
||||||
:nop => proc {},
|
:nop => proc { nil },
|
||||||
ctrl(:c) => proc { exit 1 },
|
ctrl(:c) => proc { exit 1 },
|
||||||
ctrl(:d) => proc { exit 1 if input.empty? },
|
ctrl(:d) => proc { exit 1 if input.empty? },
|
||||||
ctrl(:m) => proc {
|
ctrl(:m) => proc {
|
||||||
got = [*@matches.fetch(@vcursor.get, [])][0]
|
got = pick
|
||||||
exit 0
|
exit 0
|
||||||
},
|
},
|
||||||
ctrl(:u) => proc { input = input[cursor..-1]; cursor = 0 },
|
ctrl(:u) => proc { input = input[cursor..-1]; cursor = 0 },
|
||||||
ctrl(:a) => proc { cursor = 0 },
|
ctrl(:a) => proc { cursor = 0; nil },
|
||||||
ctrl(:e) => proc { cursor = input.length },
|
ctrl(:e) => proc { cursor = input.length; nil },
|
||||||
ctrl(:j) => proc { emit(:vcursor) { @vcursor.set { |v| v - 1 } } },
|
ctrl(:j) => proc { @vcursor.set { |v| @vcursors << v; v - 1 }; update_list false },
|
||||||
ctrl(:k) => proc { emit(:vcursor) { @vcursor.set { |v| v + 1 } } },
|
ctrl(:k) => proc { @vcursor.set { |v| @vcursors << v; v + 1 }; update_list false },
|
||||||
ctrl(:w) => proc {
|
ctrl(:w) => proc {
|
||||||
ridx = (input[0...cursor - 1].rindex(/\S\s/) || -2) + 2
|
ridx = (input[0...cursor - 1].rindex(/\S\s/) || -2) + 2
|
||||||
input = input[0...ridx] + input[cursor..-1]
|
input = input[0...ridx] + input[cursor..-1]
|
||||||
@ -619,19 +651,21 @@ class FZF
|
|||||||
},
|
},
|
||||||
127 => proc { input[cursor -= 1] = '' if cursor > 0 },
|
127 => proc { input[cursor -= 1] = '' if cursor > 0 },
|
||||||
9 => proc { |o|
|
9 => proc { |o|
|
||||||
emit(:select) {
|
if @multi && sel = pick
|
||||||
if sel = [*@matches.fetch(@vcursor.get, [])][0]
|
if @selects.has_key? sel
|
||||||
if @selects.has_key? sel
|
@selects.delete sel
|
||||||
@selects.delete sel
|
else
|
||||||
else
|
@selects[sel] = 1
|
||||||
@selects[sel] = 1
|
|
||||||
end
|
|
||||||
@vcursor.set { |v| [0, v + (o == :stab ? 1 : -1)].max }
|
|
||||||
end
|
end
|
||||||
} if @multi
|
@vcursor.set { |v|
|
||||||
|
@vcursors << v
|
||||||
|
v + (o == :stab ? 1 : -1)
|
||||||
|
}
|
||||||
|
update_list false
|
||||||
|
end
|
||||||
},
|
},
|
||||||
:left => proc { cursor = [0, cursor - 1].max },
|
:left => proc { cursor = [0, cursor - 1].max; nil },
|
||||||
:right => proc { cursor = [input.length, cursor + 1].min },
|
:right => proc { cursor = [input.length, cursor + 1].min; nil },
|
||||||
}
|
}
|
||||||
actions[ctrl(:b)] = actions[:left]
|
actions[ctrl(:b)] = actions[:left]
|
||||||
actions[ctrl(:f)] = actions[:right]
|
actions[ctrl(:f)] = actions[:right]
|
||||||
@ -642,16 +676,12 @@ class FZF
|
|||||||
|
|
||||||
while true
|
while true
|
||||||
@cursor_x.set cursor
|
@cursor_x.set cursor
|
||||||
# Update user input
|
render { print_input }
|
||||||
@smtx.synchronize do
|
|
||||||
print_input
|
|
||||||
refresh
|
|
||||||
end
|
|
||||||
|
|
||||||
ord = tty.getc.ord
|
ord = tty.getc.ord
|
||||||
if ord == 27
|
if ord == 27
|
||||||
ord = tty.getc.ord
|
case ord = tty.getc.ord
|
||||||
if ord == 91
|
when 91
|
||||||
ord = case tty.getc.ord
|
ord = case tty.getc.ord
|
||||||
when 68 then :left
|
when 68 then :left
|
||||||
when 67 then :right
|
when 67 then :right
|
||||||
@ -663,7 +693,7 @@ class FZF
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
actions.fetch(ord, proc { |ord|
|
upd = actions.fetch(ord, proc { |ord|
|
||||||
char = [ord].pack('U*')
|
char = [ord].pack('U*')
|
||||||
if char =~ /[[:print:]]/
|
if char =~ /[[:print:]]/
|
||||||
input.insert cursor, char
|
input.insert cursor, char
|
||||||
@ -672,7 +702,7 @@ class FZF
|
|||||||
}).call(ord)
|
}).call(ord)
|
||||||
|
|
||||||
# Dispatch key event
|
# Dispatch key event
|
||||||
emit(:key) { [@query.set(input.dup), cursor] }
|
emit(:key) { [@query.set(input.dup), cursor] } if upd
|
||||||
end
|
end
|
||||||
ensure
|
ensure
|
||||||
C.close_screen
|
C.close_screen
|
||||||
@ -739,7 +769,6 @@ class FZF
|
|||||||
class ExtendedFuzzyMatcher < FuzzyMatcher
|
class ExtendedFuzzyMatcher < FuzzyMatcher
|
||||||
def initialize rxflag
|
def initialize rxflag
|
||||||
super
|
super
|
||||||
require 'set'
|
|
||||||
@regexps = {}
|
@regexps = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user