Skip to content
Myr

Merge pull request #71 from junegunn/issue-70

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Add options: --prompt and --print-query

authored by Junegunn Choi · committed by Junegunn Choi · parents 3e91c18, 91d986b · Browse files · 3 files+67 added−50 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+67 added−50 removed

  • tests:+41 added−42 removed
  • other:+26 added−8 removed

3 files+67 added−50 removed

modified:test/test_fzf.rb+41 added-42 removed
31 unmodified lines
32 assert_equal false, fzf.exit033 assert_equal nil, fzf.filter34 assert_equal nil, fzf.extended35 assert_equal false, fzf.reverse36 assert_equal '> ', fzf.prompt37 assert_equal false, fzf.print_query38 end3940 def test_environment_variables41 # Deprecated
2 unmodified lines
44 assert_equal 20000, fzf.sort45 assert_equal nil, fzf.nth4647 ENV['FZF_DEFAULT_OPTS'] =46 '-x -m -s 10000 -q " hello world " +c +2 --select-1 -0 ' +47 '--no-mouse -f "goodbye world" --black --nth=3,-1,2 --reverse'48 '-x -m -s 10000 -q " hello world " +c +2 --select-1 -0 ' <<49 '--no-mouse -f "goodbye world" --black --nth=3,-1,2 --reverse --print-query'50 fzf = FZF.new []51 assert_equal 10000, fzf.sort52 assert_equal ' hello world ',53 fzf.query
7 unmodified lines
61 assert_equal false, fzf.mouse62 assert_equal true, fzf.select163 assert_equal true, fzf.exit064 assert_equal true, fzf.reverse65 assert_equal true, fzf.print_query66 assert_equal [2..2, -1..-1, 1..1], fzf.nth67 end6869 def test_option_parser70 # Long opts71 fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello --select-172 --exit-0 --filter=howdy --extended-exact70 --no-mouse --no-256 --nth=1 --reverse]73 --no-mouse --no-256 --nth=1 --reverse --prompt (hi)74 --print-query]75 assert_equal 2000, fzf.sort76 assert_equal true, fzf.multi77 assert_equal false, fzf.color78 assert_equal false, fzf.ansi256
6 unmodified lines
85 assert_equal 'howdy', fzf.filter86 assert_equal :exact, fzf.extended87 assert_equal [0..0], fzf.nth88 assert_equal true, fzf.reverse89 assert_equal '(hi)', fzf.prompt90 assert_equal true, fzf.print_query9192 # Long opts (left-to-right)93 fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query=hello94 --filter a --filter b --no-256 --black --nth -1 --nth -295 --select-1 --exit-0 --no-select-1 --no-exit-096 --no-sort -i --color --no-multi --25691 --reverse --no-reverse]97 --reverse --no-reverse --prompt (hi) --prompt=(HI)98 --print-query --no-print-query]99 assert_equal nil, fzf.sort100 assert_equal false, fzf.multi101 assert_equal true, fzf.color102 assert_equal true, fzf.ansi256
6 unmodified lines
109 assert_equal false, fzf.exit0110 assert_equal nil, fzf.extended111 assert_equal [-2..-2], fzf.nth112 assert_equal false, fzf.reverse113 assert_equal '(HI)', fzf.prompt114 assert_equal false, fzf.print_query115116 # Short opts117 fzf = FZF.new %w[-s2000 +c -m +i -qhello -x -fhowdy +2 -n3 -1 -0]118 assert_equal 2000, fzf.sort
454 unmodified lines
573 end574 end575 end576568 def test_select_1569 stream = stream_for "Hello\nWorld"577 def assert_fzf_output opts, given, expected578 stream = stream_for given579 output = StringIO.new580581 begin582 $stdout = output574 FZF.new(%w[--query=ol --select-1], stream).start583 FZF.new(opts, stream).start584 rescue SystemExit => e585 assert_equal 0, e.status577 assert_equal 'World', output.string.chomp586 assert_equal expected, output.string.chomp587 ensure588 $stdout = STDOUT589 end590 end591583 def test_select_1_without_query584 stream = stream_for "Hello World"585 output = StringIO.new592 def test_filter593 {594 %w[--filter=ol] => 'World',595 %w[--filter=ol --print-query] => "ol\nWorld",596 }.each do |opts, expected|597 assert_fzf_output opts, "Hello\nWorld", expected598 end599 end600587 begin588 $stdout = output589 FZF.new(%w[--select-1], stream).start590 rescue SystemExit => e591 assert_equal 0, e.status592 assert_equal 'Hello World', output.string.chomp593 ensure594 $stdout = STDOUT601 def test_select_1602 {603 %w[--query=ol --select-1] => 'World',604 %w[--query=ol --select-1 --print-query] => "ol\nWorld",605 }.each do |opts, expected|606 assert_fzf_output opts, "Hello\nWorld", expected607 end608 end609610 def test_select_1_without_query611 assert_fzf_output %w[--select-1], 'Hello World', 'Hello World'612 end613614 def test_select_1_ambiguity615 stream = stream_for "Hello\nWorld"616 begin617 Timeout::timeout(3) do
6 unmodified lines
624 end625 end626627 def test_exit_0612 stream = stream_for "Hello\nWorld"613 output = StringIO.new614615 begin616 $stdout = output617 FZF.new(%w[--query=zz --exit-0], stream).start618 rescue SystemExit => e619 assert_equal 0, e.status620 assert_equal '', output.string621 ensure622 $stdout = STDOUT628 {629 %w[--query=zz --exit-0] => '',630 %w[--query=zz --exit-0 --print-query] => 'zz',631 }.each do |opts, expected|632 assert_fzf_output opts, "Hello\nWorld", expected633 end634 end635636 def test_exit_0_without_query627 stream = stream_for ""628 output = StringIO.new629630 begin631 $stdout = output632 FZF.new(%w[--exit-0], stream).start633 rescue SystemExit => e634 assert_equal 0, e.status635 assert_equal '', output.string636 ensure637 $stdout = STDOUT638 end637 assert_fzf_output %w[--exit-0], '', ''638 end639640 def test_ranking_overlap_match_regions641 list = [
44 unmodified lines
modified:fzf+24 added-8 removed
5 unmodified lines
6# / /_/_ / / /_7# / __/ / /_/ __/8# /_/ /___/_/ Fuzzy finder for your shell9#10# Version: 0.8.6 (Jun 27, 2014)10# Version: 0.8.6 (Jun 30, 2014)11#12# Author: Junegunn Choi13# URL: https://github.com/junegunn/fzf14# License: MIT
34 unmodified lines
49end5051class FZF52 C = Curses53 attr_reader :rxflag, :sort, :nth, :color, :black, :ansi256, :reverse,54 :mouse, :multi, :query, :select1, :exit0, :filter, :extended53 attr_reader :rxflag, :sort, :nth, :color, :black, :ansi256, :reverse, :prompt,54 :mouse, :multi, :query, :select1, :exit0, :filter, :extended,55 :print_query5657 def sync58 @shr_mtx.synchronize { yield }59 end
31 unmodified lines
91 @filter = nil92 @nth = nil93 @delim = nil94 @reverse = false95 @prompt = '> '96 @shr_mtx = Mutex.new97 @print_query = false9899 argv =100 if opts = ENV['FZF_DEFAULT_OPTS']101 require 'shellwords'
27 unmodified lines
129 when '-0', '--exit-0' then @exit0 = true130 when '+0', '--no-exit-0' then @exit0 = false131 when '-q', '--query'132 usage 1, 'query string required' unless query = argv.shift130 @query = query.dup133 @query = query134 when /^-q(.*)$/, /^--query=(.*)$/135 @query = $1136 when '-f', '--filter'137 usage 1, 'query string required' unless query = argv.shift
15 unmodified lines
153 usage 1, 'invalid sort size' unless sort =~ /^[0-9]+$/154 @sort = sort.to_i155 when /^-s([0-9]+)$/, /^--sort=([0-9]+)$/156 @sort = $1.to_i157 when '--prompt'158 usage 1, 'prompt string required' unless prompt = argv.shift159 @prompt = prompt160 when /^--prompt=(.*)$/161 @prompt = $1162 when '--print-query' then @print_query = true163 when '--no-print-query' then @print_query = false164 when '-e', '--extended-exact' then @extended = :exact165 when '+e', '--no-extended-exact' then @extended = nil166 else167 usage 1, "illegal option: #{o}"
61 unmodified lines
229 start_search do |loaded, matches|230 len = empty ? get(:@count) : matches.length231 if loaded232 if @select1 && len == 1233 puts @query if @print_query234 puts empty ? matches.first : matches.first.first235 exit 0236 elsif @exit0 && len == 0237 puts @query if @print_query238 exit 0239 end240 end241
12 unmodified lines
254 end255 end256257 def filter_list list258 puts @filter if @print_query259 matches = matcher.match(list, @filter, '', '')260 if @sort && matches.length <= @sort261 matches = FZF.sort(matches)262 end
63 unmodified lines
326 +c, --no-color Disable colors327 +2, --no-256 Disable 256-color328 --black Use black background329 --reverse Reverse orientation330 --prompt=STR Input prompt (default: '> ')331332 Scripting333 -q, --query=STR Start the finder with the given query334 -1, --select-1 Automatically select the only match335 -0, --exit-0 Exit immediately when there's no match336 -f, --filter=STR Filter mode. Do not start interactive finder.337 --print-query Print query as the first line338339 Environment variables340 FZF_DEFAULT_COMMAND Default command to use when input is tty341 FZF_DEFAULT_OPTS Defaults options. (e.g. "-x -m --sort 10000")] + $/ + $/
24 unmodified lines
366367 def print_input368 C.setpos cursor_y, 0369 C.clrtoeol355 cprint '> ', color(:prompt, true)370 cprint @prompt, color(:prompt, true)371 C.attron(C::A_BOLD) do372 C.addstr get(:@query)373 end374 end
21 unmodified lines
396 end397398 def refresh399 query, xcur = geta(:@query, :@xcur)385 C.setpos cursor_y, 2 + width(query[0, xcur])400 C.setpos cursor_y, @prompt.length + width(query[0, xcur])401 C.refresh402 end403404 def ctrl char
613 unmodified lines
1018 when :click, :release1019 x, y, shift = val.values_at :x, :y, :shift1020 y = @reverse ? (C.lines - 1 - y) : y1021 if y == C.lines - 11007 cursor = [0, [input.length, x - 2].min].max1022 cursor = [0, [input.length, x - @prompt.length].min].max1023 elsif x > 1 && y <= max_items1024 tv = get(:@yoff) + max_items - y - 110251026 case event
34 unmodified lines
1061 end1062 end1063 ensure1064 C.close_screen1065 q, selects = geta(:@query, :@selects)1066 @stdout.puts q if @print_query1067 if got1051 selects = call(:@selects, :dup)1068 if selects.empty?1069 @stdout.puts got1070 else1071 selects.each do |sel, _|
187 unmodified lines
modified:README.md+2 added
79 unmodified lines
80 +c, --no-color Disable colors81 +2, --no-256 Disable 256-color82 --black Use black background83 --reverse Reverse orientation84 --prompt=STR Input prompt (default: '> ')8586 Scripting87 -q, --query=STR Start the finder with the given query88 -1, --select-1 Automatically select the only match89 -0, --exit-0 Exit immediately when there's no match90 -f, --filter=STR Filter mode. Do not start interactive finder.91 --print-query Print query as the first line9293 Environment variables94 FZF_DEFAULT_COMMAND Default command to use when input is tty95 FZF_DEFAULT_OPTS Defaults options. (e.g. "-x -m --sort 10000")
463 unmodified lines