mirror of
https://github.com/junegunn/fzf.git
synced 2025-08-03 21:52:00 -07:00
Compare commits
53 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
be3b948034 | ||
|
93dafff424 | ||
|
419bc17c0c | ||
|
f0a5757244 | ||
|
f0b2b98c5d | ||
|
4530819539 | ||
|
1825a73e2e | ||
|
30d4974509 | ||
|
e4a49dbb2a | ||
|
76c7f4f9c0 | ||
|
8ae604af67 | ||
|
6037e1e217 | ||
|
43acf5c8a4 | ||
|
545e8bfcee | ||
|
90ad6d50b8 | ||
|
67bdc3a0ad | ||
|
ff34c6b272 | ||
|
b2ac52462c | ||
|
a6f7caf20d | ||
|
1e9e597837 | ||
|
0dc725d09c | ||
|
1eceb6a4b9 | ||
|
8777a495bc | ||
|
83825dbbd3 | ||
|
1ac19a2097 | ||
|
833c6e1eeb | ||
|
8a0a3f9bf5 | ||
|
ddf6e5ef1e | ||
|
11a1010e9e | ||
|
75b666bf54 | ||
|
3f73554a9e | ||
|
dc67420319 | ||
|
f2d8e7e3ee | ||
|
de8116b1cf | ||
|
1460e0a10b | ||
|
c46dad465f | ||
|
0df647b2a7 | ||
|
69d6b58f88 | ||
|
8e305edcf2 | ||
|
c326e363eb | ||
|
d1298b8fff | ||
|
2a0e0ded2a | ||
|
7cecf648eb | ||
|
c3c94ea889 | ||
|
94f0c3d22b | ||
|
d717096ee3 | ||
|
1629fe079a | ||
|
6a9970c98e | ||
|
682583e88f | ||
|
fd2472d11c | ||
|
311c4a36e2 | ||
|
b98fba4cf1 | ||
|
a03b5c8c42 |
4
Gemfile
4
Gemfile
@@ -1,4 +0,0 @@
|
|||||||
source 'https://rubygems.org'
|
|
||||||
|
|
||||||
# Specify your gem's dependencies in fzf.gemspec
|
|
||||||
gemspec
|
|
139
README.md
139
README.md
@@ -16,7 +16,8 @@ fzf requires Ruby (>= 1.8.5).
|
|||||||
Installation
|
Installation
|
||||||
------------
|
------------
|
||||||
|
|
||||||
Download fzf executable and put it somewhere in your search $PATH.
|
Download [fzf executable](https://raw.github.com/junegunn/fzf/master/fzf) and
|
||||||
|
put it somewhere in your search $PATH.
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
mkdir -p ~/bin
|
mkdir -p ~/bin
|
||||||
@@ -38,15 +39,21 @@ Make sure that ~/bin is included in $PATH.
|
|||||||
export PATH=$PATH:~/bin
|
export PATH=$PATH:~/bin
|
||||||
```
|
```
|
||||||
|
|
||||||
Install as Vim plugin
|
### Install as Ruby gem
|
||||||
---------------------
|
|
||||||
|
|
||||||
fzf was not designed to be a Vim plugin, but you can use it as one. The only
|
fzf can be installed as a Ruby gem
|
||||||
reason one might consider using fzf in Vim is its speed. For a very large list
|
|
||||||
of files, fzf is significantly faster than native Vim plugins.
|
|
||||||
|
|
||||||
You can use any Vim plugin manager to install fzf as a Vim plugin. If you don't
|
```
|
||||||
use one, I recommend you try [vim-plug](https://github.com/junegunn/vim-plug).
|
gem install fzf
|
||||||
|
```
|
||||||
|
|
||||||
|
It's a bit easier to install and update the script but the Ruby gem version
|
||||||
|
takes slightly longer to start.
|
||||||
|
|
||||||
|
### Install as Vim plugin
|
||||||
|
|
||||||
|
You can use any Vim plugin manager to install fzf for Vim. If you don't use one,
|
||||||
|
I recommend you try [vim-plug](https://github.com/junegunn/vim-plug).
|
||||||
|
|
||||||
1. [Install vim-plug](https://github.com/junegunn/vim-plug#usage)
|
1. [Install vim-plug](https://github.com/junegunn/vim-plug#usage)
|
||||||
2. Edit your .vimrc
|
2. Edit your .vimrc
|
||||||
@@ -64,9 +71,12 @@ Usage
|
|||||||
```
|
```
|
||||||
usage: fzf [options]
|
usage: fzf [options]
|
||||||
|
|
||||||
-s, --sort=MAX Maximum number of matched items to sort. Default: 500
|
-m, --multi Enable multi-select
|
||||||
+s, --no-sort Keep the sequence unchanged.
|
-x, --extended Extended-search mode
|
||||||
|
-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-sensitive match
|
+i Case-sensitive match
|
||||||
|
+c, --no-color Disable colors
|
||||||
```
|
```
|
||||||
|
|
||||||
fzf will launch curses-based finder, read the list from STDIN, and write the
|
fzf will launch curses-based finder, read the list from STDIN, and write the
|
||||||
@@ -77,10 +87,11 @@ find * -type f | fzf > selected
|
|||||||
```
|
```
|
||||||
|
|
||||||
Without STDIN pipe, fzf will use find command to fetch the list of
|
Without STDIN pipe, fzf will use find command to fetch the list of
|
||||||
files (excluding hidden ones).
|
files excluding hidden ones. (You can override the default command with
|
||||||
|
`FZF_DEFAULT_COMMAND`)
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
vim `fzf`
|
vim $(fzf)
|
||||||
```
|
```
|
||||||
|
|
||||||
If you want to preserve the exact sequence of the input, provide `--no-sort` (or
|
If you want to preserve the exact sequence of the input, provide `--no-sort` (or
|
||||||
@@ -100,6 +111,26 @@ The following readline key bindings should also work as expected.
|
|||||||
- CTRL-A / CTRL-E
|
- CTRL-A / CTRL-E
|
||||||
- CTRL-B / CTRL-F
|
- CTRL-B / CTRL-F
|
||||||
- CTRL-W / CTRL-U
|
- CTRL-W / CTRL-U
|
||||||
|
- ALT-B / ALT-F
|
||||||
|
|
||||||
|
If you enable multi-select mode with `-m` option, you can select multiple items
|
||||||
|
with TAB or Shift-TAB key.
|
||||||
|
|
||||||
|
### Extended-search mode
|
||||||
|
|
||||||
|
With `-x` or `--extended` option, fzf will start in "extended-search mode".
|
||||||
|
|
||||||
|
In this 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` | inverse-fuzzy-match |
|
||||||
|
| `'wild` | Items that include `wild` | exact-match (quoted) |
|
||||||
|
| `!'fire` | Items that do not include `fire` | inverse-exact-match |
|
||||||
|
|
||||||
Usage as Vim plugin
|
Usage as Vim plugin
|
||||||
-------------------
|
-------------------
|
||||||
@@ -117,23 +148,27 @@ You can override the command which produces input to fzf.
|
|||||||
let g:fzf_command = 'find . -type f'
|
let g:fzf_command = 'find . -type f'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Most of the time, you will prefer native Vim plugins with better integration
|
||||||
|
with Vim. The only reason one might consider using fzf in Vim is its speed. For
|
||||||
|
a very large list of files, fzf is significantly faster and it does not block.
|
||||||
|
|
||||||
Useful bash examples
|
Useful bash examples
|
||||||
--------------------
|
--------------------
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
# vimf - Open selected file in Vim
|
# vimf - Open selected file in Vim
|
||||||
vimf() {
|
vimf() {
|
||||||
FILE=`fzf` && vim "$FILE"
|
FILE=$(fzf) && vim "$FILE"
|
||||||
}
|
}
|
||||||
|
|
||||||
# fd - cd to selected directory
|
# fd - cd to selected directory
|
||||||
fd() {
|
fd() {
|
||||||
DIR=`find ${1:-*} -path '*/\.*' -prune -o -type d -print 2> /dev/null | fzf` && cd "$DIR"
|
DIR=$(find ${1:-*} -path '*/\.*' -prune -o -type d -print 2> /dev/null | fzf) && cd "$DIR"
|
||||||
}
|
}
|
||||||
|
|
||||||
# fda - including hidden directories
|
# fda - including hidden directories
|
||||||
fda() {
|
fda() {
|
||||||
DIR=`find ${1:-*} -type d 2> /dev/null | fzf` && cd "$DIR"
|
DIR=$(find ${1:-*} -type d 2> /dev/null | fzf) && cd "$DIR"
|
||||||
}
|
}
|
||||||
|
|
||||||
# fh - repeat history
|
# fh - repeat history
|
||||||
@@ -143,14 +178,84 @@ fh() {
|
|||||||
|
|
||||||
# fkill - kill process
|
# fkill - kill process
|
||||||
fkill() {
|
fkill() {
|
||||||
ps -ef | sed 1d | fzf | awk '{print $2}' | xargs kill -${1:-9}
|
ps -ef | sed 1d | fzf -m | awk '{print $2}' | xargs kill -${1:-9}
|
||||||
}
|
}
|
||||||
|
|
||||||
# CTRL-T - Open fuzzy finder and paste the selected item to the command line
|
# (Assuming you don't use the default CTRL-T and CTRL-R)
|
||||||
|
|
||||||
|
# CTRL-T - Paste the selected file path into the command line
|
||||||
bind '"\er": redraw-current-line'
|
bind '"\er": redraw-current-line'
|
||||||
bind '"\C-t": " \C-u \C-a\C-k$(fzf)\e\C-e\C-y\C-a\C-y\ey\C-h\C-e\er"'
|
bind '"\C-t": " \C-u \C-a\C-k$(fzf)\e\C-e\C-y\C-a\C-y\ey\C-h\C-e\er"'
|
||||||
|
|
||||||
|
# CTRL-R - Paste the selected command from history into the command line
|
||||||
|
bind '"\C-r": " \C-e\C-u$(history | fzf +s | sed \"s/ *[0-9]* *//\")\e\C-e\er"'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
zsh widgets
|
||||||
|
-----------
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# CTRL-T - Paste the selected file(s) path into the command line
|
||||||
|
fzf-file-widget() {
|
||||||
|
local FILES
|
||||||
|
local IFS="
|
||||||
|
"
|
||||||
|
FILES=($(
|
||||||
|
find * -path '*/\.*' -prune \
|
||||||
|
-o -type f -print \
|
||||||
|
-o -type l -print 2> /dev/null | fzf -m))
|
||||||
|
unset IFS
|
||||||
|
FILES=$FILES:q
|
||||||
|
LBUFFER="${LBUFFER%% #} $FILES"
|
||||||
|
zle redisplay
|
||||||
|
}
|
||||||
|
zle -N fzf-file-widget
|
||||||
|
bindkey '^T' fzf-file-widget
|
||||||
|
|
||||||
|
# ALT-C - cd into the selected directory
|
||||||
|
fzf-cd-widget() {
|
||||||
|
cd "${$(find * -path '*/\.*' -prune \
|
||||||
|
-o -type d -print 2> /dev/null | fzf):-.}"
|
||||||
|
zle reset-prompt
|
||||||
|
}
|
||||||
|
zle -N fzf-cd-widget
|
||||||
|
bindkey '\ec' fzf-cd-widget
|
||||||
|
|
||||||
|
# CTRL-R - Paste the selected command from history into the command line
|
||||||
|
fzf-history-widget() {
|
||||||
|
LBUFFER=$(history | fzf +s | sed "s/ *[0-9]* *//")
|
||||||
|
zle redisplay
|
||||||
|
}
|
||||||
|
zle -N fzf-history-widget
|
||||||
|
bindkey '^R' fzf-history-widget
|
||||||
|
```
|
||||||
|
|
||||||
|
Tips
|
||||||
|
----
|
||||||
|
|
||||||
|
### Faster startup with `--disable-gems` options
|
||||||
|
|
||||||
|
If you're running Ruby 1.9 or above, you can improve the startup time with
|
||||||
|
`--disable-gems` option to Ruby.
|
||||||
|
|
||||||
|
- `time ruby ~/bin/fzf -h`
|
||||||
|
- 0.077 sec
|
||||||
|
- `time ruby --disable-gems ~/bin/fzf -h`
|
||||||
|
- 0.025 sec
|
||||||
|
|
||||||
|
Define fzf alias with the option as follows:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
alias fzf='ruby --disable-gems ~/bin/fzf'
|
||||||
|
```
|
||||||
|
|
||||||
|
### Incorrect display on Ruby 1.8
|
||||||
|
|
||||||
|
It is reported that the output of fzf can become unreadable on some terminals
|
||||||
|
when it's running on Ruby 1.8. If you experience the problem, upgrade your Ruby
|
||||||
|
to 1.9 or above. Ruby 1.9 or above is also required for displaying Unicode
|
||||||
|
characters.
|
||||||
|
|
||||||
License
|
License
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
757
fzf
757
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: October 29, 2013
|
# Last update: November 17, 2013
|
||||||
#
|
#
|
||||||
# Copyright (c) 2013 Junegunn Choi
|
# Copyright (c) 2013 Junegunn Choi
|
||||||
#
|
#
|
||||||
@@ -35,22 +35,46 @@
|
|||||||
# 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'
|
||||||
|
require 'set'
|
||||||
|
|
||||||
-s, --sort=MAX Maximum number of matched items to sort. Default: 500.
|
class FZF
|
||||||
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
C = Curses
|
||||||
+i Case-sensitive match]
|
attr_reader :rxflag, :sort, :color, :multi
|
||||||
exit x
|
|
||||||
|
class AtomicVar
|
||||||
|
def initialize value
|
||||||
|
@value = value
|
||||||
|
@mutex = Mutex.new
|
||||||
end
|
end
|
||||||
|
|
||||||
stdout = $stdout.clone
|
def get
|
||||||
$stdout.reopen($stderr)
|
@mutex.synchronize { @value }
|
||||||
|
end
|
||||||
|
|
||||||
usage 0 unless (%w[--help -h] & ARGV).empty?
|
def set value = nil
|
||||||
@rxflag = ARGV.delete('+i') ? 0 : Regexp::IGNORECASE
|
if block_given?
|
||||||
@sort = (ARGV.delete('+s') || ARGV.delete('--no-sort')) ? nil : 500
|
@mutex.synchronize { @value = yield @value }
|
||||||
rest = ARGV.join ' '
|
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', 1000).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]+)/)
|
if sort = rest.match(/(-s|--sort=?) ?([0-9]+)/)
|
||||||
usage 1 unless @sort
|
usage 1 unless @sort
|
||||||
@sort = sort[2].to_i
|
@sort = sort[2].to_i
|
||||||
@@ -58,20 +82,46 @@ if sort = rest.match(/(-s|--sort=?) ?([0-9]+)/)
|
|||||||
end
|
end
|
||||||
usage 1 unless rest.empty?
|
usage 1 unless rest.empty?
|
||||||
|
|
||||||
require 'thread'
|
@source = source
|
||||||
require 'curses'
|
|
||||||
|
|
||||||
@mtx = Mutex.new
|
@mtx = Mutex.new
|
||||||
@smtx = Mutex.new
|
|
||||||
@cv = ConditionVariable.new
|
@cv = ConditionVariable.new
|
||||||
@lists = []
|
|
||||||
@new = []
|
|
||||||
@query = ''
|
|
||||||
@matches = []
|
|
||||||
@count = 0
|
|
||||||
@cursor_x = 0
|
|
||||||
@vcursor = 0
|
|
||||||
@events = {}
|
@events = {}
|
||||||
|
@new = []
|
||||||
|
@queue = Queue.new
|
||||||
|
@cursor_x = AtomicVar.new(0)
|
||||||
|
@query = AtomicVar.new('')
|
||||||
|
@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
|
||||||
|
@stdout = $stdout.clone
|
||||||
|
@plcount = 0
|
||||||
|
end
|
||||||
|
|
||||||
|
def start
|
||||||
|
$stdout.reopen($stderr)
|
||||||
|
|
||||||
|
init_screen
|
||||||
|
start_reader
|
||||||
|
start_renderer
|
||||||
|
start_search
|
||||||
|
start_loop
|
||||||
|
end
|
||||||
|
|
||||||
|
def usage x
|
||||||
|
$stderr.puts %[usage: fzf [options]
|
||||||
|
|
||||||
|
-m, --multi Enable multi-select
|
||||||
|
-x, --extended Extended-search mode
|
||||||
|
-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-sensitive match
|
||||||
|
+c, --no-color Disable colors]
|
||||||
|
exit x
|
||||||
|
end
|
||||||
|
|
||||||
case RUBY_PLATFORM
|
case RUBY_PLATFORM
|
||||||
when /darwin/
|
when /darwin/
|
||||||
@@ -104,12 +154,33 @@ when /darwin/
|
|||||||
ret
|
ret
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.nfc str, offset
|
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
|
||||||
@@ -120,9 +191,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
|
||||||
@@ -132,24 +201,32 @@ when /darwin/
|
|||||||
ret << c
|
ret << c
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return [ret, offset.map { |o| omap[o] || (omap.last + 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
|
||||||
|
|
||||||
|
class Matcher
|
||||||
|
def convert_query q
|
||||||
|
UConv.nfd(q).split(//)
|
||||||
|
end
|
||||||
|
end
|
||||||
else
|
else
|
||||||
|
def convert_item item
|
||||||
|
item
|
||||||
|
end
|
||||||
|
|
||||||
|
class Matcher
|
||||||
def convert_query q
|
def convert_query q
|
||||||
q.split(//)
|
q.split(//)
|
||||||
end
|
end
|
||||||
|
|
||||||
def convert_item item
|
|
||||||
item
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -160,45 +237,47 @@ def emit event
|
|||||||
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
|
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(:prompt, 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 progress = true, 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 =
|
||||||
if fan = @fan.shift
|
if spinner = @spinner.first
|
||||||
@fan.push fan
|
cprint spinner, color(:spinner, true)
|
||||||
cprint fan, color(:fan, true)
|
|
||||||
' '
|
' '
|
||||||
else
|
else
|
||||||
' '
|
' '
|
||||||
end
|
end
|
||||||
C.attron color(:info, false) do
|
C.attron color(:info, false) do
|
||||||
progress &&= "#{prefix}#{@matches.length}/#{@count}"
|
C.addstr "#{prefix}#{@matches.length}/#{@count.get}"
|
||||||
C.addstr progress if progress
|
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 + ulen(@query[0, @cursor_x])
|
C.setpos cursor_y, 2 + width(@query[0, @cursor_x.get])
|
||||||
C.refresh
|
C.refresh
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -206,29 +285,128 @@ def ctrl char
|
|||||||
char.to_s.ord - 'a'.ord + 1
|
char.to_s.ord - 'a'.ord + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
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(:cursor, true)
|
||||||
|
cprint selected ? '>' : ' ',
|
||||||
|
chosen ? color(:chosen) : (selected ? color(:selected, 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'
|
if RUBY_VERSION.split('.').map { |e| e.rjust(3, '0') }.join > '001009'
|
||||||
def ulen str
|
@@wrx = Regexp.new '\p{Han}|\p{Katakana}|\p{Hiragana}|\p{Hangul}'
|
||||||
@urx ||= Regexp.new '\p{Han}|\p{Katakana}|\p{Hiragana}|\p{Hangul}'
|
def width str
|
||||||
str.gsub(@urx, ' ').length
|
str.gsub(@@wrx, ' ').length
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
end
|
||||||
else
|
else
|
||||||
def ulen str
|
def width str
|
||||||
str.length
|
str.length
|
||||||
end
|
end
|
||||||
|
|
||||||
class String
|
def trim str, len, left
|
||||||
|
diff = str.length - len
|
||||||
|
if diff > 0
|
||||||
|
[left ? str[diff..-1] : str[0...-diff], diff]
|
||||||
|
else
|
||||||
|
[str, 0]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
def init_screen
|
||||||
C.init_screen
|
C.init_screen
|
||||||
C.start_color
|
C.start_color
|
||||||
dbg =
|
dbg =
|
||||||
@@ -240,16 +418,17 @@ dbg =
|
|||||||
end
|
end
|
||||||
C.raw
|
C.raw
|
||||||
C.noecho
|
C.noecho
|
||||||
|
|
||||||
|
if @color
|
||||||
if C.can_change_color?
|
if C.can_change_color?
|
||||||
fg = ENV.fetch('FZF_FG', 252).to_i
|
|
||||||
bg = ENV.fetch('FZF_BG', 236).to_i
|
|
||||||
C.init_pair 1, 110, dbg
|
C.init_pair 1, 110, dbg
|
||||||
C.init_pair 2, 108, dbg
|
C.init_pair 2, 108, dbg
|
||||||
C.init_pair 3, fg + 2, bg
|
C.init_pair 3, 254, 236
|
||||||
C.init_pair 4, 151, bg
|
C.init_pair 4, 151, 236
|
||||||
C.init_pair 5, 148, dbg
|
C.init_pair 5, 148, dbg
|
||||||
C.init_pair 6, 144, dbg
|
C.init_pair 6, 144, dbg
|
||||||
C.init_pair 7, 161, bg
|
C.init_pair 7, 161, 236
|
||||||
|
C.init_pair 8, 168, 236
|
||||||
else
|
else
|
||||||
C.init_pair 1, C::COLOR_BLUE, dbg
|
C.init_pair 1, C::COLOR_BLUE, dbg
|
||||||
C.init_pair 2, C::COLOR_GREEN, dbg
|
C.init_pair 2, C::COLOR_GREEN, dbg
|
||||||
@@ -258,52 +437,67 @@ else
|
|||||||
C.init_pair 5, C::COLOR_GREEN, dbg
|
C.init_pair 5, C::COLOR_GREEN, dbg
|
||||||
C.init_pair 6, C::COLOR_WHITE, dbg
|
C.init_pair 6, C::COLOR_WHITE, dbg
|
||||||
C.init_pair 7, C::COLOR_RED, C::COLOR_BLACK
|
C.init_pair 7, C::COLOR_RED, C::COLOR_BLACK
|
||||||
|
C.init_pair 8, C::COLOR_MAGENTA, 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([:prompt, :match, :chosen, :match!,
|
||||||
:match!, :fan, :info, :red].index(sym) + 1) |
|
:spinner, :info, :cursor, :selected].index(sym) + 1) |
|
||||||
(bold ? C::A_BOLD : 0)
|
(bold ? C::A_BOLD : 0)
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
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
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
@read =
|
def start_reader
|
||||||
if $stdin.tty?
|
stream =
|
||||||
if !`which find`.empty?
|
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")
|
IO.popen("find * -path '*/\\.*' -prune -o -type f -print -o -type l -print 2> /dev/null")
|
||||||
else
|
else
|
||||||
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 = [] }
|
@spinner.clear
|
||||||
}
|
end
|
||||||
|
end
|
||||||
|
|
||||||
main = Thread.current
|
def start_search
|
||||||
|
matcher = (@xmode ? ExtendedFuzzyMatcher : FuzzyMatcher).new @rxflag
|
||||||
searcher = Thread.new {
|
searcher = Thread.new {
|
||||||
|
lists = []
|
||||||
events = {}
|
events = {}
|
||||||
fcache = {}
|
fcache = {}
|
||||||
matches = []
|
|
||||||
mcount = 0 # match count
|
|
||||||
plcount = 0 # prev list count
|
|
||||||
q = ''
|
q = ''
|
||||||
vcursor = 0
|
delay = -5
|
||||||
zz = [0, 0]
|
|
||||||
started = false
|
|
||||||
|
|
||||||
begin
|
begin
|
||||||
while true
|
while true
|
||||||
wait_for_completion = nil
|
|
||||||
@mtx.synchronize do
|
@mtx.synchronize do
|
||||||
while true
|
while true
|
||||||
events.merge! @events
|
events.merge! @events
|
||||||
wait_for_completion = !@sort && !events[:loaded]
|
|
||||||
|
|
||||||
if @events.empty? # No new events
|
if @events.empty? # No new events
|
||||||
@cv.wait @mtx
|
@cv.wait @mtx
|
||||||
@@ -313,215 +507,206 @@ searcher = Thread.new {
|
|||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
|
||||||
if !wait_for_completion && events[:new]
|
if events[:new]
|
||||||
@lists << [@new, {}]
|
lists << @new
|
||||||
@count += @new.length
|
@count.set { |c| c + @new.length }
|
||||||
|
@spinner.set { |spinner|
|
||||||
|
if e = spinner.shift
|
||||||
|
spinner.push e
|
||||||
|
end; spinner
|
||||||
|
}
|
||||||
@new = []
|
@new = []
|
||||||
fcache = {}
|
fcache.clear
|
||||||
end
|
end
|
||||||
end#mtx
|
end#mtx
|
||||||
|
|
||||||
if wait_for_completion
|
new_search = events[:key] || events.delete(:new)
|
||||||
@smtx.synchronize do
|
user_input = events[:key]
|
||||||
print_info false, " +#{@new.length}"
|
progress = 0
|
||||||
print_input
|
started_at = Time.now
|
||||||
refresh
|
|
||||||
sleep 0.1
|
|
||||||
end
|
|
||||||
next
|
|
||||||
end
|
|
||||||
|
|
||||||
new_search = events[:key] || events[:new]
|
|
||||||
user_input = events[:key] || events[:vcursor]
|
|
||||||
|
|
||||||
if new_search && !@lists.empty?
|
|
||||||
events.delete :new
|
|
||||||
q = events.delete(:key) || q
|
|
||||||
regexp = q.empty? ? nil :
|
|
||||||
Regexp.new(convert_query(q).inject('') { |sum, e|
|
|
||||||
e = Regexp.escape e
|
|
||||||
sum << "#{e}[^#{e}]*?"
|
|
||||||
}, @rxflag)
|
|
||||||
|
|
||||||
|
if new_search && !lists.empty?
|
||||||
|
q, cx = events.delete(:key) || [q, 0]
|
||||||
|
empty = matcher.empty?(q)
|
||||||
matches = fcache[q] ||=
|
matches = fcache[q] ||=
|
||||||
begin
|
begin
|
||||||
@smtx.synchronize do
|
|
||||||
print_info true, ' ..'
|
|
||||||
print_input
|
|
||||||
refresh
|
|
||||||
end unless q.empty?
|
|
||||||
|
|
||||||
found = []
|
found = []
|
||||||
skip = false
|
skip = false
|
||||||
@lists.each do |pair|
|
cnt = 0
|
||||||
@mtx.synchronize { skip = @events[:key] }
|
lists.each do |list|
|
||||||
|
cnt += list.length
|
||||||
|
skip = @mtx.synchronize { @events[:key] }
|
||||||
break if skip
|
break if skip
|
||||||
|
|
||||||
list, cache = pair
|
if !empty && (progress = 100 * cnt / @count.get) < 100 && Time.now - started_at > 0.5
|
||||||
found.concat(cache[q] ||= begin
|
render { print_info " (#{progress}%)" }
|
||||||
prefix, suffix = @query[0, @cursor_x], @query[@cursor_x..-1] || ''
|
|
||||||
prefix_cache = suffix_cache = nil
|
|
||||||
|
|
||||||
(prefix.length - 1).downto(1) do |len|
|
|
||||||
break if prefix_cache = cache[prefix[0, len]]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
0.upto(suffix.length - 1) do |idx|
|
found.concat(q.empty? ? list :
|
||||||
break if suffix_cache = cache[suffix[idx..-1]]
|
matcher.match(list, q, q[0, cx], q[cx..-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|
|
|
||||||
if regexp
|
|
||||||
# Ignore errors: e.g. invalid byte sequence in UTF-8
|
|
||||||
md = line.match(regexp) rescue nil
|
|
||||||
md ? [line, md.offset(0)] : nil
|
|
||||||
else
|
|
||||||
[line, zz]
|
|
||||||
end
|
|
||||||
}.compact
|
|
||||||
end)
|
|
||||||
end
|
end
|
||||||
next if skip
|
next if skip
|
||||||
@sort ? found : found.reverse
|
@sort ? found : found.reverse
|
||||||
end
|
end
|
||||||
|
|
||||||
mcount = matches.length
|
if !empty && @sort && matches.length <= @sort
|
||||||
if @sort && mcount <= @sort
|
matches = sort_by_rank(matches)
|
||||||
matches.replace matches.sort_by { |pair|
|
|
||||||
line, offset = pair
|
|
||||||
[offset.last - offset.first, line.length, line]
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Atomic update
|
||||||
|
@matches.set matches
|
||||||
end#new_search
|
end#new_search
|
||||||
|
|
||||||
# This small delay reduces the number of partial lists
|
# This small delay reduces the number of partial lists
|
||||||
if started && !user_input
|
sleep((delay = [20, delay + 5].min) * 0.01) unless user_input
|
||||||
sleep 0.2
|
|
||||||
end
|
|
||||||
started = true
|
|
||||||
|
|
||||||
if events.delete(:vcursor) || new_search
|
update_list new_search
|
||||||
@mtx.synchronize do
|
end#while
|
||||||
plcount = [@matches.length, max_items].min
|
rescue Exception => e
|
||||||
@matches = matches
|
@main.raise e
|
||||||
vcursor = @vcursor = [0, [@vcursor, mcount - 1, max_items - 1].min].max
|
|
||||||
end
|
end
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
# Output
|
def pick
|
||||||
@smtx.synchronize do
|
items = @matches[0, max_items]
|
||||||
item_length = [mcount, max_items].min
|
curr = [0, [@vcursor.get, items.length - 1].min].max
|
||||||
if item_length < plcount
|
[*items.fetch(curr, [])][0]
|
||||||
plcount.downto(item_length) do |idx|
|
end
|
||||||
|
|
||||||
|
def update_list wipe
|
||||||
|
render do
|
||||||
|
items = @matches[0, max_items]
|
||||||
|
|
||||||
|
# Wipe
|
||||||
|
if items.length < @plcount
|
||||||
|
@plcount.downto(items.length) do |idx|
|
||||||
C.setpos cursor_y - idx - 2, 0
|
C.setpos cursor_y - idx - 2, 0
|
||||||
C.clrtoeol
|
C.clrtoeol
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@plcount = items.length
|
||||||
|
|
||||||
maxc = C.cols - 5
|
maxc = C.cols - 3
|
||||||
matches[0, max_items].each_with_index do |item, idx|
|
vcursor = @vcursor.set { |v| [0, [v, items.length - 1].min].max }
|
||||||
next if !new_search && !((vcursor-1)..(vcursor+1)).include?(idx)
|
cleanse = Set[vcursor]
|
||||||
|
@vcursors.set { |vs|
|
||||||
line, offset = convert_item item
|
cleanse.merge vs
|
||||||
|
Set.new
|
||||||
|
}
|
||||||
|
items.each_with_index do |item, idx|
|
||||||
|
next unless wipe || cleanse.include?(idx)
|
||||||
row = cursor_y - idx - 2
|
row = cursor_y - idx - 2
|
||||||
chosen = idx == vcursor
|
chosen = idx == vcursor
|
||||||
b, e = offset
|
selected = @selects.include?([*item][0])
|
||||||
|
line, offsets = convert_item item
|
||||||
if line.length > maxc
|
tokens = format line, maxc, offsets
|
||||||
diff = e - (maxc - 2)
|
print_item row, tokens, chosen, selected
|
||||||
if diff > 2
|
end
|
||||||
line = '..' + line[diff..-1]
|
print_info
|
||||||
b -= diff - 2
|
|
||||||
b = [2, b].max
|
|
||||||
else
|
|
||||||
line = line[0, maxc] + '..'
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if line.length > maxc
|
|
||||||
line = line[0, maxc] + '..'
|
|
||||||
end
|
|
||||||
|
|
||||||
C.setpos row, 0
|
def start_renderer
|
||||||
C.clrtoeol
|
Thread.new do
|
||||||
cprint chosen ? '>' : ' ', color(:red, true)
|
begin
|
||||||
cprint ' ', chosen ? color(:chosen) : 0
|
while blk = @queue.shift
|
||||||
|
blk.call
|
||||||
C.attron color(:chosen, true) if chosen
|
|
||||||
|
|
||||||
e = [e, maxc].min
|
|
||||||
if b < maxc && 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 if !@lists.empty? || events[:loaded]
|
|
||||||
print_input
|
|
||||||
refresh
|
refresh
|
||||||
end
|
end
|
||||||
end
|
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
main.raise e
|
@main.raise e
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
}
|
|
||||||
|
|
||||||
|
def render &blk
|
||||||
|
@queue.push blk
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def start_loop
|
||||||
got = nil
|
got = nil
|
||||||
begin
|
begin
|
||||||
tty = IO.open(IO.sysopen('/dev/tty'), 'r')
|
tty = IO.open(IO.sysopen('/dev/tty'), 'r')
|
||||||
input = ''
|
input = ''
|
||||||
cursor = 0
|
cursor = 0
|
||||||
|
backword = proc {
|
||||||
|
cursor = (input[0, cursor].rindex(/\s\S/) || -1) + 1
|
||||||
|
}
|
||||||
actions = {
|
actions = {
|
||||||
:nop => proc {},
|
:nop => proc { nil },
|
||||||
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 = pick
|
||||||
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; nil },
|
||||||
ctrl(:e) => proc { cursor = input.length },
|
ctrl(:e) => proc { cursor = input.length; nil },
|
||||||
ctrl(:j) => proc { emit(:vcursor) { @vcursor -= 1 } },
|
ctrl(:j) => proc { @vcursor.set { |v| @vcursors << v; v - 1 }; update_list false },
|
||||||
ctrl(:k) => proc { emit(:vcursor) { @vcursor += 1 } },
|
ctrl(:k) => proc { @vcursor.set { |v| @vcursors << v; v + 1 }; update_list false },
|
||||||
ctrl(:w) => proc {
|
ctrl(:w) => proc {
|
||||||
ridx = (input[0...cursor - 1].rindex(/\S\s/) || -2) + 2
|
pcursor = cursor
|
||||||
input = input[0...ridx] + input[cursor..-1]
|
backword.call
|
||||||
cursor = ridx
|
input = input[0...cursor] + input[pcursor..-1]
|
||||||
},
|
},
|
||||||
127 => proc { input[cursor -= 1] = '' if cursor > 0 },
|
127 => proc { input[cursor -= 1] = '' if cursor > 0 },
|
||||||
:left => proc { cursor = [0, cursor - 1].max },
|
9 => proc { |o|
|
||||||
:right => proc { cursor = [input.length, cursor + 1].min },
|
if @multi && sel = pick
|
||||||
|
if @selects.has_key? sel
|
||||||
|
@selects.delete sel
|
||||||
|
else
|
||||||
|
@selects[sel] = 1
|
||||||
|
end
|
||||||
|
@vcursor.set { |v|
|
||||||
|
@vcursors << v
|
||||||
|
v + (o == :stab ? 1 : -1)
|
||||||
|
}
|
||||||
|
update_list false
|
||||||
|
end
|
||||||
|
},
|
||||||
|
:left => proc { cursor = [0, cursor - 1].max; nil },
|
||||||
|
:right => proc { cursor = [input.length, cursor + 1].min; nil },
|
||||||
|
:alt_b => proc { backword.call; nil },
|
||||||
|
:alt_f => proc {
|
||||||
|
cursor += (input[cursor..-1].index(/(\S\s)|(.$)/) || -1) + 1
|
||||||
|
nil
|
||||||
|
},
|
||||||
}
|
}
|
||||||
actions[ctrl(:b)] = actions[:left]
|
actions[ctrl(:b)] = actions[:left]
|
||||||
actions[ctrl(:f)] = actions[:right]
|
actions[ctrl(:f)] = actions[:right]
|
||||||
actions[ctrl(:h)] = actions[127]
|
actions[ctrl(:h)] = actions[127]
|
||||||
actions[ctrl(:n)] = actions[ctrl(:j)]
|
actions[ctrl(:n)] = actions[ctrl(:j)]
|
||||||
actions[ctrl(:p)] = actions[ctrl(:k)]
|
actions[ctrl(:p)] = actions[ctrl(:k)]
|
||||||
|
actions[:stab] = actions[9]
|
||||||
|
|
||||||
while true
|
while true
|
||||||
|
@cursor_x.set cursor
|
||||||
|
render { print_input }
|
||||||
|
|
||||||
ord = tty.getc.ord
|
ord = tty.getc.ord
|
||||||
if ord == 27
|
ord =
|
||||||
ord = tty.getc.ord
|
case ord = tty.getc.ord
|
||||||
if ord == 91
|
when 91
|
||||||
ord = case tty.getc.ord
|
case tty.getc.ord
|
||||||
when 68 then :left
|
when 68 then :left
|
||||||
when 67 then :right
|
when 67 then :right
|
||||||
when 66 then ctrl(:j)
|
when 66 then ctrl(:j)
|
||||||
when 65 then ctrl(:k)
|
when 65 then ctrl(:k)
|
||||||
|
when 90 then :stab
|
||||||
else :nop
|
else :nop
|
||||||
end
|
end
|
||||||
end
|
when 'b'.ord
|
||||||
end
|
:alt_b
|
||||||
|
when 'f'.ord
|
||||||
|
:alt_f
|
||||||
|
else
|
||||||
|
ord
|
||||||
|
end if ord == 27
|
||||||
|
|
||||||
actions.fetch(ord, proc { |ord|
|
upd = actions.fetch(ord, proc { |ord|
|
||||||
char = [ord].pack('U*')
|
char = [ord].pack('U*')
|
||||||
if char =~ /[[:print:]]/
|
if char =~ /[[:print:]]/
|
||||||
input.insert cursor, char
|
input.insert cursor, char
|
||||||
@@ -530,17 +715,135 @@ begin
|
|||||||
}).call(ord)
|
}).call(ord)
|
||||||
|
|
||||||
# Dispatch key event
|
# Dispatch key event
|
||||||
emit(:key) { @query = input.dup }
|
emit(:key) { [@query.set(input.dup), cursor] } if upd
|
||||||
|
|
||||||
# Update user input
|
|
||||||
@smtx.synchronize do
|
|
||||||
@cursor_x = cursor
|
|
||||||
print_input
|
|
||||||
refresh
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
ensure
|
ensure
|
||||||
C.close_screen
|
C.close_screen
|
||||||
stdout.puts got if got
|
if got
|
||||||
|
if @selects.empty?
|
||||||
|
@stdout.puts got
|
||||||
|
else
|
||||||
|
@selects.each do |sel, _|
|
||||||
|
@stdout.puts sel
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
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 empty? q
|
||||||
|
q.empty?
|
||||||
|
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
|
||||||
|
@regexps = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
def empty? q
|
||||||
|
parse(q).empty?
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse q
|
||||||
|
q = q.strip
|
||||||
|
@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[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 }
|
||||||
|
end
|
||||||
|
|
||||||
|
def match list, q, prefix, suffix
|
||||||
|
regexps = parse q
|
||||||
|
# Look for prefix cache
|
||||||
|
cache = @caches[list.object_id]
|
||||||
|
prefix = prefix.strip.sub(/\$\S*$/, '').sub(/(^|\s)!\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 ENV.fetch('FZF_EXECUTABLE', '1') == '1'
|
||||||
|
|
||||||
|
@@ -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.1.0'
|
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}
|
||||||
|
@@ -27,9 +27,13 @@ function! s:fzf(args)
|
|||||||
try
|
try
|
||||||
let tf = tempname()
|
let tf = tempname()
|
||||||
let prefix = exists('g:fzf_command') ? g:fzf_command.'|' : ''
|
let prefix = exists('g:fzf_command') ? g:fzf_command.'|' : ''
|
||||||
execute "silent !".prefix."/usr/bin/env ruby ".s:exec." ".a:args." > ".tf
|
let fzf = executable(s:exec) ? s:exec : 'fzf'
|
||||||
|
execute "silent !".prefix.fzf." ".a:args." > ".tf
|
||||||
if !v:shell_error
|
if !v:shell_error
|
||||||
execute 'silent e '.join(readfile(tf), '')
|
let file = join(readfile(tf), '')
|
||||||
|
if !empty(file)
|
||||||
|
execute 'silent e '.file
|
||||||
|
endif
|
||||||
endif
|
endif
|
||||||
finally
|
finally
|
||||||
silent! call delete(tf)
|
silent! call delete(tf)
|
||||||
|
306
test/test_fzf.rb
Normal file
306
test/test_fzf.rb
Normal file
@@ -0,0 +1,306 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
# encoding: utf-8
|
||||||
|
|
||||||
|
require 'minitest/autorun'
|
||||||
|
$LOAD_PATH.unshift File.expand_path('../..', __FILE__)
|
||||||
|
ENV['FZF_EXECUTABLE'] = '0'
|
||||||
|
load 'fzf'
|
||||||
|
|
||||||
|
class TestFZF < MiniTest::Unit::TestCase
|
||||||
|
def test_default_options
|
||||||
|
fzf = FZF.new []
|
||||||
|
assert_equal 1000, fzf.sort
|
||||||
|
assert_equal false, fzf.multi
|
||||||
|
assert_equal true, fzf.color
|
||||||
|
assert_equal Regexp::IGNORECASE, fzf.rxflag
|
||||||
|
|
||||||
|
begin
|
||||||
|
ENV['FZF_DEFAULT_SORT'] = '1500'
|
||||||
|
fzf = FZF.new []
|
||||||
|
assert_equal 1500, 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', '')
|
||||||
|
|
||||||
|
# '
|
||||||
|
assert_equal %w[juiceful juiceless juicily],
|
||||||
|
match.call('il', '').map { |e| e.first }
|
||||||
|
assert_equal %w[juicily],
|
||||||
|
match.call("'il", '').map { |e| e.first }
|
||||||
|
assert_equal (list - %w[juicily]).sort,
|
||||||
|
match.call("!'il", '').map { |e| e.first }.sort
|
||||||
|
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