mirror of
https://github.com/Llewellynvdm/fzf.git
synced 2024-11-22 04:45:14 +00:00
Add -q option (initial query)
This commit is contained in:
parent
4745d50931
commit
c4dec4d34b
57
fzf
57
fzf
@ -41,7 +41,7 @@ require 'set'
|
|||||||
|
|
||||||
class FZF
|
class FZF
|
||||||
C = Curses
|
C = Curses
|
||||||
attr_reader :rxflag, :sort, :color, :multi
|
attr_reader :rxflag, :sort, :color, :multi, :query
|
||||||
|
|
||||||
class AtomicVar
|
class AtomicVar
|
||||||
def initialize value
|
def initialize value
|
||||||
@ -67,20 +67,36 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
def initialize argv, source = $stdin
|
def initialize argv, source = $stdin
|
||||||
usage 0 unless (%w[--help -h] & argv).empty?
|
@rxflag = Regexp::IGNORECASE
|
||||||
@rxflag = argv.delete('+i') ? 0 : Regexp::IGNORECASE
|
@sort = ENV.fetch('FZF_DEFAULT_SORT', 1000).to_i
|
||||||
@sort = %w[+s --no-sort].map { |e| argv.delete e }.compact.empty? ?
|
@color = true
|
||||||
ENV.fetch('FZF_DEFAULT_SORT', 1000).to_i : nil
|
@multi = false
|
||||||
@color = %w[+c --no-color].map { |e| argv.delete e }.compact.empty?
|
@xmode = false
|
||||||
@multi = !%w[-m --multi].map { |e| argv.delete e }.compact.empty?
|
|
||||||
@xmode = !%w[-x --extended].map { |e| argv.delete e }.compact.empty?
|
argv = argv.dup
|
||||||
rest = argv.join ' '
|
while o = argv.shift
|
||||||
if sort = rest.match(/(-s|--sort=?) ?([0-9]+)/)
|
case o
|
||||||
usage 1 unless @sort
|
when '-h', '--help' then usage 0
|
||||||
@sort = sort[2].to_i
|
when '-m', '--multi' then @multi = true
|
||||||
rest = rest.delete sort[0]
|
when '-x', '--extended' then @xmode = true
|
||||||
|
when '+i' then @rxflag = 0
|
||||||
|
when '+s', '--no-sort' then @sort = nil
|
||||||
|
when '+c', '--no-color' then @color = false
|
||||||
|
when '-q', '--query'
|
||||||
|
usage 1, 'query string required' unless query = argv.shift
|
||||||
|
@query = AtomicVar.new query.dup
|
||||||
|
when /^-q(.*)$/, /^--query=(.*)$/
|
||||||
|
@query = AtomicVar.new($1)
|
||||||
|
when '-s', '--sort'
|
||||||
|
usage 1, 'sort size required' unless sort = argv.shift
|
||||||
|
usage 1, 'invalid sort size' unless sort =~ /^[0-9]+$/
|
||||||
|
@sort = sort.to_i
|
||||||
|
when /^-s([0-9]+)$/, /^--sort=([0-9]+)$/
|
||||||
|
@sort = $1.to_i
|
||||||
|
else
|
||||||
|
usage 1, "illegal option: #{o}"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
usage 1 unless rest.empty?
|
|
||||||
|
|
||||||
@source = source
|
@source = source
|
||||||
@mtx = Mutex.new
|
@mtx = Mutex.new
|
||||||
@ -88,8 +104,8 @@ class FZF
|
|||||||
@events = {}
|
@events = {}
|
||||||
@new = []
|
@new = []
|
||||||
@queue = Queue.new
|
@queue = Queue.new
|
||||||
@cursor_x = AtomicVar.new(0)
|
@query ||= AtomicVar.new('')
|
||||||
@query = AtomicVar.new('')
|
@cursor_x = AtomicVar.new(@query.length)
|
||||||
@matches = AtomicVar.new([])
|
@matches = AtomicVar.new([])
|
||||||
@count = AtomicVar.new(0)
|
@count = AtomicVar.new(0)
|
||||||
@vcursor = AtomicVar.new(0)
|
@vcursor = AtomicVar.new(0)
|
||||||
@ -111,11 +127,13 @@ class FZF
|
|||||||
start_loop
|
start_loop
|
||||||
end
|
end
|
||||||
|
|
||||||
def usage x
|
def usage x, message = nil
|
||||||
|
$stderr.puts message if message
|
||||||
$stderr.puts %[usage: fzf [options]
|
$stderr.puts %[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
|
||||||
-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-sensitive match
|
+i Case-sensitive match
|
||||||
@ -637,8 +655,8 @@ class FZF
|
|||||||
got = nil
|
got = nil
|
||||||
begin
|
begin
|
||||||
tty = IO.open(IO.sysopen('/dev/tty'), 'r')
|
tty = IO.open(IO.sysopen('/dev/tty'), 'r')
|
||||||
input = ''
|
input = @query.get.dup
|
||||||
cursor = 0
|
cursor = input.length
|
||||||
backword = proc {
|
backword = proc {
|
||||||
cursor = (input[0, cursor].rindex(/\s\S/) || -1) + 1
|
cursor = (input[0, cursor].rindex(/\s\S/) || -1) + 1
|
||||||
}
|
}
|
||||||
@ -690,6 +708,7 @@ class FZF
|
|||||||
actions[ctrl(:g)] = actions[ctrl(:c)] = actions[:esc]
|
actions[ctrl(:g)] = actions[ctrl(:c)] = actions[:esc]
|
||||||
actions[:stab] = actions[9]
|
actions[:stab] = actions[9]
|
||||||
|
|
||||||
|
emit(:key) { [@query.get, cursor] } unless @query.empty?
|
||||||
while true
|
while true
|
||||||
@cursor_x.set cursor
|
@cursor_x.set cursor
|
||||||
render { print_input }
|
render { print_input }
|
||||||
|
@ -25,22 +25,35 @@ 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]
|
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello]
|
||||||
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
|
||||||
|
|
||||||
# Short opts
|
# Short opts
|
||||||
fzf = FZF.new %w[-s 2000 +c -m +i]
|
fzf = FZF.new %w[-s 2000 +c -m +i -qhello]
|
||||||
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
|
||||||
|
|
||||||
|
# Left-to-right
|
||||||
|
fzf = FZF.new %w[-qhello -s 2000 --no-sort -q world]
|
||||||
|
assert_equal nil, fzf.sort
|
||||||
|
assert_equal 'world', fzf.query.get
|
||||||
|
|
||||||
|
fzf = FZF.new %w[--query hello +s -s 2000 --query=world]
|
||||||
|
assert_equal 2000, fzf.sort
|
||||||
|
assert_equal 'world', fzf.query.get
|
||||||
|
rescue SystemExit => e
|
||||||
|
assert false, "Exited"
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_invalid_option
|
def test_invalid_option
|
||||||
[%w[-s 2000 +s], %w[yo dawg]].each do |argv|
|
[%w[--unknown], %w[yo dawg]].each do |argv|
|
||||||
assert_raises(SystemExit) do
|
assert_raises(SystemExit) do
|
||||||
fzf = FZF.new argv
|
fzf = FZF.new argv
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user