|
|
@ -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
|
|
|
|
end
|
|
|
|
when '+i' then @rxflag = 0
|
|
|
|
usage 1 unless rest.empty?
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
@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 }
|
|
|
|