Skip to content
Myr

Refactoring: separate renderer thread

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 f0a5757 · Browse files · 1 file+108 added−79 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+108 added−79 removed

  • other:+108 added−79 removed

1 file+108 added−79 removed

modified:fzf+108 added-79 removed
35 unmodified lines
36# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.3738require 'thread'39require 'curses'40require 'set'4142class FZF43 C = Curses44 attr_reader :rxflag, :sort, :color, :multi
41 unmodified lines
86 @mtx = Mutex.new87 @cv = ConditionVariable.new88 @events = {}89 @new = []89 @smtx = Mutex.new90 @queue = Queue.new91 @cursor_x = AtomicVar.new(0)92 @query = AtomicVar.new('')93 @matches = AtomicVar.new([])94 @count = AtomicVar.new(0)95 @vcursor = AtomicVar.new(0)96 @vcursors = AtomicVar.new(Set.new)97 @spinner = AtomicVar.new('-\|/-\|/'.split(//))98 @selects = AtomicVar.new({}) # ordered >= 1.999 @main = Thread.current100 @stdout = $stdout.clone101 @plcount = 0102 end103104 def start100 @stdout = $stdout.clone105 $stdout.reopen($stderr)106107 init_screen108 start_reader109 start_renderer110 start_search111 start_loop112 end113
146 unmodified lines
260 def print_info msg = nil261 C.setpos cursor_y - 1, 0262 C.clrtoeol263 prefix =259 if spinner = @spinner.shift260 @spinner.push spinner264 if spinner = @spinner.first265 cprint spinner, color(:spinner, true)266 ' '267 else268 ' '
215 unmodified lines
484 end485 end486487 def start_search484 main = Thread.current488 matcher = (@xmode ? ExtendedFuzzyMatcher : FuzzyMatcher).new @rxflag489 searcher = Thread.new {490 lists = []491 events = {}492 fcache = {}490 mcount = 0 # match count491 plcount = 0 # prev list count493 q = ''494 delay = -5495496 begin
12 unmodified lines
509510 if events[:new]511 lists << @new512 @count.set { |c| c + @new.length }513 @spinner.set { |spinner|514 if e = spinner.shift515 spinner.push e516 end; spinner517 }512 @new = []513 fcache = {}518 @new = []519 fcache.clear520 end521 end#mtx522523 new_search = events[:key] || events.delete(:new)518 user_input = events[:key] || events[:vcursor] || events.delete(:select)524 user_input = events[:key]525 progress = 0526 started_at = Time.now527528 if new_search && !lists.empty?529 q, cx = events.delete(:key) || [q, 0]524525 plcount = [@matches.length, max_items].min526 @matches.set(fcache[q] ||=530 empty = matcher.empty?(q)531 matches = fcache[q] ||=532 begin533 found = []534 skip = false535 cnt = 0
1 unmodified line
537 cnt += list.length538 skip = @mtx.synchronize { @events[:key] }539 break if skip540536 progress = (100 * cnt / @count.get)537 if progress < 100 && Time.now - started_at > 0.5 && !q.empty?538 @smtx.synchronize do539 print_info " (#{progress}%)"540 refresh541 end541 if !empty && (progress = 100 * cnt / @count.get) < 100 && Time.now - started_at > 0.5542 render { print_info " (#{progress}%)" }543 end544545 found.concat(q.empty? ? list :546 matcher.match(list, q, q[0, cx], q[cx..-1]))547 end548 next if skip549 @sort ? found : found.reverse549 end)550 end551551 mcount = @matches.length552 if @sort && mcount <= @sort && !matcher.empty?(q)553 @matches.set { |m| sort_by_rank m }552 if !empty && @sort && matches.length <= @sort553 matches = sort_by_rank(matches)554 end555556 # Atomic update557 @matches.set matches558 end#new_search559560 # This small delay reduces the number of partial lists561 sleep((delay = [20, delay + 5].min) * 0.01) unless user_input562560 if events.delete(:vcursor) || new_search561 @vcursor.set { |v| [0, [v, mcount - 1, max_items - 1].min].max }562 end563 update_list new_search564 end#while565 rescue Exception => e566 @main.raise e567 end568 }569 end570564 # Output565 @smtx.synchronize do566 item_length = [mcount, max_items].min567 if item_length < plcount568 plcount.downto(item_length) do |idx|569 C.setpos cursor_y - idx - 2, 0570 C.clrtoeol571 end572 end571 def pick572 items = @matches[0, max_items]573 curr = [0, [@vcursor.get, items.length - 1].min].max574 [*items.fetch(curr, [])][0]575 end576574 maxc = C.cols - 3575 vcursor = @vcursor.get576 @matches[0, max_items].each_with_index do |item, idx|577 next if !new_search && !((vcursor-1)..(vcursor+1)).include?(idx)578 row = cursor_y - idx - 2579 chosen = idx == vcursor580 selected = @selects.include?([*item][0])581 line, offsets = convert_item item582 tokens = format line, maxc, offsets583 print_item row, tokens, chosen, selected584 end577 def update_list wipe578 render do579 items = @matches[0, max_items]580586 print_info if !lists.empty? || events[:loaded]587 refresh588 end589 end#while581 # Wipe582 if items.length < @plcount583 @plcount.downto(items.length) do |idx|584 C.setpos cursor_y - idx - 2, 0585 C.clrtoeol586 end587 end588 @plcount = items.length589590 maxc = C.cols - 3591 vcursor = @vcursor.set { |v| [0, [v, items.length - 1].min].max }592 cleanse = Set[vcursor]593 @vcursors.set { |vs|594 cleanse.merge vs595 Set.new596 }597 items.each_with_index do |item, idx|598 next unless wipe || cleanse.include?(idx)599 row = cursor_y - idx - 2600 chosen = idx == vcursor601 selected = @selects.include?([*item][0])602 line, offsets = convert_item item603 tokens = format line, maxc, offsets604 print_item row, tokens, chosen, selected605 end606 print_info607 end608 end609610 def start_renderer611 Thread.new do612 begin613 while blk = @queue.shift614 blk.call615 refresh616 end617 rescue Exception => e591 main.raise e618 @main.raise e619 end593 }620 end621 end622623 def render &blk624 @queue.push blk625 nil626 end627628 def start_loop629 got = nil630 begin631 tty = IO.open(IO.sysopen('/dev/tty'), 'r')632 input = ''633 cursor = 0634 actions = {603 :nop => proc {},635 :nop => proc { nil },636 ctrl(:c) => proc { exit 1 },637 ctrl(:d) => proc { exit 1 if input.empty? },638 ctrl(:m) => proc {607 got = [*@matches.fetch(@vcursor.get, [])][0]639 got = pick640 exit 0641 },642 ctrl(:u) => proc { input = input[cursor..-1]; cursor = 0 },611 ctrl(:a) => proc { cursor = 0 },612 ctrl(:e) => proc { cursor = input.length },613 ctrl(:j) => proc { emit(:vcursor) { @vcursor.set { |v| v - 1 } } },614 ctrl(:k) => proc { emit(:vcursor) { @vcursor.set { |v| v + 1 } } },643 ctrl(:a) => proc { cursor = 0; nil },644 ctrl(:e) => proc { cursor = input.length; nil },645 ctrl(:j) => proc { @vcursor.set { |v| @vcursors << v; v - 1 }; update_list false },646 ctrl(:k) => proc { @vcursor.set { |v| @vcursors << v; v + 1 }; update_list false },647 ctrl(:w) => proc {648 ridx = (input[0...cursor - 1].rindex(/\S\s/) || -2) + 2649 input = input[0...ridx] + input[cursor..-1]650 cursor = ridx651 },652 127 => proc { input[cursor -= 1] = '' if cursor > 0 },653 9 => proc { |o|622 emit(:select) {623 if sel = [*@matches.fetch(@vcursor.get, [])][0]624 if @selects.has_key? sel625 @selects.delete sel626 else627 @selects[sel] = 1654 if @multi && sel = pick655 if @selects.has_key? sel656 @selects.delete sel657 else658 @selects[sel] = 1628 end629 @vcursor.set { |v| [0, v + (o == :stab ? 1 : -1)].max }659 end631 } if @multi660 @vcursor.set { |v|661 @vcursors << v662 v + (o == :stab ? 1 : -1)663 }664 update_list false665 end666 },633 :left => proc { cursor = [0, cursor - 1].max },634 :right => proc { cursor = [input.length, cursor + 1].min },667 :left => proc { cursor = [0, cursor - 1].max; nil },668 :right => proc { cursor = [input.length, cursor + 1].min; nil },669 }670 actions[ctrl(:b)] = actions[:left]671 actions[ctrl(:f)] = actions[:right]672 actions[ctrl(:h)] = actions[127]
2 unmodified lines
675 actions[:stab] = actions[9]676677 while true678 @cursor_x.set cursor645 # Update user input646 @smtx.synchronize do647 print_input648 refresh649 end679 render { print_input }680681 ord = tty.getc.ord682 if ord == 27653 ord = tty.getc.ord654 if ord == 91683 case ord = tty.getc.ord684 when 91685 ord = case tty.getc.ord686 when 68 then :left687 when 67 then :right688 when 66 then ctrl(:j)
3 unmodified lines
692 end693 end694 end695666 actions.fetch(ord, proc { |ord|696 upd = actions.fetch(ord, proc { |ord|697 char = [ord].pack('U*')698 if char =~ /[[:print:]]/699 input.insert cursor, char700 cursor += 1701 end702 }).call(ord)703704 # Dispatch key event675 emit(:key) { [@query.set(input.dup), cursor] }705 emit(:key) { [@query.set(input.dup), cursor] } if upd706 end707 ensure708 C.close_screen709 if got
58 unmodified lines
768769 class ExtendedFuzzyMatcher < FuzzyMatcher770 def initialize rxflag771 super742 require 'set'772 @regexps = {}773 end774775 def empty? q
61 unmodified lines