mirror of
https://github.com/Llewellynvdm/fzf.git
synced 2024-11-29 16:23:57 +00:00
Add --print-query option (#70)
This commit is contained in:
parent
502973ff75
commit
4d72bd098a
13
fzf
13
fzf
@ -51,7 +51,8 @@ end
|
||||
class FZF
|
||||
C = Curses
|
||||
attr_reader :rxflag, :sort, :nth, :color, :black, :ansi256, :reverse, :prompt,
|
||||
:mouse, :multi, :query, :select1, :exit0, :filter, :extended
|
||||
:mouse, :multi, :query, :select1, :exit0, :filter, :extended,
|
||||
:print_query
|
||||
|
||||
def sync
|
||||
@shr_mtx.synchronize { yield }
|
||||
@ -93,6 +94,7 @@ class FZF
|
||||
@reverse = false
|
||||
@prompt = '> '
|
||||
@shr_mtx = Mutex.new
|
||||
@print_query = false
|
||||
|
||||
argv =
|
||||
if opts = ENV['FZF_DEFAULT_OPTS']
|
||||
@ -157,6 +159,8 @@ class FZF
|
||||
@prompt = prompt
|
||||
when /^--prompt=(.*)$/
|
||||
@prompt = $1
|
||||
when '--print-query' then @print_query = true
|
||||
when '--no-print-query' then @print_query = false
|
||||
when '-e', '--extended-exact' then @extended = :exact
|
||||
when '+e', '--no-extended-exact' then @extended = nil
|
||||
else
|
||||
@ -226,9 +230,11 @@ class FZF
|
||||
len = empty ? get(:@count) : matches.length
|
||||
if loaded
|
||||
if @select1 && len == 1
|
||||
puts @query if @print_query
|
||||
puts empty ? matches.first : matches.first.first
|
||||
exit 0
|
||||
elsif @exit0 && len == 0
|
||||
puts @query if @print_query
|
||||
exit 0
|
||||
end
|
||||
end
|
||||
@ -249,6 +255,7 @@ class FZF
|
||||
end
|
||||
|
||||
def filter_list list
|
||||
puts @filter if @print_query
|
||||
matches = matcher.match(list, @filter, '', '')
|
||||
if @sort && matches.length <= @sort
|
||||
matches = FZF.sort(matches)
|
||||
@ -327,6 +334,7 @@ class FZF
|
||||
-1, --select-1 Automatically select the only match
|
||||
-0, --exit-0 Exit immediately when there's no match
|
||||
-f, --filter=STR Filter mode. Do not start interactive finder.
|
||||
--print-query Print query as the first line
|
||||
|
||||
Environment variables
|
||||
FZF_DEFAULT_COMMAND Default command to use when input is tty
|
||||
@ -1054,8 +1062,9 @@ class FZF
|
||||
end
|
||||
ensure
|
||||
C.close_screen
|
||||
q, selects = geta(:@query, :@selects)
|
||||
@stdout.puts q if @print_query
|
||||
if got
|
||||
selects = call(:@selects, :dup)
|
||||
if selects.empty?
|
||||
@stdout.puts got
|
||||
else
|
||||
|
@ -34,6 +34,7 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
assert_equal nil, fzf.extended
|
||||
assert_equal false, fzf.reverse
|
||||
assert_equal '> ', fzf.prompt
|
||||
assert_equal false, fzf.print_query
|
||||
end
|
||||
|
||||
def test_environment_variables
|
||||
@ -44,8 +45,8 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
assert_equal nil, fzf.nth
|
||||
|
||||
ENV['FZF_DEFAULT_OPTS'] =
|
||||
'-x -m -s 10000 -q " hello world " +c +2 --select-1 -0 ' +
|
||||
'--no-mouse -f "goodbye world" --black --nth=3,-1,2 --reverse'
|
||||
'-x -m -s 10000 -q " hello world " +c +2 --select-1 -0 ' <<
|
||||
'--no-mouse -f "goodbye world" --black --nth=3,-1,2 --reverse --print-query'
|
||||
fzf = FZF.new []
|
||||
assert_equal 10000, fzf.sort
|
||||
assert_equal ' hello world ',
|
||||
@ -61,6 +62,7 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
assert_equal true, fzf.select1
|
||||
assert_equal true, fzf.exit0
|
||||
assert_equal true, fzf.reverse
|
||||
assert_equal true, fzf.print_query
|
||||
assert_equal [2..2, -1..-1, 1..1], fzf.nth
|
||||
end
|
||||
|
||||
@ -68,7 +70,8 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
# Long opts
|
||||
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello --select-1
|
||||
--exit-0 --filter=howdy --extended-exact
|
||||
--no-mouse --no-256 --nth=1 --reverse --prompt (hi)]
|
||||
--no-mouse --no-256 --nth=1 --reverse --prompt (hi)
|
||||
--print-query]
|
||||
assert_equal 2000, fzf.sort
|
||||
assert_equal true, fzf.multi
|
||||
assert_equal false, fzf.color
|
||||
@ -84,13 +87,15 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
assert_equal [0..0], fzf.nth
|
||||
assert_equal true, fzf.reverse
|
||||
assert_equal '(hi)', fzf.prompt
|
||||
assert_equal true, fzf.print_query
|
||||
|
||||
# Long opts (left-to-right)
|
||||
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query=hello
|
||||
--filter a --filter b --no-256 --black --nth -1 --nth -2
|
||||
--select-1 --exit-0 --no-select-1 --no-exit-0
|
||||
--no-sort -i --color --no-multi --256
|
||||
--reverse --no-reverse --prompt (hi) --prompt=(HI)]
|
||||
--reverse --no-reverse --prompt (hi) --prompt=(HI)
|
||||
--print-query --no-print-query]
|
||||
assert_equal nil, fzf.sort
|
||||
assert_equal false, fzf.multi
|
||||
assert_equal true, fzf.color
|
||||
@ -106,6 +111,7 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
assert_equal [-2..-2], fzf.nth
|
||||
assert_equal false, fzf.reverse
|
||||
assert_equal '(HI)', fzf.prompt
|
||||
assert_equal false, fzf.print_query
|
||||
|
||||
# Short opts
|
||||
fzf = FZF.new %w[-s2000 +c -m +i -qhello -x -fhowdy +2 -n3 -1 -0]
|
||||
@ -568,36 +574,43 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
def test_select_1
|
||||
stream = stream_for "Hello\nWorld"
|
||||
def assert_fzf_output opts, given, expected
|
||||
stream = stream_for given
|
||||
output = StringIO.new
|
||||
|
||||
begin
|
||||
$stdout = output
|
||||
FZF.new(%w[--query=ol --select-1], stream).start
|
||||
FZF.new(opts, stream).start
|
||||
rescue SystemExit => e
|
||||
assert_equal 0, e.status
|
||||
assert_equal 'World', output.string.chomp
|
||||
assert_equal expected, output.string.chomp
|
||||
ensure
|
||||
$stdout = STDOUT
|
||||
end
|
||||
end
|
||||
|
||||
def test_select_1_without_query
|
||||
stream = stream_for "Hello World"
|
||||
output = StringIO.new
|
||||
|
||||
begin
|
||||
$stdout = output
|
||||
FZF.new(%w[--select-1], stream).start
|
||||
rescue SystemExit => e
|
||||
assert_equal 0, e.status
|
||||
assert_equal 'Hello World', output.string.chomp
|
||||
ensure
|
||||
$stdout = STDOUT
|
||||
def test_filter
|
||||
{
|
||||
%w[--filter=ol] => 'World',
|
||||
%w[--filter=ol --print-query] => "ol\nWorld",
|
||||
}.each do |opts, expected|
|
||||
assert_fzf_output opts, "Hello\nWorld", expected
|
||||
end
|
||||
end
|
||||
|
||||
def test_select_1
|
||||
{
|
||||
%w[--query=ol --select-1] => 'World',
|
||||
%w[--query=ol --select-1 --print-query] => "ol\nWorld",
|
||||
}.each do |opts, expected|
|
||||
assert_fzf_output opts, "Hello\nWorld", expected
|
||||
end
|
||||
end
|
||||
|
||||
def test_select_1_without_query
|
||||
assert_fzf_output %w[--select-1], 'Hello World', 'Hello World'
|
||||
end
|
||||
|
||||
def test_select_1_ambiguity
|
||||
stream = stream_for "Hello\nWorld"
|
||||
begin
|
||||
@ -612,33 +625,16 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
end
|
||||
|
||||
def test_exit_0
|
||||
stream = stream_for "Hello\nWorld"
|
||||
output = StringIO.new
|
||||
|
||||
begin
|
||||
$stdout = output
|
||||
FZF.new(%w[--query=zz --exit-0], stream).start
|
||||
rescue SystemExit => e
|
||||
assert_equal 0, e.status
|
||||
assert_equal '', output.string
|
||||
ensure
|
||||
$stdout = STDOUT
|
||||
{
|
||||
%w[--query=zz --exit-0] => '',
|
||||
%w[--query=zz --exit-0 --print-query] => 'zz',
|
||||
}.each do |opts, expected|
|
||||
assert_fzf_output opts, "Hello\nWorld", expected
|
||||
end
|
||||
end
|
||||
|
||||
def test_exit_0_without_query
|
||||
stream = stream_for ""
|
||||
output = StringIO.new
|
||||
|
||||
begin
|
||||
$stdout = output
|
||||
FZF.new(%w[--exit-0], stream).start
|
||||
rescue SystemExit => e
|
||||
assert_equal 0, e.status
|
||||
assert_equal '', output.string
|
||||
ensure
|
||||
$stdout = STDOUT
|
||||
end
|
||||
assert_fzf_output %w[--exit-0], '', ''
|
||||
end
|
||||
|
||||
def test_ranking_overlap_match_regions
|
||||
|
Loading…
Reference in New Issue
Block a user