Merge pull request #69 from junegunn/scrollable

Make the list scrollable
This commit is contained in:
Junegunn Choi 2014-06-27 13:32:04 +09:00
commit b824928b0b
2 changed files with 172 additions and 91 deletions

217
fzf Executable file → Normal file
View File

@ -7,7 +7,7 @@
# / __/ / /_/ __/ # / __/ / /_/ __/
# /_/ /___/_/ Fuzzy finder for your shell # /_/ /___/_/ Fuzzy finder for your shell
# #
# Version: 0.8.5 (Jun 15, 2014) # Version: 0.8.6 (Jun 27, 2014)
# #
# Author: Junegunn Choi # Author: Junegunn Choi
# URL: https://github.com/junegunn/fzf # URL: https://github.com/junegunn/fzf
@ -53,24 +53,26 @@ class FZF
attr_reader :rxflag, :sort, :nth, :color, :black, :ansi256, :reverse, attr_reader :rxflag, :sort, :nth, :color, :black, :ansi256, :reverse,
:mouse, :multi, :query, :select1, :exit0, :filter, :extended :mouse, :multi, :query, :select1, :exit0, :filter, :extended
class AtomicVar def sync
def initialize value @shr_mtx.synchronize { yield }
@value = value end
@mutex = Mutex.new
end
def get def get name
@mutex.synchronize { @value } sync { instance_variable_get name }
end end
def set value = nil def geta(*names)
@mutex.synchronize do sync { names.map { |name| instance_variable_get name } }
@value = block_given? ? yield(@value) : value end
end
end
def method_missing sym, *args, &blk def call(name, method, *args)
@mutex.synchronize { @value.send(sym, *args, &blk) } 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
end end
@ -89,6 +91,7 @@ class FZF
@nth = nil @nth = nil
@delim = nil @delim = nil
@reverse = false @reverse = false
@shr_mtx = Mutex.new
argv = argv =
if opts = ENV['FZF_DEFAULT_OPTS'] if opts = ENV['FZF_DEFAULT_OPTS']
@ -124,9 +127,9 @@ class FZF
when '+0', '--no-exit-0' then @exit0 = false when '+0', '--no-exit-0' then @exit0 = false
when '-q', '--query' when '-q', '--query'
usage 1, 'query string required' unless query = argv.shift usage 1, 'query string required' unless query = argv.shift
@query = AtomicVar.new query.dup @query = query.dup
when /^-q(.*)$/, /^--query=(.*)$/ when /^-q(.*)$/, /^--query=(.*)$/
@query = AtomicVar.new($1) @query = $1
when '-f', '--filter' when '-f', '--filter'
usage 1, 'query string required' unless query = argv.shift usage 1, 'query string required' unless query = argv.shift
@filter = query @filter = query
@ -155,25 +158,29 @@ class FZF
end end
end end
@source = source.clone @source = source.clone
@mtx = Mutex.new @evt_mtx = Mutex.new
@cv = ConditionVariable.new @cv = ConditionVariable.new
@events = {} @events = {}
@new = [] @new = []
@queue = Queue.new @queue = Queue.new
@pending = nil @pending = nil
@rev_dir = @reverse ? -1 : 1
unless @filter unless @filter
@query ||= AtomicVar.new('') # Shared variables: needs protection
@cursor_x = AtomicVar.new(@query.length) @query ||= ''
@matches = AtomicVar.new([]) @matches = []
@count = AtomicVar.new(0) @count = 0
@vcursor = AtomicVar.new(0) @xcur = @query.length
@vcursors = AtomicVar.new(Set.new) @ycur = 0
@spinner = AtomicVar.new('-\|/-\|/'.split(//)) @yoff = 0
@selects = AtomicVar.new({}) # ordered >= 1.9 @dirty = Set.new
@main = Thread.current @spinner = '-\|/-\|/'.split(//)
@plcount = 0 @selects = {} # ordered >= 1.9
@main = Thread.current
@plcount = 0
end end
end end
@ -206,10 +213,11 @@ class FZF
filter_list @new filter_list @new
else else
start_reader 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 if @select1 || @exit0
start_search do |loaded, matches| start_search do |loaded, matches|
len = empty ? @count.get : matches.length len = empty ? get(:@count) : matches.length
if loaded if loaded
if @select1 && len == 1 if @select1 && len == 1
puts empty ? matches.first : matches.first.first puts empty ? matches.first : matches.first.first
@ -320,7 +328,7 @@ class FZF
end end
def emit event def emit event
@mtx.synchronize do @evt_mtx.synchronize do
@events[event] = yield @events[event] = yield
@cv.broadcast @cv.broadcast
end end
@ -346,31 +354,35 @@ class FZF
C.clrtoeol C.clrtoeol
cprint '> ', color(:prompt, true) cprint '> ', color(:prompt, true)
C.attron(C::A_BOLD) do C.attron(C::A_BOLD) do
C.addstr @query.get C.addstr get(:@query)
end end
end end
def print_info msg = nil def print_info msg = nil
C.setpos cursor_y(1), 0 C.setpos cursor_y(1), 0
C.clrtoeol C.clrtoeol
prefix = prefix =
if spinner = @spinner.first if spin_char = call(:@spinner, :first)
cprint spinner, color(:spinner, true) cprint spin_char, color(:spinner, true)
' ' ' '
else else
' ' ' '
end end
C.attron color(:info, false) do C.attron color(:info, false) do
C.addstr "#{prefix}#{@matches.length}/#{@count.get}" sync do
if (selected = @selects.length) > 0 C.addstr "#{prefix}#{@matches.length}/#{@count}"
C.addstr " (#{selected})" if (selected = @selects.length) > 0
C.addstr " (#{selected})"
end
end end
C.addstr msg if msg C.addstr msg if msg
end end
end end
def refresh 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 C.refresh
end end
@ -580,12 +592,12 @@ class FZF
begin begin
while true while true
@mtx.synchronize do @evt_mtx.synchronize do
while true while true
events.merge! @events events.merge! @events
if @events.empty? # No new events if @events.empty? # No new events
@cv.wait @mtx @cv.wait @evt_mtx
next next
end end
@events.clear @events.clear
@ -594,8 +606,8 @@ class FZF
if events[:new] if events[:new]
lists << @new lists << @new
@count.set { |c| c + @new.length } set(:@count) { |c| c + @new.length }
@spinner.set { |spinner| set(:@spinner) { |spinner|
if e = spinner.shift if e = spinner.shift
spinner.push e spinner.push e
end; spinner end; spinner
@ -619,10 +631,10 @@ class FZF
cnt = 0 cnt = 0
lists.each do |list| lists.each do |list|
cnt += list.length cnt += list.length
skip = @mtx.synchronize { @events[:key] } skip = @evt_mtx.synchronize { @events[:key] }
break if skip 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}%)" } render { print_info " (#{progress}%)" }
end end
@ -641,7 +653,7 @@ class FZF
end end
# Atomic update # Atomic update
@matches.set matches set(:@matches, matches)
end#new_search end#new_search
callback = nil if callback && callback = nil if callback &&
@ -660,14 +672,44 @@ class FZF
end end
def pick def pick
items = @matches[0, max_items] sync do
curr = [0, [@vcursor.get, items.length - 1].min].max [*@matches.fetch(@ycur, [])][0]
[*items.fetch(curr, [])][0] 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 end
def update_list wipe def update_list wipe
render do render do
items = @matches[0, max_items] pos, items = sync {
changed, @yoff, @ycur =
constrain(@yoff, @ycur, @matches.length, max_items)
wipe ||= changed
[@ycur - @yoff, @matches[@yoff, max_items]]
}
# Wipe # Wipe
if items.length < @plcount if items.length < @plcount
@ -678,20 +720,18 @@ class FZF
end end
@plcount = items.length @plcount = items.length
maxc = C.cols - 3 dirty = Set[pos]
vcursor = @vcursor.set { |v| [0, [v, items.length - 1].min].max } set(:@dirty) do |vs|
cleanse = Set[vcursor] dirty.merge vs
@vcursors.set { |vs|
cleanse.merge vs
Set.new Set.new
} end
items.each_with_index do |item, idx| items.each_with_index do |item, idx|
next unless wipe || cleanse.include?(idx) next unless wipe || dirty.include?(idx)
row = cursor_y(idx + 2) row = cursor_y(idx + 2)
chosen = idx == vcursor chosen = idx == pos
selected = @selects.include?([*item][0]) selected = @selects.include?([*item][0])
line, offsets = item line, offsets = item
tokens = format line, maxc, offsets tokens = format line, C.cols - 3, offsets
print_item row, tokens, chosen, selected print_item row, tokens, chosen, selected
end end
print_info print_info
@ -720,7 +760,10 @@ class FZF
end end
def vselect &prc def vselect &prc
@vcursor.set { |v| @vcursors << v; prc.call v } sync do
@dirty << @ycur - @yoff
@ycur = prc.call @ycur
end
update_list false update_list false
end end
@ -885,7 +928,7 @@ class FZF
def start_loop def start_loop
got = nil got = nil
begin begin
input = @query.get.dup input = call(:@query, :dup)
cursor = input.length cursor = input.length
yanked = '' yanked = ''
mouse_event = MouseEvent.new mouse_event = MouseEvent.new
@ -912,8 +955,8 @@ class FZF
}, },
ctrl(:a) => proc { cursor = 0; nil }, ctrl(:a) => proc { cursor = 0; nil },
ctrl(:e) => proc { cursor = input.length; nil }, ctrl(:e) => proc { cursor = input.length; nil },
ctrl(:j) => proc { vselect { |v| v - (@reverse ? -1 : 1) } }, ctrl(:j) => proc { vselect { |v| v - @rev_dir } },
ctrl(:k) => proc { vselect { |v| v + (@reverse ? -1 : 1) } }, ctrl(:k) => proc { vselect { |v| v + @rev_dir } },
ctrl(:w) => proc { ctrl(:w) => proc {
pcursor = cursor pcursor = cursor
backword.call backword.call
@ -924,26 +967,28 @@ class FZF
ctrl(:h) => proc { input[cursor -= 1] = '' if cursor > 0 }, ctrl(:h) => proc { input[cursor -= 1] = '' if cursor > 0 },
ctrl(:i) => proc { |o| ctrl(:i) => proc { |o|
if @multi && sel = pick if @multi && sel = pick
if @selects.has_key? sel sync do
@selects.delete sel if @selects.has_key? sel
else @selects.delete sel
@selects[sel] = 1 else
@selects[sel] = 1
end
end end
vselect { |v| v + case o vselect { |v| v + case o
when :stab then 1 when :stab then 1
when :sclick then 0 when :sclick then 0
else -1 else -1
end * (@reverse ? -1 : 1) } end * @rev_dir }
end end
}, },
ctrl(:b) => proc { cursor = [0, cursor - 1].max; nil }, ctrl(:b) => proc { cursor = [0, cursor - 1].max; nil },
ctrl(:f) => proc { cursor = [input.length, cursor + 1].min; nil }, ctrl(:f) => proc { cursor = [input.length, cursor + 1].min; nil },
ctrl(:l) => proc { render { C.clear; C.refresh }; update_list true }, ctrl(:l) => proc { render { C.clear; C.refresh }; update_list true },
:del => proc { input[cursor] = '' if input.length > cursor }, :del => proc { input[cursor] = '' if input.length > cursor },
:pgup => proc { vselect { |_| max_items } }, :pgup => proc { vselect { |v| v + @rev_dir * (max_items - 1) } },
:pgdn => proc { vselect { |_| 0 } }, :pgdn => proc { vselect { |v| v - @rev_dir * (max_items - 1) } },
:alt_b => proc { backword.call; nil }, :alt_b => proc { backword.call; nil },
:alt_f => proc { :alt_f => proc {
cursor += (input[cursor..-1].index(/(\S\s)|(.$)/) || -1) + 1 cursor += (input[cursor..-1].index(/(\S\s)|(.$)/) || -1) + 1
nil nil
}, },
@ -958,10 +1003,10 @@ class FZF
when :click, :release when :click, :release
x, y, shift = val.values_at :x, :y, :shift x, y, shift = val.values_at :x, :y, :shift
y = @reverse ? (C.lines - 1 - y) : y y = @reverse ? (C.lines - 1 - y) : y
if y == cursor_y if y == C.lines - 1
cursor = [0, [input.length, x - 2].min].max cursor = [0, [input.length, x - 2].min].max
elsif x > 1 && y <= max_items elsif x > 1 && y <= max_items
tv = max_items - y - 1 tv = get(:@yoff) + max_items - y - 1
case event case event
when :click when :click
@ -979,6 +1024,7 @@ class FZF
actions[ctrl(:i)].call(:sclick) if shift actions[ctrl(:i)].call(:sclick) if shift
actions[ctrl(diff > 0 ? :j : :k)].call actions[ctrl(diff > 0 ? :j : :k)].call
end end
nil
end end
} }
} }
@ -989,23 +1035,24 @@ class FZF
actions[ctrl(:q)] = actions[ctrl(:g)] = actions[ctrl(:c)] = actions[:esc] actions[ctrl(:q)] = actions[ctrl(:g)] = actions[ctrl(:c)] = actions[:esc]
while true while true
@cursor_x.set cursor set(:@xcur, cursor)
render { print_input } render { print_input }
if key = get_input(actions) if key = get_input(actions)
upd = actions.fetch(key, actions[:default]).call(key) upd = actions.fetch(key, actions[:default]).call(key)
# Dispatch key event # Dispatch key event
emit(:key) { [@query.set(input.dup), cursor] } if upd emit(:key) { [set(:@query, input.dup), cursor] } if upd
end end
end end
ensure ensure
C.close_screen C.close_screen
if got if got
if @selects.empty? selects = call(:@selects, :dup)
if selects.empty?
@stdout.puts got @stdout.puts got
else else
@selects.each do |sel, _| selects.each do |sel, _|
@stdout.puts sel @stdout.puts sel
end end
end end

