Make scrollable (#68)

This commit is contained in:
Junegunn Choi 2014-06-26 12:50:52 +09:00
parent 6e69339f6b
commit e392da20e8
2 changed files with 122 additions and 86 deletions

156
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 26, 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
@mutex = Mutex.new
end 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
@ -156,22 +159,26 @@ class FZF
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 = get(:@query).length
@vcursors = AtomicVar.new(Set.new) @ycur = 0
@spinner = AtomicVar.new('-\|/-\|/'.split(//)) @yoff = 0
@selects = AtomicVar.new({}) # ordered >= 1.9 @dirty = Set.new
@spinner = '-\|/-\|/'.split(//)
@selects = {} # ordered >= 1.9
@main = Thread.current @main = Thread.current
@plcount = 0 @plcount = 0
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
C.addstr "#{prefix}#{@matches.length}/#{@count}"
if (selected = @selects.length) > 0 if (selected = @selects.length) > 0
C.addstr " (#{selected})" 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,32 @@ 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 end
def update_list wipe def update_list wipe
render do 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 # Wipe
if items.length < @plcount if items.length < @plcount
@ -679,16 +709,16 @@ class FZF
@plcount = items.length @plcount = items.length
maxc = C.cols - 3 maxc = C.cols - 3
vcursor = @vcursor.set { |v| [0, [v, items.length - 1].min].max } cline = ycur - offset
cleanse = Set[vcursor] cleanse = Set[cline]
@vcursors.set { |vs| set(:@dirty) do |vs|
cleanse.merge 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 || cleanse.include?(idx)
row = cursor_y(idx + 2) row = cursor_y(idx + 2)
chosen = idx == vcursor chosen = idx == cline
selected = @selects.include?([*item][0]) selected = @selects.include?([*item][0])
line, offsets = item line, offsets = item
tokens = format line, maxc, offsets tokens = format line, maxc, offsets
@ -720,7 +750,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 +918,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 +945,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,24 +957,26 @@ 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
sync do
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 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
@ -961,7 +996,7 @@ class FZF
if y == cursor_y if y == cursor_y
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
@ -989,23 +1024,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