From e392da20e8901a72615029ab85e9efe1622da2cd Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Thu, 26 Jun 2014 12:50:52 +0900 Subject: [PATCH 1/6] Make scrollable (#68) --- fzf | 196 ++++++++++++++++++++++++++++------------------- test/test_fzf.rb | 12 +-- 2 files changed, 122 insertions(+), 86 deletions(-) mode change 100755 => 100644 fzf diff --git a/fzf b/fzf old mode 100755 new mode 100644 index 4adda6d..19a83bf --- a/fzf +++ b/fzf @@ -7,7 +7,7 @@ # / __/ / /_/ __/ # /_/ /___/_/ Fuzzy finder for your shell # -# Version: 0.8.5 (Jun 15, 2014) +# Version: 0.8.6 (Jun 26, 2014) # # Author: Junegunn Choi # URL: https://github.com/junegunn/fzf @@ -53,24 +53,26 @@ class FZF attr_reader :rxflag, :sort, :nth, :color, :black, :ansi256, :reverse, :mouse, :multi, :query, :select1, :exit0, :filter, :extended - class AtomicVar - def initialize value - @value = value - @mutex = Mutex.new - end + def sync + @shr_mtx.synchronize { yield } + end - def get - @mutex.synchronize { @value } - end + def get name + sync { instance_variable_get name } + end - def set value = nil - @mutex.synchronize do - @value = block_given? ? yield(@value) : value - end - end + def geta(*names) + sync { names.map { |name| instance_variable_get name } } + end - def method_missing sym, *args, &blk - @mutex.synchronize { @value.send(sym, *args, &blk) } + def call(name, method, *args) + sync { instance_variable_get(name).send(method, *args) } + end + + def set name, value = nil + sync do + instance_variable_set name, + (block_given? ? yield(instance_variable_get name) : value) end end @@ -89,6 +91,7 @@ class FZF @nth = nil @delim = nil @reverse = false + @shr_mtx = Mutex.new argv = if opts = ENV['FZF_DEFAULT_OPTS'] @@ -124,9 +127,9 @@ class FZF when '+0', '--no-exit-0' then @exit0 = false when '-q', '--query' usage 1, 'query string required' unless query = argv.shift - @query = AtomicVar.new query.dup + @query = query.dup when /^-q(.*)$/, /^--query=(.*)$/ - @query = AtomicVar.new($1) + @query = $1 when '-f', '--filter' usage 1, 'query string required' unless query = argv.shift @filter = query @@ -155,25 +158,29 @@ class FZF end end - @source = source.clone - @mtx = Mutex.new - @cv = ConditionVariable.new - @events = {} - @new = [] - @queue = Queue.new - @pending = nil + @source = source.clone + @evt_mtx = Mutex.new + @cv = ConditionVariable.new + @events = {} + @new = [] + @queue = Queue.new + @pending = nil + @rev_dir = @reverse ? -1 : 1 unless @filter - @query ||= AtomicVar.new('') - @cursor_x = AtomicVar.new(@query.length) - @matches = AtomicVar.new([]) - @count = AtomicVar.new(0) - @vcursor = AtomicVar.new(0) - @vcursors = AtomicVar.new(Set.new) - @spinner = AtomicVar.new('-\|/-\|/'.split(//)) - @selects = AtomicVar.new({}) # ordered >= 1.9 - @main = Thread.current - @plcount = 0 + # Shared variables: needs protection + @query ||= '' + @matches = [] + @count = 0 + @xcur = get(:@query).length + @ycur = 0 + @yoff = 0 + @dirty = Set.new + @spinner = '-\|/-\|/'.split(//) + @selects = {} # ordered >= 1.9 + + @main = Thread.current + @plcount = 0 end end @@ -206,10 +213,11 @@ class FZF filter_list @new else start_reader - emit(:key) { q = @query.get; [q, q.length] } unless empty = @query.empty? + query = get(:@query) + emit(:key) { [query, query.length] } unless empty = query.empty? if @select1 || @exit0 start_search do |loaded, matches| - len = empty ? @count.get : matches.length + len = empty ? get(:@count) : matches.length if loaded if @select1 && len == 1 puts empty ? matches.first : matches.first.first @@ -320,7 +328,7 @@ class FZF end def emit event - @mtx.synchronize do + @evt_mtx.synchronize do @events[event] = yield @cv.broadcast end @@ -346,31 +354,35 @@ class FZF C.clrtoeol cprint '> ', color(:prompt, true) C.attron(C::A_BOLD) do - C.addstr @query.get + C.addstr get(:@query) end end def print_info msg = nil C.setpos cursor_y(1), 0 C.clrtoeol + prefix = - if spinner = @spinner.first - cprint spinner, color(:spinner, true) + if spin_char = call(:@spinner, :first) + cprint spin_char, color(:spinner, true) ' ' else ' ' end C.attron color(:info, false) do - C.addstr "#{prefix}#{@matches.length}/#{@count.get}" - if (selected = @selects.length) > 0 - C.addstr " (#{selected})" + sync do + C.addstr "#{prefix}#{@matches.length}/#{@count}" + if (selected = @selects.length) > 0 + C.addstr " (#{selected})" + end end C.addstr msg if msg end end def refresh - C.setpos cursor_y, 2 + width(@query[0, @cursor_x.get]) + query, xcur = geta(:@query, :@xcur) + C.setpos cursor_y, 2 + width(query[0, xcur]) C.refresh end @@ -580,12 +592,12 @@ class FZF begin while true - @mtx.synchronize do + @evt_mtx.synchronize do while true events.merge! @events if @events.empty? # No new events - @cv.wait @mtx + @cv.wait @evt_mtx next end @events.clear @@ -594,8 +606,8 @@ class FZF if events[:new] lists << @new - @count.set { |c| c + @new.length } - @spinner.set { |spinner| + set(:@count) { |c| c + @new.length } + set(:@spinner) { |spinner| if e = spinner.shift spinner.push e end; spinner @@ -619,10 +631,10 @@ class FZF cnt = 0 lists.each do |list| cnt += list.length - skip = @mtx.synchronize { @events[:key] } + skip = @evt_mtx.synchronize { @events[:key] } break if skip - if !empty && (progress = 100 * cnt / @count.get) < 100 && Time.now - started_at > 0.5 + if !empty && (progress = 100 * cnt / get(:@count)) < 100 && Time.now - started_at > 0.5 render { print_info " (#{progress}%)" } end @@ -641,7 +653,7 @@ class FZF end # Atomic update - @matches.set matches + set(:@matches, matches) end#new_search callback = nil if callback && @@ -660,14 +672,32 @@ class FZF end def pick - items = @matches[0, max_items] - curr = [0, [@vcursor.get, items.length - 1].min].max - [*items.fetch(curr, [])][0] + sync do + [*@matches.fetch(@ycur, [])][0] + end end def update_list wipe render do - items = @matches[0, max_items] + offset, ycur, items = sync { + cnt = @matches.length + pos = @ycur - @yoff + @ycur = [0, [@ycur, cnt - 1].min].max + + if @ycur - @yoff >= max_items + @yoff = @ycur - max_items + 1 + wipe = true + elsif @yoff >= @ycur + @yoff = @ycur + wipe = true + end + if cnt - @yoff < max_items + @yoff = [0, cnt - max_items].max + wipe = true + @ycur = [0, [@yoff + pos, cnt - 1].min].max + end + [@yoff, @ycur, @matches[@yoff, max_items]] + } # Wipe if items.length < @plcount @@ -679,16 +709,16 @@ class FZF @plcount = items.length maxc = C.cols - 3 - vcursor = @vcursor.set { |v| [0, [v, items.length - 1].min].max } - cleanse = Set[vcursor] - @vcursors.set { |vs| + cline = ycur - offset + cleanse = Set[cline] + set(:@dirty) do |vs| cleanse.merge vs Set.new - } + end items.each_with_index do |item, idx| next unless wipe || cleanse.include?(idx) row = cursor_y(idx + 2) - chosen = idx == vcursor + chosen = idx == cline selected = @selects.include?([*item][0]) line, offsets = item tokens = format line, maxc, offsets @@ -720,7 +750,10 @@ class FZF end def vselect &prc - @vcursor.set { |v| @vcursors << v; prc.call v } + sync do + @dirty << @ycur - @yoff + @ycur = prc.call @ycur + end update_list false end @@ -885,7 +918,7 @@ class FZF def start_loop got = nil begin - input = @query.get.dup + input = call(:@query, :dup) cursor = input.length yanked = '' mouse_event = MouseEvent.new @@ -912,8 +945,8 @@ class FZF }, ctrl(:a) => proc { cursor = 0; nil }, ctrl(:e) => proc { cursor = input.length; nil }, - ctrl(:j) => proc { vselect { |v| v - (@reverse ? -1 : 1) } }, - ctrl(:k) => proc { vselect { |v| v + (@reverse ? -1 : 1) } }, + ctrl(:j) => proc { vselect { |v| v - @rev_dir } }, + ctrl(:k) => proc { vselect { |v| v + @rev_dir } }, ctrl(:w) => proc { pcursor = cursor backword.call @@ -924,26 +957,28 @@ class FZF ctrl(:h) => proc { input[cursor -= 1] = '' if cursor > 0 }, ctrl(:i) => proc { |o| if @multi && sel = pick - if @selects.has_key? sel - @selects.delete sel - else - @selects[sel] = 1 + sync do + if @selects.has_key? sel + @selects.delete sel + else + @selects[sel] = 1 + end end vselect { |v| v + case o when :stab then 1 when :sclick then 0 else -1 - end * (@reverse ? -1 : 1) } + end * @rev_dir } end }, ctrl(:b) => proc { cursor = [0, cursor - 1].max; nil }, ctrl(:f) => proc { cursor = [input.length, cursor + 1].min; nil }, ctrl(:l) => proc { render { C.clear; C.refresh }; update_list true }, - :del => proc { input[cursor] = '' if input.length > cursor }, - :pgup => proc { vselect { |_| max_items } }, - :pgdn => proc { vselect { |_| 0 } }, - :alt_b => proc { backword.call; nil }, - :alt_f => proc { + :del => proc { input[cursor] = '' if input.length > cursor }, + :pgup => proc { vselect { |v| v + @rev_dir * (max_items - 1) } }, + :pgdn => proc { vselect { |v| v - @rev_dir * (max_items - 1) } }, + :alt_b => proc { backword.call; nil }, + :alt_f => proc { cursor += (input[cursor..-1].index(/(\S\s)|(.$)/) || -1) + 1 nil }, @@ -961,7 +996,7 @@ class FZF if y == cursor_y cursor = [0, [input.length, x - 2].min].max elsif x > 1 && y <= max_items - tv = max_items - y - 1 + tv = get(:@yoff) + max_items - y - 1 case event when :click @@ -989,23 +1024,24 @@ class FZF actions[ctrl(:q)] = actions[ctrl(:g)] = actions[ctrl(:c)] = actions[:esc] while true - @cursor_x.set cursor + set(:@xcur, cursor) render { print_input } if key = get_input(actions) upd = actions.fetch(key, actions[:default]).call(key) # Dispatch key event - emit(:key) { [@query.set(input.dup), cursor] } if upd + emit(:key) { [set(:@query, input.dup), cursor] } if upd end end ensure C.close_screen if got - if @selects.empty? + selects = call(:@selects, :dup) + if selects.empty? @stdout.puts got else - @selects.each do |sel, _| + selects.each do |sel, _| @stdout.puts sel end end diff --git a/test/test_fzf.rb b/test/test_fzf.rb index 4c82104..e562f34 100644 --- a/test/test_fzf.rb +++ b/test/test_fzf.rb @@ -27,7 +27,7 @@ class TestFZF < MiniTest::Unit::TestCase assert_equal true, fzf.color assert_equal false, fzf.black assert_equal true, fzf.ansi256 - assert_equal '', fzf.query.get + assert_equal '', fzf.query assert_equal false, fzf.select1 assert_equal false, fzf.exit0 assert_equal nil, fzf.filter @@ -48,7 +48,7 @@ class TestFZF < MiniTest::Unit::TestCase fzf = FZF.new [] assert_equal 10000, fzf.sort assert_equal ' hello world ', - fzf.query.get + fzf.query assert_equal 'goodbye world', fzf.filter assert_equal :fuzzy, fzf.extended @@ -75,7 +75,7 @@ class TestFZF < MiniTest::Unit::TestCase assert_equal false, fzf.black assert_equal false, fzf.mouse assert_equal 0, fzf.rxflag - assert_equal 'hello', fzf.query.get + assert_equal 'hello', fzf.query assert_equal true, fzf.select1 assert_equal true, fzf.exit0 assert_equal 'howdy', fzf.filter @@ -97,7 +97,7 @@ class TestFZF < MiniTest::Unit::TestCase assert_equal true, fzf.mouse assert_equal 1, fzf.rxflag assert_equal 'b', fzf.filter - assert_equal 'hello', fzf.query.get + assert_equal 'hello', fzf.query assert_equal false, fzf.select1 assert_equal false, fzf.exit0 assert_equal nil, fzf.extended @@ -111,7 +111,7 @@ class TestFZF < MiniTest::Unit::TestCase assert_equal false, fzf.color assert_equal false, fzf.ansi256 assert_equal 0, fzf.rxflag - assert_equal 'hello', fzf.query.get + assert_equal 'hello', fzf.query assert_equal 'howdy', fzf.filter assert_equal :fuzzy, fzf.extended assert_equal [2..2], fzf.nth @@ -129,7 +129,7 @@ class TestFZF < MiniTest::Unit::TestCase assert_equal true, fzf.ansi256 assert_equal false, fzf.black assert_equal 1, fzf.rxflag - assert_equal 'world', fzf.query.get + assert_equal 'world', fzf.query assert_equal false, fzf.select1 assert_equal false, fzf.exit0 assert_equal 'world', fzf.filter From 05118cc440979f40a34bd64e8aef072cf2d627ac Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Thu, 26 Jun 2014 15:35:04 +0900 Subject: [PATCH 2/6] Minor corrections - Suppress warning message on Ruby 1.8.5 - Remove unnecessary code --- fzf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fzf b/fzf index 19a83bf..781df68 100644 --- a/fzf +++ b/fzf @@ -72,7 +72,7 @@ class FZF def set name, value = nil sync do instance_variable_set name, - (block_given? ? yield(instance_variable_get name) : value) + (block_given? ? yield(instance_variable_get(name)) : value) end end @@ -172,7 +172,7 @@ class FZF @query ||= '' @matches = [] @count = 0 - @xcur = get(:@query).length + @xcur = @query.length @ycur = 0 @yoff = 0 @dirty = Set.new From 72ec0a34082c3f7b1c87653e2a7b8878bb33ce1d Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Thu, 26 Jun 2014 19:40:29 +0900 Subject: [PATCH 3/6] Add test cases for result scroll --- fzf | 42 +++++++++++++++++++++++++++--------------- test/test_fzf.rb | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 15 deletions(-) diff --git a/fzf b/fzf index 781df68..2c654a6 100644 --- a/fzf +++ b/fzf @@ -677,25 +677,37 @@ class FZF end end + def constrain offset, cursor, count, height + original = [offset, cursor] + diffpos = cursor - offset + + # Constrain cursor + cursor = [0, [cursor, count - 1].min].max + + # Ceil + if cursor > offset + (height - 1) + offset = cursor - (height - 1) + # Floor + elsif offset > cursor + offset = cursor + end + + # Adjustment + if count - offset < height + offset = [0, count - height].max + cursor = [0, [offset + diffpos, count - 1].min].max + end + + [[offset, cursor] != original, offset, cursor] + end + def update_list wipe render do offset, ycur, items = sync { - cnt = @matches.length - pos = @ycur - @yoff - @ycur = [0, [@ycur, cnt - 1].min].max + changed, @yoff, @ycur = + constrain(@yoff, @ycur, @matches.length, max_items) + wipe ||= changed - if @ycur - @yoff >= max_items - @yoff = @ycur - max_items + 1 - wipe = true - elsif @yoff >= @ycur - @yoff = @ycur - wipe = true - end - if cnt - @yoff < max_items - @yoff = [0, cnt - max_items].max - wipe = true - @ycur = [0, [@yoff + pos, cnt - 1].min].max - end [@yoff, @ycur, @matches[@yoff, max_items]] } diff --git a/test/test_fzf.rb b/test/test_fzf.rb index e562f34..a67ebd5 100644 --- a/test/test_fzf.rb +++ b/test/test_fzf.rb @@ -648,5 +648,39 @@ class TestFZF < MiniTest::Unit::TestCase ['1 3 4 2', [[0, 24], [12, 17]]], ], FZF.sort(FZF::ExtendedFuzzyMatcher.new(nil).match(list, '12 34', '', '')) end + + def test_constrain + fzf = FZF.new [] + + # [#**** ] + assert_equal [false, 0, 0], fzf.constrain(0, 0, 5, 100) + + # *****[**#** ... ] => [**#******* ... ] + assert_equal [true, 0, 2], fzf.constrain(5, 7, 10, 100) + + # [**********]**#** => ***[*********#]** + assert_equal [true, 3, 12], fzf.constrain(0, 12, 15, 10) + + # *****[**#** ] => ***[**#****] + assert_equal [true, 3, 5], fzf.constrain(5, 7, 10, 7) + + # *****[**#** ] => ****[**#***] + assert_equal [true, 4, 6], fzf.constrain(5, 7, 10, 6) + + # ***** [#] => ****[#] + assert_equal [true, 4, 4], fzf.constrain(10, 10, 5, 1) + + # [ ] #**** => [#]**** + assert_equal [true, 0, 0], fzf.constrain(-5, 0, 5, 1) + + # [ ] **#** => **[#]** + assert_equal [true, 2, 2], fzf.constrain(-5, 2, 5, 1) + + # [***** #] => [****# ] + assert_equal [true, 0, 4], fzf.constrain(0, 7, 5, 10) + + # **[***** #] => [******# ] + assert_equal [true, 0, 6], fzf.constrain(2, 10, 7, 10) + end end From 56ace10a37547c3072fae97168ae7f78e0e1fbb4 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Fri, 27 Jun 2014 00:04:07 +0900 Subject: [PATCH 4/6] Fix mouse-click on --reverse mode --- fzf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fzf b/fzf index 2c654a6..3e3ceac 100644 --- a/fzf +++ b/fzf @@ -7,7 +7,7 @@ # / __/ / /_/ __/ # /_/ /___/_/ Fuzzy finder for your shell # -# Version: 0.8.6 (Jun 26, 2014) +# Version: 0.8.6 (Jun 27, 2014) # # Author: Junegunn Choi # URL: https://github.com/junegunn/fzf @@ -1005,7 +1005,7 @@ class FZF when :click, :release x, y, shift = val.values_at :x, :y, :shift y = @reverse ? (C.lines - 1 - y) : y - if y == cursor_y + if y == C.lines - 1 cursor = [0, [input.length, x - 2].min].max elsif x > 1 && y <= max_items tv = get(:@yoff) + max_items - y - 1 From b5350b24ff31fb91d7705daf25ba0fe3158af88a Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Fri, 27 Jun 2014 08:28:32 +0900 Subject: [PATCH 5/6] Avoid unnecessary redraw --- fzf | 1 + 1 file changed, 1 insertion(+) diff --git a/fzf b/fzf index 3e3ceac..015c6f8 100644 --- a/fzf +++ b/fzf @@ -1026,6 +1026,7 @@ class FZF actions[ctrl(:i)].call(:sclick) if shift actions[ctrl(diff > 0 ? :j : :k)].call end + nil end } } From ccca34f9f70a90b31ddc4a1daeb0727e955662db Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Fri, 27 Jun 2014 12:35:30 +0900 Subject: [PATCH 6/6] Minor refactoring --- fzf | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/fzf b/fzf index 015c6f8..465858f 100644 --- a/fzf +++ b/fzf @@ -703,12 +703,12 @@ class FZF def update_list wipe render do - offset, ycur, items = sync { + pos, items = sync { changed, @yoff, @ycur = constrain(@yoff, @ycur, @matches.length, max_items) wipe ||= changed - [@yoff, @ycur, @matches[@yoff, max_items]] + [@ycur - @yoff, @matches[@yoff, max_items]] } # Wipe @@ -720,20 +720,18 @@ class FZF end @plcount = items.length - maxc = C.cols - 3 - cline = ycur - offset - cleanse = Set[cline] + dirty = Set[pos] set(:@dirty) do |vs| - cleanse.merge vs + dirty.merge vs Set.new end items.each_with_index do |item, idx| - next unless wipe || cleanse.include?(idx) + next unless wipe || dirty.include?(idx) row = cursor_y(idx + 2) - chosen = idx == cline + chosen = idx == pos selected = @selects.include?([*item][0]) line, offsets = item - tokens = format line, maxc, offsets + tokens = format line, C.cols - 3, offsets print_item row, tokens, chosen, selected end print_info