View File

@ -27,7 +27,7 @@ class TestFZF < MiniTest::Unit::TestCase
assert_equal true, fzf.color assert_equal true, fzf.color
assert_equal false, fzf.black assert_equal false, fzf.black
assert_equal true, fzf.ansi256 assert_equal true, fzf.ansi256
assert_equal '', fzf.query.get assert_equal '', fzf.query
assert_equal false, fzf.select1 assert_equal false, fzf.select1
assert_equal false, fzf.exit0 assert_equal false, fzf.exit0
assert_equal nil, fzf.filter assert_equal nil, fzf.filter
@ -48,7 +48,7 @@ class TestFZF < MiniTest::Unit::TestCase
fzf = FZF.new [] fzf = FZF.new []
assert_equal 10000, fzf.sort assert_equal 10000, fzf.sort
assert_equal ' hello world ', assert_equal ' hello world ',
fzf.query.get fzf.query
assert_equal 'goodbye world', assert_equal 'goodbye world',
fzf.filter fzf.filter
assert_equal :fuzzy, fzf.extended assert_equal :fuzzy, fzf.extended
@ -75,7 +75,7 @@ class TestFZF < MiniTest::Unit::TestCase
assert_equal false, fzf.black assert_equal false, fzf.black
assert_equal false, fzf.mouse assert_equal false, fzf.mouse
assert_equal 0, fzf.rxflag 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.select1
assert_equal true, fzf.exit0 assert_equal true, fzf.exit0
assert_equal 'howdy', fzf.filter assert_equal 'howdy', fzf.filter
@ -97,7 +97,7 @@ class TestFZF < MiniTest::Unit::TestCase
assert_equal true, fzf.mouse assert_equal true, fzf.mouse
assert_equal 1, fzf.rxflag assert_equal 1, fzf.rxflag
assert_equal 'b', fzf.filter 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.select1
assert_equal false, fzf.exit0 assert_equal false, fzf.exit0
assert_equal nil, fzf.extended assert_equal nil, fzf.extended
@ -111,7 +111,7 @@ class TestFZF < MiniTest::Unit::TestCase
assert_equal false, fzf.color assert_equal false, fzf.color
assert_equal false, fzf.ansi256 assert_equal false, fzf.ansi256
assert_equal 0, fzf.rxflag assert_equal 0, fzf.rxflag
assert_equal 'hello', fzf.query.get assert_equal 'hello', fzf.query
assert_equal 'howdy', fzf.filter assert_equal 'howdy', fzf.filter
assert_equal :fuzzy, fzf.extended assert_equal :fuzzy, fzf.extended
assert_equal [2..2], fzf.nth assert_equal [2..2], fzf.nth
@ -129,7 +129,7 @@ class TestFZF < MiniTest::Unit::TestCase
assert_equal true, fzf.ansi256 assert_equal true, fzf.ansi256
assert_equal false, fzf.black assert_equal false, fzf.black
assert_equal 1, fzf.rxflag 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.select1
assert_equal false, fzf.exit0 assert_equal false, fzf.exit0
assert_equal 'world', fzf.filter assert_equal 'world', fzf.filter
@ -648,5 +648,39 @@ class TestFZF < MiniTest::Unit::TestCase
['1 3 4 2', [[0, 24], [12, 17]]], ['1 3 4 2', [[0, 24], [12, 17]]],
], FZF.sort(FZF::ExtendedFuzzyMatcher.new(nil).match(list, '12 34', '', '')) ], FZF.sort(FZF::ExtendedFuzzyMatcher.new(nil).match(list, '12 34', '', ''))
end 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 end