Add -q option (initial query)
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 4745d50 · Browse files · 2 files·+62 added−30 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+62 added−30 removed
- tests:+24 added−11 removed
- other:+38 added−19 removed
Provenance
- Commit
c4dec4d- Tree
b8c4c3f- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
4745d50
2 files changed+62 added−30 removed
modified:test/test_fzf.rb+24 added-11 removed
23 unmodified lines24 end25 26 def test_option_parser27 # Long opts28- fzf = FZF.new %w[--sort=2000 --no-color --multi +i]29- assert_equal 2000, fzf.sort30- assert_equal true, fzf.multi31- assert_equal false, fzf.color32- assert_equal 0, fzf.rxflag28+ fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello]29+ assert_equal 2000, fzf.sort30+ assert_equal true, fzf.multi31+ assert_equal false, fzf.color32+ assert_equal 0, fzf.rxflag33+ assert_equal 'hello', fzf.query.get34 35 # Short opts35- fzf = FZF.new %w[-s 2000 +c -m +i]36- assert_equal 2000, fzf.sort37- assert_equal true, fzf.multi38- assert_equal false, fzf.color39- assert_equal 0, fzf.rxflag36+ fzf = FZF.new %w[-s 2000 +c -m +i -qhello]37+ assert_equal 2000, fzf.sort38+ assert_equal true, fzf.multi39+ assert_equal false, fzf.color40+ assert_equal 0, fzf.rxflag41+ assert_equal 'hello', fzf.query.get42+43+ # Left-to-right44+ fzf = FZF.new %w[-qhello -s 2000 --no-sort -q world]45+ assert_equal nil, fzf.sort46+ assert_equal 'world', fzf.query.get47+48+ fzf = FZF.new %w[--query hello +s -s 2000 --query=world]49+ assert_equal 2000, fzf.sort50+ assert_equal 'world', fzf.query.get51+ rescue SystemExit => e52+ assert false, "Exited"53 end54 55 def test_invalid_option43- [%w[-s 2000 +s], %w[yo dawg]].each do |argv|56+ [%w[--unknown], %w[yo dawg]].each do |argv|57 assert_raises(SystemExit) do58 fzf = FZF.new argv59 end60 end276 unmodified linesmodified:fzf+38 added-19 removed
39 unmodified lines40 require 'set'41 42 class FZF43 C = Curses44- attr_reader :rxflag, :sort, :color, :multi44+ attr_reader :rxflag, :sort, :color, :multi, :query45 46 class AtomicVar47 def initialize value48 @value = value17 unmodified lines66 end67 end68 69 def initialize argv, source = $stdin70- usage 0 unless (%w[--help -h] & argv).empty?71- @rxflag = argv.delete('+i') ? 0 : Regexp::IGNORECASE72- @sort = %w[+s --no-sort].map { |e| argv.delete e }.compact.empty? ?73- ENV.fetch('FZF_DEFAULT_SORT', 1000).to_i : nil74- @color = %w[+c --no-color].map { |e| argv.delete e }.compact.empty?75- @multi = !%w[-m --multi].map { |e| argv.delete e }.compact.empty?76- @xmode = !%w[-x --extended].map { |e| argv.delete e }.compact.empty?77- rest = argv.join ' '78- if sort = rest.match(/(-s|--sort=?) ?([0-9]+)/)79- usage 1 unless @sort80- @sort = sort[2].to_i81- rest = rest.delete sort[0]70+ @rxflag = Regexp::IGNORECASE71+ @sort = ENV.fetch('FZF_DEFAULT_SORT', 1000).to_i72+ @color = true73+ @multi = false74+ @xmode = false75+76+ argv = argv.dup77+ while o = argv.shift78+ case o79+ when '-h', '--help' then usage 080+ when '-m', '--multi' then @multi = true81+ when '-x', '--extended' then @xmode = true82+ when '+i' then @rxflag = 083+ when '+s', '--no-sort' then @sort = nil84+ when '+c', '--no-color' then @color = false85+ when '-q', '--query'86+ usage 1, 'query string required' unless query = argv.shift87+ @query = AtomicVar.new query.dup88+ when /^-q(.*)$/, /^--query=(.*)$/89+ @query = AtomicVar.new($1)90+ when '-s', '--sort'91+ usage 1, 'sort size required' unless sort = argv.shift92+ usage 1, 'invalid sort size' unless sort =~ /^[0-9]+$/93+ @sort = sort.to_i94+ when /^-s([0-9]+)$/, /^--sort=([0-9]+)$/95+ @sort = $1.to_i96+ else97+ usage 1, "illegal option: #{o}"98+ end99 end83- usage 1 unless rest.empty?100 101 @source = source102 @mtx = Mutex.new103 @cv = ConditionVariable.new104 @events = {}105 @new = []106 @queue = Queue.new91- @cursor_x = AtomicVar.new(0)92- @query = AtomicVar.new('')107+ @query ||= AtomicVar.new('')108+ @cursor_x = AtomicVar.new(@query.length)109 @matches = AtomicVar.new([])110 @count = AtomicVar.new(0)111 @vcursor = AtomicVar.new(0)112 @vcursors = AtomicVar.new(Set.new)13 unmodified lines126 start_search127 start_loop128 end129 114- def usage x130+ def usage x, message = nil131+ $stderr.puts message if message132 $stderr.puts %[usage: fzf [options]133 134 -m, --multi Enable multi-select135 -x, --extended Extended-search mode136+ -q, --query=STR Initial query137 -s, --sort=MAX Maximum number of matched items to sort. Default: 1000138 +s, --no-sort Do not sort the result. Keep the sequence unchanged.139 +i Case-sensitive match140 +c, --no-color Disable colors]513 unmodified lines654 def start_loop655 got = nil656 begin657 tty = IO.open(IO.sysopen('/dev/tty'), 'r')640- input = ''641- cursor = 0658+ input = @query.get.dup659+ cursor = input.length660 backword = proc {661 cursor = (input[0, cursor].rindex(/\s\S/) || -1) + 1662 }663 actions = {43 unmodified lines707 actions[ctrl(:p)] = actions[ctrl(:k)]708 actions[ctrl(:g)] = actions[ctrl(:c)] = actions[:esc]709 actions[:stab] = actions[9]710 711+ emit(:key) { [@query.get, cursor] } unless @query.empty?712 while true713 @cursor_x.set cursor714 render { print_input }715 160 unmodified lines