Add -f (--filter) option (#15)
This commit adds --filter option so that fzf can be used as a simple unix filter instead of being an interactive fuzzy finder.
This commit is contained in:
parent
1155da7e1c
commit
301290663d
@ -55,6 +55,7 @@ usage: fzf [options]
|
|||||||
-m, --multi Enable multi-select
|
-m, --multi Enable multi-select
|
||||||
-x, --extended Extended-search mode
|
-x, --extended Extended-search mode
|
||||||
-q, --query=STR Initial query
|
-q, --query=STR Initial query
|
||||||
|
-f, --filter=STR Filter mode. Do not start interactive finder.
|
||||||
-s, --sort=MAX Maximum number of matched items to sort (default: 1000)
|
-s, --sort=MAX Maximum number of matched items to sort (default: 1000)
|
||||||
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
||||||
-i Case-insensitive match (default: smart-case match)
|
-i Case-insensitive match (default: smart-case match)
|
||||||
|
74
fzf
74
fzf
@ -7,7 +7,7 @@
|
|||||||
# / __/ / /_/ __/
|
# / __/ / /_/ __/
|
||||||
# /_/ /___/_/ Fuzzy finder for your shell
|
# /_/ /___/_/ Fuzzy finder for your shell
|
||||||
#
|
#
|
||||||
# Version: 0.7.2-devel (February 1, 2014)
|
# Version: 0.7.2 (February 2, 2014)
|
||||||
#
|
#
|
||||||
# Author: Junegunn Choi
|
# Author: Junegunn Choi
|
||||||
# URL: https://github.com/junegunn/fzf
|
# URL: https://github.com/junegunn/fzf
|
||||||
@ -50,7 +50,7 @@ end
|
|||||||
|
|
||||||
class FZF
|
class FZF
|
||||||
C = Curses
|
C = Curses
|
||||||
attr_reader :rxflag, :sort, :color, :mouse, :multi, :query, :extended
|
attr_reader :rxflag, :sort, :color, :mouse, :multi, :query, :filter, :extended
|
||||||
|
|
||||||
class AtomicVar
|
class AtomicVar
|
||||||
def initialize value
|
def initialize value
|
||||||
@ -63,10 +63,8 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
def set value = nil
|
def set value = nil
|
||||||
if block_given?
|
@mutex.synchronize do
|
||||||
@mutex.synchronize { @value = yield @value }
|
@value = block_given? ? yield(@value) : value
|
||||||
else
|
|
||||||
@mutex.synchronize { @value = value }
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -82,6 +80,7 @@ class FZF
|
|||||||
@multi = false
|
@multi = false
|
||||||
@extended = false
|
@extended = false
|
||||||
@mouse = true
|
@mouse = true
|
||||||
|
@filter = nil
|
||||||
|
|
||||||
argv =
|
argv =
|
||||||
if opts = ENV['FZF_DEFAULT_OPTS']
|
if opts = ENV['FZF_DEFAULT_OPTS']
|
||||||
@ -109,6 +108,11 @@ class FZF
|
|||||||
@query = AtomicVar.new query.dup
|
@query = AtomicVar.new query.dup
|
||||||
when /^-q(.*)$/, /^--query=(.*)$/
|
when /^-q(.*)$/, /^--query=(.*)$/
|
||||||
@query = AtomicVar.new($1)
|
@query = AtomicVar.new($1)
|
||||||
|
when '-f', '--filter'
|
||||||
|
usage 1, 'query string required' unless query = argv.shift
|
||||||
|
@filter = query
|
||||||
|
when /^-f(.*)$/, /^--filter=(.*)$/
|
||||||
|
@filter = $1
|
||||||
when '-s', '--sort'
|
when '-s', '--sort'
|
||||||
usage 1, 'sort size required' unless sort = argv.shift
|
usage 1, 'sort size required' unless sort = argv.shift
|
||||||
usage 1, 'invalid sort size' unless sort =~ /^[0-9]+$/
|
usage 1, 'invalid sort size' unless sort =~ /^[0-9]+$/
|
||||||
@ -126,27 +130,44 @@ class FZF
|
|||||||
@events = {}
|
@events = {}
|
||||||
@new = []
|
@new = []
|
||||||
@queue = Queue.new
|
@queue = Queue.new
|
||||||
@query ||= AtomicVar.new('')
|
|
||||||
@cursor_x = AtomicVar.new(@query.length)
|
unless @filter
|
||||||
@matches = AtomicVar.new([])
|
@query ||= AtomicVar.new('')
|
||||||
@count = AtomicVar.new(0)
|
@cursor_x = AtomicVar.new(@query.length)
|
||||||
@vcursor = AtomicVar.new(0)
|
@matches = AtomicVar.new([])
|
||||||
@vcursors = AtomicVar.new(Set.new)
|
@count = AtomicVar.new(0)
|
||||||
@spinner = AtomicVar.new('-\|/-\|/'.split(//))
|
@vcursor = AtomicVar.new(0)
|
||||||
@selects = AtomicVar.new({}) # ordered >= 1.9
|
@vcursors = AtomicVar.new(Set.new)
|
||||||
@main = Thread.current
|
@spinner = AtomicVar.new('-\|/-\|/'.split(//))
|
||||||
@stdout = $stdout.clone
|
@selects = AtomicVar.new({}) # ordered >= 1.9
|
||||||
@plcount = 0
|
@main = Thread.current
|
||||||
|
@plcount = 0
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def start
|
def start
|
||||||
$stdout.reopen($stderr)
|
if @filter
|
||||||
|
start_reader(false).join
|
||||||
|
filter_list @new
|
||||||
|
else
|
||||||
|
@stdout = $stdout.clone
|
||||||
|
$stdout.reopen($stderr)
|
||||||
|
|
||||||
init_screen
|
start_reader true
|
||||||
start_reader
|
init_screen
|
||||||
start_renderer
|
start_renderer
|
||||||
start_search
|
start_search
|
||||||
start_loop
|
start_loop
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def filter_list list
|
||||||
|
matcher = (@extended ? ExtendedFuzzyMatcher : FuzzyMatcher).new @rxflag
|
||||||
|
matches = matcher.match(list, @filter, '', '')
|
||||||
|
if @sort && matches.length <= @sort
|
||||||
|
matches = sort_by_rank(matches)
|
||||||
|
end
|
||||||
|
matches.each { |m| puts m.first }
|
||||||
end
|
end
|
||||||
|
|
||||||
def version
|
def version
|
||||||
@ -168,6 +189,7 @@ class FZF
|
|||||||
-m, --multi Enable multi-select
|
-m, --multi Enable multi-select
|
||||||
-x, --extended Extended-search mode
|
-x, --extended Extended-search mode
|
||||||
-q, --query=STR Initial query
|
-q, --query=STR Initial query
|
||||||
|
-f, --filter=STR Filter mode. Do not start interactive finder.
|
||||||
-s, --sort=MAX Maximum number of matched items to sort (default: 1000)
|
-s, --sort=MAX Maximum number of matched items to sort (default: 1000)
|
||||||
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
||||||
-i Case-insensitive match (default: smart-case match)
|
-i Case-insensitive match (default: smart-case match)
|
||||||
@ -535,7 +557,7 @@ class FZF
|
|||||||
C.refresh
|
C.refresh
|
||||||
end
|
end
|
||||||
|
|
||||||
def start_reader
|
def start_reader curses
|
||||||
stream =
|
stream =
|
||||||
if @source.tty?
|
if @source.tty?
|
||||||
if default_command = ENV['FZF_DEFAULT_COMMAND']
|
if default_command = ENV['FZF_DEFAULT_COMMAND']
|
||||||
@ -546,7 +568,7 @@ class FZF
|
|||||||
exit 1
|
exit 1
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
$stdin.reopen IO.open(IO.sysopen('/dev/tty'), 'r')
|
$stdin.reopen IO.open(IO.sysopen('/dev/tty'), 'r') if curses
|
||||||
@source
|
@source
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -555,7 +577,7 @@ class FZF
|
|||||||
emit(:new) { @new << line.chomp }
|
emit(:new) { @new << line.chomp }
|
||||||
end
|
end
|
||||||
emit(:loaded) { true }
|
emit(:loaded) { true }
|
||||||
@spinner.clear
|
@spinner.clear if curses
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -29,11 +29,13 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
fzf = FZF.new []
|
fzf = FZF.new []
|
||||||
assert_equal 20000, fzf.sort
|
assert_equal 20000, fzf.sort
|
||||||
|
|
||||||
ENV['FZF_DEFAULT_OPTS'] = '-x -m -s 10000 -q " hello world " +c --no-mouse'
|
ENV['FZF_DEFAULT_OPTS'] = '-x -m -s 10000 -q " hello world " +c --no-mouse -f "goodbye world"'
|
||||||
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.get
|
||||||
|
assert_equal 'goodbye world',
|
||||||
|
fzf.filter
|
||||||
assert_equal true, fzf.extended
|
assert_equal true, fzf.extended
|
||||||
assert_equal true, fzf.multi
|
assert_equal true, fzf.multi
|
||||||
assert_equal false, fzf.color
|
assert_equal false, fzf.color
|
||||||
@ -42,42 +44,48 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
|
|
||||||
def test_option_parser
|
def test_option_parser
|
||||||
# Long opts
|
# Long opts
|
||||||
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello --extended --no-mouse]
|
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello
|
||||||
|
--filter=howdy --extended --no-mouse]
|
||||||
assert_equal 2000, fzf.sort
|
assert_equal 2000, fzf.sort
|
||||||
assert_equal true, fzf.multi
|
assert_equal true, fzf.multi
|
||||||
assert_equal false, fzf.color
|
assert_equal false, fzf.color
|
||||||
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.get
|
||||||
|
assert_equal 'howdy', fzf.filter
|
||||||
assert_equal true, fzf.extended
|
assert_equal true, fzf.extended
|
||||||
|
|
||||||
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello
|
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello
|
||||||
|
--filter a --filter b
|
||||||
--no-sort -i --color --no-multi]
|
--no-sort -i --color --no-multi]
|
||||||
assert_equal nil, fzf.sort
|
assert_equal nil, fzf.sort
|
||||||
assert_equal false, fzf.multi
|
assert_equal false, fzf.multi
|
||||||
assert_equal true, fzf.color
|
assert_equal true, fzf.color
|
||||||
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 'hello', fzf.query.get
|
assert_equal 'hello', fzf.query.get
|
||||||
assert_equal false, fzf.extended
|
assert_equal false, fzf.extended
|
||||||
|
|
||||||
# Short opts
|
# Short opts
|
||||||
fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x]
|
fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x -fhowdy]
|
||||||
assert_equal 2000, fzf.sort
|
assert_equal 2000, fzf.sort
|
||||||
assert_equal true, fzf.multi
|
assert_equal true, fzf.multi
|
||||||
assert_equal false, fzf.color
|
assert_equal false, fzf.color
|
||||||
assert_equal 0, fzf.rxflag
|
assert_equal 0, fzf.rxflag
|
||||||
assert_equal 'hello', fzf.query.get
|
assert_equal 'hello', fzf.query.get
|
||||||
|
assert_equal 'howdy', fzf.filter
|
||||||
assert_equal true, fzf.extended
|
assert_equal true, fzf.extended
|
||||||
|
|
||||||
# Left-to-right
|
# Left-to-right
|
||||||
fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x
|
fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x -fgoodbye
|
||||||
-s 3000 -c +m -i -q world +x]
|
-s 3000 -c +m -i -q world +x -fworld]
|
||||||
assert_equal 3000, fzf.sort
|
assert_equal 3000, fzf.sort
|
||||||
assert_equal false, fzf.multi
|
assert_equal false, fzf.multi
|
||||||
assert_equal true, fzf.color
|
assert_equal true, fzf.color
|
||||||
assert_equal 1, fzf.rxflag
|
assert_equal 1, fzf.rxflag
|
||||||
assert_equal 'world', fzf.query.get
|
assert_equal 'world', fzf.query.get
|
||||||
|
assert_equal 'world', fzf.filter
|
||||||
assert_equal false, fzf.extended
|
assert_equal false, fzf.extended
|
||||||
|
|
||||||
fzf = FZF.new %w[--query hello +s -s 2000 --query=world]
|
fzf = FZF.new %w[--query hello +s -s 2000 --query=world]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user