mirror of
https://github.com/junegunn/fzf.git
synced 2025-08-02 13:12:00 -07:00
Compare commits
94 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
b2d2be55ef | ||
|
7280e8ebc2 | ||
|
c7e86ad4f1 | ||
|
f2b2c022be | ||
|
7747daa9ec | ||
|
c2943e7681 | ||
|
d5fc03d867 | ||
|
b0eca20dc2 | ||
|
aad335475c | ||
|
c3676bf986 | ||
|
6fb4b6d097 | ||
|
6aa168833b | ||
|
0d83cae2ec | ||
|
773d9976a0 | ||
|
3723829b0a | ||
|
13cb198b5c | ||
|
79f645aa6c | ||
|
42d479d071 | ||
|
d7f50b1e41 | ||
|
39eb85596c | ||
|
bff7e9edf5 | ||
|
98ccc03a21 | ||
|
3b668ed448 | ||
|
33b28be941 | ||
|
76fe23b928 | ||
|
622c54f4a3 | ||
|
e09993f919 | ||
|
7ee6fd1f6d | ||
|
2dca6f0cb2 | ||
|
159dd7f069 | ||
|
b30f21e074 | ||
|
636c86cf6f | ||
|
5483e41b2a | ||
|
1c89994c94 | ||
|
e1bc4b983e | ||
|
cb3645ea95 | ||
|
04ebaddf5e | ||
|
45e1f1ae57 | ||
|
c1d5f7cef7 | ||
|
df663c4e41 | ||
|
d3742782f3 | ||
|
faff17b2a9 | ||
|
9a3cddc92e | ||
|
bd2763d863 | ||
|
b2bb22d883 | ||
|
ad8ec7f387 | ||
|
cf0ca8578c | ||
|
07aee79bd8 | ||
|
344b57fe33 | ||
|
18a2fbf54a | ||
|
39af56cf8f | ||
|
2d3a0a1034 | ||
|
655fa5d9aa | ||
|
9a49a29c7f | ||
|
89ae45cda4 | ||
|
f660ad35b2 | ||
|
c61738ae43 | ||
|
c4dec4d34b | ||
|
a797604255 | ||
|
25840d3bc7 | ||
|
4745d50931 | ||
|
04bf3abe99 | ||
|
57f7963eee | ||
|
2fa21e5dd6 | ||
|
9c4c37aa36 | ||
|
2540c9062f | ||
|
f28274109f | ||
|
724724bd8c | ||
|
64541cb5f8 | ||
|
179b00ed6c | ||
|
a9fd496691 | ||
|
b14c57e656 | ||
|
fa5617e076 | ||
|
e52a1d5fad | ||
|
423e26b0c9 | ||
|
84921df0e3 | ||
|
6a5e1de6f3 | ||
|
90adda73b0 | ||
|
be3b948034 | ||
|
93dafff424 | ||
|
419bc17c0c | ||
|
f0a5757244 | ||
|
f0b2b98c5d | ||
|
4530819539 | ||
|
1825a73e2e | ||
|
30d4974509 | ||
|
e4a49dbb2a | ||
|
76c7f4f9c0 | ||
|
8ae604af67 | ||
|
6037e1e217 | ||
|
43acf5c8a4 | ||
|
545e8bfcee | ||
|
90ad6d50b8 | ||
|
67bdc3a0ad |
282
README.md
282
README.md
@@ -16,39 +16,19 @@ fzf requires Ruby (>= 1.8.5).
|
|||||||
Installation
|
Installation
|
||||||
------------
|
------------
|
||||||
|
|
||||||
Download [fzf executable](https://raw.github.com/junegunn/fzf/master/fzf) and
|
Clone this repository and run
|
||||||
put it somewhere in your search $PATH.
|
|
||||||
|
|
||||||
```sh
|
|
||||||
mkdir -p ~/bin
|
|
||||||
wget https://raw.github.com/junegunn/fzf/master/fzf -O ~/bin/fzf
|
|
||||||
chmod +x ~/bin/fzf
|
|
||||||
```
|
|
||||||
|
|
||||||
Or you can just clone this repository and run
|
|
||||||
[install](https://github.com/junegunn/fzf/blob/master/install) script.
|
[install](https://github.com/junegunn/fzf/blob/master/install) script.
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
git clone https://github.com/junegunn/fzf.git
|
git clone https://github.com/junegunn/fzf.git ~/.fzf
|
||||||
fzf/install
|
~/.fzf/install
|
||||||
```
|
```
|
||||||
|
|
||||||
Make sure that ~/bin is included in $PATH.
|
The script will setup:
|
||||||
|
|
||||||
```sh
|
- `fzf` executable
|
||||||
export PATH=$PATH:~/bin
|
- Key bindings (`CTRL-T`, `CTRL-R`, etc.)
|
||||||
```
|
- Fuzzy auto-completion for bash
|
||||||
|
|
||||||
### Install as Ruby gem
|
|
||||||
|
|
||||||
fzf can be installed as a Ruby gem
|
|
||||||
|
|
||||||
```
|
|
||||||
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
|
### Install as Vim plugin
|
||||||
|
|
||||||
@@ -71,11 +51,20 @@ Usage
|
|||||||
```
|
```
|
||||||
usage: fzf [options]
|
usage: fzf [options]
|
||||||
|
|
||||||
-m, --multi Enable multi-select
|
Options
|
||||||
-s, --sort=MAX Maximum number of matched items to sort. Default: 500
|
-m, --multi Enable multi-select
|
||||||
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
-x, --extended Extended-search mode
|
||||||
+i Case-sensitive match
|
-q, --query=STR Initial query
|
||||||
+c, --no-color Disable colors
|
-s, --sort=MAX Maximum number of matched items to sort (default: 1000)
|
||||||
|
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
||||||
|
-i Case-insensitive match (default: smart-case match)
|
||||||
|
+i Case-sensitive match
|
||||||
|
+c, --no-color Disable colors
|
||||||
|
--no-mouse Disable mouse
|
||||||
|
|
||||||
|
Environment variables
|
||||||
|
FZF_DEFAULT_COMMAND Default command to use when input is tty
|
||||||
|
FZF_DEFAULT_OPTS Defaults options. (e.g. "-x -m --sort 10000")
|
||||||
```
|
```
|
||||||
|
|
||||||
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
|
||||||
@@ -103,39 +92,39 @@ history | fzf +s
|
|||||||
### Key binding
|
### Key binding
|
||||||
|
|
||||||
Use CTRL-J and CTRL-K (or CTRL-N and CTRL-P) to change the selection, press
|
Use CTRL-J and CTRL-K (or CTRL-N and CTRL-P) to change the selection, press
|
||||||
enter key to select the item. CTRL-C will terminate the finder.
|
enter key to select the item. CTRL-C, CTRL-G, or ESC will terminate the finder.
|
||||||
|
|
||||||
The following readline key bindings should also work as expected.
|
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
|
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.
|
||||||
|
|
||||||
Usage as Vim plugin
|
You can also use mouse. Click on an item to select it or shift-click to select
|
||||||
-------------------
|
multiple items. Use mouse wheel to move the cursor up and down.
|
||||||
|
|
||||||
If you install fzf as a Vim plugin, `:FZF` command will be added.
|
### Extended-search mode
|
||||||
|
|
||||||
```vim
|
With `-x` or `--extended` option, fzf will start in "extended-search mode".
|
||||||
:FZF
|
|
||||||
:FZF --no-sort
|
|
||||||
```
|
|
||||||
|
|
||||||
You can override the command which produces input to fzf.
|
In this mode, you can specify multiple patterns delimited by spaces,
|
||||||
|
such as: `^music .mp3$ sbtrkt !rmx`
|
||||||
|
|
||||||
```vim
|
| Token | Description | Match type |
|
||||||
let g:fzf_command = 'find . -type f'
|
| -------- | -------------------------------- | -------------------- |
|
||||||
```
|
| `^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 |
|
||||||
|
|
||||||
Most of the time, you will prefer native Vim plugins with better integration
|
Useful examples
|
||||||
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
|
|
||||||
--------------------
|
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
# vimf - Open selected file in Vim
|
# vimf - Open selected file in Vim
|
||||||
@@ -150,7 +139,7 @@ fd() {
|
|||||||
|
|
||||||
# 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
|
||||||
@@ -162,56 +151,139 @@ fh() {
|
|||||||
fkill() {
|
fkill() {
|
||||||
ps -ef | sed 1d | fzf -m | awk '{print $2}' | xargs kill -${1:-9}
|
ps -ef | sed 1d | fzf -m | awk '{print $2}' | xargs kill -${1:-9}
|
||||||
}
|
}
|
||||||
|
|
||||||
# (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 '"\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
|
Key bindings for command line
|
||||||
-----------
|
-----------------------------
|
||||||
|
|
||||||
|
The install script will setup the following key bindings.
|
||||||
|
|
||||||
|
### bash
|
||||||
|
|
||||||
|
- `CTRL-T` - Paste the selected file path(s) into the command line
|
||||||
|
- `CTRL-R` - Paste the selected command from history into the command line
|
||||||
|
|
||||||
|
The source code can be found in `~/.fzf.bash`.
|
||||||
|
|
||||||
|
### zsh
|
||||||
|
|
||||||
|
- `CTRL-T` - Paste the selected file path(s) into the command line
|
||||||
|
- `CTRL-R` - Paste the selected command from history into the command line
|
||||||
|
- `ALT-C` - cd into the selected directory
|
||||||
|
|
||||||
|
The source code can be found in `~/.fzf.zsh`.
|
||||||
|
|
||||||
|
Auto-completion
|
||||||
|
---------------
|
||||||
|
|
||||||
|
Disclaimer: *Auto-completion feature is currently experimental, it can change
|
||||||
|
over time*
|
||||||
|
|
||||||
|
### bash
|
||||||
|
|
||||||
|
#### Files and directories
|
||||||
|
|
||||||
|
Fuzzy completion for files and directories can be triggered if the word before
|
||||||
|
the cursor ends with the trigger sequence which is by default `**`.
|
||||||
|
|
||||||
|
- `COMMAND [DIRECTORY/][FUZZY_PATTERN]**<TAB>`
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
# CTRL-T - Paste the selected file(s) path into the command line
|
# Files under current directory
|
||||||
fzf-file-widget() {
|
# - You can select multiple items with TAB key
|
||||||
local FILES
|
vim **<TAB>
|
||||||
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
|
# Files under parent directory
|
||||||
fzf-cd-widget() {
|
vim ../**<TAB>
|
||||||
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
|
# Files under parent directory that match `fzf`
|
||||||
fzf-history-widget() {
|
vim ../fzf**<TAB>
|
||||||
LBUFFER=$(history | fzf +s | sed "s/ *[0-9]* *//")
|
|
||||||
zle redisplay
|
# Files under your home directory
|
||||||
}
|
vim ~/**<TAB>
|
||||||
zle -N fzf-history-widget
|
|
||||||
bindkey '^R' fzf-history-widget
|
|
||||||
|
# Directories under current directory (single-selection)
|
||||||
|
cd **<TAB>
|
||||||
|
|
||||||
|
# Directories under ~/github that match `fzf`
|
||||||
|
cd ~/github/fzf**<TAB>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Process IDs
|
||||||
|
|
||||||
|
Fuzzy completion for PIDs is provided for kill command. In this case
|
||||||
|
there is no trigger sequence, just press tab key after kill command.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# Can select multiple processes with <TAB> or <Shift-TAB> keys
|
||||||
|
kill -9 <TAB>
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Host names
|
||||||
|
|
||||||
|
For ssh and telnet commands, fuzzy completion for host names is provided. The
|
||||||
|
names are extracted from /etc/hosts and ~/.ssh/config.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
ssh **<TAB>
|
||||||
|
telnet **<TAB>
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Settings
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# Use ~~ as the trigger sequence instead of the default **
|
||||||
|
export FZF_COMPLETION_TRIGGER='~~'
|
||||||
|
|
||||||
|
# Options to fzf command
|
||||||
|
export FZF_COMPLETION_OPTS='+c -x'
|
||||||
|
```
|
||||||
|
|
||||||
|
### zsh
|
||||||
|
|
||||||
|
TODO :smiley:
|
||||||
|
|
||||||
|
(Pull requests are appreciated.)
|
||||||
|
|
||||||
|
Usage as Vim plugin
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
If you install fzf as a Vim plugin, `:FZF` command will be added.
|
||||||
|
|
||||||
|
```vim
|
||||||
|
" Look for files under current directory
|
||||||
|
:FZF
|
||||||
|
|
||||||
|
" Look for files under your home directory
|
||||||
|
:FZF ~
|
||||||
|
|
||||||
|
" With options
|
||||||
|
:FZF --no-sort -m /tmp
|
||||||
|
```
|
||||||
|
|
||||||
|
You can override the source command which produces input to fzf.
|
||||||
|
|
||||||
|
```vim
|
||||||
|
let g:fzf_source = 'find . -type f'
|
||||||
|
```
|
||||||
|
|
||||||
|
And you can predefine default options to fzf command.
|
||||||
|
|
||||||
|
```vim
|
||||||
|
let g:fzf_options = '--no-color --extended'
|
||||||
|
```
|
||||||
|
|
||||||
|
For more advanced uses, you can call `fzf#run` function as follows.
|
||||||
|
|
||||||
|
```vim
|
||||||
|
:call fzf#run('tabedit', '-m +c')
|
||||||
|
```
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
Tips
|
Tips
|
||||||
----
|
----
|
||||||
|
|
||||||
@@ -225,12 +297,18 @@ If you're running Ruby 1.9 or above, you can improve the startup time with
|
|||||||
- `time ruby --disable-gems ~/bin/fzf -h`
|
- `time ruby --disable-gems ~/bin/fzf -h`
|
||||||
- 0.025 sec
|
- 0.025 sec
|
||||||
|
|
||||||
Define fzf alias with the option as follows:
|
You can define fzf function with the option as follows:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
alias fzf='ruby --disable-gems ~/bin/fzf'
|
fzf() {
|
||||||
|
ruby --disable-gems ~/bin/fzf "$@"
|
||||||
|
}
|
||||||
|
export -f fzf
|
||||||
```
|
```
|
||||||
|
|
||||||
|
However, this is automatically set up in your .bashrc and .zshrc if you use the
|
||||||
|
bundled [install](https://github.com/junegunn/fzf/blob/master/install) script.
|
||||||
|
|
||||||
### Incorrect display on Ruby 1.8
|
### Incorrect display on Ruby 1.8
|
||||||
|
|
||||||
It is reported that the output of fzf can become unreadable on some terminals
|
It is reported that the output of fzf can become unreadable on some terminals
|
||||||
@@ -238,6 +316,20 @@ 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
|
to 1.9 or above. Ruby 1.9 or above is also required for displaying Unicode
|
||||||
characters.
|
characters.
|
||||||
|
|
||||||
|
### Ranking algorithm
|
||||||
|
|
||||||
|
fzf sorts the result first by the length of the matched substring, then by the
|
||||||
|
length of the whole string. However it only does so when the number of matches
|
||||||
|
is less than the limit which is by default 1000, in order to avoid the cost of
|
||||||
|
sorting a large list and limit the response time of the query.
|
||||||
|
|
||||||
|
This limit can be adjusted with `-s` option, or with the environment variable
|
||||||
|
`FZF_DEFAULT_OPTS`.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
export FZF_DEFAULT_OPTS="--sort 20000"
|
||||||
|
```
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
|
167
fzf-completion.bash
Normal file
167
fzf-completion.bash
Normal file
@@ -0,0 +1,167 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# ____ ____
|
||||||
|
# / __/___ / __/
|
||||||
|
# / /_/_ / / /_
|
||||||
|
# / __/ / /_/ __/
|
||||||
|
# /_/ /___/_/-completion.bash
|
||||||
|
#
|
||||||
|
# - $FZF_COMPLETION_TRIGGER (default: '**')
|
||||||
|
# - $FZF_COMPLETION_OPTS (default: empty)
|
||||||
|
|
||||||
|
_fzf_opts_completion() {
|
||||||
|
local cur prev opts
|
||||||
|
COMPREPLY=()
|
||||||
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||||
|
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
||||||
|
opts="-m --multi -x --extended -s --sort +s +i +c --no-color"
|
||||||
|
|
||||||
|
case "${prev}" in
|
||||||
|
--sort|-s)
|
||||||
|
COMPREPLY=( $(compgen -W "$(seq 2000 1000 10000)" -- ${cur}) )
|
||||||
|
return 0
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
if [[ ${cur} =~ ^-|\+ ]]; then
|
||||||
|
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
_fzf_generic_completion() {
|
||||||
|
local cur base dir leftover matches trigger
|
||||||
|
COMPREPLY=()
|
||||||
|
trigger=${FZF_COMPLETION_TRIGGER:-**}
|
||||||
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||||
|
if [[ ${cur} == *"$trigger" ]]; then
|
||||||
|
base=${cur:0:${#cur}-${#trigger}}
|
||||||
|
eval base=$base
|
||||||
|
|
||||||
|
dir="$base"
|
||||||
|
while [ 1 ]; do
|
||||||
|
if [ -z "$dir" -o -d "$dir" ]; then
|
||||||
|
leftover=${base/#"$dir"}
|
||||||
|
leftover=${leftover/#\/}
|
||||||
|
[ "$dir" = './' ] && dir=''
|
||||||
|
tput sc
|
||||||
|
matches=$(find "$dir"* $1 2> /dev/null | fzf $FZF_COMPLETION_OPTS $2 -q "$leftover" | while read item; do
|
||||||
|
printf '%q ' "$item"
|
||||||
|
done)
|
||||||
|
matches=${matches% }
|
||||||
|
if [ -n "$matches" ]; then
|
||||||
|
COMPREPLY=( "$matches" )
|
||||||
|
else
|
||||||
|
COMPREPLY=( "$cur" )
|
||||||
|
fi
|
||||||
|
tput rc
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
dir=$(dirname "$dir")
|
||||||
|
[[ "$dir" =~ /$ ]] || dir="$dir"/
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
_fzf_all_completion() {
|
||||||
|
_fzf_generic_completion \
|
||||||
|
"-name .git -prune -o -name .svn -prune -o -type d -print -o -type f -print -o -type l -print" \
|
||||||
|
"-m"
|
||||||
|
}
|
||||||
|
|
||||||
|
_fzf_file_completion() {
|
||||||
|
_fzf_generic_completion \
|
||||||
|
"-name .git -prune -o -name .svn -prune -o -type f -print -o -type l -print" \
|
||||||
|
"-m"
|
||||||
|
}
|
||||||
|
|
||||||
|
_fzf_dir_completion() {
|
||||||
|
_fzf_generic_completion \
|
||||||
|
"-name .git -prune -o -name .svn -prune -o -type d -print" \
|
||||||
|
""
|
||||||
|
}
|
||||||
|
|
||||||
|
_fzf_kill_completion() {
|
||||||
|
[ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1
|
||||||
|
|
||||||
|
local selected
|
||||||
|
tput sc
|
||||||
|
selected=$(ps -ef | sed 1d | fzf -m $FZF_COMPLETION_OPTS | awk '{print $2}' | tr '\n' ' ')
|
||||||
|
tput rc
|
||||||
|
|
||||||
|
if [ -n "$selected" ]; then
|
||||||
|
COMPREPLY=( "$selected" )
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
_fzf_telnet_completion() {
|
||||||
|
local cur selected trigger
|
||||||
|
trigger=${FZF_COMPLETION_TRIGGER:-**}
|
||||||
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||||
|
[[ ${cur} == *"$trigger" ]] || return 1
|
||||||
|
cur=${cur:0:${#cur}-${#trigger}}
|
||||||
|
|
||||||
|
tput sc
|
||||||
|
selected=$(grep -v '^\s*\(#\|$\)' /etc/hosts | awk '{print $2}' | sort -u | fzf $FZF_COMPLETION_OPTS -q "$cur")
|
||||||
|
tput rc
|
||||||
|
|
||||||
|
if [ -n "$selected" ]; then
|
||||||
|
COMPREPLY=("$selected")
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
_fzf_ssh_completion() {
|
||||||
|
local cur selected trigger
|
||||||
|
trigger=${FZF_COMPLETION_TRIGGER:-**}
|
||||||
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||||
|
[[ ${cur} == *"$trigger" ]] || return 1
|
||||||
|
cur=${cur:0:${#cur}-${#trigger}}
|
||||||
|
|
||||||
|
tput sc
|
||||||
|
selected=$(cat \
|
||||||
|
<(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | grep -i ^host) \
|
||||||
|
<(grep -v '^\s*\(#\|$\)' /etc/hosts) | \
|
||||||
|
awk '{print $2}' | sort -u | fzf $FZF_COMPLETION_OPTS -q "$cur")
|
||||||
|
tput rc
|
||||||
|
|
||||||
|
if [ -n "$selected" ]; then
|
||||||
|
COMPREPLY=("$selected")
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
complete -F _fzf_opts_completion fzf
|
||||||
|
|
||||||
|
# Directory
|
||||||
|
for cmd in "cd pushd rmdir"; do
|
||||||
|
complete -F _fzf_dir_completion -o default -o bashdefault $cmd
|
||||||
|
done
|
||||||
|
|
||||||
|
# File
|
||||||
|
for cmd in "
|
||||||
|
awk cat diff diff3
|
||||||
|
emacs ex file ftp g++ gcc gvim head hg java
|
||||||
|
javac ld less more mvim patch perl python ruby
|
||||||
|
sed sftp sort source tail tee uniq vi view vim wc"; do
|
||||||
|
complete -F _fzf_file_completion -o default -o bashdefault $cmd
|
||||||
|
done
|
||||||
|
|
||||||
|
# Anything
|
||||||
|
for cmd in "
|
||||||
|
basename bunzip2 bzip2 chmod chown curl cp dirname du
|
||||||
|
find git grep gunzip gzip hg jar
|
||||||
|
ln ls mv open rm rsync scp
|
||||||
|
svn tar unzip zip"; do
|
||||||
|
complete -F _fzf_all_completion -o default -o bashdefault $cmd
|
||||||
|
done
|
||||||
|
|
||||||
|
# Kill completion
|
||||||
|
complete -F _fzf_kill_completion -o nospace -o default -o bashdefault kill
|
||||||
|
|
||||||
|
# Host completion
|
||||||
|
complete -F _fzf_ssh_completion -o default -o bashdefault ssh
|
||||||
|
complete -F _fzf_telnet_completion -o default -o bashdefault telnet
|
||||||
|
|
9
fzf-completion.zsh
Normal file
9
fzf-completion.zsh
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#!/bin/zsh
|
||||||
|
# ____ ____
|
||||||
|
# / __/___ / __/
|
||||||
|
# / /_/_ / / /_
|
||||||
|
# / __/ / /_/ __/
|
||||||
|
# /_/ /___/_/-completion.zsh
|
||||||
|
#
|
||||||
|
|
||||||
|
# TODO
|
@@ -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.7.1'
|
||||||
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}
|
||||||
@@ -12,4 +12,6 @@ Gem::Specification.new do |spec|
|
|||||||
spec.bindir = '.'
|
spec.bindir = '.'
|
||||||
spec.files = %w[fzf.gemspec]
|
spec.files = %w[fzf.gemspec]
|
||||||
spec.executables = 'fzf'
|
spec.executables = 'fzf'
|
||||||
|
|
||||||
|
spec.add_runtime_dependency 'curses', '~> 1.0.0'
|
||||||
end
|
end
|
||||||
|
183
install
183
install
@@ -1,7 +1,184 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
cd `dirname $BASH_SOURCE`
|
cd `dirname $BASH_SOURCE`
|
||||||
mkdir -p ~/bin
|
fzf_base=`pwd`
|
||||||
ln -sf `pwd`/fzf ~/bin/fzf
|
|
||||||
chmod +x ~/bin/fzf
|
# ruby executable
|
||||||
|
echo -n "Checking Ruby executable ... "
|
||||||
|
ruby=`which ruby`
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo "ruby executable not found!"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# System ruby is preferred
|
||||||
|
curses_check="begin; require 'curses'; rescue Exception; exit 1; end"
|
||||||
|
system_ruby=/usr/bin/ruby
|
||||||
|
if [ -x $system_ruby -a $system_ruby != "$ruby" ]; then
|
||||||
|
$system_ruby --disable-gems -e "$curses_check" 2> /dev/null
|
||||||
|
[ $? -eq 0 ] && ruby=$system_ruby
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "OK ($ruby)"
|
||||||
|
|
||||||
|
# Curses-support
|
||||||
|
echo -n "Checking Curses support ... "
|
||||||
|
"$ruby" -e "$curses_check"
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
|
echo "OK"
|
||||||
|
else
|
||||||
|
echo "Not found"
|
||||||
|
echo "Installing 'curses' gem ... "
|
||||||
|
/usr/bin/env gem install curses
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo "Failed to install 'curses' gem."
|
||||||
|
echo "Try installing it as root: sudo gem install curses"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Ruby version
|
||||||
|
echo -n "Checking Ruby version ... "
|
||||||
|
"$ruby" -e 'exit RUBY_VERSION >= "1.9"'
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
|
echo ">= 1.9"
|
||||||
|
"$ruby" --disable-gems -e "$curses_check"
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
|
fzf_cmd="$ruby --disable-gems $fzf_base/fzf"
|
||||||
|
else
|
||||||
|
fzf_cmd="$ruby $fzf_base/fzf"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "< 1.9"
|
||||||
|
fzf_cmd="$ruby $fzf_base/fzf"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Auto-completion
|
||||||
|
read -p "Do you want to add auto-completion support? ([y]/n) " -n 1 -r
|
||||||
|
echo
|
||||||
|
[[ ! $REPLY =~ ^[Nn]$ ]]
|
||||||
|
auto_completion=$?
|
||||||
|
|
||||||
|
# Key-bindings
|
||||||
|
read -p "Do you want to add key bindings? ([y]/n) " -n 1 -r
|
||||||
|
echo
|
||||||
|
[[ ! $REPLY =~ ^[Nn]$ ]]
|
||||||
|
key_bindings=$?
|
||||||
|
|
||||||
|
echo
|
||||||
|
for shell in bash zsh; do
|
||||||
|
echo -n "Generate ~/.fzf.$shell ... "
|
||||||
|
src=~/.fzf.${shell}
|
||||||
|
|
||||||
|
fzf_completion="source $fzf_base/fzf-completion.${shell}"
|
||||||
|
if [ $auto_completion -ne 0 ]; then
|
||||||
|
fzf_completion="# $fzf_completion"
|
||||||
|
fi
|
||||||
|
|
||||||
|
cat > $src << EOF
|
||||||
|
# Setup fzf function
|
||||||
|
# ------------------
|
||||||
|
unalias fzf 2> /dev/null
|
||||||
|
fzf() {
|
||||||
|
$fzf_cmd "\$@"
|
||||||
|
}
|
||||||
|
export -f fzf > /dev/null
|
||||||
|
|
||||||
|
# Auto-completion
|
||||||
|
# ---------------
|
||||||
|
$fzf_completion
|
||||||
|
|
||||||
|
EOF
|
||||||
|
|
||||||
|
if [ $key_bindings -eq 0 ]; then
|
||||||
|
if [ $shell = bash ]; then
|
||||||
|
cat >> $src << "EOF"
|
||||||
|
# Key bindings
|
||||||
|
# ------------
|
||||||
|
# Required to refresh the prompt after fzf
|
||||||
|
bind '"\er": redraw-current-line'
|
||||||
|
|
||||||
|
# CTRL-T - Paste the selected file path into the command line
|
||||||
|
__fsel() {
|
||||||
|
find * -path '*/\.*' -prune \
|
||||||
|
-o -type f -print \
|
||||||
|
-o -type l -print 2> /dev/null | fzf -m | while read item; do
|
||||||
|
printf '%q ' "$item"
|
||||||
|
done
|
||||||
|
echo
|
||||||
|
}
|
||||||
|
bind '"\C-t": " \C-u \C-a\C-k$(__fsel)\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"'
|
||||||
|
|
||||||
|
EOF
|
||||||
|
else
|
||||||
|
cat >> $src << "EOF"
|
||||||
|
# Key bindings
|
||||||
|
# ------------
|
||||||
|
# CTRL-T - Paste the selected file path(s) 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
|
||||||
|
|
||||||
|
EOF
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "OK"
|
||||||
|
done
|
||||||
|
|
||||||
|
echo
|
||||||
|
for shell in bash zsh; do
|
||||||
|
rc=~/.${shell}rc
|
||||||
|
src="source ~/.fzf.${shell}"
|
||||||
|
|
||||||
|
echo "Update $rc:"
|
||||||
|
echo " - $src"
|
||||||
|
if [ $(grep -F "$src" $rc | wc -l) -gt 0 ]; then
|
||||||
|
echo " - Not added (already being sourced)"
|
||||||
|
else
|
||||||
|
echo $src >> $rc
|
||||||
|
echo " - Added"
|
||||||
|
fi
|
||||||
|
echo
|
||||||
|
done
|
||||||
|
|
||||||
|
cat << EOF
|
||||||
|
Finished. Reload your .bashrc or .zshrc.
|
||||||
|
source ~/.bashrc # bash
|
||||||
|
source ~/.zshrc # zsh
|
||||||
|
|
||||||
|
To uninstall fzf, simply remove the added line.
|
||||||
|
EOF
|
||||||
|
|
||||||
|
@@ -23,23 +23,37 @@
|
|||||||
|
|
||||||
let s:exec = expand('<sfile>:h:h').'/fzf'
|
let s:exec = expand('<sfile>:h:h').'/fzf'
|
||||||
|
|
||||||
function! s:fzf(args)
|
function! s:escape(path)
|
||||||
|
return substitute(a:path, ' ', '\\ ', 'g')
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! fzf#run(command, ...)
|
||||||
|
let cwd = getcwd()
|
||||||
try
|
try
|
||||||
let tf = tempname()
|
let args = copy(a:000)
|
||||||
let prefix = exists('g:fzf_command') ? g:fzf_command.'|' : ''
|
if len(args) > 0 && isdirectory(expand(args[-1]))
|
||||||
let fzf = executable(s:exec) ? s:exec : 'fzf'
|
let dir = remove(args, -1)
|
||||||
execute "silent !".prefix.fzf." ".a:args." > ".tf
|
execute 'chdir '.s:escape(dir)
|
||||||
|
endif
|
||||||
|
let argstr = join(args)
|
||||||
|
let tf = tempname()
|
||||||
|
let prefix = exists('g:fzf_source') ? g:fzf_source.'|' : ''
|
||||||
|
let fzf = executable(s:exec) ? s:exec : 'fzf'
|
||||||
|
let options = empty(argstr) ? get(g:, 'fzf_options', '') : argstr
|
||||||
|
execute "silent !".prefix.fzf.' '.options." > ".tf
|
||||||
if !v:shell_error
|
if !v:shell_error
|
||||||
let file = join(readfile(tf), '')
|
for line in readfile(tf)
|
||||||
if !empty(file)
|
if !empty(line)
|
||||||
execute 'silent e '.file
|
execute a:command.' '.s:escape(line)
|
||||||
endif
|
endif
|
||||||
|
endfor
|
||||||
endif
|
endif
|
||||||
finally
|
finally
|
||||||
silent! call delete(tf)
|
execute 'chdir '.s:escape(cwd)
|
||||||
redraw!
|
redraw!
|
||||||
|
silent! call delete(tf)
|
||||||
endtry
|
endtry
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
command! -nargs=* FZF call s:fzf(<q-args>)
|
command! -nargs=* -complete=dir FZF call fzf#run('silent e', <f-args>)
|
||||||
|
|
||||||
|
429
test/test_fzf.rb
Normal file
429
test/test_fzf.rb
Normal file
@@ -0,0 +1,429 @@
|
|||||||
|
#!/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 setup
|
||||||
|
ENV.delete 'FZF_DEFAULT_SORT'
|
||||||
|
ENV.delete 'FZF_DEFAULT_OPTS'
|
||||||
|
ENV.delete 'FZF_DEFAULT_COMMAND'
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_default_options
|
||||||
|
fzf = FZF.new []
|
||||||
|
assert_equal 1000, fzf.sort
|
||||||
|
assert_equal false, fzf.multi
|
||||||
|
assert_equal true, fzf.color
|
||||||
|
assert_equal nil, fzf.rxflag
|
||||||
|
assert_equal true, fzf.mouse
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_environment_variables
|
||||||
|
# Deprecated
|
||||||
|
ENV['FZF_DEFAULT_SORT'] = '20000'
|
||||||
|
fzf = FZF.new []
|
||||||
|
assert_equal 20000, fzf.sort
|
||||||
|
|
||||||
|
ENV['FZF_DEFAULT_OPTS'] = '-x -m -s 10000 -q " hello world " +c --no-mouse'
|
||||||
|
fzf = FZF.new []
|
||||||
|
assert_equal 10000, fzf.sort
|
||||||
|
assert_equal ' hello world ',
|
||||||
|
fzf.query.get
|
||||||
|
assert_equal true, fzf.extended
|
||||||
|
assert_equal true, fzf.multi
|
||||||
|
assert_equal false, fzf.color
|
||||||
|
assert_equal false, fzf.mouse
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_option_parser
|
||||||
|
# Long opts
|
||||||
|
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello --extended --no-mouse]
|
||||||
|
assert_equal 2000, fzf.sort
|
||||||
|
assert_equal true, fzf.multi
|
||||||
|
assert_equal false, fzf.color
|
||||||
|
assert_equal false, fzf.mouse
|
||||||
|
assert_equal 0, fzf.rxflag
|
||||||
|
assert_equal 'hello', fzf.query.get
|
||||||
|
assert_equal true, fzf.extended
|
||||||
|
|
||||||
|
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello
|
||||||
|
--no-sort -i --color --no-multi]
|
||||||
|
assert_equal nil, fzf.sort
|
||||||
|
assert_equal false, fzf.multi
|
||||||
|
assert_equal true, fzf.color
|
||||||
|
assert_equal true, fzf.mouse
|
||||||
|
assert_equal 1, fzf.rxflag
|
||||||
|
assert_equal 'hello', fzf.query.get
|
||||||
|
assert_equal false, fzf.extended
|
||||||
|
|
||||||
|
# Short opts
|
||||||
|
fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x]
|
||||||
|
assert_equal 2000, fzf.sort
|
||||||
|
assert_equal true, fzf.multi
|
||||||
|
assert_equal false, fzf.color
|
||||||
|
assert_equal 0, fzf.rxflag
|
||||||
|
assert_equal 'hello', fzf.query.get
|
||||||
|
assert_equal true, fzf.extended
|
||||||
|
|
||||||
|
# Left-to-right
|
||||||
|
fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x
|
||||||
|
-s 3000 -c +m -i -q world +x]
|
||||||
|
assert_equal 3000, fzf.sort
|
||||||
|
assert_equal false, fzf.multi
|
||||||
|
assert_equal true, fzf.color
|
||||||
|
assert_equal 1, fzf.rxflag
|
||||||
|
assert_equal 'world', fzf.query.get
|
||||||
|
assert_equal false, fzf.extended
|
||||||
|
|
||||||
|
fzf = FZF.new %w[--query hello +s -s 2000 --query=world]
|
||||||
|
assert_equal 2000, fzf.sort
|
||||||
|
assert_equal 'world', fzf.query.get
|
||||||
|
rescue SystemExit => e
|
||||||
|
assert false, "Exited"
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_invalid_option
|
||||||
|
[%w[--unknown], %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_rxflag
|
||||||
|
assert_equal nil, FZF::FuzzyMatcher.new(nil).rxflag
|
||||||
|
assert_equal 0, FZF::FuzzyMatcher.new(0).rxflag
|
||||||
|
assert_equal 1, FZF::FuzzyMatcher.new(1).rxflag
|
||||||
|
|
||||||
|
assert_equal 1, FZF::FuzzyMatcher.new(nil).rxflag_for('abc')
|
||||||
|
assert_equal 0, FZF::FuzzyMatcher.new(nil).rxflag_for('Abc')
|
||||||
|
assert_equal 0, FZF::FuzzyMatcher.new(0).rxflag_for('abc')
|
||||||
|
assert_equal 0, FZF::FuzzyMatcher.new(0).rxflag_for('Abc')
|
||||||
|
assert_equal 1, FZF::FuzzyMatcher.new(1).rxflag_for('abc')
|
||||||
|
assert_equal 1, FZF::FuzzyMatcher.new(1).rxflag_for('Abc')
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_fuzzy_matcher_case_sensitive
|
||||||
|
# Smart-case match (Uppercase found)
|
||||||
|
assert_equal [['Fruit', [[0, 5]]]],
|
||||||
|
FZF::FuzzyMatcher.new(nil).match(%w[Fruit Grapefruit], 'Fruit', '', '').sort
|
||||||
|
|
||||||
|
# Smart-case match (Uppercase not-found)
|
||||||
|
assert_equal [["Fruit", [[0, 5]]], ["Grapefruit", [[5, 10]]]],
|
||||||
|
FZF::FuzzyMatcher.new(nil).match(%w[Fruit Grapefruit], 'fruit', '', '').sort
|
||||||
|
|
||||||
|
# Case-sensitive match (-i)
|
||||||
|
assert_equal [['Fruit', [[0, 5]]]],
|
||||||
|
FZF::FuzzyMatcher.new(0).match(%w[Fruit Grapefruit], 'Fruit', '', '').sort
|
||||||
|
|
||||||
|
# Case-insensitive match (+i)
|
||||||
|
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_case_sensitive
|
||||||
|
%w['Fruit Fruit$].each do |q|
|
||||||
|
# Smart-case match (Uppercase found)
|
||||||
|
assert_equal [['Fruit', [[0, 5]]]],
|
||||||
|
FZF::ExtendedFuzzyMatcher.new(nil).match(%w[Fruit Grapefruit], q, '', '').sort
|
||||||
|
|
||||||
|
# Smart-case match (Uppercase not-found)
|
||||||
|
assert_equal [["Fruit", [[0, 5]]], ["Grapefruit", [[5, 10]]]],
|
||||||
|
FZF::ExtendedFuzzyMatcher.new(nil).match(%w[Fruit Grapefruit], q.downcase, '', '').sort
|
||||||
|
|
||||||
|
# Case-sensitive match (-i)
|
||||||
|
assert_equal [['Fruit', [[0, 5]]]],
|
||||||
|
FZF::ExtendedFuzzyMatcher.new(0).match(%w[Fruit Grapefruit], q, '', '').sort
|
||||||
|
|
||||||
|
# Case-insensitive match (+i)
|
||||||
|
assert_equal [["Fruit", [[0, 5]]], ["Grapefruit", [[5, 10]]]],
|
||||||
|
FZF::ExtendedFuzzyMatcher.new(Regexp::IGNORECASE).
|
||||||
|
match(%w[Fruit Grapefruit], q, '', '').sort
|
||||||
|
end
|
||||||
|
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('^juici$', '').length
|
||||||
|
assert_equal 1, match.call('^juice$', '').length
|
||||||
|
assert_equal 0, match.call('^.*$', '').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 2, nfd.length
|
||||||
|
assert_equal 6, nfd.join.length
|
||||||
|
assert_equal NFD, nfd.join
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_nfd_fuzzy_matcher
|
||||||
|
matcher = FZF::FuzzyMatcher.new 0
|
||||||
|
assert_equal [], matcher.match([NFD + NFD], '할', '', '')
|
||||||
|
match = matcher.match([NFD + NFD], '글글', '', '')
|
||||||
|
assert_equal [[NFD + NFD, [[3, 12]]]], match
|
||||||
|
assert_equal ['한글한글', [[1, 4]]], FZF::UConv.nfc(*match.first)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_nfd_extended_fuzzy_matcher
|
||||||
|
matcher = FZF::ExtendedFuzzyMatcher.new 0
|
||||||
|
assert_equal [], matcher.match([NFD], "'글글", '', '')
|
||||||
|
match = matcher.match([NFD], "'한글", '', '')
|
||||||
|
assert_equal [[NFD, [[0, 6]]]], match
|
||||||
|
assert_equal ['한글', [[0, 2]]], FZF::UConv.nfc(*match.first)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_split
|
||||||
|
assert_equal ["a", "b", "c", "\xFF", "d", "e", "f"],
|
||||||
|
FZF::UConv.split("abc\xFFdef")
|
||||||
|
end
|
||||||
|
|
||||||
|
# ^$ -> matches empty item
|
||||||
|
def test_format_empty_item
|
||||||
|
fzf = FZF.new []
|
||||||
|
item = ['', [[0, 0]]]
|
||||||
|
line, offsets = fzf.convert_item item
|
||||||
|
tokens = fzf.format line, 80, offsets
|
||||||
|
assert_equal [], tokens
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
Reference in New Issue
Block a user