2013-10-31 15:17:00 +00:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
# encoding: utf-8
|
2013-10-23 01:26:55 +00:00
|
|
|
#
|
|
|
|
# ____ ____
|
|
|
|
# / __/___ / __/
|
|
|
|
# / /_/_ / / /_
|
|
|
|
# / __/ / /_/ __/
|
|
|
|
# /_/ /___/_/ Fuzzy finder for your shell
|
|
|
|
#
|
2014-12-23 16:30:45 +00:00
|
|
|
# Version: 0.8.9 (Dec 24, 2014)
|
2015-03-06 04:21:55 +00:00
|
|
|
# Deprecation alert:
|
|
|
|
# This script is no longer maintained. Use the new Go version.
|
2013-12-27 17:25:24 +00:00
|
|
|
#
|
|
|
|
# Author: Junegunn Choi
|
|
|
|
# URL: https://github.com/junegunn/fzf
|
|
|
|
# License: MIT
|
2013-10-23 01:26:55 +00:00
|
|
|
#
|
2014-01-31 16:56:32 +00:00
|
|
|
# Copyright (c) 2014 Junegunn Choi
|
2013-10-23 01:26:55 +00:00
|
|
|
#
|
|
|
|
# MIT License
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
# a copy of this software and associated documentation files (the
|
|
|
|
# "Software"), to deal in the Software without restriction, including
|
|
|
|
# without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
# distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
# permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
# the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be
|
|
|
|
# included in all copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2014-07-26 16:08:30 +00:00
|
|
|
begin
|
|
|
|
require 'curses'
|
|
|
|
rescue LoadError
|
|
|
|
$stderr.puts 'curses gem is not installed. Try `gem install curses`.'
|
2015-01-02 06:23:37 +00:00
|
|
|
sleep 1
|
2014-07-26 16:08:30 +00:00
|
|
|
exit 1
|
|
|
|
end
|
2013-10-23 01:26:55 +00:00
|
|
|
require 'thread'
|
2013-11-16 15:05:42 +00:00
|
|
|
require 'set'
|
2013-10-23 01:26:55 +00:00
|
|
|
|
2014-01-27 17:58:20 +00:00
|
|
|
unless String.method_defined? :force_encoding
|
|
|
|
class String
|
|
|
|
def force_encoding *arg
|
|
|
|
self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-01 04:46:24 +00:00
|
|
|
class String
|
|
|
|
attr_accessor :orig
|
|
|
|
|
|
|
|
def tokenize delim, nth
|
|
|
|
unless delim
|
|
|
|
# AWK default
|
|
|
|
prefix_length = (index(/\S/) || 0) rescue 0
|
|
|
|
tokens = scan(/\S+\s*/) rescue []
|
|
|
|
else
|
|
|
|
prefix_length = 0
|
|
|
|
tokens = scan(delim) rescue []
|
|
|
|
end
|
|
|
|
nth.map { |n|
|
|
|
|
if n.begin == 0 && n.end == -1
|
|
|
|
[prefix_length, tokens.join]
|
|
|
|
elsif part = tokens[n]
|
|
|
|
[prefix_length + (tokens[0...(n.begin)] || []).join.length,
|
|
|
|
part.join]
|
|
|
|
end
|
|
|
|
}.compact
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-14 11:50:27 +00:00
|
|
|
class FZF
|
|
|
|
C = Curses
|
2014-06-30 03:00:59 +00:00
|
|
|
attr_reader :rxflag, :sort, :nth, :color, :black, :ansi256, :reverse, :prompt,
|
2014-06-30 03:23:37 +00:00
|
|
|
:mouse, :multi, :query, :select1, :exit0, :filter, :extended,
|
2014-11-01 04:46:24 +00:00
|
|
|
:print_query, :with_nth
|
2013-11-14 11:50:27 +00:00
|
|
|
|
2014-06-26 03:50:52 +00:00
|
|
|
def sync
|
|
|
|
@shr_mtx.synchronize { yield }
|
|
|
|
end
|
2013-11-14 11:50:27 +00:00
|
|
|
|
2014-06-26 03:50:52 +00:00
|
|
|
def get name
|
|
|
|
sync { instance_variable_get name }
|
|
|
|
end
|
2013-11-14 11:50:27 +00:00
|
|
|
|
2014-06-26 03:50:52 +00:00
|
|
|
def geta(*names)
|
|
|
|
sync { names.map { |name| instance_variable_get name } }
|
|
|
|
end
|
2013-10-28 06:51:58 +00:00
|
|
|
|
2014-06-26 03:50:52 +00:00
|
|
|
def call(name, method, *args)
|
|
|
|
sync { instance_variable_get(name).send(method, *args) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def set name, value = nil
|
|
|
|
sync do
|
|
|
|
instance_variable_set name,
|
2014-06-26 06:35:04 +00:00
|
|
|
(block_given? ? yield(instance_variable_get(name)) : value)
|
2013-11-14 11:50:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize argv, source = $stdin
|
2014-01-22 03:03:17 +00:00
|
|
|
@rxflag = nil
|
|
|
|
@sort = ENV.fetch('FZF_DEFAULT_SORT', 1000).to_i
|
|
|
|
@color = true
|
2014-03-07 08:32:14 +00:00
|
|
|
@ansi256 = true
|
2014-03-08 19:06:12 +00:00
|
|
|
@black = false
|
2014-01-22 03:03:17 +00:00
|
|
|
@multi = false
|
2014-03-06 11:52:38 +00:00
|
|
|
@mouse = true
|
2014-03-04 12:29:45 +00:00
|
|
|
@extended = nil
|
2014-04-02 12:41:57 +00:00
|
|
|
@select1 = false
|
|
|
|
@exit0 = false
|
2014-02-01 16:45:44 +00:00
|
|
|
@filter = nil
|
2014-03-29 08:05:35 +00:00
|
|
|
@nth = nil
|
2014-11-01 04:46:24 +00:00
|
|
|
@with_nth = nil
|
2014-03-29 08:05:35 +00:00
|
|
|
@delim = nil
|
2014-05-17 13:07:18 +00:00
|
|
|
@reverse = false
|
2014-06-30 03:00:59 +00:00
|
|
|
@prompt = '> '
|
2014-06-26 03:50:52 +00:00
|
|
|
@shr_mtx = Mutex.new
|
2015-03-31 06:40:33 +00:00
|
|
|
@expect = false
|
2014-06-30 03:23:37 +00:00
|
|
|
@print_query = false
|
2014-01-22 03:03:17 +00:00
|
|
|
|
|
|
|
argv =
|
|
|
|
if opts = ENV['FZF_DEFAULT_OPTS']
|
|
|
|
require 'shellwords'
|
|
|
|
Shellwords.shellwords(opts) + argv
|
|
|
|
else
|
|
|
|
argv.dup
|
|
|
|
end
|
2013-11-23 10:21:02 +00:00
|
|
|
while o = argv.shift
|
|
|
|
case o
|
2014-04-03 05:51:01 +00:00
|
|
|
when '--version' then FZF.version
|
2014-01-22 03:03:17 +00:00
|
|
|
when '-h', '--help' then usage 0
|
|
|
|
when '-m', '--multi' then @multi = true
|
|
|
|
when '+m', '--no-multi' then @multi = false
|
2014-03-04 12:29:45 +00:00
|
|
|
when '-x', '--extended' then @extended = :fuzzy
|
|
|
|
when '+x', '--no-extended' then @extended = nil
|
2014-01-22 03:03:17 +00:00
|
|
|
when '-i' then @rxflag = Regexp::IGNORECASE
|
|
|
|
when '+i' then @rxflag = 0
|
|
|
|
when '-c', '--color' then @color = true
|
|
|
|
when '+c', '--no-color' then @color = false
|
2014-03-07 08:32:14 +00:00
|
|
|
when '-2', '--256' then @ansi256 = true
|
|
|
|
when '+2', '--no-256' then @ansi256 = false
|
2014-03-08 19:06:12 +00:00
|
|
|
when '--black' then @black = true
|
|
|
|
when '--no-black' then @black = false
|
2014-03-07 08:32:14 +00:00
|
|
|
when '--mouse' then @mouse = true
|
2014-03-06 11:52:38 +00:00
|
|
|
when '--no-mouse' then @mouse = false
|
2014-05-17 13:07:18 +00:00
|
|
|
when '--reverse' then @reverse = true
|
|
|
|
when '--no-reverse' then @reverse = false
|
2014-01-22 03:03:17 +00:00
|
|
|
when '+s', '--no-sort' then @sort = nil
|
2014-04-02 12:41:57 +00:00
|
|
|
when '-1', '--select-1' then @select1 = true
|
|
|
|
when '+1', '--no-select-1' then @select1 = false
|
|
|
|
when '-0', '--exit-0' then @exit0 = true
|
|
|
|
when '+0', '--no-exit-0' then @exit0 = false
|
2013-11-23 10:21:02 +00:00
|
|
|
when '-q', '--query'
|
|
|
|
usage 1, 'query string required' unless query = argv.shift
|
2014-06-30 03:00:59 +00:00
|
|
|
@query = query
|
2013-11-23 10:21:02 +00:00
|
|
|
when /^-q(.*)$/, /^--query=(.*)$/
|
2014-06-26 03:50:52 +00:00
|
|
|
@query = $1
|
2014-02-01 16:45:44 +00:00
|
|
|
when '-f', '--filter'
|
|
|
|
usage 1, 'query string required' unless query = argv.shift
|
|
|
|
@filter = query
|
|
|
|
when /^-f(.*)$/, /^--filter=(.*)$/
|
|
|
|
@filter = $1
|
2014-03-29 08:05:35 +00:00
|
|
|
when '-n', '--nth'
|
2014-06-13 15:26:55 +00:00
|
|
|
usage 1, 'field expression required' unless nth = argv.shift
|
2014-04-01 16:49:07 +00:00
|
|
|
@nth = parse_nth nth
|
2014-06-13 15:26:55 +00:00
|
|
|
when /^-n([0-9,-\.]+)$/, /^--nth=([0-9,-\.]+)$/
|
2014-04-01 16:49:07 +00:00
|
|
|
@nth = parse_nth $1
|
2014-11-01 04:46:24 +00:00
|
|
|
when '--with-nth'
|
|
|
|
usage 1, 'field expression required' unless nth = argv.shift
|
|
|
|
@with_nth = parse_nth nth
|
|
|
|
when /^--with-nth=([0-9,-\.]+)$/
|
|
|
|
@with_nth = parse_nth $1
|
2014-03-29 08:05:35 +00:00
|
|
|
when '-d', '--delimiter'
|
|
|
|
usage 1, 'delimiter required' unless delim = argv.shift
|
|
|
|
@delim = FZF.build_delim_regex delim
|
|
|
|
when /^-d(.+)$/, /^--delimiter=(.+)$/
|
|
|
|
@delim = FZF.build_delim_regex $1
|
2013-11-23 10:21:02 +00:00
|
|
|
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
|
2014-06-30 03:00:59 +00:00
|
|
|
when '--prompt'
|
|
|
|
usage 1, 'prompt string required' unless prompt = argv.shift
|
|
|
|
@prompt = prompt
|
|
|
|
when /^--prompt=(.*)$/
|
|
|
|
@prompt = $1
|
2014-06-30 03:23:37 +00:00
|
|
|
when '--print-query' then @print_query = true
|
|
|
|
when '--no-print-query' then @print_query = false
|
2014-03-04 12:29:45 +00:00
|
|
|
when '-e', '--extended-exact' then @extended = :exact
|
|
|
|
when '+e', '--no-extended-exact' then @extended = nil
|
2015-03-31 06:40:33 +00:00
|
|
|
when '--expect'
|
|
|
|
argv.shift
|
|
|
|
@expect = true
|
|
|
|
when /^--expect=(.*)$/
|
|
|
|
@expect = true
|
2015-05-20 15:38:40 +00:00
|
|
|
when '--toggle-sort', '--tiebreak', '--color', '--bind'
|
2015-03-31 13:05:02 +00:00
|
|
|
argv.shift
|
2015-04-16 05:19:28 +00:00
|
|
|
when '--tac', '--no-tac', '--sync', '--no-sync', '--hscroll', '--no-hscroll',
|
2015-05-20 15:38:40 +00:00
|
|
|
'--inline-info', '--no-inline-info', /^--bind=(.*)$/,
|
2015-04-17 17:52:30 +00:00
|
|
|
/^--color=(.*)$/, /^--toggle-sort=(.*)$/, /^--tiebreak=(.*)$/
|
2015-03-09 17:16:32 +00:00
|
|
|
# XXX
|
2013-11-23 10:21:02 +00:00
|
|
|
else
|
|
|
|
usage 1, "illegal option: #{o}"
|
|
|
|
end
|
2013-11-14 11:50:27 +00:00
|
|
|
end
|
|
|
|
|
2014-06-26 03:50:52 +00:00
|
|
|
@source = source.clone
|
|
|
|
@evt_mtx = Mutex.new
|
|
|
|
@cv = ConditionVariable.new
|
|
|
|
@events = {}
|
|
|
|
@new = []
|
|
|
|
@queue = Queue.new
|
|
|
|
@pending = nil
|
|
|
|
@rev_dir = @reverse ? -1 : 1
|
2014-11-01 04:46:24 +00:00
|
|
|
@stdout = $stdout.clone
|
2014-02-01 16:45:44 +00:00
|
|
|
|
|
|
|
unless @filter
|
2014-06-26 03:50:52 +00:00
|
|
|
# Shared variables: needs protection
|
|
|
|
@query ||= ''
|
|
|
|
@matches = []
|
|
|
|
@count = 0
|
2014-06-26 06:35:04 +00:00
|
|
|
@xcur = @query.length
|
2014-06-26 03:50:52 +00:00
|
|
|
@ycur = 0
|
|
|
|
@yoff = 0
|
|
|
|
@dirty = Set.new
|
|
|
|
@spinner = '-\|/-\|/'.split(//)
|
|
|
|
@selects = {} # ordered >= 1.9
|
|
|
|
|
|
|
|
@main = Thread.current
|
|
|
|
@plcount = 0
|
2014-02-01 16:45:44 +00:00
|
|
|
end
|
2013-11-14 11:50:27 +00:00
|
|
|
end
|
|
|
|
|
2014-04-01 16:49:07 +00:00
|
|
|
def parse_nth nth
|
2014-11-01 04:46:24 +00:00
|
|
|
ranges = nth.split(',').map { |expr|
|
2014-06-13 15:26:55 +00:00
|
|
|
x = proc { usage 1, "invalid field expression: #{expr}" }
|
|
|
|
first, second = expr.split('..', 2)
|
|
|
|
x.call if !first.empty? && first.to_i == 0 ||
|
|
|
|
second && !second.empty? && (second.to_i == 0 || second.include?('.'))
|
|
|
|
|
|
|
|
first = first.empty? ? 1 : first.to_i
|
|
|
|
second = case second
|
|
|
|
when nil then first
|
|
|
|
when '' then -1
|
|
|
|
else second.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
Range.new(*[first, second].map { |e| e > 0 ? e - 1 : e })
|
2014-04-01 16:49:07 +00:00
|
|
|
}
|
2014-11-01 04:46:24 +00:00
|
|
|
ranges == [0..-1] ? nil : ranges
|
2014-04-01 16:49:07 +00:00
|
|
|
end
|
|
|
|
|
2014-03-29 08:05:35 +00:00
|
|
|
def FZF.build_delim_regex delim
|
|
|
|
Regexp.compile(delim) rescue (delim = Regexp.escape(delim))
|
|
|
|
Regexp.compile "(?:.*?#{delim})|(?:.+?$)"
|
|
|
|
end
|
|
|
|
|
2014-11-03 14:58:10 +00:00
|
|
|
def burp string, orig = nil
|
|
|
|
@stdout.puts(orig || string.orig || string)
|
2014-11-01 04:46:24 +00:00
|
|
|
end
|
|
|
|
|
2013-11-14 11:50:27 +00:00
|
|
|
def start
|
2014-02-01 16:45:44 +00:00
|
|
|
if @filter
|
2014-04-02 12:41:57 +00:00
|
|
|
start_reader.join
|
2014-02-01 16:45:44 +00:00
|
|
|
filter_list @new
|
|
|
|
else
|
2014-04-02 12:41:57 +00:00
|
|
|
start_reader
|
2014-06-26 03:50:52 +00:00
|
|
|
query = get(:@query)
|
|
|
|
emit(:key) { [query, query.length] } unless empty = query.empty?
|
2014-04-02 16:06:40 +00:00
|
|
|
if @select1 || @exit0
|
2014-04-02 12:41:57 +00:00
|
|
|
start_search do |loaded, matches|
|
2014-06-26 03:50:52 +00:00
|
|
|
len = empty ? get(:@count) : matches.length
|
2014-04-02 12:41:57 +00:00
|
|
|
if loaded
|
|
|
|
if @select1 && len == 1
|
2014-06-30 03:23:37 +00:00
|
|
|
puts @query if @print_query
|
2015-03-31 06:40:33 +00:00
|
|
|
puts if @expect
|
2014-11-01 04:46:24 +00:00
|
|
|
burp(empty ? matches.first : matches.first.first)
|
2014-04-02 12:41:57 +00:00
|
|
|
exit 0
|
|
|
|
elsif @exit0 && len == 0
|
2014-06-30 03:23:37 +00:00
|
|
|
puts @query if @print_query
|
2015-03-31 06:40:33 +00:00
|
|
|
puts if @expect
|
2014-04-02 16:06:40 +00:00
|
|
|
exit 0
|
2014-04-02 12:41:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if loaded || len > 1
|
|
|
|
start_renderer
|
|
|
|
Thread.new { start_loop }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
sleep
|
|
|
|
else
|
|
|
|
start_search
|
|
|
|
start_renderer
|
|
|
|
start_loop
|
|
|
|
end
|
2014-02-01 16:45:44 +00:00
|
|
|
end
|
|
|
|
end
|
2013-11-14 11:50:27 +00:00
|
|
|
|
2014-02-01 16:45:44 +00:00
|
|
|
def filter_list list
|
2014-06-30 03:23:37 +00:00
|
|
|
puts @filter if @print_query
|
2014-04-02 12:41:57 +00:00
|
|
|
matches = matcher.match(list, @filter, '', '')
|
2014-02-01 16:45:44 +00:00
|
|
|
if @sort && matches.length <= @sort
|
2014-04-03 05:51:01 +00:00
|
|
|
matches = FZF.sort(matches)
|
2014-02-01 16:45:44 +00:00
|
|
|
end
|
|
|
|
matches.each { |m| puts m.first }
|
2013-11-14 11:50:27 +00:00
|
|
|
end
|
|
|
|
|
2014-04-02 12:41:57 +00:00
|
|
|
def matcher
|
|
|
|
@matcher ||=
|
|
|
|
if @extended
|
|
|
|
ExtendedFuzzyMatcher.new @rxflag, @extended, @nth, @delim
|
|
|
|
else
|
|
|
|
FuzzyMatcher.new @rxflag, @nth, @delim
|
|
|
|
end
|
2014-03-04 12:29:45 +00:00
|
|
|
end
|
|
|
|
|
2014-04-03 05:51:01 +00:00
|
|
|
class << self
|
|
|
|
def version
|
|
|
|
File.open(__FILE__, 'r') do |f|
|
|
|
|
f.each_line do |line|
|
|
|
|
if line =~ /Version: (.*)/
|
|
|
|
$stdout.puts 'fzf ' << $1
|
|
|
|
exit
|
|
|
|
end
|
2013-12-27 17:25:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-04-03 05:51:01 +00:00
|
|
|
|
|
|
|
def sort list
|
|
|
|
list.sort_by { |tuple| rank tuple }
|
|
|
|
end
|
|
|
|
|
|
|
|
def rank tuple
|
|
|
|
line, offsets = tuple
|
|
|
|
matchlen = 0
|
|
|
|
pe = 0
|
|
|
|
offsets.sort.each do |pair|
|
|
|
|
b, e = pair
|
|
|
|
b = pe if pe > b
|
|
|
|
pe = e if e > pe
|
|
|
|
matchlen += e - b if e > b
|
|
|
|
end
|
|
|
|
[matchlen, line.length, line]
|
|
|
|
end
|
2013-12-27 17:25:24 +00:00
|
|
|
end
|
|
|
|
|
2013-11-23 10:21:02 +00:00
|
|
|
def usage x, message = nil
|
|
|
|
$stderr.puts message if message
|
2013-11-14 11:50:27 +00:00
|
|
|
$stderr.puts %[usage: fzf [options]
|
|
|
|
|
2014-04-02 12:41:57 +00:00
|
|
|
Search
|
2014-11-01 04:46:24 +00:00
|
|
|
-x, --extended Extended-search mode
|
|
|
|
-e, --extended-exact Extended-search mode (exact match)
|
|
|
|
-i Case-insensitive match (default: smart-case match)
|
|
|
|
+i Case-sensitive match
|
|
|
|
-n, --nth=N[,..] Comma-separated list of field index expressions
|
|
|
|
for limiting search scope. Each can be a non-zero
|
|
|
|
integer or a range expression ([BEGIN]..[END])
|
|
|
|
--with-nth=N[,..] Transform the item using index expressions for search
|
|
|
|
-d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)
|
2014-04-02 12:41:57 +00:00
|
|
|
|
|
|
|
Search result
|
2014-11-01 04:46:24 +00:00
|
|
|
-s, --sort=MAX Maximum number of matched items to sort (default: 1000)
|
|
|
|
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
2014-04-02 12:41:57 +00:00
|
|
|
|
|
|
|
Interface
|
2014-11-01 04:46:24 +00:00
|
|
|
-m, --multi Enable multi-select with tab/shift-tab
|
|
|
|
--no-mouse Disable mouse
|
|
|
|
+c, --no-color Disable colors
|
|
|
|
+2, --no-256 Disable 256-color
|
|
|
|
--black Use black background
|
|
|
|
--reverse Reverse orientation
|
|
|
|
--prompt=STR Input prompt (default: '> ')
|
2014-04-02 12:41:57 +00:00
|
|
|
|
|
|
|
Scripting
|
2014-11-01 04:46:24 +00:00
|
|
|
-q, --query=STR Start the finder with the given query
|
|
|
|
-1, --select-1 Automatically select the only match
|
|
|
|
-0, --exit-0 Exit immediately when there's no match
|
|
|
|
-f, --filter=STR Filter mode. Do not start interactive finder.
|
|
|
|
--print-query Print query as the first line
|
2014-01-22 03:03:17 +00:00
|
|
|
|
|
|
|
Environment variables
|
2014-11-01 04:46:24 +00:00
|
|
|
FZF_DEFAULT_COMMAND Default command to use when input is tty
|
|
|
|
FZF_DEFAULT_OPTS Defaults options. (e.g. "-x -m --sort 10000")] + $/ + $/
|
|
|
|
exit x
|
2013-11-14 11:50:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def emit event
|
2014-06-26 03:50:52 +00:00
|
|
|
@evt_mtx.synchronize do
|
2013-11-14 11:50:27 +00:00
|
|
|
@events[event] = yield
|
|
|
|
@cv.broadcast
|
|
|
|
end
|
2013-10-28 06:51:58 +00:00
|
|
|
end
|
|
|
|
|
2013-11-14 11:50:27 +00:00
|
|
|
def max_items; C.lines - 2; end
|
2014-05-17 13:07:18 +00:00
|
|
|
|
|
|
|
def cursor_y offset = 0
|
|
|
|
@reverse ? (offset) : (C.lines - 1 - offset)
|
|
|
|
end
|
|
|
|
|
2013-11-14 11:50:27 +00:00
|
|
|
def cprint str, col
|
|
|
|
C.attron(col) do
|
2013-11-15 12:49:00 +00:00
|
|
|
addstr_safe str
|
2013-11-14 11:50:27 +00:00
|
|
|
end if str
|
2013-10-28 06:51:58 +00:00
|
|
|
end
|
2013-11-15 12:49:00 +00:00
|
|
|
def addstr_safe str
|
2014-08-16 15:22:22 +00:00
|
|
|
str = str.gsub("\0", '') rescue str
|
|
|
|
C.addstr str
|
2013-11-15 12:49:00 +00:00
|
|
|
end
|
2013-11-14 11:50:27 +00:00
|
|
|
|
|
|
|
def print_input
|
|
|
|
C.setpos cursor_y, 0
|
|
|
|
C.clrtoeol
|
2014-06-30 03:00:59 +00:00
|
|
|
cprint @prompt, color(:prompt, true)
|
2013-11-14 11:50:27 +00:00
|
|
|
C.attron(C::A_BOLD) do
|
2014-06-26 03:50:52 +00:00
|
|
|
C.addstr get(:@query)
|
2013-11-14 11:50:27 +00:00
|
|
|
end
|
2013-10-28 06:51:58 +00:00
|
|
|
end
|
|
|
|
|
2013-11-14 11:50:27 +00:00
|
|
|
def print_info msg = nil
|
2014-05-17 13:07:18 +00:00
|
|
|
C.setpos cursor_y(1), 0
|
2013-11-14 11:50:27 +00:00
|
|
|
C.clrtoeol
|
2014-06-26 03:50:52 +00:00
|
|
|
|
2013-11-14 11:50:27 +00:00
|
|
|
prefix =
|
2014-06-26 03:50:52 +00:00
|
|
|
if spin_char = call(:@spinner, :first)
|
|
|
|
cprint spin_char, color(:spinner, true)
|
2013-11-14 11:50:27 +00:00
|
|
|
' '
|
|
|
|
else
|
|
|
|
' '
|
|
|
|
end
|
|
|
|
C.attron color(:info, false) do
|
2014-06-26 03:50:52 +00:00
|
|
|
sync do
|
|
|
|
C.addstr "#{prefix}#{@matches.length}/#{@count}"
|
|
|
|
if (selected = @selects.length) > 0
|
|
|
|
C.addstr " (#{selected})"
|
|
|
|
end
|
2013-11-14 11:50:27 +00:00
|
|
|
end
|
|
|
|
C.addstr msg if msg
|
|
|
|
end
|
2013-10-28 06:51:58 +00:00
|
|
|
end
|
|
|
|
|
2013-11-14 11:50:27 +00:00
|
|
|
def refresh
|
2014-06-26 03:50:52 +00:00
|
|
|
query, xcur = geta(:@query, :@xcur)
|
2014-06-30 03:00:59 +00:00
|
|
|
C.setpos cursor_y, @prompt.length + width(query[0, xcur])
|
2013-11-14 11:50:27 +00:00
|
|
|
C.refresh
|
2013-10-24 15:40:04 +00:00
|
|
|
end
|
2013-11-14 11:50:27 +00:00
|
|
|
|
|
|
|
def ctrl char
|
|
|
|
char.to_s.ord - 'a'.ord + 1
|
2013-10-29 08:46:04 +00:00
|
|
|
end
|
2013-11-14 11:50:27 +00:00
|
|
|
|
|
|
|
def format line, limit, offsets
|
|
|
|
offsets ||= []
|
|
|
|
maxe = offsets.map { |e| e.last }.max || 0
|
|
|
|
|
|
|
|
# Overflow
|
|
|
|
if width(line) > limit
|
|
|
|
ewidth = width(line[0...maxe])
|
|
|
|
# Stri..
|
|
|
|
if ewidth <= limit - 2
|
|
|
|
line, _ = trim line, limit - 2, false
|
|
|
|
line << '..'
|
|
|
|
# ..ring
|
|
|
|
else
|
|
|
|
# ..ri..
|
|
|
|
line = line[0...maxe] + '..' if ewidth < width(line) - 2
|
|
|
|
line, diff = trim line, limit - 2, true
|
|
|
|
offsets = offsets.map { |pair|
|
|
|
|
b, e = pair
|
|
|
|
b += 2 - diff
|
|
|
|
e += 2 - diff
|
|
|
|
b = [2, b].max
|
|
|
|
[b, e]
|
|
|
|
}
|
|
|
|
line = '..' + line
|
|
|
|
end
|
2013-10-24 03:52:31 +00:00
|
|
|
end
|
2013-11-14 11:50:27 +00:00
|
|
|
|
|
|
|
tokens = []
|
|
|
|
index = 0
|
|
|
|
offsets.select { |pair| pair.first < pair.last }.
|
|
|
|
sort_by { |pair| pair }.each do |pair|
|
|
|
|
b, e = pair.map { |x| [index, x].max }
|
|
|
|
tokens << [line[index...b], false]
|
|
|
|
tokens << [line[b...e], true]
|
|
|
|
index = e
|
2013-11-14 11:04:46 +00:00
|
|
|
end
|
2013-12-09 05:46:06 +00:00
|
|
|
tokens << [line[index..-1], false] if index < line.length
|
2013-11-14 11:50:27 +00:00
|
|
|
tokens.reject { |pair| pair.first.empty? }
|
2013-11-14 11:04:46 +00:00
|
|
|
end
|
|
|
|
|
2013-11-14 11:50:27 +00:00
|
|
|
def print_item row, tokens, chosen, selected
|
|
|
|
# Cursor
|
|
|
|
C.setpos row, 0
|
|
|
|
C.clrtoeol
|
2013-11-16 07:23:59 +00:00
|
|
|
cprint chosen ? '>' : ' ', color(:cursor, true)
|
2013-11-14 11:50:27 +00:00
|
|
|
cprint selected ? '>' : ' ',
|
2013-11-16 07:23:59 +00:00
|
|
|
chosen ? color(:chosen) : (selected ? color(:selected, true) : 0)
|
2013-11-14 11:50:27 +00:00
|
|
|
|
|
|
|
# Highlighted item
|
|
|
|
C.attron color(:chosen, true) if chosen
|
|
|
|
tokens.each do |pair|
|
|
|
|
token, highlighted = pair
|
|
|
|
|
|
|
|
if highlighted
|
|
|
|
cprint token, color(chosen ? :match! : :match, chosen)
|
|
|
|
C.attron color(:chosen, true) if chosen
|
|
|
|
else
|
2013-11-15 12:49:00 +00:00
|
|
|
addstr_safe token
|
2013-11-14 11:50:27 +00:00
|
|
|
end
|
2013-11-14 11:04:46 +00:00
|
|
|
end
|
2013-11-14 11:50:27 +00:00
|
|
|
C.attroff color(:chosen, true) if chosen
|
2013-11-14 11:04:46 +00:00
|
|
|
end
|
|
|
|
|
2014-03-05 09:00:20 +00:00
|
|
|
AFTER_1_9 = RUBY_VERSION.split('.').map { |e| e.rjust(3, '0') }.join >= '001009'
|
|
|
|
|
|
|
|
if AFTER_1_9
|
2013-11-14 11:50:27 +00:00
|
|
|
@@wrx = Regexp.new '\p{Han}|\p{Katakana}|\p{Hiragana}|\p{Hangul}'
|
|
|
|
def width str
|
2014-01-29 18:12:12 +00:00
|
|
|
str.gsub(@@wrx, ' ').length rescue str.length
|
2013-11-02 15:06:50 +00:00
|
|
|
end
|
2013-10-23 01:26:55 +00:00
|
|
|
|
2013-11-14 11:50:27 +00:00
|
|
|
def trim str, len, left
|
|
|
|
width = width str
|
|
|
|
diff = 0
|
|
|
|
while width > len
|
2014-06-06 16:17:38 +00:00
|
|
|
width -= ((left ? str[0, 1] : str[-1, 1]) =~ @@wrx ? 2 : 1) rescue 1
|
2013-11-14 11:50:27 +00:00
|
|
|
str = left ? str[1..-1] : str[0...-1]
|
|
|
|
diff += 1
|
|
|
|
end
|
|
|
|
[str, diff]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
def width str
|
|
|
|
str.length
|
2013-11-02 15:06:50 +00:00
|
|
|
end
|
|
|
|
|
2013-11-14 11:50:27 +00:00
|
|
|
def trim str, len, left
|
|
|
|
diff = str.length - len
|
|
|
|
if diff > 0
|
|
|
|
[left ? str[diff..-1] : str[0...-diff], diff]
|
|
|
|
else
|
|
|
|
[str, 0]
|
|
|
|
end
|
2013-10-23 01:26:55 +00:00
|
|
|
end
|
2013-10-24 03:16:32 +00:00
|
|
|
|
2013-11-14 11:50:27 +00:00
|
|
|
class ::String
|
|
|
|
def ord
|
|
|
|
self.unpack('c').first
|
|
|
|
end
|
2013-10-24 03:16:32 +00:00
|
|
|
end
|
2013-11-03 02:45:14 +00:00
|
|
|
|
2013-11-14 11:50:27 +00:00
|
|
|
class ::Fixnum
|
|
|
|
def ord
|
|
|
|
self
|
|
|
|
end
|
|
|
|
end
|
2013-11-03 02:45:14 +00:00
|
|
|
end
|
2013-11-14 11:50:27 +00:00
|
|
|
|
|
|
|
def init_screen
|
2014-04-02 12:41:57 +00:00
|
|
|
$stdout.reopen($stderr)
|
|
|
|
|
2013-11-14 11:50:27 +00:00
|
|
|
C.init_screen
|
2014-03-06 11:52:38 +00:00
|
|
|
C.mousemask C::ALL_MOUSE_EVENTS if @mouse
|
2013-11-14 11:50:27 +00:00
|
|
|
C.start_color
|
|
|
|
dbg =
|
2014-03-08 19:06:12 +00:00
|
|
|
if !@black && C.respond_to?(:use_default_colors)
|
2013-11-14 11:50:27 +00:00
|
|
|
C.use_default_colors
|
|
|
|
-1
|
|
|
|
else
|
|
|
|
C::COLOR_BLACK
|
|
|
|
end
|
|
|
|
C.raw
|
|
|
|
C.noecho
|
|
|
|
|
|
|
|
if @color
|
2014-03-07 08:32:14 +00:00
|
|
|
if @ansi256 && ENV['TERM'].to_s =~ /256/
|
2013-11-16 07:23:59 +00:00
|
|
|
C.init_pair 1, 110, dbg
|
|
|
|
C.init_pair 2, 108, dbg
|
|
|
|
C.init_pair 3, 254, 236
|
|
|
|
C.init_pair 4, 151, 236
|
|
|
|
C.init_pair 5, 148, dbg
|
|
|
|
C.init_pair 6, 144, dbg
|
|
|
|
C.init_pair 7, 161, 236
|
|
|
|
C.init_pair 8, 168, 236
|
2013-11-14 11:50:27 +00:00
|
|
|
else
|
2013-11-16 07:23:59 +00:00
|
|
|
C.init_pair 1, C::COLOR_BLUE, dbg
|
|
|
|
C.init_pair 2, C::COLOR_GREEN, dbg
|
|
|
|
C.init_pair 3, C::COLOR_YELLOW, C::COLOR_BLACK
|
|
|
|
C.init_pair 4, C::COLOR_GREEN, C::COLOR_BLACK
|
|
|
|
C.init_pair 5, C::COLOR_GREEN, dbg
|
|
|
|
C.init_pair 6, C::COLOR_WHITE, dbg
|
|
|
|
C.init_pair 7, C::COLOR_RED, C::COLOR_BLACK
|
|
|
|
C.init_pair 8, C::COLOR_MAGENTA, C::COLOR_BLACK
|
2013-11-14 11:50:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.color sym, bold = false
|
2013-11-16 07:23:59 +00:00
|
|
|
C.color_pair([:prompt, :match, :chosen, :match!,
|
|
|
|
:spinner, :info, :cursor, :selected].index(sym) + 1) |
|
2013-11-14 11:50:27 +00:00
|
|
|
(bold ? C::A_BOLD : 0)
|
|
|
|
end
|
2013-10-23 01:26:55 +00:00
|
|
|
else
|
2013-11-14 11:50:27 +00:00
|
|
|
def self.color sym, bold = false
|
|
|
|
case sym
|
|
|
|
when :chosen
|
|
|
|
bold ? C::A_REVERSE : 0
|
|
|
|
when :match
|
|
|
|
C::A_UNDERLINE
|
|
|
|
when :match!
|
|
|
|
C::A_REVERSE | C::A_UNDERLINE
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end | (bold ? C::A_BOLD : 0)
|
|
|
|
end
|
2013-10-23 01:26:55 +00:00
|
|
|
end
|
2014-02-01 01:07:59 +00:00
|
|
|
|
|
|
|
C.refresh
|
2013-10-23 01:26:55 +00:00
|
|
|
end
|
|
|
|
|
2014-04-02 12:41:57 +00:00
|
|
|
def start_reader
|
2013-11-14 11:50:27 +00:00
|
|
|
stream =
|
|
|
|
if @source.tty?
|
2015-03-13 04:08:42 +00:00
|
|
|
default_command = ENV['FZF_DEFAULT_COMMAND']
|
|
|
|
if default_command && !default_command.empty?
|
2013-11-14 11:50:27 +00:00
|
|
|
IO.popen(default_command)
|
|
|
|
elsif !`which find`.empty?
|
|
|
|
IO.popen("find * -path '*/\\.*' -prune -o -type f -print -o -type l -print 2> /dev/null")
|
|
|
|
else
|
|
|
|
exit 1
|
2013-10-23 01:26:55 +00:00
|
|
|
end
|
2013-11-14 11:50:27 +00:00
|
|
|
else
|
|
|
|
@source
|
|
|
|
end
|
2013-10-23 01:26:55 +00:00
|
|
|
|
2013-11-14 11:50:27 +00:00
|
|
|
Thread.new do
|
2014-11-01 04:46:24 +00:00
|
|
|
if @with_nth
|
|
|
|
while line = stream.gets
|
|
|
|
emit(:new) { @new << transform(line) }
|
|
|
|
end
|
|
|
|
else
|
|
|
|
while line = stream.gets
|
|
|
|
emit(:new) { @new << line.chomp }
|
|
|
|
end
|
2013-11-14 11:50:27 +00:00
|
|
|
end
|
|
|
|
emit(:loaded) { true }
|
2014-04-02 12:41:57 +00:00
|
|
|
@spinner.clear if @spinner
|
2013-11-14 11:50:27 +00:00
|
|
|
end
|
|
|
|
end
|
2013-11-09 18:56:18 +00:00
|
|
|
|
2014-11-01 04:46:24 +00:00
|
|
|
def transform line
|
|
|
|
line = line.chomp
|
|
|
|
mut = (line =~ / $/ ? line : line + ' ').
|
|
|
|
tokenize(@delim, @with_nth).map { |e| e.last }.join('').sub(/ *$/, '')
|
|
|
|
mut.orig = line
|
|
|
|
mut
|
|
|
|
end
|
|
|
|
|
2014-04-02 12:41:57 +00:00
|
|
|
def start_search &callback
|
|
|
|
Thread.new do
|
2013-11-14 11:50:27 +00:00
|
|
|
lists = []
|
|
|
|
events = {}
|
|
|
|
fcache = {}
|
|
|
|
q = ''
|
|
|
|
delay = -5
|
|
|
|
|
|
|
|
begin
|
|
|
|
while true
|
2014-06-26 03:50:52 +00:00
|
|
|
@evt_mtx.synchronize do
|
2013-11-14 11:50:27 +00:00
|
|
|
while true
|
|
|
|
events.merge! @events
|
|
|
|
|
|
|
|
if @events.empty? # No new events
|
2014-06-26 03:50:52 +00:00
|
|
|
@cv.wait @evt_mtx
|
2013-11-14 11:50:27 +00:00
|
|
|
next
|
|
|
|
end
|
|
|
|
@events.clear
|
|
|
|
break
|
|
|
|
end
|
2013-10-23 01:26:55 +00:00
|
|
|
|
2013-11-14 11:50:27 +00:00
|
|
|
if events[:new]
|
|
|
|
lists << @new
|
2014-06-26 03:50:52 +00:00
|
|
|
set(:@count) { |c| c + @new.length }
|
|
|
|
set(:@spinner) { |spinner|
|
2013-11-16 15:05:42 +00:00
|
|
|
if e = spinner.shift
|
|
|
|
spinner.push e
|
|
|
|
end; spinner
|
|
|
|
}
|
|
|
|
@new = []
|
|
|
|
fcache.clear
|
2013-11-14 11:50:27 +00:00
|
|
|
end
|
|
|
|
end#mtx
|
|
|
|
|
|
|
|
new_search = events[:key] || events.delete(:new)
|
2013-11-16 15:05:42 +00:00
|
|
|
user_input = events[:key]
|
2013-11-14 11:50:27 +00:00
|
|
|
progress = 0
|
|
|
|
started_at = Time.now
|
|
|
|
|
2014-04-02 12:41:57 +00:00
|
|
|
if updated = new_search && !lists.empty?
|
2013-11-14 11:50:27 +00:00
|
|
|
q, cx = events.delete(:key) || [q, 0]
|
2013-11-16 15:05:42 +00:00
|
|
|
empty = matcher.empty?(q)
|
2013-12-25 16:43:20 +00:00
|
|
|
unless matches = fcache[q]
|
|
|
|
found = []
|
|
|
|
skip = false
|
|
|
|
cnt = 0
|
|
|
|
lists.each do |list|
|
|
|
|
cnt += list.length
|
2014-06-26 03:50:52 +00:00
|
|
|
skip = @evt_mtx.synchronize { @events[:key] }
|
2013-12-25 16:43:20 +00:00
|
|
|
break if skip
|
|
|
|
|
2014-06-26 03:50:52 +00:00
|
|
|
if !empty && (progress = 100 * cnt / get(:@count)) < 100 && Time.now - started_at > 0.5
|
2013-12-25 16:43:20 +00:00
|
|
|
render { print_info " (#{progress}%)" }
|
2013-11-02 11:12:39 +00:00
|
|
|
end
|
2013-12-25 16:43:20 +00:00
|
|
|
|
|
|
|
found.concat(q.empty? ? list :
|
|
|
|
matcher.match(list, q, q[0, cx], q[cx..-1]))
|
2013-11-16 15:05:42 +00:00
|
|
|
end
|
2014-06-15 09:35:47 +00:00
|
|
|
if skip
|
|
|
|
sleep 0.1
|
|
|
|
next
|
|
|
|
end
|
2014-02-02 12:41:08 +00:00
|
|
|
matches = @sort ? found : found.reverse
|
|
|
|
if !empty && @sort && matches.length <= @sort
|
2014-04-03 05:51:01 +00:00
|
|
|
matches = FZF.sort(matches)
|
2014-02-02 12:41:08 +00:00
|
|
|
end
|
|
|
|
fcache[q] = matches
|
2013-11-14 11:50:27 +00:00
|
|
|
end
|
2013-11-16 15:05:42 +00:00
|
|
|
|
|
|
|
# Atomic update
|
2014-06-26 03:50:52 +00:00
|
|
|
set(:@matches, matches)
|
2013-11-14 11:50:27 +00:00
|
|
|
end#new_search
|
2013-10-29 06:46:19 +00:00
|
|
|
|
2014-04-02 12:41:57 +00:00
|
|
|
callback = nil if callback &&
|
|
|
|
(updated || events[:loaded]) &&
|
|
|
|
callback.call(events[:loaded], matches)
|
|
|
|
|
2013-11-14 11:50:27 +00:00
|
|
|
# This small delay reduces the number of partial lists
|
|
|
|
sleep((delay = [20, delay + 5].min) * 0.01) unless user_input
|
2013-10-24 09:56:33 +00:00
|
|
|
|
2013-11-16 15:05:42 +00:00
|
|
|
update_list new_search
|
|
|
|
end#while
|
|
|
|
rescue Exception => e
|
|
|
|
@main.raise e
|
|
|
|
end
|
2014-04-02 12:41:57 +00:00
|
|
|
end
|
2013-11-16 15:05:42 +00:00
|
|
|
end
|
2013-10-29 06:46:19 +00:00
|
|
|
|
2013-11-16 15:05:42 +00:00
|
|
|
def pick
|
2014-06-26 03:50:52 +00:00
|
|
|
sync do
|
2014-11-03 14:48:37 +00:00
|
|
|
item = @matches[@ycur]
|
|
|
|
item.is_a?(Array) ? item[0] : item
|
2014-06-26 03:50:52 +00:00
|
|
|
end
|
2013-11-16 15:05:42 +00:00
|
|
|
end
|
2013-10-23 01:26:55 +00:00
|
|
|
|
2014-06-26 10:40:29 +00:00
|
|
|
def constrain offset, cursor, count, height
|
|
|
|
original = [offset, cursor]
|
|
|
|
diffpos = cursor - offset
|
|
|
|
|
|
|
|
# Constrain cursor
|
|
|
|
cursor = [0, [cursor, count - 1].min].max
|
|
|
|
|
|
|
|
# Ceil
|
|
|
|
if cursor > offset + (height - 1)
|
|
|
|
offset = cursor - (height - 1)
|
|
|
|
# Floor
|
|
|
|
elsif offset > cursor
|
|
|
|
offset = cursor
|
|
|
|
end
|
|
|
|
|
|
|
|
# Adjustment
|
|
|
|
if count - offset < height
|
|
|
|
offset = [0, count - height].max
|
|
|
|
cursor = [0, [offset + diffpos, count - 1].min].max
|
|
|
|
end
|
|
|
|
|
|
|
|
[[offset, cursor] != original, offset, cursor]
|
|
|
|
end
|
|
|
|
|
2013-11-16 15:05:42 +00:00
|
|
|
def update_list wipe
|
|
|
|
render do
|
2014-06-27 03:35:30 +00:00
|
|
|
pos, items = sync {
|
2014-06-26 10:40:29 +00:00
|
|
|
changed, @yoff, @ycur =
|
|
|
|
constrain(@yoff, @ycur, @matches.length, max_items)
|
|
|
|
wipe ||= changed
|
|
|
|
|
2014-06-27 03:35:30 +00:00
|
|
|
[@ycur - @yoff, @matches[@yoff, max_items]]
|
2014-06-26 03:50:52 +00:00
|
|
|
}
|
2013-10-23 01:26:55 +00:00
|
|
|
|
2013-11-16 15:05:42 +00:00
|
|
|
# Wipe
|
|
|
|
if items.length < @plcount
|
|
|
|
@plcount.downto(items.length) do |idx|
|
2014-05-17 13:07:18 +00:00
|
|
|
C.setpos cursor_y(idx + 2), 0
|
2013-11-16 15:05:42 +00:00
|
|
|
C.clrtoeol
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@plcount = items.length
|
|
|
|
|
2014-06-27 03:35:30 +00:00
|
|
|
dirty = Set[pos]
|
2014-06-26 03:50:52 +00:00
|
|
|
set(:@dirty) do |vs|
|
2014-06-27 03:35:30 +00:00
|
|
|
dirty.merge vs
|
2013-11-16 15:05:42 +00:00
|
|
|
Set.new
|
2014-06-26 03:50:52 +00:00
|
|
|
end
|
2013-11-16 15:05:42 +00:00
|
|
|
items.each_with_index do |item, idx|
|
2014-06-27 03:35:30 +00:00
|
|
|
next unless wipe || dirty.include?(idx)
|
2014-05-17 13:07:18 +00:00
|
|
|
row = cursor_y(idx + 2)
|
2014-06-27 03:35:30 +00:00
|
|
|
chosen = idx == pos
|
2013-11-16 15:05:42 +00:00
|
|
|
selected = @selects.include?([*item][0])
|
2014-05-28 16:08:44 +00:00
|
|
|
line, offsets = item
|
2014-06-27 03:35:30 +00:00
|
|
|
tokens = format line, C.cols - 3, offsets
|
2013-11-16 15:05:42 +00:00
|
|
|
print_item row, tokens, chosen, selected
|
|
|
|
end
|
|
|
|
print_info
|
2013-11-24 03:56:26 +00:00
|
|
|
print_input
|
2013-11-16 15:05:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def start_renderer
|
2014-04-02 12:41:57 +00:00
|
|
|
init_screen
|
|
|
|
|
2013-11-16 15:05:42 +00:00
|
|
|
Thread.new do
|
|
|
|
begin
|
|
|
|
while blk = @queue.shift
|
2014-03-06 03:21:09 +00:00
|
|
|
blk.call
|
|
|
|
refresh
|
2013-11-16 15:05:42 +00:00
|
|
|
end
|
2013-11-14 11:50:27 +00:00
|
|
|
rescue Exception => e
|
2013-11-16 15:05:42 +00:00
|
|
|
@main.raise e
|
2013-11-14 11:50:27 +00:00
|
|
|
end
|
2013-11-16 15:05:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def render &blk
|
|
|
|
@queue.push blk
|
|
|
|
nil
|
2013-11-14 11:50:27 +00:00
|
|
|
end
|
2013-10-23 01:26:55 +00:00
|
|
|
|
2014-01-28 10:02:55 +00:00
|
|
|
def vselect &prc
|
2014-06-26 03:50:52 +00:00
|
|
|
sync do
|
|
|
|
@dirty << @ycur - @yoff
|
|
|
|
@ycur = prc.call @ycur
|
|
|
|
end
|
2014-01-28 10:02:55 +00:00
|
|
|
update_list false
|
|
|
|
end
|
|
|
|
|
2014-01-28 17:08:18 +00:00
|
|
|
def num_unicode_bytes chr
|
|
|
|
# http://en.wikipedia.org/wiki/UTF-8
|
|
|
|
if chr & 0b10000000 > 0
|
|
|
|
bytes = 0
|
|
|
|
7.downto(2) do |shift|
|
|
|
|
break if (chr >> shift) & 0x1 == 0
|
|
|
|
bytes += 1
|
|
|
|
end
|
|
|
|
bytes
|
|
|
|
else
|
|
|
|
1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-07 16:55:11 +00:00
|
|
|
def read_nb chars = 1, default = nil, tries = 10
|
2014-03-07 16:28:32 +00:00
|
|
|
tries.times do |_|
|
|
|
|
begin
|
|
|
|
return @tty.read_nonblock(chars).ord
|
|
|
|
rescue Exception
|
|
|
|
sleep 0.01
|
|
|
|
end
|
|
|
|
end
|
|
|
|
default
|
2014-03-06 11:52:38 +00:00
|
|
|
end
|
|
|
|
|
2014-03-07 16:55:48 +00:00
|
|
|
def read_nbs
|
|
|
|
ords = []
|
|
|
|
while ord = read_nb
|
|
|
|
ords << ord
|
|
|
|
end
|
|
|
|
ords
|
|
|
|
end
|
|
|
|
|
2014-03-06 11:52:38 +00:00
|
|
|
def get_mouse
|
|
|
|
case ord = read_nb
|
2014-03-07 08:30:44 +00:00
|
|
|
when 32, 36, 40, 48, # mouse-down / shift / cmd / ctrl
|
|
|
|
35, 39, 43, 51 # mouse-up / shift / cmd / ctrl
|
2014-03-06 11:52:38 +00:00
|
|
|
x = read_nb - 33
|
|
|
|
y = read_nb - 33
|
|
|
|
{ :event => (ord % 2 == 0 ? :click : :release),
|
|
|
|
:x => x, :y => y, :shift => ord >= 36 }
|
2014-03-07 08:30:44 +00:00
|
|
|
when 96, 100, 104, 112, # scroll-up / shift / cmd / ctrl
|
|
|
|
97, 101, 105, 113 # scroll-down / shift / cmd / ctrl
|
2014-03-06 11:52:38 +00:00
|
|
|
read_nb(2)
|
|
|
|
{ :event => :scroll, :diff => (ord % 2 == 0 ? -1 : 1), :shift => ord >= 100 }
|
|
|
|
else
|
|
|
|
# e.g. 40, 43, 104, 105
|
|
|
|
read_nb(2)
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-06 03:21:09 +00:00
|
|
|
def get_input actions
|
2014-10-07 02:49:40 +00:00
|
|
|
@tty ||=
|
|
|
|
begin
|
|
|
|
require 'io/console'
|
|
|
|
IO.console
|
|
|
|
rescue LoadError
|
|
|
|
IO.open(IO.sysopen('/dev/tty'), 'r')
|
|
|
|
end
|
2014-03-05 02:21:20 +00:00
|
|
|
|
|
|
|
if pending = @pending
|
|
|
|
@pending = nil
|
|
|
|
return pending
|
|
|
|
end
|
|
|
|
|
2014-03-06 03:21:09 +00:00
|
|
|
str = ''
|
|
|
|
while true
|
|
|
|
ord =
|
|
|
|
if str.empty?
|
|
|
|
@tty.getc.ord
|
2014-03-05 02:21:20 +00:00
|
|
|
else
|
2014-03-06 03:21:09 +00:00
|
|
|
begin
|
|
|
|
ord = @tty.read_nonblock(1).ord
|
|
|
|
if (nb = num_unicode_bytes(ord)) > 1
|
|
|
|
ords = [ord]
|
|
|
|
(nb - 1).times do |_|
|
|
|
|
ords << @tty.read_nonblock(1).ord
|
|
|
|
end
|
|
|
|
# UTF-8 TODO Ruby 1.8
|
|
|
|
ords.pack('C*').force_encoding('UTF-8')
|
|
|
|
else
|
|
|
|
ord
|
|
|
|
end
|
|
|
|
rescue Exception
|
|
|
|
return str
|
|
|
|
end
|
2014-03-05 02:21:20 +00:00
|
|
|
end
|
2014-03-06 03:21:09 +00:00
|
|
|
|
|
|
|
ord =
|
2014-03-07 16:28:32 +00:00
|
|
|
case read_nb(1, :esc)
|
2014-05-21 01:22:17 +00:00
|
|
|
when 91, 79
|
2014-03-06 11:52:38 +00:00
|
|
|
case read_nb(1, nil)
|
2014-03-06 03:21:09 +00:00
|
|
|
when 68 then ctrl(:b)
|
|
|
|
when 67 then ctrl(:f)
|
|
|
|
when 66 then ctrl(:j)
|
|
|
|
when 65 then ctrl(:k)
|
|
|
|
when 90 then :stab
|
2014-03-07 16:28:32 +00:00
|
|
|
when 50 then read_nb; :ins
|
|
|
|
when 51 then read_nb; :del
|
|
|
|
when 53 then read_nb; :pgup
|
|
|
|
when 54 then read_nb; :pgdn
|
2014-03-07 16:55:48 +00:00
|
|
|
when 49
|
|
|
|
case read_nbs
|
|
|
|
when [59, 50, 68] then ctrl(:a)
|
|
|
|
when [59, 50, 67] then ctrl(:e)
|
2014-07-18 04:09:21 +00:00
|
|
|
when [59, 53, 68] then :alt_b
|
|
|
|
when [59, 53, 67] then :alt_f
|
2014-03-07 16:55:48 +00:00
|
|
|
when [126] then ctrl(:a)
|
|
|
|
end
|
2014-03-07 16:28:32 +00:00
|
|
|
when 52 then read_nb; ctrl(:e)
|
|
|
|
when 72 then ctrl(:a)
|
|
|
|
when 70 then ctrl(:e)
|
2014-03-06 11:52:38 +00:00
|
|
|
when 77
|
|
|
|
get_mouse
|
2014-03-06 03:21:09 +00:00
|
|
|
end
|
|
|
|
when 'b', 98 then :alt_b
|
2014-12-23 03:22:19 +00:00
|
|
|
when 'd', 100 then :alt_d
|
2014-03-06 03:21:09 +00:00
|
|
|
when 'f', 102 then :alt_f
|
|
|
|
when :esc then :esc
|
2014-12-23 16:30:45 +00:00
|
|
|
when 127 then :alt_bs
|
2014-03-06 03:21:09 +00:00
|
|
|
else next
|
|
|
|
end if ord == 27
|
|
|
|
|
2014-03-06 11:52:38 +00:00
|
|
|
return ord if ord.nil? || ord.is_a?(Hash)
|
|
|
|
|
2014-03-06 03:21:09 +00:00
|
|
|
if actions.has_key?(ord)
|
|
|
|
if str.empty?
|
|
|
|
return ord
|
|
|
|
else
|
|
|
|
@pending = ord
|
|
|
|
return str
|
|
|
|
end
|
|
|
|
else
|
|
|
|
unless ord.is_a? String
|
|
|
|
ord = [ord].pack('U*')
|
|
|
|
end
|
|
|
|
str << ord if ord =~ /[[:print:]]/
|
2014-03-05 02:21:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-06 11:52:38 +00:00
|
|
|
class MouseEvent
|
|
|
|
DOUBLE_CLICK_INTERVAL = 0.5
|
|
|
|
|
|
|
|
attr_reader :v
|
|
|
|
|
|
|
|
def initialize v = nil
|
|
|
|
@c = 0
|
|
|
|
@v = v
|
|
|
|
@t = Time.at 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def v= v
|
|
|
|
@c = (@v == v && within?) ? @c + 1 : 0
|
|
|
|
@v = v
|
|
|
|
@t = Time.now
|
|
|
|
end
|
|
|
|
|
|
|
|
def double? v
|
|
|
|
@c == 1 && @v == v && within?
|
|
|
|
end
|
|
|
|
|
|
|
|
def within?
|
|
|
|
(Time.now - @t) < DOUBLE_CLICK_INTERVAL
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-14 11:50:27 +00:00
|
|
|
def start_loop
|
|
|
|
got = nil
|
|
|
|
begin
|
2014-06-26 03:50:52 +00:00
|
|
|
input = call(:@query, :dup)
|
2014-03-15 07:54:25 +00:00
|
|
|
cursor = input.length
|
|
|
|
yanked = ''
|
|
|
|
mouse_event = MouseEvent.new
|
2013-11-16 16:19:16 +00:00
|
|
|
backword = proc {
|
2014-12-23 16:30:45 +00:00
|
|
|
cursor = (input[0, cursor].rindex(/[^[:alnum:]][[:alnum:]]/) || -1) + 1
|
2014-12-23 03:22:19 +00:00
|
|
|
nil
|
|
|
|
}
|
|
|
|
forward = proc {
|
2014-12-23 16:30:45 +00:00
|
|
|
cursor += (input[cursor..-1].index(/([[:alnum:]][^[:alnum:]])|(.$)/) || -1) + 1
|
2014-12-23 03:22:19 +00:00
|
|
|
nil
|
2013-11-16 16:19:16 +00:00
|
|
|
}
|
2014-12-23 16:30:45 +00:00
|
|
|
rubout = proc { |regex|
|
|
|
|
pcursor = cursor
|
|
|
|
cursor = (input[0, cursor].rindex(regex) || -1) + 1
|
|
|
|
if pcursor > cursor
|
|
|
|
yanked = input[cursor...pcursor]
|
|
|
|
input = input[0...cursor] + input[pcursor..-1]
|
|
|
|
end
|
|
|
|
}
|
2013-11-14 11:50:27 +00:00
|
|
|
actions = {
|
2013-11-20 12:18:51 +00:00
|
|
|
:esc => proc { exit 1 },
|
2014-06-12 14:43:09 +00:00
|
|
|
ctrl(:d) => proc {
|
|
|
|
if input.empty?
|
|
|
|
exit 1
|
|
|
|
elsif cursor < input.length
|
|
|
|
input = input[0...cursor] + input[(cursor + 1)..-1]
|
|
|
|
end
|
|
|
|
},
|
2013-11-14 11:50:27 +00:00
|
|
|
ctrl(:m) => proc {
|
2013-11-16 15:05:42 +00:00
|
|
|
got = pick
|
2013-11-14 11:50:27 +00:00
|
|
|
exit 0
|
|
|
|
},
|
2014-03-15 07:54:25 +00:00
|
|
|
ctrl(:u) => proc {
|
|
|
|
yanked = input[0...cursor] if cursor > 0
|
|
|
|
input = input[cursor..-1]
|
|
|
|
cursor = 0
|
|
|
|
},
|
2013-11-16 15:05:42 +00:00
|
|
|
ctrl(:a) => proc { cursor = 0; nil },
|
|
|
|
ctrl(:e) => proc { cursor = input.length; nil },
|
2014-06-26 03:50:52 +00:00
|
|
|
ctrl(:j) => proc { vselect { |v| v - @rev_dir } },
|
|
|
|
ctrl(:k) => proc { vselect { |v| v + @rev_dir } },
|
2014-12-23 16:30:45 +00:00
|
|
|
ctrl(:w) => proc { rubout.call /\s\S/ },
|
2014-03-15 07:54:25 +00:00
|
|
|
ctrl(:y) => proc { actions[:default].call yanked },
|
2014-01-27 17:58:20 +00:00
|
|
|
ctrl(:h) => proc { input[cursor -= 1] = '' if cursor > 0 },
|
|
|
|
ctrl(:i) => proc { |o|
|
2013-11-16 15:05:42 +00:00
|
|
|
if @multi && sel = pick
|
2014-06-26 03:50:52 +00:00
|
|
|
sync do
|
|
|
|
if @selects.has_key? sel
|
|
|
|
@selects.delete sel
|
|
|
|
else
|
2014-11-03 14:58:10 +00:00
|
|
|
@selects[sel] = sel.orig
|
2014-06-26 03:50:52 +00:00
|
|
|
end
|
2013-11-14 11:50:27 +00:00
|
|
|
end
|
2014-03-06 11:52:38 +00:00
|
|
|
vselect { |v| v + case o
|
|
|
|
when :stab then 1
|
|
|
|
when :sclick then 0
|
|
|
|
else -1
|
2014-06-26 03:50:52 +00:00
|
|
|
end * @rev_dir }
|
2013-11-16 15:05:42 +00:00
|
|
|
end
|
2013-11-14 11:50:27 +00:00
|
|
|
},
|
2014-01-27 17:58:20 +00:00
|
|
|
ctrl(:b) => proc { cursor = [0, cursor - 1].max; nil },
|
|
|
|
ctrl(:f) => proc { cursor = [input.length, cursor + 1].min; nil },
|
2014-01-31 16:56:32 +00:00
|
|
|
ctrl(:l) => proc { render { C.clear; C.refresh }; update_list true },
|
2014-06-26 03:50:52 +00:00
|
|
|
:del => proc { input[cursor] = '' if input.length > cursor },
|
|
|
|
:pgup => proc { vselect { |v| v + @rev_dir * (max_items - 1) } },
|
|
|
|
:pgdn => proc { vselect { |v| v - @rev_dir * (max_items - 1) } },
|
2014-12-23 16:30:45 +00:00
|
|
|
:alt_bs => proc { rubout.call /[^[:alnum:]][[:alnum:]]/ },
|
2014-12-23 03:22:19 +00:00
|
|
|
:alt_b => proc { backword.call },
|
|
|
|
:alt_d => proc {
|
|
|
|
pcursor = cursor
|
|
|
|
forward.call
|
|
|
|
if cursor > pcursor
|
|
|
|
yanked = input[pcursor...cursor]
|
|
|
|
input = input[0...pcursor] + input[cursor..-1]
|
|
|
|
cursor = pcursor
|
|
|
|
end
|
|
|
|
},
|
2014-06-26 03:50:52 +00:00
|
|
|
:alt_f => proc {
|
2014-12-23 03:22:19 +00:00
|
|
|
forward.call
|
2013-11-16 16:19:16 +00:00
|
|
|
},
|
2014-03-15 07:54:25 +00:00
|
|
|
:default => proc { |val|
|
|
|
|
case val
|
|
|
|
when String
|
|
|
|
input.insert cursor, val
|
|
|
|
cursor += val.length
|
|
|
|
when Hash
|
|
|
|
event = val[:event]
|
|
|
|
case event
|
|
|
|
when :click, :release
|
|
|
|
x, y, shift = val.values_at :x, :y, :shift
|
2014-06-14 19:32:21 +00:00
|
|
|
y = @reverse ? (C.lines - 1 - y) : y
|
2014-06-26 15:04:07 +00:00
|
|
|
if y == C.lines - 1
|
2014-06-30 03:00:59 +00:00
|
|
|
cursor = [0, [input.length, x - @prompt.length].min].max
|
2014-03-15 07:54:25 +00:00
|
|
|
elsif x > 1 && y <= max_items
|
2014-06-26 03:50:52 +00:00
|
|
|
tv = get(:@yoff) + max_items - y - 1
|
2014-03-15 07:54:25 +00:00
|
|
|
|
|
|
|
case event
|
|
|
|
when :click
|
|
|
|
vselect { |_| tv }
|
|
|
|
actions[ctrl(:i)].call(:sclick) if shift
|
|
|
|
mouse_event.v = tv
|
|
|
|
when :release
|
|
|
|
if !shift && mouse_event.double?(tv)
|
|
|
|
actions[ctrl(:m)].call
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
when :scroll
|
|
|
|
diff, shift = val.values_at :diff, :shift
|
|
|
|
actions[ctrl(:i)].call(:sclick) if shift
|
|
|
|
actions[ctrl(diff > 0 ? :j : :k)].call
|
|
|
|
end
|
2014-06-26 23:28:32 +00:00
|
|
|
nil
|
2014-03-15 07:54:25 +00:00
|
|
|
end
|
|
|
|
}
|
2013-11-14 11:50:27 +00:00
|
|
|
}
|
2014-03-06 03:21:09 +00:00
|
|
|
actions[ctrl(:p)] = actions[ctrl(:k)]
|
|
|
|
actions[ctrl(:n)] = actions[ctrl(:j)]
|
|
|
|
actions[:stab] = actions[ctrl(:i)]
|
|
|
|
actions[127] = actions[ctrl(:h)]
|
2014-01-29 17:51:06 +00:00
|
|
|
actions[ctrl(:q)] = actions[ctrl(:g)] = actions[ctrl(:c)] = actions[:esc]
|
2013-11-14 11:50:27 +00:00
|
|
|
|
|
|
|
while true
|
2014-06-26 03:50:52 +00:00
|
|
|
set(:@xcur, cursor)
|
2013-11-16 15:05:42 +00:00
|
|
|
render { print_input }
|
2013-10-23 01:26:55 +00:00
|
|
|
|
2014-03-06 03:21:09 +00:00
|
|
|
if key = get_input(actions)
|
2014-03-15 07:54:25 +00:00
|
|
|
upd = actions.fetch(key, actions[:default]).call(key)
|
2013-10-23 01:26:55 +00:00
|
|
|
|
2014-03-06 03:21:09 +00:00
|
|
|
# Dispatch key event
|
2014-06-26 03:50:52 +00:00
|
|
|
emit(:key) { [set(:@query, input.dup), cursor] } if upd
|
2014-03-06 03:21:09 +00:00
|
|
|
end
|
2013-10-23 01:26:55 +00:00
|
|
|
end
|
2013-11-14 11:50:27 +00:00
|
|
|
ensure
|
|
|
|
C.close_screen
|
2014-06-30 03:23:37 +00:00
|
|
|
q, selects = geta(:@query, :@selects)
|
|
|
|
@stdout.puts q if @print_query
|
2015-03-31 06:40:33 +00:00
|
|
|
@stdout.puts if @expect
|
2013-11-14 11:50:27 +00:00
|
|
|
if got
|
2014-06-26 03:50:52 +00:00
|
|
|
if selects.empty?
|
2014-11-01 04:46:24 +00:00
|
|
|
burp got
|
2013-11-15 15:31:22 +00:00
|
|
|
else
|
2014-11-03 14:58:10 +00:00
|
|
|
selects.each do |sel, orig|
|
|
|
|
burp sel, orig
|
2013-11-15 15:31:22 +00:00
|
|
|
end
|
2013-11-14 11:50:27 +00:00
|
|
|
end
|
2013-10-23 01:26:55 +00:00
|
|
|
end
|
2013-11-14 11:50:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-29 08:05:35 +00:00
|
|
|
class Matcher
|
|
|
|
class MatchData
|
|
|
|
def initialize n
|
|
|
|
@n = n
|
|
|
|
end
|
|
|
|
|
|
|
|
def offset _
|
|
|
|
@n
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize nth, delim
|
2014-06-13 15:26:55 +00:00
|
|
|
@nth = nth
|
2014-03-29 08:05:35 +00:00
|
|
|
@delim = delim
|
|
|
|
@tokens_cache = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def tokenize str
|
2014-11-01 04:46:24 +00:00
|
|
|
@tokens_cache[str] ||= str.tokenize(@delim, @nth)
|
2014-03-29 08:05:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def do_match str, pat
|
|
|
|
if @nth
|
2014-08-14 18:02:07 +00:00
|
|
|
tokenize(str).each do |pair|
|
|
|
|
prefix_length, token = pair
|
|
|
|
if md = token.match(pat) rescue nil
|
|
|
|
return MatchData.new(md.offset(0).map { |o| o + prefix_length })
|
2014-04-01 16:49:07 +00:00
|
|
|
end
|
2014-03-29 08:05:35 +00:00
|
|
|
end
|
2014-04-01 16:49:07 +00:00
|
|
|
nil
|
2014-03-29 08:05:35 +00:00
|
|
|
else
|
|
|
|
str.match(pat) rescue nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-14 11:50:27 +00:00
|
|
|
class FuzzyMatcher < Matcher
|
2013-11-15 11:40:57 +00:00
|
|
|
attr_reader :caches, :rxflag
|
2013-11-14 11:50:27 +00:00
|
|
|
|
2014-03-29 08:05:35 +00:00
|
|
|
def initialize rxflag, nth = nil, delim = nil
|
|
|
|
super nth, delim
|
2013-11-15 11:40:57 +00:00
|
|
|
@caches = Hash.new { |h, k| h[k] = {} }
|
2013-11-14 11:50:27 +00:00
|
|
|
@regexp = {}
|
|
|
|
@rxflag = rxflag
|
2013-11-03 01:43:48 +00:00
|
|
|
end
|
|
|
|
|
2013-11-15 15:58:46 +00:00
|
|
|
def empty? q
|
|
|
|
q.empty?
|
|
|
|
end
|
|
|
|
|
2013-12-20 06:30:48 +00:00
|
|
|
def rxflag_for q
|
|
|
|
@rxflag || (q =~ /[A-Z]/ ? 0 : Regexp::IGNORECASE)
|
|
|
|
end
|
|
|
|
|
2013-11-14 17:13:18 +00:00
|
|
|
def fuzzy_regex q
|
|
|
|
@regexp[q] ||= begin
|
2013-12-20 06:30:48 +00:00
|
|
|
q = q.downcase if @rxflag == Regexp::IGNORECASE
|
2014-05-28 16:08:44 +00:00
|
|
|
Regexp.new(q.split(//).inject('') { |sum, e|
|
2013-11-14 11:50:27 +00:00
|
|
|
e = Regexp.escape e
|
2013-11-17 03:32:38 +00:00
|
|
|
sum << (e.length > 1 ? "(?:#{e}).*?" : # FIXME: not equivalent
|
|
|
|
"#{e}[^#{e}]*?")
|
2013-12-20 06:30:48 +00:00
|
|
|
}, rxflag_for(q))
|
2013-10-23 01:26:55 +00:00
|
|
|
end
|
2013-11-14 17:13:18 +00:00
|
|
|
end
|
2013-10-23 01:26:55 +00:00
|
|
|
|
2013-11-14 17:13:18 +00:00
|
|
|
def match list, q, prefix, suffix
|
|
|
|
regexp = fuzzy_regex q
|
2013-11-14 11:50:27 +00:00
|
|
|
|
2013-11-15 11:40:57 +00:00
|
|
|
cache = @caches[list.object_id]
|
2013-11-14 11:50:27 +00:00
|
|
|
prefix_cache = nil
|
|
|
|
(prefix.length - 1).downto(1) do |len|
|
|
|
|
break if prefix_cache = cache[prefix[0, len]]
|
2013-10-23 01:26:55 +00:00
|
|
|
end
|
|
|
|
|
2013-11-14 11:50:27 +00:00
|
|
|
suffix_cache = nil
|
|
|
|
0.upto(suffix.length - 1) do |idx|
|
|
|
|
break if suffix_cache = cache[suffix[idx..-1]]
|
|
|
|
end unless suffix.empty?
|
|
|
|
|
|
|
|
partial_cache = [prefix_cache,
|
|
|
|
suffix_cache].compact.sort_by { |e| e.length }.first
|
|
|
|
cache[q] ||= (partial_cache ?
|
|
|
|
partial_cache.map { |e| e.first } : list).map { |line|
|
|
|
|
# Ignore errors: e.g. invalid byte sequence in UTF-8
|
2014-03-29 08:05:35 +00:00
|
|
|
md = do_match(line, regexp)
|
2013-11-14 11:50:27 +00:00
|
|
|
md && [line, [md.offset(0)]]
|
|
|
|
}.compact
|
2013-11-09 18:56:18 +00:00
|
|
|
end
|
|
|
|
end
|
2013-11-14 17:13:18 +00:00
|
|
|
|
2013-11-15 11:40:57 +00:00
|
|
|
class ExtendedFuzzyMatcher < FuzzyMatcher
|
2014-03-29 08:05:35 +00:00
|
|
|
def initialize rxflag, mode = :fuzzy, nth = nil, delim = nil
|
|
|
|
super rxflag, nth, delim
|
2013-11-15 11:40:57 +00:00
|
|
|
@regexps = {}
|
2014-03-04 12:29:45 +00:00
|
|
|
@mode = mode
|
2013-11-15 11:40:57 +00:00
|
|
|
end
|
|
|
|
|
2013-11-15 15:58:46 +00:00
|
|
|
def empty? q
|
|
|
|
parse(q).empty?
|
|
|
|
end
|
2013-11-15 11:40:57 +00:00
|
|
|
|
2013-11-15 15:58:46 +00:00
|
|
|
def parse q
|
|
|
|
q = q.strip
|
|
|
|
@regexps[q] ||= q.split(/\s+/).map { |w|
|
2013-11-14 17:13:18 +00:00
|
|
|
invert =
|
|
|
|
if w =~ /^!/
|
|
|
|
w = w[1..-1]
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2013-11-15 11:40:57 +00:00
|
|
|
[ @regexp[w] ||=
|
|
|
|
case w
|
|
|
|
when ''
|
|
|
|
nil
|
2013-12-09 05:46:06 +00:00
|
|
|
when /^\^(.*)\$$/
|
2014-05-28 16:08:44 +00:00
|
|
|
Regexp.new('^' << Regexp.escape($1) << '$', rxflag_for(w))
|
2013-11-15 15:58:46 +00:00
|
|
|
when /^'/
|
2014-03-04 12:29:45 +00:00
|
|
|
if @mode == :fuzzy && w.length > 1
|
|
|
|
exact_regex w[1..-1]
|
|
|
|
elsif @mode == :exact
|
|
|
|
exact_regex w
|
|
|
|
end
|
2013-11-15 11:40:57 +00:00
|
|
|
when /^\^/
|
|
|
|
w.length > 1 ?
|
2014-05-28 16:08:44 +00:00
|
|
|
Regexp.new('^' << Regexp.escape(w[1..-1]), rxflag_for(w)) : nil
|
2013-11-15 11:40:57 +00:00
|
|
|
when /\$$/
|
|
|
|
w.length > 1 ?
|
2014-05-28 16:08:44 +00:00
|
|
|
Regexp.new(Regexp.escape(w[0..-2]) << '$', rxflag_for(w)) : nil
|
2013-11-15 11:40:57 +00:00
|
|
|
else
|
2014-03-04 12:29:45 +00:00
|
|
|
@mode == :fuzzy ? fuzzy_regex(w) : exact_regex(w)
|
2013-11-15 11:40:57 +00:00
|
|
|
end, invert ]
|
2013-11-14 17:13:18 +00:00
|
|
|
}.select { |pair| pair.first }
|
2013-11-15 15:58:46 +00:00
|
|
|
end
|
2013-11-14 17:13:18 +00:00
|
|
|
|
2014-03-04 12:29:45 +00:00
|
|
|
def exact_regex w
|
2014-05-28 16:08:44 +00:00
|
|
|
Regexp.new(Regexp.escape(w), rxflag_for(w))
|
2014-03-04 12:29:45 +00:00
|
|
|
end
|
|
|
|
|
2013-11-15 15:58:46 +00:00
|
|
|
def match list, q, prefix, suffix
|
|
|
|
regexps = parse q
|
2013-11-15 11:40:57 +00:00
|
|
|
# Look for prefix cache
|
|
|
|
cache = @caches[list.object_id]
|
2013-11-15 15:58:46 +00:00
|
|
|
prefix = prefix.strip.sub(/\$\S*$/, '').sub(/(^|\s)!\S*$/, '')
|
2013-11-15 11:40:57 +00:00
|
|
|
prefix_cache = nil
|
|
|
|
(prefix.length - 1).downto(1) do |len|
|
|
|
|
break if prefix_cache = cache[Set[@regexps[prefix[0, len]]]]
|
|
|
|
end
|
|
|
|
|
|
|
|
cache[Set[regexps]] ||= (prefix_cache ?
|
|
|
|
prefix_cache.map { |e| e.first } :
|
|
|
|
list).map { |line|
|
2013-11-14 17:13:18 +00:00
|
|
|
offsets = []
|
|
|
|
regexps.all? { |pair|
|
|
|
|
regexp, invert = pair
|
2014-03-29 08:05:35 +00:00
|
|
|
md = do_match(line, regexp)
|
2013-11-14 17:13:18 +00:00
|
|
|
if md && !invert
|
|
|
|
offsets << md.offset(0)
|
|
|
|
elsif !md && invert
|
|
|
|
true
|
|
|
|
end
|
|
|
|
} && [line, offsets]
|
|
|
|
}.select { |e| e }
|
|
|
|
end
|
|
|
|
end
|
2013-11-14 11:50:27 +00:00
|
|
|
end#FZF
|
|
|
|
|
2013-11-16 16:37:56 +00:00
|
|
|
FZF.new(ARGV, $stdin).start if ENV.fetch('FZF_EXECUTABLE', '1') == '1'
|
2013-10-23 01:26:55 +00:00
|
|
|
|