mirror of
https://github.com/junegunn/fzf.git
synced 2025-08-11 02:02:01 -07:00
Merge branch 'devel'
This commit is contained in:
13
README.md
13
README.md
@@ -114,6 +114,19 @@ The following readline key bindings should also work as expected.
|
|||||||
If you enable multi-select mode with `-m` option, you can select multiple items
|
If you enable multi-select mode with `-m` option, you can select multiple items
|
||||||
with TAB or Shift-TAB key.
|
with TAB or Shift-TAB key.
|
||||||
|
|
||||||
|
### Extended mode
|
||||||
|
|
||||||
|
With `-x` or `--extended` option, fzf will start in "extended mode".
|
||||||
|
|
||||||
|
In extended mode, you can specify multiple patterns delimited by spaces, such as: `^music .mp3$ sbtrkt !rmx`
|
||||||
|
|
||||||
|
| Token | Description | Match type |
|
||||||
|
| -------- | ----------------------------- | -------------------- |
|
||||||
|
| `^music` | Items that start with `music` | prefix-exact-match |
|
||||||
|
| `.mp3$` | Items that end with `.mp3` | suffix-exact-match |
|
||||||
|
| `sbtrkt` | Items that match `sbtrkt` | fuzzy-match |
|
||||||
|
| `!rmx` | Items that do not match `rmx` | invert-fuzzy-match |
|
||||||
|
|
||||||
Usage as Vim plugin
|
Usage as Vim plugin
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
|
7
Rakefile
7
Rakefile
@@ -1 +1,8 @@
|
|||||||
require "bundler/gem_tasks"
|
require "bundler/gem_tasks"
|
||||||
|
require 'rake/testtask'
|
||||||
|
|
||||||
|
Rake::TestTask.new(:test) do |test|
|
||||||
|
test.pattern = 'test/**/test_*.rb'
|
||||||
|
test.verbose = true
|
||||||
|
end
|
||||||
|
|
||||||
|
624
fzf
624
fzf
@@ -10,7 +10,7 @@
|
|||||||
# URL: https://github.com/junegunn/fzf
|
# URL: https://github.com/junegunn/fzf
|
||||||
# Author: Junegunn Choi
|
# Author: Junegunn Choi
|
||||||
# License: MIT
|
# License: MIT
|
||||||
# Last update: November 10, 2013
|
# Last update: November 15, 2013
|
||||||
#
|
#
|
||||||
# Copyright (c) 2013 Junegunn Choi
|
# Copyright (c) 2013 Junegunn Choi
|
||||||
#
|
#
|
||||||
@@ -35,52 +35,91 @@
|
|||||||
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
def usage x
|
require 'thread'
|
||||||
puts %[usage: fzf [options]
|
require 'curses'
|
||||||
|
|
||||||
|
class FZF
|
||||||
|
C = Curses
|
||||||
|
attr_reader :rxflag, :sort, :color, :multi
|
||||||
|
|
||||||
|
class AtomicVar
|
||||||
|
def initialize value
|
||||||
|
@value = value
|
||||||
|
@mutex = Mutex.new
|
||||||
|
end
|
||||||
|
|
||||||
|
def get
|
||||||
|
@mutex.synchronize { @value }
|
||||||
|
end
|
||||||
|
|
||||||
|
def set value = nil
|
||||||
|
if block_given?
|
||||||
|
@mutex.synchronize { @value = yield @value }
|
||||||
|
else
|
||||||
|
@mutex.synchronize { @value = value }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def method_missing sym, *args, &blk
|
||||||
|
@mutex.synchronize { @value.send(sym, *args, &blk) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize argv, source = $stdin
|
||||||
|
usage 0 unless (%w[--help -h] & argv).empty?
|
||||||
|
@rxflag = argv.delete('+i') ? 0 : Regexp::IGNORECASE
|
||||||
|
@sort = %w[+s --no-sort].map { |e| argv.delete e }.compact.empty? ?
|
||||||
|
ENV.fetch('FZF_DEFAULT_SORT', 500).to_i : nil
|
||||||
|
@color = %w[+c --no-color].map { |e| argv.delete e }.compact.empty?
|
||||||
|
@multi = !%w[-m --multi].map { |e| argv.delete e }.compact.empty?
|
||||||
|
@xmode = !%w[-x --extended].map { |e| argv.delete e }.compact.empty?
|
||||||
|
rest = argv.join ' '
|
||||||
|
if sort = rest.match(/(-s|--sort=?) ?([0-9]+)/)
|
||||||
|
usage 1 unless @sort
|
||||||
|
@sort = sort[2].to_i
|
||||||
|
rest = rest.delete sort[0]
|
||||||
|
end
|
||||||
|
usage 1 unless rest.empty?
|
||||||
|
|
||||||
|
@source = source
|
||||||
|
@mtx = Mutex.new
|
||||||
|
@cv = ConditionVariable.new
|
||||||
|
@events = {}
|
||||||
|
@new = []
|
||||||
|
@smtx = Mutex.new
|
||||||
|
@cursor_x = AtomicVar.new(0)
|
||||||
|
@query = AtomicVar.new('')
|
||||||
|
@matches = AtomicVar.new([])
|
||||||
|
@count = AtomicVar.new(0)
|
||||||
|
@vcursor = AtomicVar.new(0)
|
||||||
|
@fan = AtomicVar.new('-\|/-\|/'.split(//))
|
||||||
|
@selects = AtomicVar.new({}) # ordered >= 1.9
|
||||||
|
end
|
||||||
|
|
||||||
|
def start
|
||||||
|
@stdout = $stdout.clone
|
||||||
|
$stdout.reopen($stderr)
|
||||||
|
|
||||||
|
init_screen
|
||||||
|
start_reader
|
||||||
|
start_search
|
||||||
|
start_loop
|
||||||
|
end
|
||||||
|
|
||||||
|
def usage x
|
||||||
|
$stderr.puts %[usage: fzf [options]
|
||||||
|
|
||||||
-m, --multi Enable multi-select
|
-m, --multi Enable multi-select
|
||||||
|
-x, --extended Extended mode
|
||||||
-s, --sort=MAX Maximum number of matched items to sort. Default: 500.
|
-s, --sort=MAX Maximum number of matched items to sort. Default: 500.
|
||||||
+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
|
||||||
+c, --no-color Disable colors]
|
+c, --no-color Disable colors]
|
||||||
exit x
|
exit x
|
||||||
end
|
end
|
||||||
|
|
||||||
stdout = $stdout.clone
|
case RUBY_PLATFORM
|
||||||
$stdout.reopen($stderr)
|
when /darwin/
|
||||||
|
|
||||||
usage 0 unless (%w[--help -h] & ARGV).empty?
|
|
||||||
@rxflag = ARGV.delete('+i') ? 0 : Regexp::IGNORECASE
|
|
||||||
@sort = %w[+s --no-sort].map { |e| ARGV.delete e }.compact.empty? ?
|
|
||||||
ENV.fetch('FZF_DEFAULT_SORT', 500).to_i : nil
|
|
||||||
@color = %w[+c --no-color].map { |e| ARGV.delete e }.compact.empty?
|
|
||||||
@multi = !%w[-m --multi].map { |e| ARGV.delete e }.compact.empty?
|
|
||||||
rest = ARGV.join ' '
|
|
||||||
if sort = rest.match(/(-s|--sort=?) ?([0-9]+)/)
|
|
||||||
usage 1 unless @sort
|
|
||||||
@sort = sort[2].to_i
|
|
||||||
rest = rest.delete sort[0]
|
|
||||||
end
|
|
||||||
usage 1 unless rest.empty?
|
|
||||||
|
|
||||||
require 'thread'
|
|
||||||
require 'curses'
|
|
||||||
|
|
||||||
@mtx = Mutex.new
|
|
||||||
@smtx = Mutex.new
|
|
||||||
@cv = ConditionVariable.new
|
|
||||||
@lists = []
|
|
||||||
@new = []
|
|
||||||
@query = ''
|
|
||||||
@matches = []
|
|
||||||
@count = 0
|
|
||||||
@cursor_x = 0
|
|
||||||
@vcursor = 0
|
|
||||||
@events = {}
|
|
||||||
@selects = {} # ordered >= 1.9
|
|
||||||
|
|
||||||
case RUBY_PLATFORM
|
|
||||||
when /darwin/
|
|
||||||
module UConv
|
module UConv
|
||||||
CHOSUNG = 0x1100
|
CHOSUNG = 0x1100
|
||||||
JUNGSUNG = 0x1161
|
JUNGSUNG = 0x1161
|
||||||
@@ -110,12 +149,33 @@ when /darwin/
|
|||||||
ret
|
ret
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.nfc str, b = 0, e = 0
|
def self.to_nfc arr
|
||||||
|
[NFC_BEGIN + arr[0] * JJCOUNT +
|
||||||
|
(arr[1] || 0) * JONGSUNGS +
|
||||||
|
(arr[2] || 0)].pack('U*')
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
def self.nfc str, offsets = []
|
||||||
ret = ''
|
ret = ''
|
||||||
omap = []
|
omap = []
|
||||||
pend = []
|
pend = []
|
||||||
str.split(//).each_with_index do |c, idx|
|
split(str).each_with_index do |c, idx|
|
||||||
cp = c.ord
|
cp =
|
||||||
|
begin
|
||||||
|
c.ord
|
||||||
|
rescue Exception
|
||||||
|
next
|
||||||
|
end
|
||||||
omap << ret.length
|
omap << ret.length
|
||||||
unless pend.empty?
|
unless pend.empty?
|
||||||
if cp >= JUNGSUNG && cp < JUNGSUNG + JUNGSUNGS
|
if cp >= JUNGSUNG && cp < JUNGSUNG + JUNGSUNGS
|
||||||
@@ -126,9 +186,7 @@ when /darwin/
|
|||||||
next
|
next
|
||||||
else
|
else
|
||||||
omap[-1] = omap[-1] + 1
|
omap[-1] = omap[-1] + 1
|
||||||
ret << [NFC_BEGIN + pend[0] * JJCOUNT +
|
ret << to_nfc(pend)
|
||||||
(pend[1] || 0) * JONGSUNGS +
|
|
||||||
(pend[2] || 0)].pack('U*')
|
|
||||||
pend.clear
|
pend.clear
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -138,54 +196,63 @@ when /darwin/
|
|||||||
ret << c
|
ret << c
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return [ret, omap[b] || 0, omap[e] || ((omap.last || 0) + 1)]
|
ret << to_nfc(pend) unless pend.empty?
|
||||||
|
return [ret,
|
||||||
|
offsets.map { |pair|
|
||||||
|
b, e = pair
|
||||||
|
[omap[b] || 0, omap[e] || ((omap.last || 0) + 1)] }]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def convert_query q
|
|
||||||
UConv.nfd(q).split(//)
|
|
||||||
end
|
|
||||||
|
|
||||||
def convert_item item
|
def convert_item item
|
||||||
UConv.nfc(*item)
|
UConv.nfc(*item)
|
||||||
end
|
end
|
||||||
else
|
|
||||||
def convert_query q
|
|
||||||
q.split(//)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
class Matcher
|
||||||
|
def convert_query q
|
||||||
|
UConv.nfd(q).split(//)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
def convert_item item
|
def convert_item item
|
||||||
item
|
item
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def emit event
|
class Matcher
|
||||||
|
def convert_query q
|
||||||
|
q.split(//)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def emit event
|
||||||
@mtx.synchronize do
|
@mtx.synchronize do
|
||||||
@events[event] = yield
|
@events[event] = yield
|
||||||
@cv.broadcast
|
@cv.broadcast
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
C = Curses
|
def max_items; C.lines - 2; end
|
||||||
def max_items; C.lines - 2; end
|
def cursor_y; C.lines - 1; end
|
||||||
def cursor_y; C.lines - 1; end
|
def cprint str, col
|
||||||
def cprint str, col
|
|
||||||
C.attron(col) do
|
C.attron(col) do
|
||||||
C.addstr str.gsub("\0", '')
|
addstr_safe str
|
||||||
end if str
|
end if str
|
||||||
end
|
end
|
||||||
|
def addstr_safe str
|
||||||
|
C.addstr str.gsub("\0", '')
|
||||||
|
end
|
||||||
|
|
||||||
def print_input
|
def print_input
|
||||||
C.setpos cursor_y, 0
|
C.setpos cursor_y, 0
|
||||||
C.clrtoeol
|
C.clrtoeol
|
||||||
cprint '> ', color(:blue, true)
|
cprint '> ', color(:blue, true)
|
||||||
C.attron(C::A_BOLD) do
|
C.attron(C::A_BOLD) do
|
||||||
C.addstr @query
|
C.addstr @query.get
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def print_info selected, msg = nil
|
def print_info msg = nil
|
||||||
@fan ||= '-\|/-\|/'.split(//)
|
|
||||||
C.setpos cursor_y - 1, 0
|
C.setpos cursor_y - 1, 0
|
||||||
C.clrtoeol
|
C.clrtoeol
|
||||||
prefix =
|
prefix =
|
||||||
@@ -197,38 +264,118 @@ def print_info selected, msg = nil
|
|||||||
' '
|
' '
|
||||||
end
|
end
|
||||||
C.attron color(:info, false) do
|
C.attron color(:info, false) do
|
||||||
C.addstr "#{prefix}#{@matches.length}/#{@count}"
|
C.addstr "#{prefix}#{@matches.length}/#{@count.get}"
|
||||||
C.addstr " (#{selected})" if selected > 0
|
if (selected = @selects.length) > 0
|
||||||
|
C.addstr " (#{selected})"
|
||||||
|
end
|
||||||
C.addstr msg if msg
|
C.addstr msg if msg
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def refresh
|
def refresh
|
||||||
C.setpos cursor_y, 2 + width(@query[0, @cursor_x])
|
C.setpos cursor_y, 2 + width(@query[0, @cursor_x.get])
|
||||||
C.refresh
|
C.refresh
|
||||||
end
|
end
|
||||||
|
|
||||||
def ctrl char
|
def ctrl char
|
||||||
char.to_s.ord - 'a'.ord + 1
|
char.to_s.ord - 'a'.ord + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
if RUBY_VERSION.split('.').map { |e| e.rjust(3, '0') }.join > '001009'
|
def format line, limit, offsets
|
||||||
@wrx = Regexp.new '\p{Han}|\p{Katakana}|\p{Hiragana}|\p{Hangul}'
|
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
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
end
|
||||||
|
tokens << [line[index..-1], false]
|
||||||
|
tokens.reject { |pair| pair.first.empty? }
|
||||||
|
end
|
||||||
|
|
||||||
|
def print_item row, tokens, chosen, selected
|
||||||
|
# Cursor
|
||||||
|
C.setpos row, 0
|
||||||
|
C.clrtoeol
|
||||||
|
cprint chosen ? '>' : ' ', color(:red, true)
|
||||||
|
cprint selected ? '>' : ' ',
|
||||||
|
chosen ? color(:chosen) : (selected ? color(:red, true) : 0)
|
||||||
|
|
||||||
|
# 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
|
||||||
|
addstr_safe token
|
||||||
|
end
|
||||||
|
end
|
||||||
|
C.attroff color(:chosen, true) if chosen
|
||||||
|
end
|
||||||
|
|
||||||
|
def sort_by_rank list
|
||||||
|
list.sort_by { |tuple|
|
||||||
|
line, offsets = tuple
|
||||||
|
matchlen = 0
|
||||||
|
pe = nil
|
||||||
|
offsets.sort.each do |pair|
|
||||||
|
b, e = pair
|
||||||
|
b = pe if pe && pe > b
|
||||||
|
pe = e
|
||||||
|
matchlen += e - b
|
||||||
|
end
|
||||||
|
[matchlen, line.length, line]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
if RUBY_VERSION.split('.').map { |e| e.rjust(3, '0') }.join > '001009'
|
||||||
|
@@wrx = Regexp.new '\p{Han}|\p{Katakana}|\p{Hiragana}|\p{Hangul}'
|
||||||
def width str
|
def width str
|
||||||
str.gsub(@wrx, ' ').length
|
str.gsub(@@wrx, ' ').length
|
||||||
end
|
end
|
||||||
|
|
||||||
def trim str, len, left
|
def trim str, len, left
|
||||||
width = width str
|
width = width str
|
||||||
diff = 0
|
diff = 0
|
||||||
while width > len
|
while width > len
|
||||||
width -= (left ? str[0, 1] : str[-1, 1]) =~ @wrx ? 2 : 1
|
width -= (left ? str[0, 1] : str[-1, 1]) =~ @@wrx ? 2 : 1
|
||||||
str = left ? str[1..-1] : str[0...-1]
|
str = left ? str[1..-1] : str[0...-1]
|
||||||
diff += 1
|
diff += 1
|
||||||
end
|
end
|
||||||
[str, diff]
|
[str, diff]
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
def width str
|
def width str
|
||||||
str.length
|
str.length
|
||||||
end
|
end
|
||||||
@@ -242,32 +389,33 @@ else
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class String
|
class ::String
|
||||||
def ord
|
def ord
|
||||||
self.unpack('c').first
|
self.unpack('c').first
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Fixnum
|
class ::Fixnum
|
||||||
def ord
|
def ord
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
C.init_screen
|
def init_screen
|
||||||
C.start_color
|
C.init_screen
|
||||||
dbg =
|
C.start_color
|
||||||
|
dbg =
|
||||||
if C.respond_to?(:use_default_colors)
|
if C.respond_to?(:use_default_colors)
|
||||||
C.use_default_colors
|
C.use_default_colors
|
||||||
-1
|
-1
|
||||||
else
|
else
|
||||||
C::COLOR_BLACK
|
C::COLOR_BLACK
|
||||||
end
|
end
|
||||||
C.raw
|
C.raw
|
||||||
C.noecho
|
C.noecho
|
||||||
|
|
||||||
if @color
|
if @color
|
||||||
if C.can_change_color?
|
if C.can_change_color?
|
||||||
fg = ENV.fetch('FZF_FG', 252).to_i
|
fg = ENV.fetch('FZF_FG', 252).to_i
|
||||||
bg = ENV.fetch('FZF_BG', 236).to_i
|
bg = ENV.fetch('FZF_BG', 236).to_i
|
||||||
@@ -288,13 +436,13 @@ if @color
|
|||||||
C.init_pair 7, C::COLOR_RED, C::COLOR_BLACK
|
C.init_pair 7, C::COLOR_RED, C::COLOR_BLACK
|
||||||
end
|
end
|
||||||
|
|
||||||
def color sym, bold = false
|
def self.color sym, bold = false
|
||||||
C.color_pair([:blue, :match, :chosen,
|
C.color_pair([:blue, :match, :chosen,
|
||||||
:match!, :fan, :info, :red].index(sym) + 1) |
|
:match!, :fan, :info, :red].index(sym) + 1) |
|
||||||
(bold ? C::A_BOLD : 0)
|
(bold ? C::A_BOLD : 0)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
def color sym, bold = false
|
def self.color sym, bold = false
|
||||||
case sym
|
case sym
|
||||||
when :chosen
|
when :chosen
|
||||||
bold ? C::A_REVERSE : 0
|
bold ? C::A_REVERSE : 0
|
||||||
@@ -306,10 +454,12 @@ else
|
|||||||
0
|
0
|
||||||
end | (bold ? C::A_BOLD : 0)
|
end | (bold ? C::A_BOLD : 0)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
@read =
|
def start_reader
|
||||||
if $stdin.tty?
|
stream =
|
||||||
|
if @source.tty?
|
||||||
if default_command = ENV['FZF_DEFAULT_COMMAND']
|
if default_command = ENV['FZF_DEFAULT_COMMAND']
|
||||||
IO.popen(default_command)
|
IO.popen(default_command)
|
||||||
elsif !`which find`.empty?
|
elsif !`which find`.empty?
|
||||||
@@ -318,27 +468,28 @@ end
|
|||||||
exit 1
|
exit 1
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
$stdin
|
@source
|
||||||
end
|
end
|
||||||
|
|
||||||
reader = Thread.new {
|
Thread.new do
|
||||||
while line = @read.gets
|
while line = stream.gets
|
||||||
emit(:new) { @new << line.chomp }
|
emit(:new) { @new << line.chomp }
|
||||||
end
|
end
|
||||||
emit(:loaded) { true }
|
emit(:loaded) { true }
|
||||||
@smtx.synchronize { @fan = [] }
|
@fan.clear
|
||||||
}
|
end
|
||||||
|
end
|
||||||
|
|
||||||
main = Thread.current
|
def start_search
|
||||||
searcher = Thread.new {
|
main = Thread.current
|
||||||
|
matcher = (@xmode ? ExtendedFuzzyMatcher : FuzzyMatcher).new @rxflag
|
||||||
|
searcher = Thread.new {
|
||||||
|
lists = []
|
||||||
events = {}
|
events = {}
|
||||||
fcache = {}
|
fcache = {}
|
||||||
matches = []
|
|
||||||
selects = {}
|
|
||||||
mcount = 0 # match count
|
mcount = 0 # match count
|
||||||
plcount = 0 # prev list count
|
plcount = 0 # prev list count
|
||||||
q = ''
|
q = ''
|
||||||
vcursor = 0
|
|
||||||
delay = -5
|
delay = -5
|
||||||
|
|
||||||
begin
|
begin
|
||||||
@@ -356,15 +507,11 @@ searcher = Thread.new {
|
|||||||
end
|
end
|
||||||
|
|
||||||
if events[:new]
|
if events[:new]
|
||||||
@lists << [@new, {}]
|
lists << @new
|
||||||
@count += @new.length
|
@count.set { |c| c + @new.length }
|
||||||
@new = []
|
@new = []
|
||||||
fcache = {}
|
fcache = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
if events[:select]
|
|
||||||
selects = @selects.dup
|
|
||||||
end
|
|
||||||
end#mtx
|
end#mtx
|
||||||
|
|
||||||
new_search = events[:key] || events.delete(:new)
|
new_search = events[:key] || events.delete(:new)
|
||||||
@@ -372,69 +519,38 @@ searcher = Thread.new {
|
|||||||
progress = 0
|
progress = 0
|
||||||
started_at = Time.now
|
started_at = Time.now
|
||||||
|
|
||||||
if new_search && !@lists.empty?
|
if new_search && !lists.empty?
|
||||||
q = events.delete(:key) || q
|
q, cx = events.delete(:key) || [q, 0]
|
||||||
|
|
||||||
unless q.empty?
|
plcount = [@matches.length, max_items].min
|
||||||
q = q.downcase if @rxflag != 0
|
@matches.set(fcache[q] ||=
|
||||||
regexp = Regexp.new(convert_query(q).inject('') { |sum, e|
|
|
||||||
e = Regexp.escape e
|
|
||||||
sum << "#{e}[^#{e}]*?"
|
|
||||||
}, @rxflag)
|
|
||||||
end
|
|
||||||
|
|
||||||
matches = fcache[q] ||=
|
|
||||||
begin
|
begin
|
||||||
found = []
|
found = []
|
||||||
skip = false
|
skip = false
|
||||||
cnt = 0
|
cnt = 0
|
||||||
@lists.each do |pair|
|
lists.each do |list|
|
||||||
list, cache = pair
|
|
||||||
cnt += list.length
|
cnt += list.length
|
||||||
|
skip = @mtx.synchronize { @events[:key] }
|
||||||
@mtx.synchronize {
|
|
||||||
skip = @events[:key]
|
|
||||||
progress = (100 * cnt / @count)
|
|
||||||
}
|
|
||||||
break if skip
|
break if skip
|
||||||
|
|
||||||
found.concat(cache[q] ||= q.empty? ? list : begin
|
progress = (100 * cnt / @count.get)
|
||||||
if progress < 100 && Time.now - started_at > 0.5
|
if progress < 100 && Time.now - started_at > 0.5 && !q.empty?
|
||||||
@smtx.synchronize do
|
@smtx.synchronize do
|
||||||
print_info selects.length, " (#{progress}%)"
|
print_info " (#{progress}%)"
|
||||||
refresh
|
refresh
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
prefix, suffix = @query[0, @cursor_x], @query[@cursor_x..-1] || ''
|
found.concat(q.empty? ? list :
|
||||||
prefix_cache = suffix_cache = nil
|
matcher.match(list, q, q[0, cx], q[cx..-1]))
|
||||||
|
|
||||||
(prefix.length - 1).downto(1) do |len|
|
|
||||||
break if prefix_cache = cache[prefix[0, len]]
|
|
||||||
end
|
|
||||||
|
|
||||||
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
|
|
||||||
(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
|
|
||||||
end)
|
|
||||||
end
|
end
|
||||||
next if skip
|
next if skip
|
||||||
@sort ? found : found.reverse
|
@sort ? found : found.reverse
|
||||||
end
|
end)
|
||||||
|
|
||||||
mcount = matches.length
|
mcount = @matches.length
|
||||||
if @sort && mcount <= @sort && !q.empty?
|
if @sort && mcount <= @sort && !q.empty?
|
||||||
matches.replace matches.sort_by { |triple|
|
@matches.set { |m| sort_by_rank m }
|
||||||
line, b, e = triple
|
|
||||||
[b ? (e - b) : 0, line.length, line]
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
end#new_search
|
end#new_search
|
||||||
|
|
||||||
@@ -442,11 +558,7 @@ searcher = Thread.new {
|
|||||||
sleep((delay = [20, delay + 5].min) * 0.01) unless user_input
|
sleep((delay = [20, delay + 5].min) * 0.01) unless user_input
|
||||||
|
|
||||||
if events.delete(:vcursor) || new_search
|
if events.delete(:vcursor) || new_search
|
||||||
@mtx.synchronize do
|
@vcursor.set { |v| [0, [v, mcount - 1, max_items - 1].min].max }
|
||||||
plcount = [@matches.length, max_items].min
|
|
||||||
@matches = matches
|
|
||||||
vcursor = @vcursor = [0, [@vcursor, mcount - 1, max_items - 1].min].max
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Output
|
# Output
|
||||||
@@ -460,65 +572,30 @@ searcher = Thread.new {
|
|||||||
end
|
end
|
||||||
|
|
||||||
maxc = C.cols - 3
|
maxc = C.cols - 3
|
||||||
matches[0, max_items].each_with_index do |item, idx|
|
vcursor = @vcursor.get
|
||||||
|
@matches[0, max_items].each_with_index do |item, idx|
|
||||||
next if !new_search && !((vcursor-1)..(vcursor+1)).include?(idx)
|
next if !new_search && !((vcursor-1)..(vcursor+1)).include?(idx)
|
||||||
|
|
||||||
line, b, e = convert_item item
|
|
||||||
b ||= 0
|
|
||||||
e ||= 0
|
|
||||||
row = cursor_y - idx - 2
|
row = cursor_y - idx - 2
|
||||||
chosen = idx == vcursor
|
chosen = idx == vcursor
|
||||||
|
selected = @selects.include?([*item][0])
|
||||||
# Overflow
|
line, offsets = convert_item item
|
||||||
if width(line) > maxc
|
tokens = format line, maxc, offsets
|
||||||
ewidth = width(line[0...e])
|
print_item row, tokens, chosen, selected
|
||||||
# Stri..
|
|
||||||
if ewidth <= maxc - 2
|
|
||||||
line, _ = trim line, maxc - 2, false
|
|
||||||
line << '..'
|
|
||||||
# ..ring
|
|
||||||
else
|
|
||||||
# ..ri..
|
|
||||||
line = line[0...e] + '..' if ewidth < width(line) - 2
|
|
||||||
line, diff = trim line, maxc - 2, true
|
|
||||||
b += 2 - diff
|
|
||||||
e += 2 - diff
|
|
||||||
b = [2, b].max
|
|
||||||
line = '..' + line
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
C.setpos row, 0
|
print_info if !lists.empty? || events[:loaded]
|
||||||
C.clrtoeol
|
|
||||||
cprint chosen ? '>' : ' ', color(:red, true)
|
|
||||||
selected = selects.include?([*item][0])
|
|
||||||
cprint selected ? '>' : ' ',
|
|
||||||
chosen ? color(:chosen) : (selected ? color(:red, true) : 0)
|
|
||||||
|
|
||||||
C.attron color(:chosen, true) if chosen
|
|
||||||
|
|
||||||
if b < e
|
|
||||||
C.addstr line[0, b]
|
|
||||||
cprint line[b...e], color(chosen ? :match! : :match, chosen)
|
|
||||||
C.attron color(:chosen, true) if chosen
|
|
||||||
C.addstr line[e..-1] || ''
|
|
||||||
else
|
|
||||||
C.addstr line
|
|
||||||
end
|
|
||||||
C.attroff color(:chosen, true) if chosen
|
|
||||||
end
|
|
||||||
|
|
||||||
print_info selects.length if !@lists.empty? || events[:loaded]
|
|
||||||
refresh
|
refresh
|
||||||
end
|
end
|
||||||
end#while
|
end#while
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
main.raise e
|
main.raise e
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
end
|
||||||
|
|
||||||
got = nil
|
def start_loop
|
||||||
begin
|
got = nil
|
||||||
|
begin
|
||||||
tty = IO.open(IO.sysopen('/dev/tty'), 'r')
|
tty = IO.open(IO.sysopen('/dev/tty'), 'r')
|
||||||
input = ''
|
input = ''
|
||||||
cursor = 0
|
cursor = 0
|
||||||
@@ -527,16 +604,14 @@ begin
|
|||||||
ctrl(:c) => proc { exit 1 },
|
ctrl(:c) => proc { exit 1 },
|
||||||
ctrl(:d) => proc { exit 1 if input.empty? },
|
ctrl(:d) => proc { exit 1 if input.empty? },
|
||||||
ctrl(:m) => proc {
|
ctrl(:m) => proc {
|
||||||
@mtx.synchronize do
|
got = [*@matches.fetch(@vcursor.get, [])][0]
|
||||||
got = [*@matches.fetch(@vcursor, [])][0]
|
|
||||||
end
|
|
||||||
exit 0
|
exit 0
|
||||||
},
|
},
|
||||||
ctrl(:u) => proc { input = input[cursor..-1]; cursor = 0 },
|
ctrl(:u) => proc { input = input[cursor..-1]; cursor = 0 },
|
||||||
ctrl(:a) => proc { cursor = 0 },
|
ctrl(:a) => proc { cursor = 0 },
|
||||||
ctrl(:e) => proc { cursor = input.length },
|
ctrl(:e) => proc { cursor = input.length },
|
||||||
ctrl(:j) => proc { emit(:vcursor) { @vcursor -= 1 } },
|
ctrl(:j) => proc { emit(:vcursor) { @vcursor.set { |v| v - 1 } } },
|
||||||
ctrl(:k) => proc { emit(:vcursor) { @vcursor += 1 } },
|
ctrl(:k) => proc { emit(:vcursor) { @vcursor.set { |v| v + 1 } } },
|
||||||
ctrl(:w) => proc {
|
ctrl(:w) => proc {
|
||||||
ridx = (input[0...cursor - 1].rindex(/\S\s/) || -2) + 2
|
ridx = (input[0...cursor - 1].rindex(/\S\s/) || -2) + 2
|
||||||
input = input[0...ridx] + input[cursor..-1]
|
input = input[0...ridx] + input[cursor..-1]
|
||||||
@@ -545,13 +620,13 @@ begin
|
|||||||
127 => proc { input[cursor -= 1] = '' if cursor > 0 },
|
127 => proc { input[cursor -= 1] = '' if cursor > 0 },
|
||||||
9 => proc { |o|
|
9 => proc { |o|
|
||||||
emit(:select) {
|
emit(:select) {
|
||||||
if sel = [*@matches.fetch(@vcursor, [])][0]
|
if sel = [*@matches.fetch(@vcursor.get, [])][0]
|
||||||
if @selects.has_key? sel
|
if @selects.has_key? sel
|
||||||
@selects.delete sel
|
@selects.delete sel
|
||||||
else
|
else
|
||||||
@selects[sel] = 1
|
@selects[sel] = 1
|
||||||
end
|
end
|
||||||
@vcursor = [0, @vcursor + (o == :stab ? 1 : -1)].max
|
@vcursor.set { |v| [0, v + (o == :stab ? 1 : -1)].max }
|
||||||
end
|
end
|
||||||
} if @multi
|
} if @multi
|
||||||
},
|
},
|
||||||
@@ -566,9 +641,9 @@ begin
|
|||||||
actions[:stab] = actions[9]
|
actions[:stab] = actions[9]
|
||||||
|
|
||||||
while true
|
while true
|
||||||
|
@cursor_x.set cursor
|
||||||
# Update user input
|
# Update user input
|
||||||
@smtx.synchronize do
|
@smtx.synchronize do
|
||||||
@cursor_x = cursor
|
|
||||||
print_input
|
print_input
|
||||||
refresh
|
refresh
|
||||||
end
|
end
|
||||||
@@ -597,16 +672,121 @@ begin
|
|||||||
}).call(ord)
|
}).call(ord)
|
||||||
|
|
||||||
# Dispatch key event
|
# Dispatch key event
|
||||||
emit(:key) { @query = input.dup }
|
emit(:key) { [@query.set(input.dup), cursor] }
|
||||||
end
|
end
|
||||||
ensure
|
ensure
|
||||||
C.close_screen
|
C.close_screen
|
||||||
if got
|
if got
|
||||||
@selects.delete got
|
@selects.delete got
|
||||||
@selects.each do |sel, _|
|
@selects.each do |sel, _|
|
||||||
stdout.puts sel
|
@stdout.puts sel
|
||||||
|
end
|
||||||
|
@stdout.puts got
|
||||||
|
end
|
||||||
end
|
end
|
||||||
stdout.puts got
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
class FuzzyMatcher < Matcher
|
||||||
|
attr_reader :caches, :rxflag
|
||||||
|
|
||||||
|
def initialize rxflag
|
||||||
|
@caches = Hash.new { |h, k| h[k] = {} }
|
||||||
|
@regexp = {}
|
||||||
|
@rxflag = rxflag
|
||||||
|
end
|
||||||
|
|
||||||
|
def fuzzy_regex q
|
||||||
|
@regexp[q] ||= begin
|
||||||
|
q = q.downcase if @rxflag != 0
|
||||||
|
Regexp.new(convert_query(q).inject('') { |sum, e|
|
||||||
|
e = Regexp.escape e
|
||||||
|
sum << "#{e}[^#{e}]*?"
|
||||||
|
}, @rxflag)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def match list, q, prefix, suffix
|
||||||
|
regexp = fuzzy_regex q
|
||||||
|
|
||||||
|
cache = @caches[list.object_id]
|
||||||
|
prefix_cache = nil
|
||||||
|
(prefix.length - 1).downto(1) do |len|
|
||||||
|
break if prefix_cache = cache[prefix[0, len]]
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class ExtendedFuzzyMatcher < FuzzyMatcher
|
||||||
|
def initialize rxflag
|
||||||
|
super
|
||||||
|
require 'set'
|
||||||
|
@regexps = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
def match list, q, prefix, suffix
|
||||||
|
q = q.strip
|
||||||
|
|
||||||
|
regexps = @regexps[q] ||= q.split(/\s+/).map { |w|
|
||||||
|
invert =
|
||||||
|
if w =~ /^!/
|
||||||
|
w = w[1..-1]
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
[ @regexp[w] ||=
|
||||||
|
case w
|
||||||
|
when ''
|
||||||
|
nil
|
||||||
|
when /^\^/
|
||||||
|
w.length > 1 ?
|
||||||
|
Regexp.new('^' << Regexp.escape(w[1..-1]), rxflag) : nil
|
||||||
|
when /\$$/
|
||||||
|
w.length > 1 ?
|
||||||
|
Regexp.new(Regexp.escape(w[0..-2]) << '$', rxflag) : nil
|
||||||
|
else
|
||||||
|
fuzzy_regex w
|
||||||
|
end, invert ]
|
||||||
|
}.select { |pair| pair.first }
|
||||||
|
|
||||||
|
# Look for prefix cache
|
||||||
|
cache = @caches[list.object_id]
|
||||||
|
prefix = prefix.strip.sub(/\$\S+$/, '').sub(/!\S+$/, '')
|
||||||
|
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|
|
||||||
|
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
|
||||||
|
end#FZF
|
||||||
|
|
||||||
|
FZF.new(ARGV, $stdin).start if $0 == __FILE__
|
||||||
|
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
Gem::Specification.new do |spec|
|
Gem::Specification.new do |spec|
|
||||||
spec.name = 'fzf'
|
spec.name = 'fzf'
|
||||||
spec.version = '0.3.1'
|
spec.version = '0.4.0'
|
||||||
spec.authors = ['Junegunn Choi']
|
spec.authors = ['Junegunn Choi']
|
||||||
spec.email = ['junegunn.c@gmail.com']
|
spec.email = ['junegunn.c@gmail.com']
|
||||||
spec.description = %q{Fuzzy finder for your shell}
|
spec.description = %q{Fuzzy finder for your shell}
|
||||||
|
297
test/test_fzf.rb
Normal file
297
test/test_fzf.rb
Normal file
@@ -0,0 +1,297 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
# encoding: utf-8
|
||||||
|
|
||||||
|
require 'minitest/autorun'
|
||||||
|
$LOAD_PATH.unshift File.expand_path('../..', __FILE__)
|
||||||
|
load 'fzf'
|
||||||
|
|
||||||
|
class TestFZF < MiniTest::Unit::TestCase
|
||||||
|
def test_default_options
|
||||||
|
fzf = FZF.new []
|
||||||
|
assert_equal 500, fzf.sort
|
||||||
|
assert_equal false, fzf.multi
|
||||||
|
assert_equal true, fzf.color
|
||||||
|
assert_equal Regexp::IGNORECASE, fzf.rxflag
|
||||||
|
|
||||||
|
begin
|
||||||
|
ENV['FZF_DEFAULT_SORT'] = '1000'
|
||||||
|
fzf = FZF.new []
|
||||||
|
assert_equal 1000, fzf.sort
|
||||||
|
ensure
|
||||||
|
ENV.delete 'FZF_DEFAULT_SORT'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_option_parser
|
||||||
|
# Long opts
|
||||||
|
fzf = FZF.new %w[--sort=2000 --no-color --multi +i]
|
||||||
|
assert_equal 2000, fzf.sort
|
||||||
|
assert_equal true, fzf.multi
|
||||||
|
assert_equal false, fzf.color
|
||||||
|
assert_equal 0, fzf.rxflag
|
||||||
|
|
||||||
|
# Short opts
|
||||||
|
fzf = FZF.new %w[-s 2000 +c -m +i]
|
||||||
|
assert_equal 2000, fzf.sort
|
||||||
|
assert_equal true, fzf.multi
|
||||||
|
assert_equal false, fzf.color
|
||||||
|
assert_equal 0, fzf.rxflag
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_invalid_option
|
||||||
|
[%w[-s 2000 +s], %w[yo dawg]].each do |argv|
|
||||||
|
assert_raises(SystemExit) do
|
||||||
|
fzf = FZF.new argv
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# FIXME Only on 1.9 or above
|
||||||
|
def test_width
|
||||||
|
fzf = FZF.new []
|
||||||
|
assert_equal 5, fzf.width('abcde')
|
||||||
|
assert_equal 4, fzf.width('한글')
|
||||||
|
assert_equal 5, fzf.width('한글.')
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_trim
|
||||||
|
fzf = FZF.new []
|
||||||
|
assert_equal ['사.', 6], fzf.trim('가나다라마바사.', 4, true)
|
||||||
|
assert_equal ['바사.', 5], fzf.trim('가나다라마바사.', 5, true)
|
||||||
|
assert_equal ['바사.', 5], fzf.trim('가나다라마바사.', 6, true)
|
||||||
|
assert_equal ['마바사.', 4], fzf.trim('가나다라마바사.', 7, true)
|
||||||
|
assert_equal ['가나', 6], fzf.trim('가나다라마바사.', 4, false)
|
||||||
|
assert_equal ['가나', 6], fzf.trim('가나다라마바사.', 5, false)
|
||||||
|
assert_equal ['가나a', 6], fzf.trim('가나ab라마바사.', 5, false)
|
||||||
|
assert_equal ['가나ab', 5], fzf.trim('가나ab라마바사.', 6, false)
|
||||||
|
assert_equal ['가나ab', 5], fzf.trim('가나ab라마바사.', 7, false)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_format
|
||||||
|
fzf = FZF.new []
|
||||||
|
assert_equal [['01234..', false]], fzf.format('0123456789', 7, [])
|
||||||
|
assert_equal [['012', false], ['34', true], ['..', false]],
|
||||||
|
fzf.format('0123456789', 7, [[3, 5]])
|
||||||
|
assert_equal [['..56', false], ['789', true]],
|
||||||
|
fzf.format('0123456789', 7, [[7, 10]])
|
||||||
|
assert_equal [['..56', false], ['78', true], ['9', false]],
|
||||||
|
fzf.format('0123456789', 7, [[7, 9]])
|
||||||
|
|
||||||
|
(3..5).each do |i|
|
||||||
|
assert_equal [['..', false], ['567', true], ['89', false]],
|
||||||
|
fzf.format('0123456789', 7, [[i, 8]])
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_equal [['..', false], ['345', true], ['..', false]],
|
||||||
|
fzf.format('0123456789', 7, [[3, 6]])
|
||||||
|
assert_equal [['012', false], ['34', true], ['..', false]],
|
||||||
|
fzf.format('0123456789', 7, [[3, 5]])
|
||||||
|
|
||||||
|
# Multi-region
|
||||||
|
assert_equal [["0", true], ["1", false], ["2", true], ["34..", false]],
|
||||||
|
fzf.format('0123456789', 7, [[0, 1], [2, 3]])
|
||||||
|
|
||||||
|
assert_equal [["..", false], ["5", true], ["6", false], ["78", true], ["9", false]],
|
||||||
|
fzf.format('0123456789', 7, [[3, 6], [7, 9]])
|
||||||
|
|
||||||
|
assert_equal [["..", false], ["3", true], ["4", false], ["5", true], ["..", false]],
|
||||||
|
fzf.format('0123456789', 7, [[3, 4], [5, 6]])
|
||||||
|
|
||||||
|
# Multi-region Overlap
|
||||||
|
assert_equal [["..", false], ["345", true], ["..", false]],
|
||||||
|
fzf.format('0123456789', 7, [[4, 5], [3, 6]])
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_fuzzy_matcher
|
||||||
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE
|
||||||
|
list = %w[
|
||||||
|
juice
|
||||||
|
juiceful
|
||||||
|
juiceless
|
||||||
|
juicily
|
||||||
|
juiciness
|
||||||
|
juicy]
|
||||||
|
assert matcher.caches.empty?
|
||||||
|
assert_equal(
|
||||||
|
[["juice", [[0, 1]]],
|
||||||
|
["juiceful", [[0, 1]]],
|
||||||
|
["juiceless", [[0, 1]]],
|
||||||
|
["juicily", [[0, 1]]],
|
||||||
|
["juiciness", [[0, 1]]],
|
||||||
|
["juicy", [[0, 1]]]], matcher.match(list, 'j', '', '').sort)
|
||||||
|
assert !matcher.caches.empty?
|
||||||
|
assert_equal [list.object_id], matcher.caches.keys
|
||||||
|
assert_equal 1, matcher.caches[list.object_id].length
|
||||||
|
assert_equal 6, matcher.caches[list.object_id]['j'].length
|
||||||
|
|
||||||
|
assert_equal(
|
||||||
|
[["juicily", [[0, 5]]],
|
||||||
|
["juiciness", [[0, 5]]]], matcher.match(list, 'jii', '', '').sort)
|
||||||
|
|
||||||
|
assert_equal(
|
||||||
|
[["juicily", [[2, 5]]],
|
||||||
|
["juiciness", [[2, 5]]]], matcher.match(list, 'ii', '', '').sort)
|
||||||
|
|
||||||
|
assert_equal 3, matcher.caches[list.object_id].length
|
||||||
|
assert_equal 2, matcher.caches[list.object_id]['ii'].length
|
||||||
|
|
||||||
|
# TODO : partial_cache
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_fuzzy_matcher_case_sensitive
|
||||||
|
assert_equal [['Fruit', [[0, 5]]]],
|
||||||
|
FZF::FuzzyMatcher.new(0).match(%w[Fruit Grapefruit], 'Fruit', '', '').sort
|
||||||
|
|
||||||
|
assert_equal [["Fruit", [[0, 5]]], ["Grapefruit", [[5, 10]]]],
|
||||||
|
FZF::FuzzyMatcher.new(Regexp::IGNORECASE).
|
||||||
|
match(%w[Fruit Grapefruit], 'Fruit', '', '').sort
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_extended_fuzzy_matcher
|
||||||
|
matcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE
|
||||||
|
list = %w[
|
||||||
|
juice
|
||||||
|
juiceful
|
||||||
|
juiceless
|
||||||
|
juicily
|
||||||
|
juiciness
|
||||||
|
juicy
|
||||||
|
_juice]
|
||||||
|
match = proc { |q, prefix|
|
||||||
|
matcher.match(list, q, prefix, '').sort.map { |p| [p.first, p.last.sort] }
|
||||||
|
}
|
||||||
|
|
||||||
|
assert matcher.caches.empty?
|
||||||
|
3.times do
|
||||||
|
['y j', 'j y'].each do |pat|
|
||||||
|
(0..pat.length - 1).each do |prefix_length|
|
||||||
|
prefix = pat[0, prefix_length]
|
||||||
|
assert_equal(
|
||||||
|
[["juicily", [[0, 1], [6, 7]]],
|
||||||
|
["juicy", [[0, 1], [4, 5]]]],
|
||||||
|
match.call(pat, prefix))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# $
|
||||||
|
assert_equal [["juiceful", [[7, 8]]]], match.call('l$', '')
|
||||||
|
assert_equal [["juiceful", [[7, 8]]],
|
||||||
|
["juiceless", [[5, 6]]],
|
||||||
|
["juicily", [[5, 6]]]], match.call('l', '')
|
||||||
|
|
||||||
|
# ^
|
||||||
|
assert_equal list.length, match.call('j', '').length
|
||||||
|
assert_equal list.length - 1, match.call('^j', '').length
|
||||||
|
|
||||||
|
# !
|
||||||
|
assert_equal 0, match.call('!j', '').length
|
||||||
|
|
||||||
|
# ! + ^
|
||||||
|
assert_equal [["_juice", []]], match.call('!^j', '')
|
||||||
|
|
||||||
|
# ! + $
|
||||||
|
assert_equal list.length - 1, match.call('!l$', '').length
|
||||||
|
|
||||||
|
# ! + f
|
||||||
|
assert_equal [["juicy", [[4, 5]]]], match.call('y !l', '')
|
||||||
|
end
|
||||||
|
assert !matcher.caches.empty?
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_xfuzzy_matcher_prefix_cache
|
||||||
|
matcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE
|
||||||
|
list = %w[
|
||||||
|
a.java
|
||||||
|
b.java
|
||||||
|
java.jive
|
||||||
|
c.java$
|
||||||
|
d.java
|
||||||
|
]
|
||||||
|
2.times do
|
||||||
|
assert_equal 5, matcher.match(list, 'java', 'java', '').length
|
||||||
|
assert_equal 3, matcher.match(list, 'java$', 'java$', '').length
|
||||||
|
assert_equal 1, matcher.match(list, 'java$$', 'java$$', '').length
|
||||||
|
|
||||||
|
assert_equal 0, matcher.match(list, '!java', '!java', '').length
|
||||||
|
assert_equal 4, matcher.match(list, '!^jav', '!^jav', '').length
|
||||||
|
assert_equal 4, matcher.match(list, '!^java', '!^java', '').length
|
||||||
|
assert_equal 2, matcher.match(list, '!^java !b !c', '!^java', '').length
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_sort_by_rank
|
||||||
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE
|
||||||
|
xmatcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE
|
||||||
|
list = %w[
|
||||||
|
0____1
|
||||||
|
0_____1
|
||||||
|
01
|
||||||
|
____0_1
|
||||||
|
01_
|
||||||
|
_01_
|
||||||
|
0______1
|
||||||
|
___01___
|
||||||
|
]
|
||||||
|
assert_equal(
|
||||||
|
[["01", [[0, 2]]],
|
||||||
|
["01_", [[0, 2]]],
|
||||||
|
["_01_", [[1, 3]]],
|
||||||
|
["___01___", [[3, 5]]],
|
||||||
|
["____0_1", [[4, 7]]],
|
||||||
|
["0____1", [[0, 6]]],
|
||||||
|
["0_____1", [[0, 7]]],
|
||||||
|
["0______1", [[0, 8]]]],
|
||||||
|
FZF.new([]).sort_by_rank(matcher.match(list, '01', '', '')))
|
||||||
|
|
||||||
|
assert_equal(
|
||||||
|
[["01", [[0, 1], [1, 2]]],
|
||||||
|
["01_", [[0, 1], [1, 2]]],
|
||||||
|
["_01_", [[1, 2], [2, 3]]],
|
||||||
|
["0____1", [[0, 1], [5, 6]]],
|
||||||
|
["0_____1", [[0, 1], [6, 7]]],
|
||||||
|
["____0_1", [[4, 5], [6, 7]]],
|
||||||
|
["0______1", [[0, 1], [7, 8]]],
|
||||||
|
["___01___", [[3, 4], [4, 5]]]],
|
||||||
|
FZF.new([]).sort_by_rank(xmatcher.match(list, '0 1', '', '')))
|
||||||
|
|
||||||
|
assert_equal(
|
||||||
|
[["_01_", [[1, 3], [0, 4]]],
|
||||||
|
["0____1", [[0, 6], [1, 3]]],
|
||||||
|
["0_____1", [[0, 7], [1, 3]]],
|
||||||
|
["0______1", [[0, 8], [1, 3]]],
|
||||||
|
["___01___", [[3, 5], [0, 2]]],
|
||||||
|
["____0_1", [[4, 7], [0, 2]]]],
|
||||||
|
FZF.new([]).sort_by_rank(xmatcher.match(list, '01 __', '', '')))
|
||||||
|
end
|
||||||
|
|
||||||
|
if RUBY_PLATFORM =~ /darwin/
|
||||||
|
NFD = '한글'
|
||||||
|
def test_nfc
|
||||||
|
assert_equal 6, NFD.length
|
||||||
|
assert_equal ["한글", [[0, 1], [1, 2]]],
|
||||||
|
FZF::UConv.nfc(NFD, [[0, 3], [3, 6]])
|
||||||
|
|
||||||
|
nfd2 = 'before' + NFD + 'after'
|
||||||
|
assert_equal 6 + 6 + 5, nfd2.length
|
||||||
|
|
||||||
|
nfc, offsets = FZF::UConv.nfc(nfd2, [[4, 14], [9, 13]])
|
||||||
|
o1, o2 = offsets
|
||||||
|
assert_equal 'before한글after', nfc
|
||||||
|
assert_equal 're한글af', nfc[(o1.first...o1.last)]
|
||||||
|
assert_equal '글a', nfc[(o2.first...o2.last)]
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_nfd
|
||||||
|
nfc = '한글'
|
||||||
|
nfd = FZF::UConv.nfd(nfc)
|
||||||
|
assert_equal 6, nfd.length
|
||||||
|
assert_equal NFD, nfd
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_split
|
||||||
|
assert_equal ["a", "b", "c", "\xFF", "d", "e", "f"],
|
||||||
|
FZF::UConv.split("abc\xFFdef")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
Reference in New Issue
Block a user