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-03-07 08:30:44 +00:00
|
|
|
# Version: 0.8.0 (March 7, 2014)
|
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.
|
|
|
|
|
|
|
|
require 'thread'
|
|
|
|
require 'curses'
|
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
|
|
|
|
|
2013-11-14 11:50:27 +00:00
|
|
|
class FZF
|
|
|
|
C = Curses
|
2014-03-06 11:52:38 +00:00
|
|
|
attr_reader :rxflag, :sort, :color, :mouse, :multi, :query, :filter, :extended
|
2013-11-14 11:50:27 +00:00
|
|
|
|
|
|
|
class AtomicVar
|
|
|
|
def initialize value
|
|
|
|
@value = value
|
|
|
|
@mutex = Mutex.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def get
|
|
|
|
@mutex.synchronize { @value }
|
|
|
|
end
|
|
|
|
|
|
|
|
def set value = nil
|
2014-02-01 16:45:44 +00:00
|
|
|
@mutex.synchronize do
|
|
|
|
@value = block_given? ? yield(@value) : value
|
2013-10-28 06:51:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-14 11:50:27 +00:00
|
|
|
def method_missing sym, *args, &blk
|
|
|
|
@mutex.synchronize { @value.send(sym, *args, &blk) }
|
|
|
|
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
|
|
|
|
@multi = false
|
2014-03-06 11:52:38 +00:00
|
|
|
@mouse = true
|
2014-03-04 12:29:45 +00:00
|
|
|
@extended = nil
|
2014-02-01 16:45:44 +00:00
|
|
|
@filter = nil
|
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-01-22 03:03:17 +00:00
|
|
|
when '--version' then version
|
|
|
|
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-06 11:52:38 +00:00
|
|
|
when '--no-mouse' then @mouse = false
|
2014-01-22 03:03:17 +00:00
|
|
|
when '+s', '--no-sort' then @sort = nil
|
2013-11-23 10:21:02 +00:00
|
|
|
when '-q', '--query'
|
|
|
|
usage 1, 'query string required' unless query = argv.shift
|
|
|
|
@query = AtomicVar.new query.dup
|
|
|
|
when /^-q(.*)$/, /^--query=(.*)$/
|
|
|
|
@query = AtomicVar.new($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
|
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-03-04 12:29:45 +00:00
|
|
|
when '-e', '--extended-exact' then @extended = :exact
|
|
|
|
when '+e', '--no-extended-exact' then @extended = nil
|
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-01-27 17:58:20 +00:00
|
|
|
@source = source.clone
|
2013-11-14 11:50:27 +00:00
|
|
|
@mtx = Mutex.new
|
|
|
|
@cv = ConditionVariable.new
|
|
|
|
@events = {}
|
|
|
|
@new = []
|
2013-11-16 15:05:42 +00:00
|
|
|
@queue = Queue.new
|
2014-03-06 03:21:09 +00:00
|
|
|
@pending = nil
|
2014-02-01 16:45:44 +00:00
|
|
|
|
|
|
|
unless @filter
|
|
|
|
@query ||= AtomicVar.new('')
|
|
|
|
@cursor_x = AtomicVar.new(@query.length)
|
|
|
|
@matches = AtomicVar.new([])
|
|
|
|
@count = AtomicVar.new(0)
|
|
|
|
@vcursor = AtomicVar.new(0)
|
|
|
|
@vcursors = AtomicVar.new(Set.new)
|
|
|
|
@spinner = AtomicVar.new('-\|/-\|/'.split(//))
|
|
|
|
@selects = AtomicVar.new({}) # ordered >= 1.9
|
|
|
|
@main = Thread.current
|
|
|
|
@plcount = 0
|
|
|
|
end
|
2013-11-14 11:50:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def start
|
2014-02-01 16:45:44 +00:00
|
|
|
if @filter
|
|
|
|
start_reader(false).join
|
|
|
|
filter_list @new
|
|
|
|
else
|
|
|
|
@stdout = $stdout.clone
|
|
|
|
$stdout.reopen($stderr)
|
|
|
|
|
|
|
|
start_reader true
|
|
|
|
init_screen
|
|
|
|
start_renderer
|
|
|
|
start_search
|
|
|
|
start_loop
|
|
|
|
end
|
|
|
|
end
|
2013-11-14 11:50:27 +00:00
|
|
|
|
2014-02-01 16:45:44 +00:00
|
|
|
def filter_list list
|
2014-03-04 12:29:45 +00:00
|
|
|
matches = get_matcher.match(list, @filter, '', '')
|
2014-02-01 16:45:44 +00:00
|
|
|
if @sort && matches.length <= @sort
|
|
|
|
matches = sort_by_rank(matches)
|
|
|
|
end
|
|
|
|
matches.each { |m| puts m.first }
|
2013-11-14 11:50:27 +00:00
|
|
|
end
|
|
|
|
|
2014-03-04 12:29:45 +00:00
|
|
|
def get_matcher
|
|
|
|
if @extended
|
|
|
|
ExtendedFuzzyMatcher.new @rxflag, @extended
|
|
|
|
else
|
|
|
|
FuzzyMatcher.new @rxflag
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-27 17:25:24 +00:00
|
|
|
def version
|
|
|
|
File.open(__FILE__, 'r') do |f|
|
|
|
|
f.each_line do |line|
|
|
|
|
if line =~ /Version: (.*)/
|
|
|
|
$stdout.puts "fzf " << $1
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
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-01-22 03:03:17 +00:00
|
|
|
Options
|
|
|
|
-m, --multi Enable multi-select
|
|
|
|
-x, --extended Extended-search mode
|
2014-03-04 12:29:45 +00:00
|
|
|
-e, --extended-exact Extended-search mode (exact match)
|
2014-01-22 03:03:17 +00:00
|
|
|
-q, --query=STR Initial query
|
2014-02-01 16:45:44 +00:00
|
|
|
-f, --filter=STR Filter mode. Do not start interactive finder.
|
2014-01-22 03:03:17 +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.
|
|
|
|
-i Case-insensitive match (default: smart-case match)
|
|
|
|
+i Case-sensitive match
|
|
|
|
+c, --no-color Disable colors
|
2014-03-06 11:52:38 +00:00
|
|
|
--no-mouse Disable mouse
|
2014-01-22 03:03:17 +00:00
|
|
|
|
|
|
|
Environment variables
|
|
|
|
FZF_DEFAULT_COMMAND Default command to use when input is tty
|
2014-01-30 06:41:44 +00:00
|
|
|
FZF_DEFAULT_OPTS Defaults options. (e.g. "-x -m --sort 10000")] + $/ + $/
|
2013-11-14 11:50:27 +00:00
|
|
|
exit x
|
|
|
|
end
|
|
|
|
|
|
|
|
case RUBY_PLATFORM
|
|
|
|
when /darwin/
|
|
|
|
module UConv
|
|
|
|
CHOSUNG = 0x1100
|
|
|
|
JUNGSUNG = 0x1161
|
|
|
|
JONGSUNG = 0x11A7
|
|
|
|
CHOSUNGS = 19
|
|
|
|
JUNGSUNGS = 21
|
|
|
|
JONGSUNGS = 28
|
|
|
|
JJCOUNT = JUNGSUNGS * JONGSUNGS
|
|
|
|
NFC_BEGIN = 0xAC00
|
|
|
|
NFC_END = NFC_BEGIN + CHOSUNGS * JUNGSUNGS * JONGSUNGS
|
|
|
|
|
|
|
|
def self.nfd str
|
2013-11-17 03:32:38 +00:00
|
|
|
str.split(//).map do |c|
|
2013-11-14 11:50:27 +00:00
|
|
|
cp = c.ord
|
|
|
|
if cp >= NFC_BEGIN && cp < NFC_END
|
2013-11-17 03:32:38 +00:00
|
|
|
chr = ''
|
2013-11-14 11:50:27 +00:00
|
|
|
idx = cp - NFC_BEGIN
|
|
|
|
cho = CHOSUNG + idx / JJCOUNT
|
|
|
|
jung = JUNGSUNG + (idx % JJCOUNT) / JONGSUNGS
|
|
|
|
jong = JONGSUNG + idx % JONGSUNGS
|
2013-11-17 03:32:38 +00:00
|
|
|
chr << cho << jung
|
|
|
|
chr << jong if jong != JONGSUNG
|
|
|
|
chr
|
2013-10-28 06:51:58 +00:00
|
|
|
else
|
2013-11-17 03:32:38 +00:00
|
|
|
c
|
2013-10-28 06:51:58 +00:00
|
|
|
end
|
|
|
|
end
|
2013-11-14 11:50:27 +00:00
|
|
|
end
|
|
|
|
|
2013-11-15 11:40:57 +00:00
|
|
|
def self.to_nfc arr
|
|
|
|
[NFC_BEGIN + arr[0] * JJCOUNT +
|
|
|
|
(arr[1] || 0) * JONGSUNGS +
|
|
|
|
(arr[2] || 0)].pack('U*')
|
|
|
|
end
|
|
|
|
|
2013-11-15 12:49:00 +00:00
|
|
|
if String.method_defined?(:each_char)
|
|
|
|
def self.split str
|
|
|
|
str.each_char.to_a
|
|
|
|
end
|
|
|
|
else
|
|
|
|
def self.split str
|
|
|
|
str.split('')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-14 11:50:27 +00:00
|
|
|
def self.nfc str, offsets = []
|
|
|
|
ret = ''
|
|
|
|
omap = []
|
|
|
|
pend = []
|
2013-11-15 12:49:00 +00:00
|
|
|
split(str).each_with_index do |c, idx|
|
|
|
|
cp =
|
|
|
|
begin
|
|
|
|
c.ord
|
|
|
|
rescue Exception
|
|
|
|
next
|
|
|
|
end
|
2013-11-14 11:50:27 +00:00
|
|
|
omap << ret.length
|
|
|
|
unless pend.empty?
|
|
|
|
if cp >= JUNGSUNG && cp < JUNGSUNG + JUNGSUNGS
|
|
|
|
pend << cp - JUNGSUNG
|
|
|
|
next
|
|
|
|
elsif cp >= JONGSUNG && cp < JONGSUNG + JONGSUNGS
|
|
|
|
pend << cp - JONGSUNG
|
|
|
|
next
|
|
|
|
else
|
|
|
|
omap[-1] = omap[-1] + 1
|
2013-11-15 11:40:57 +00:00
|
|
|
ret << to_nfc(pend)
|
2013-11-14 11:50:27 +00:00
|
|
|
pend.clear
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if cp >= CHOSUNG && cp < CHOSUNG + CHOSUNGS
|
|
|
|
pend << cp - CHOSUNG
|
|
|
|
else
|
|
|
|
ret << c
|
|
|
|
end
|
2013-10-28 06:51:58 +00:00
|
|
|
end
|
2013-11-15 11:40:57 +00:00
|
|
|
ret << to_nfc(pend) unless pend.empty?
|
2013-11-14 11:50:27 +00:00
|
|
|
return [ret,
|
|
|
|
offsets.map { |pair|
|
|
|
|
b, e = pair
|
|
|
|
[omap[b] || 0, omap[e] || ((omap.last || 0) + 1)] }]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def convert_item item
|
|
|
|
UConv.nfc(*item)
|
|
|
|
end
|
|
|
|
|
|
|
|
class Matcher
|
2013-11-17 02:20:06 +00:00
|
|
|
def query_chars q
|
2013-11-17 03:32:38 +00:00
|
|
|
UConv.nfd(q)
|
2013-11-14 11:50:27 +00:00
|
|
|
end
|
2013-11-17 03:32:38 +00:00
|
|
|
|
|
|
|
def sanitize q
|
|
|
|
UConv.nfd(q).join
|
2013-11-17 02:47:52 +00:00
|
|
|
end
|
|
|
|
end
|
2013-11-17 03:32:38 +00:00
|
|
|
else
|
2013-11-14 11:50:27 +00:00
|
|
|
def convert_item item
|
|
|
|
item
|
|
|
|
end
|
|
|
|
|
|
|
|
class Matcher
|
2013-11-17 02:20:06 +00:00
|
|
|
def query_chars q
|
2013-11-14 11:50:27 +00:00
|
|
|
q.split(//)
|
2013-10-28 06:51:58 +00:00
|
|
|
end
|
2013-11-17 03:32:38 +00:00
|
|
|
|
|
|
|
def sanitize q
|
|
|
|
q
|
|
|
|
end
|
2013-10-28 06:51:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-14 11:50:27 +00:00
|
|
|
def emit event
|
|
|
|
@mtx.synchronize do
|
|
|
|
@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
|
|
|
|
def cursor_y; C.lines - 1; end
|
|
|
|
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
|
|
|
|
C.addstr str.gsub("\0", '')
|
|
|
|
end
|
2013-11-14 11:50:27 +00:00
|
|
|
|
|
|
|
def print_input
|
|
|
|
C.setpos cursor_y, 0
|
|
|
|
C.clrtoeol
|
2013-11-16 07:23:59 +00:00
|
|
|
cprint '> ', color(:prompt, true)
|
2013-11-14 11:50:27 +00:00
|
|
|
C.attron(C::A_BOLD) do
|
|
|
|
C.addstr @query.get
|
|
|
|
end
|
2013-10-28 06:51:58 +00:00
|
|
|
end
|
|
|
|
|
2013-11-14 11:50:27 +00:00
|
|
|
def print_info msg = nil
|
|
|
|
C.setpos cursor_y - 1, 0
|
|
|
|
C.clrtoeol
|
|
|
|
prefix =
|
2013-11-16 15:05:42 +00:00
|
|
|
if spinner = @spinner.first
|
2013-11-16 07:23:59 +00:00
|
|
|
cprint spinner, color(:spinner, true)
|
2013-11-14 11:50:27 +00:00
|
|
|
' '
|
|
|
|
else
|
|
|
|
' '
|
|
|
|
end
|
|
|
|
C.attron color(:info, false) do
|
|
|
|
C.addstr "#{prefix}#{@matches.length}/#{@count.get}"
|
|
|
|
if (selected = @selects.length) > 0
|
|
|
|
C.addstr " (#{selected})"
|
|
|
|
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
|
|
|
|
C.setpos cursor_y, 2 + width(@query[0, @cursor_x.get])
|
|
|
|
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
|
|
|
|
|
2013-11-14 11:50:27 +00:00
|
|
|
def sort_by_rank list
|
|
|
|
list.sort_by { |tuple|
|
|
|
|
line, offsets = tuple
|
2013-11-15 11:40:57 +00:00
|
|
|
matchlen = 0
|
|
|
|
pe = nil
|
|
|
|
offsets.sort.each do |pair|
|
|
|
|
b, e = pair
|
|
|
|
b = pe if pe && pe > b
|
|
|
|
pe = e
|
|
|
|
matchlen += e - b
|
|
|
|
end
|
2013-11-14 11:50:27 +00:00
|
|
|
[matchlen, line.length, line]
|
|
|
|
}
|
2013-11-02 15:06:50 +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
|
|
|
|
width -= (left ? str[0, 1] : str[-1, 1]) =~ @@wrx ? 2 : 1
|
|
|
|
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
|
|
|
|
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 =
|
|
|
|
if C.respond_to?(:use_default_colors)
|
|
|
|
C.use_default_colors
|
|
|
|
-1
|
|
|
|
else
|
|
|
|
C::COLOR_BLACK
|
|
|
|
end
|
|
|
|
C.raw
|
|
|
|
C.noecho
|
|
|
|
|
|
|
|
if @color
|
2014-02-20 04:38:04 +00:00
|
|
|
if 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-02-01 16:45:44 +00:00
|
|
|
def start_reader curses
|
2013-11-14 11:50:27 +00:00
|
|
|
stream =
|
|
|
|
if @source.tty?
|
|
|
|
if default_command = ENV['FZF_DEFAULT_COMMAND']
|
|
|
|
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
|
|
|
|
while line = stream.gets
|
|
|
|
emit(:new) { @new << line.chomp }
|
|
|
|
end
|
|
|
|
emit(:loaded) { true }
|
2014-02-01 16:45:44 +00:00
|
|
|
@spinner.clear if curses
|
2013-11-14 11:50:27 +00:00
|
|
|
end
|
|
|
|
end
|
2013-11-09 18:56:18 +00:00
|
|
|
|
2013-11-14 11:50:27 +00:00
|
|
|
def start_search
|
2014-03-04 12:29:45 +00:00
|
|
|
matcher = get_matcher
|
2013-11-14 11:50:27 +00:00
|
|
|
searcher = Thread.new {
|
|
|
|
lists = []
|
|
|
|
events = {}
|
|
|
|
fcache = {}
|
|
|
|
q = ''
|
|
|
|
delay = -5
|
|
|
|
|
|
|
|
begin
|
|
|
|
while true
|
|
|
|
@mtx.synchronize do
|
|
|
|
while true
|
|
|
|
events.merge! @events
|
|
|
|
|
|
|
|
if @events.empty? # No new events
|
|
|
|
@cv.wait @mtx
|
|
|
|
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
|
|
|
|
@count.set { |c| c + @new.length }
|
2013-11-16 15:05:42 +00:00
|
|
|
@spinner.set { |spinner|
|
|
|
|
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
|
|
|
|
|
|
|
|
if new_search && !lists.empty?
|
|
|
|
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
|
|
|
|
skip = @mtx.synchronize { @events[:key] }
|
|
|
|
break if skip
|
|
|
|
|
|
|
|
if !empty && (progress = 100 * cnt / @count.get) < 100 && Time.now - started_at > 0.5
|
|
|
|
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
|
2013-12-25 16:43:20 +00:00
|
|
|
next if skip
|
2014-02-02 12:41:08 +00:00
|
|
|
matches = @sort ? found : found.reverse
|
|
|
|
if !empty && @sort && matches.length <= @sort
|
|
|
|
matches = sort_by_rank(matches)
|
|
|
|
end
|
|
|
|
fcache[q] = matches
|
2013-11-14 11:50:27 +00:00
|
|
|
end
|
2013-11-16 15:05:42 +00:00
|
|
|
|
|
|
|
# Atomic update
|
|
|
|
@matches.set matches
|
2013-11-14 11:50:27 +00:00
|
|
|
end#new_search
|
2013-10-29 06:46:19 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
end
|
2013-10-29 06:46:19 +00:00
|
|
|
|
2013-11-16 15:05:42 +00:00
|
|
|
def pick
|
|
|
|
items = @matches[0, max_items]
|
|
|
|
curr = [0, [@vcursor.get, items.length - 1].min].max
|
|
|
|
[*items.fetch(curr, [])][0]
|
|
|
|
end
|
2013-10-23 01:26:55 +00:00
|
|
|
|
2013-11-16 15:05:42 +00:00
|
|
|
def update_list wipe
|
|
|
|
render do
|
|
|
|
items = @matches[0, max_items]
|
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|
|
|
|
|
C.setpos cursor_y - idx - 2, 0
|
|
|
|
C.clrtoeol
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@plcount = items.length
|
|
|
|
|
|
|
|
maxc = C.cols - 3
|
|
|
|
vcursor = @vcursor.set { |v| [0, [v, items.length - 1].min].max }
|
|
|
|
cleanse = Set[vcursor]
|
|
|
|
@vcursors.set { |vs|
|
|
|
|
cleanse.merge vs
|
|
|
|
Set.new
|
|
|
|
}
|
|
|
|
items.each_with_index do |item, idx|
|
|
|
|
next unless wipe || cleanse.include?(idx)
|
|
|
|
row = cursor_y - idx - 2
|
|
|
|
chosen = idx == vcursor
|
|
|
|
selected = @selects.include?([*item][0])
|
|
|
|
line, offsets = convert_item item
|
|
|
|
tokens = format line, maxc, offsets
|
|
|
|
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
|
|
|
|
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
|
|
|
|
@vcursor.set { |v| @vcursors << v; prc.call v }
|
|
|
|
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-06 11:52:38 +00:00
|
|
|
def read_nb chars = 1, default = nil
|
|
|
|
@tty.read_nonblock(chars).ord rescue default
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
@tty ||= IO.open(IO.sysopen('/dev/tty'), 'r')
|
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-06 11:52:38 +00:00
|
|
|
case ord = read_nb(1, :esc)
|
2014-03-06 03:21:09 +00:00
|
|
|
when 91
|
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-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
|
|
|
|
when 'f', 102 then :alt_f
|
|
|
|
when :esc then :esc
|
|
|
|
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
|
2013-11-23 10:21:02 +00:00
|
|
|
input = @query.get.dup
|
|
|
|
cursor = input.length
|
2013-11-16 16:19:16 +00:00
|
|
|
backword = proc {
|
|
|
|
cursor = (input[0, cursor].rindex(/\s\S/) || -1) + 1
|
|
|
|
}
|
2013-11-14 11:50:27 +00:00
|
|
|
actions = {
|
2013-11-20 12:18:51 +00:00
|
|
|
:esc => proc { exit 1 },
|
2013-11-14 11:50:27 +00:00
|
|
|
ctrl(:d) => proc { exit 1 if input.empty? },
|
|
|
|
ctrl(:m) => proc {
|
2013-11-16 15:05:42 +00:00
|
|
|
got = pick
|
2013-11-14 11:50:27 +00:00
|
|
|
exit 0
|
|
|
|
},
|
|
|
|
ctrl(:u) => proc { 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-01-28 10:02:55 +00:00
|
|
|
ctrl(:j) => proc { vselect { |v| v - 1 } },
|
|
|
|
ctrl(:k) => proc { vselect { |v| v + 1 } },
|
2013-11-14 11:50:27 +00:00
|
|
|
ctrl(:w) => proc {
|
2013-11-16 16:19:16 +00:00
|
|
|
pcursor = cursor
|
|
|
|
backword.call
|
|
|
|
input = input[0...cursor] + input[pcursor..-1]
|
2013-11-14 11:50:27 +00:00
|
|
|
},
|
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
|
|
|
|
if @selects.has_key? sel
|
|
|
|
@selects.delete sel
|
|
|
|
else
|
|
|
|
@selects[sel] = 1
|
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
|
|
|
|
end }
|
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 },
|
2013-11-16 16:19:16 +00:00
|
|
|
:alt_b => proc { backword.call; nil },
|
|
|
|
:alt_f => proc {
|
|
|
|
cursor += (input[cursor..-1].index(/(\S\s)|(.$)/) || -1) + 1
|
|
|
|
nil
|
|
|
|
},
|
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
|
|
|
|
2013-11-23 10:21:02 +00:00
|
|
|
emit(:key) { [@query.get, cursor] } unless @query.empty?
|
2014-03-06 11:52:38 +00:00
|
|
|
mouse = MouseEvent.new
|
2013-11-14 11:50:27 +00:00
|
|
|
while true
|
|
|
|
@cursor_x.set 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-06 11:52:38 +00:00
|
|
|
upd = actions.fetch(key, 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
|
|
|
|
if y == cursor_y
|
|
|
|
cursor = [0, [input.length, x - 2].min].max
|
|
|
|
elsif x > 1 && y <= max_items
|
|
|
|
tv = max_items - y - 1
|
|
|
|
|
|
|
|
case event
|
|
|
|
when :click
|
|
|
|
vselect { |_| tv }
|
|
|
|
actions[ctrl(:i)].call(:sclick) if shift
|
|
|
|
mouse.v = tv
|
|
|
|
when :release
|
|
|
|
if !shift && mouse.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
|
|
|
|
end
|
2014-03-06 03:21:09 +00:00
|
|
|
}).call(key)
|
2013-10-23 01:26:55 +00:00
|
|
|
|
2014-03-06 03:21:09 +00:00
|
|
|
# Dispatch key event
|
|
|
|
emit(:key) { [@query.set(input.dup), cursor] } if upd
|
|
|
|
end
|
2013-10-23 01:26:55 +00:00
|
|
|
end
|
2013-11-14 11:50:27 +00:00
|
|
|
ensure
|
|
|
|
C.close_screen
|
|
|
|
if got
|
2013-11-15 15:31:22 +00:00
|
|
|
if @selects.empty?
|
|
|
|
@stdout.puts got
|
|
|
|
else
|
|
|
|
@selects.each do |sel, _|
|
|
|
|
@stdout.puts sel
|
|
|
|
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
|
|
|
|
|
|
|
|
class FuzzyMatcher < Matcher
|
2013-11-15 11:40:57 +00:00
|
|
|
attr_reader :caches, :rxflag
|
2013-11-14 11:50:27 +00:00
|
|
|
|
|
|
|
def initialize rxflag
|
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
|
2013-11-17 02:20:06 +00:00
|
|
|
Regexp.new(query_chars(q).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
|
|
|
|
md = line.match(regexp) rescue nil
|
|
|
|
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-04 12:29:45 +00:00
|
|
|
def initialize rxflag, mode = :fuzzy
|
|
|
|
super rxflag
|
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 /^\^(.*)\$$/
|
2013-12-20 06:30:48 +00:00
|
|
|
Regexp.new('^' << sanitize(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 ?
|
2013-12-20 06:30:48 +00:00
|
|
|
Regexp.new('^' << sanitize(Regexp.escape(w[1..-1])), rxflag_for(w)) : nil
|
2013-11-15 11:40:57 +00:00
|
|
|
when /\$$/
|
|
|
|
w.length > 1 ?
|
2013-12-20 06:30:48 +00:00
|
|
|
Regexp.new(sanitize(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
|
|
|
|
Regexp.new(sanitize(Regexp.escape(w)), rxflag_for(w))
|
|
|
|
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
|
|
|
|
md = line.match(regexp) rescue nil
|
|
|
|
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
|
|
|
|