Implement --select-1 and --exit-0 (#27, #36)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Junegunn Choi · committed by Junegunn Choi · parent ab9fbf1 · Browse files · 3 files·+183 added−77 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+183 added−77 removed
- tests:+90 added−11 removed
- other:+93 added−66 removed
Provenance
- Commit
22d3929- Tree
6d97491- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
ab9fbf1
3 files changed+183 added−77 removed
modified:test/test_fzf.rb+90 added-11 removed
1 #!/usr/bin/env ruby2 # encoding: utf-83 4+require 'curses'5+require 'timeout'6+require 'stringio'7 require 'minitest/autorun'8 $LOAD_PATH.unshift File.expand_path('../..', __FILE__)9 ENV['FZF_EXECUTABLE'] = '0'10 load 'fzf'11 unmodified lines22 assert_equal false, fzf.multi23 assert_equal true, fzf.color24 assert_equal nil, fzf.rxflag25 assert_equal true, fzf.mouse26+ assert_equal nil, fzf.nth27+ assert_equal true, fzf.color28+ assert_equal false, fzf.black29+ assert_equal true, fzf.ansi25630+ assert_equal '', fzf.query.get31+ assert_equal false, fzf.select132+ assert_equal false, fzf.exit033+ assert_equal nil, fzf.filter34+ assert_equal nil, fzf.extended35 end36 37 def test_environment_variables38 # Deprecated2 unmodified lines41 assert_equal 20000, fzf.sort42 assert_equal nil, fzf.nth43 44 ENV['FZF_DEFAULT_OPTS'] =33- '-x -m -s 10000 -q " hello world " +c +2 --no-mouse -f "goodbye world" --black --nth=3,-1,2'45+ '-x -m -s 10000 -q " hello world " +c +2 --select-1 -0 ' +46+ '--no-mouse -f "goodbye world" --black --nth=3,-1,2'47 fzf = FZF.new []48 assert_equal 10000, fzf.sort49 assert_equal ' hello world ',50 fzf.query.get4 unmodified lines55 assert_equal false, fzf.color56 assert_equal false, fzf.ansi25657 assert_equal true, fzf.black58 assert_equal false, fzf.mouse59+ assert_equal true, fzf.select160+ assert_equal true, fzf.exit061 assert_equal [3, -1, 2], fzf.nth62 end63 64 def test_option_parser65 # Long opts51- fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello52- --filter=howdy --extended-exact --no-mouse --no-256 --nth=1]66+ fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello --select-167+ --exit-0 --filter=howdy --extended-exact68+ --no-mouse --no-256 --nth=1]69 assert_equal 2000, fzf.sort70 assert_equal true, fzf.multi71 assert_equal false, fzf.color72 assert_equal false, fzf.ansi25673 assert_equal false, fzf.black74 assert_equal false, fzf.mouse75 assert_equal 0, fzf.rxflag76 assert_equal 'hello', fzf.query.get77+ assert_equal true, fzf.select178+ assert_equal true, fzf.exit079 assert_equal 'howdy', fzf.filter80 assert_equal :exact, fzf.extended81 assert_equal [1], fzf.nth82 83+ # Long opts (left-to-right)65- fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello66- --filter a --filter b --no-256 --black --nth -284+ fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query=hello85+ --filter a --filter b --no-256 --black --nth -1 --nth -286+ --select-1 --exit-0 --no-select-1 --no-exit-087 --no-sort -i --color --no-multi --256]88 assert_equal nil, fzf.sort89 assert_equal false, fzf.multi90 assert_equal true, fzf.color2 unmodified lines93 assert_equal true, fzf.mouse94 assert_equal 1, fzf.rxflag95 assert_equal 'b', fzf.filter96 assert_equal 'hello', fzf.query.get97+ assert_equal false, fzf.select198+ assert_equal false, fzf.exit099 assert_equal nil, fzf.extended100 assert_equal [-2], fzf.nth101 102 # Short opts81- fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x -fhowdy +2 -n3]103+ fzf = FZF.new %w[-s2000 +c -m +i -qhello -x -fhowdy +2 -n3 -1 -0]104 assert_equal 2000, fzf.sort105 assert_equal true, fzf.multi106 assert_equal false, fzf.color107 assert_equal false, fzf.ansi2561 unmodified line109 assert_equal 'hello', fzf.query.get110 assert_equal 'howdy', fzf.filter111 assert_equal :fuzzy, fzf.extended112 assert_equal [3], fzf.nth113+ assert_equal true, fzf.select1114+ assert_equal true, fzf.exit0115 116 # Left-to-right117 fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x -fgoodbye +2 -n3 -n4,594- -s 3000 -c +m -i -q world +x -fworld -2 --black --no-black]118+ -s 3000 -c +m -i -q world +x -fworld -2 --black --no-black119+ -1 -0 +1 +0120+ ]121 assert_equal 3000, fzf.sort122 assert_equal false, fzf.multi123 assert_equal true, fzf.color124 assert_equal true, fzf.ansi256125 assert_equal false, fzf.black126 assert_equal 1, fzf.rxflag127 assert_equal 'world', fzf.query.get128+ assert_equal false, fzf.select1129+ assert_equal false, fzf.exit0130 assert_equal 'world', fzf.filter131 assert_equal nil, fzf.extended132 assert_equal [4, 5], fzf.nth105-106- fzf = FZF.new %w[--query hello +s -s 2000 --query=world]107- assert_equal 2000, fzf.sort108- assert_equal 'world', fzf.query.get133 rescue SystemExit => e134 assert false, "Exited"135 end136 424 unmodified lines561 562 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [2], regex563 assert_equal [[list[0], [[1, 2]]], [list[1], [[8, 9]]]], matcher.match(list, 'f', '', '')564 end565+566+ def stream_for str567+ StringIO.new(str).tap do |sio|568+ sio.instance_eval do569+ alias org_gets gets570+571+ def gets572+ org_gets.tap { |e| sleep 0.5 unless e.nil? }573+ end574+ end575+ end576+ end577+578+ def test_select_1579+ stream = stream_for "Hello\nWorld"580+ output = StringIO.new581+582+ begin583+ $stdout = output584+ FZF.new(%w[--query=ol --select-1], stream).start585+ rescue SystemExit => e586+ assert_equal 0, e.status587+ assert_equal 'World', output.string.chomp588+ ensure589+ $stdout = STDOUT590+ end591+ end592+593+ def test_select_1_ambiguity594+ stream = stream_for "Hello\nWorld"595+ begin596+ Timeout::timeout(3) do597+ FZF.new(%w[--query=o --select-1], stream).start598+ end599+ flunk 'Should not reach here'600+ rescue Exception => e601+ Curses.close_screen602+ assert_instance_of Timeout::Error, e603+ end604+ end605+606+ def test_exit_0607+ stream = stream_for "Hello\nWorld"608+ output = StringIO.new609+610+ begin611+ $stdout = output612+ FZF.new(%w[--query=zz --exit-0], stream).start613+ rescue SystemExit => e614+ assert_equal 1, e.status615+ assert_equal '', output.string616+ ensure617+ $stdout = STDOUT618+ end619+ end620 end621 modified:fzf+73 added-33 removed
49 unmodified lines50 51 class FZF52 C = Curses53 attr_reader :rxflag, :sort, :nth, :color, :black, :ansi256,54- :mouse, :multi, :query, :filter, :extended54+ :mouse, :multi, :query, :select1, :exit0, :filter, :extended55 56 class AtomicVar57 def initialize value58 @value = value23 unmodified lines82 @black = false83 @multi = false84 @mouse = true85 @extended = nil86+ @select1 = false87+ @exit0 = false88 @filter = nil89 @nth = nil90 @delim = nil91 22 unmodified lines114 when '--no-black' then @black = false115 when '--mouse' then @mouse = true116 when '--no-mouse' then @mouse = false117 when '+s', '--no-sort' then @sort = nil118+ when '-1', '--select-1' then @select1 = true119+ when '+1', '--no-select-1' then @select1 = false120+ when '-0', '--exit-0' then @exit0 = true121+ when '+0', '--no-exit-0' then @exit0 = false122 when '-q', '--query'123 usage 1, 'query string required' unless query = argv.shift124 @query = AtomicVar.new query.dup125 when /^-q(.*)$/, /^--query=(.*)$/63 unmodified lines189 end190 191 def start192 if @filter187- start_reader(false).join193+ start_reader.join194 filter_list @new195 else190- @stdout = $stdout.clone191- $stdout.reopen($stderr)196+ start_reader197+ emit(:key) { q = @query.get; [q, q.length] } unless @query.empty?198+ if !@query.empty? && (@select1 || @exit0)199+ start_search do |loaded, matches|200+ len = matches.length201+ if loaded202+ if @select1 && len == 1203+ puts matches.first.first204+ exit 0205+ elsif @exit0 && len == 0206+ exit 1207+ end208+ end209 193- start_reader true194- init_screen195- start_renderer196- start_search197- start_loop210+ if loaded || len > 1211+ start_renderer212+ Thread.new { start_loop }213+ end214+ end215+216+ sleep217+ else218+ start_search219+ start_renderer220+ start_loop221+ end222 end223 end224 225 def filter_list list202- matches = get_matcher.match(list, @filter, '', '')226+ matches = matcher.match(list, @filter, '', '')227 if @sort && matches.length <= @sort228 matches = sort_by_rank(matches)229 end230 matches.each { |m| puts m.first }231 end232 233+ def matcher209- def get_matcher210- if @extended211- ExtendedFuzzyMatcher.new @rxflag, @extended, @nth, @delim212- else213- FuzzyMatcher.new @rxflag, @nth, @delim214- end234+ @matcher ||=235+ if @extended236+ ExtendedFuzzyMatcher.new @rxflag, @extended, @nth, @delim237+ else238+ FuzzyMatcher.new @rxflag, @nth, @delim239+ end240 end241 242 def version243 File.open(__FILE__, 'r') do |f|244 f.each_line do |line|245 if line =~ /Version: (.*)/221- $stdout.puts "fzf " << $1246+ $stdout.puts 'fzf ' << $1247 exit248 end249 end250 end2 unmodified lines253 def usage x, message = nil254 $stderr.puts message if message255 $stderr.puts %[usage: fzf [options]256 232- Options233- -m, --multi Enable multi-select257+ Search258 -x, --extended Extended-search mode259 -e, --extended-exact Extended-search mode (exact match)236- -q, --query=STR Initial query237- -f, --filter=STR Filter mode. Do not start interactive finder.260+ -i Case-insensitive match (default: smart-case match)261+ +i Case-sensitive match262 -n, --nth=[-]N[,..] Comma-separated list of field indexes for limiting263 search scope (positive or negative integers)264 -d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)265+266+ Search result267 -s, --sort=MAX Maximum number of matched items to sort (default: 1000)268 +s, --no-sort Do not sort the result. Keep the sequence unchanged.243- -i Case-insensitive match (default: smart-case match)244- +i Case-sensitive match269+270+ Interface271+ -m, --multi Enable multi-select with tab/shift-tab272+ --no-mouse Disable mouse273 +c, --no-color Disable colors274 +2, --no-256 Disable 256-color275 --black Use black background248- --no-mouse Disable mouse276 277+ Scripting278+ -q, --query=STR Start the finder with the given query279+ -1, --select-1 (with --query) Automatically select the only match280+ -0, --exit-0 (with --query) Exit when there's no match281+ -f, --filter=STR Filter mode. Do not start interactive finder.282+283 Environment variables284 FZF_DEFAULT_COMMAND Default command to use when input is tty285 FZF_DEFAULT_OPTS Defaults options. (e.g. "-x -m --sort 10000")] + $/ + $/286 exit x292 unmodified lines579 end580 end581 582 def init_screen583+ @stdout = $stdout.clone584+ $stdout.reopen($stderr)585+586 C.init_screen587 C.mousemask C::ALL_MOUSE_EVENTS if @mouse588 C.start_color589 dbg =49 unmodified lines639 640 C.refresh641 end642 607- def start_reader curses643+ def start_reader644 stream =645 if @source.tty?646 if default_command = ENV['FZF_DEFAULT_COMMAND']647 IO.popen(default_command)10 unmodified lines658 while line = stream.gets659 emit(:new) { @new << line.chomp }660 end661 emit(:loaded) { true }626- @spinner.clear if curses662+ @spinner.clear if @spinner663 end664 end665 630- def start_search631- matcher = get_matcher632- searcher = Thread.new {666+ def start_search &callback667+ Thread.new do668 lists = []669 events = {}670 fcache = {}671 q = ''30 unmodified lines702 user_input = events[:key]703 progress = 0704 started_at = Time.now705 671- if new_search && !lists.empty?706+ if updated = new_search && !lists.empty?707 q, cx = events.delete(:key) || [q, 0]708 empty = matcher.empty?(q)709 unless matches = fcache[q]710 found = []22 unmodified lines733 # Atomic update734 @matches.set matches735 end#new_search736 737+ callback = nil if callback &&738+ (updated || events[:loaded]) &&739+ callback.call(events[:loaded], matches)740+741 # This small delay reduces the number of partial lists742 sleep((delay = [20, delay + 5].min) * 0.01) unless user_input743 744 update_list new_search745 end#while746 rescue Exception => e747 @main.raise e748 end710- }749+ end750 end751 752 def pick753 items = @matches[0, max_items]35 unmodified lines789 end790 end791 792 def start_renderer793+ init_screen794+795 Thread.new do796 begin797 while blk = @queue.shift798 blk.call271 unmodified lines1070 actions[:stab] = actions[ctrl(:i)]1071 actions[127] = actions[ctrl(:h)]1072 actions[ctrl(:q)] = actions[ctrl(:g)] = actions[ctrl(:c)] = actions[:esc]1073 1033- emit(:key) { [@query.get, cursor] } unless @query.empty?1074 while true1075 @cursor_x.set cursor1076 render { print_input }1077 54 unmodified lines1132 @nth.each do |n|1133 if (token = tokens[n]) && (md = token.match(pat) rescue nil)1134 prefix_length += (tokens[0...n] || []).join.length1135 offset = md.offset(0).map { |o| o + prefix_length }1096- return MatchData.new offset1136+ return MatchData.new(offset)1137 end1138 end1139 nil1140 else137 unmodified linesmodified:README.md+20 added-33 removed
48 unmodified lines49 50 ```51 usage: fzf [options]52 53- Options54- -m, --multi Enable multi-select53+ Search54 -x, --extended Extended-search mode55 -e, --extended-exact Extended-search mode (exact match)57- -q, --query=STR Initial query58- -f, --filter=STR Filter mode. Do not start interactive finder.56+ -i Case-insensitive match (default: smart-case match)57+ +i Case-sensitive match58 -n, --nth=[-]N[,..] Comma-separated list of field indexes for limiting59 search scope (positive or negative integers)60 -d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)61+62+ Search result63 -s, --sort=MAX Maximum number of matched items to sort (default: 1000)64 +s, --no-sort Do not sort the result. Keep the sequence unchanged.64- -i Case-insensitive match (default: smart-case match)65- +i Case-sensitive match65+66+ Interface67+ -m, --multi Enable multi-select with tab/shift-tab68+ --no-mouse Disable mouse69 +c, --no-color Disable colors70 +2, --no-256 Disable 256-color71 --black Use black background69- --no-mouse Disable mouse72 73+ Scripting74+ -q, --query=STR Start the finder with the given query75+ -1, --select-1 (with --query) Automatically select the only match76+ -0, --exit-0 (with --query) Exit when there's no match77+ -f, --filter=STR Filter mode. Do not start interactive finder.78+79 Environment variables80 FZF_DEFAULT_COMMAND Default command to use when input is tty81 FZF_DEFAULT_OPTS Defaults options. (e.g. "-x -m --sort 10000")82 ```61 unmodified lines144 Useful examples145 ---------------146 147 ```sh140-# vimf - Open selected file in Vim141-vimf() {148+# fe [FUZZY PATTERN] - Open the selected file with the default editor149+# - Bypass fuzzy finder if there's only one match (--select-1)150+# - Exit if there's no match (--exit-0)151+fe() {152 local file143- file=$(fzf --query="$1") && vim "$file"153+ file=$(fzf --query="$1" --select-1 --exit-0) && ${EDITOR:-vim} "$file"154 }155 156 # fd - cd to selected directory157 fd() {44 unmodified lines202 cut -c1-80 | fzf --nth=1,2203 ) && $EDITOR $(cut -f3 <<< "$line") -c "set nocst" \204 -c "silent tag $(cut -f2 <<< "$line")"205 }196-197-# fq1 [QUERY]198-# - Immediately select the file when there's only one match.199-# If not, start the fuzzy finder as usual.200-fq1() {201- local lines202- lines=$(fzf --filter="$1" --no-sort)203- if [ -z "$lines" ]; then204- return 1205- elif [ $(wc -l <<< "$lines") -eq 1 ]; then206- echo "$lines"207- else208- echo "$lines" | fzf --query="$1"209- fi210-}211-212-# fe [QUERY]213-# - Open the selected file with the default editor214-# (Bypass fuzzy finder when there's only one match)215-fe() {216- local file217- file=$(fq1 "$1") && ${EDITOR:-vim} "$file"218-}206 ```207 208 Key bindings for command line209 -----------------------------228 unmodified lines