mirror of
https://github.com/junegunn/fzf.git
synced 2025-08-01 20:52:06 -07:00
Compare commits
175 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
f6b1a6278f | ||
|
db58182483 | ||
|
6e9f0882da | ||
|
7ed18579dc | ||
|
f250fc8f86 | ||
|
6eea9603c2 | ||
|
20915529b7 | ||
|
b3efccca81 | ||
|
809d465de5 | ||
|
7d15071c63 | ||
|
89eb1575e7 | ||
|
5d6ed935a4 | ||
|
0528435386 | ||
|
fe22213b51 | ||
|
aab42eaaba | ||
|
16031b0d54 | ||
|
ded184daaf | ||
|
ecf90bd25b | ||
|
d82e38adc1 | ||
|
af677e7e35 | ||
|
6ad38bdad3 | ||
|
8b80136a87 | ||
|
97de919152 | ||
|
0eafa725b9 | ||
|
fa212efe5f | ||
|
a9056ce90c | ||
|
16682a3f92 | ||
|
02c01c81a0 | ||
|
22d3929ae3 | ||
|
ab9fbf1967 | ||
|
608ec2b806 | ||
|
e5ae4f0ef6 | ||
|
67ba87d390 | ||
|
77d45cb173 | ||
|
d83febea46 | ||
|
546a315884 | ||
|
af616457e3 | ||
|
1a100a2919 | ||
|
a85bb93b69 | ||
|
057eda060c | ||
|
48f9ee6763 | ||
|
52b74abb99 | ||
|
ec4b8a59fa | ||
|
cf8dbf8047 | ||
|
995d380200 | ||
|
ae86cdf09a | ||
|
2b346659a0 | ||
|
49081711a9 | ||
|
e7439ce193 | ||
|
b8e438b6be | ||
|
678e950b6d | ||
|
9ea651f1cd | ||
|
bd98a08b89 | ||
|
f02bb4fdac | ||
|
0a8352a5cd | ||
|
737423995d | ||
|
2916bf7ee4 | ||
|
fa54c5d9b0 | ||
|
693b6651b4 | ||
|
5c71ecb267 | ||
|
1ba50eba98 | ||
|
2c8a256b13 | ||
|
f4c5aa03d7 | ||
|
c6acb2a639 | ||
|
2296013174 | ||
|
8a3e8c2d81 | ||
|
ae84d8c7a4 | ||
|
dbd627c38a | ||
|
d172c3ce03 | ||
|
9904f5354e | ||
|
f345bf7983 | ||
|
875f9b6534 | ||
|
871dfb709d | ||
|
19e24bd644 | ||
|
457a240457 | ||
|
bbf4567dd8 | ||
|
27d3b52843 | ||
|
dcb4694ec1 | ||
|
2fb8ae010f | ||
|
65ae6cabb5 | ||
|
86a66da04d | ||
|
d66b02b0cd | ||
|
b3182c3304 | ||
|
2dbca00bfb | ||
|
b22fd6de6d | ||
|
245ee42763 | ||
|
98bef4600c | ||
|
f5d53b94fe | ||
|
00c8a68430 | ||
|
c1be834ff9 | ||
|
2c0dc2f3b1 | ||
|
1c94fef720 | ||
|
b711d76b8e | ||
|
4396ab7548 | ||
|
2b8c2b9f2a | ||
|
426284c87e | ||
|
089691faaf | ||
|
301290663d | ||
|
1155da7e1c | ||
|
eca0a99fb4 | ||
|
96215c4619 | ||
|
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 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
pkg
|
pkg
|
||||||
Gemfile.lock
|
Gemfile.lock
|
||||||
|
.DS_Store
|
||||||
|
515
README.md
515
README.md
@@ -16,54 +16,43 @@ 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.
|
In case you don't have git installed:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
export PATH=$PATH:~/bin
|
mkdir -p ~/.fzf
|
||||||
|
curl -L https://github.com/junegunn/fzf/archive/master.tar.gz |
|
||||||
|
tar xz --strip-components 1 -C ~/.fzf
|
||||||
|
~/.fzf/install
|
||||||
```
|
```
|
||||||
|
|
||||||
### Install as Ruby gem
|
The script will setup:
|
||||||
|
|
||||||
fzf can be installed as a Ruby gem
|
- `fzf` function (bash, zsh, fish)
|
||||||
|
- Key bindings (`CTRL-T`, `CTRL-R`, and `ALT-C`) (bash, zsh, fish)
|
||||||
|
- Fuzzy auto-completion (bash)
|
||||||
|
|
||||||
```
|
If you don't use any of the aforementioned shells, you have to manually place
|
||||||
gem install fzf
|
fzf executable in a directory included in `$PATH`. Key bindings and
|
||||||
```
|
auto-completion will not be available in that case.
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
You can use any Vim plugin manager to install fzf for Vim. If you don't use one,
|
Once you have cloned the repository, add the following line to your .vimrc.
|
||||||
I recommend you try [vim-plug](https://github.com/junegunn/vim-plug).
|
|
||||||
|
|
||||||
1. [Install vim-plug](https://github.com/junegunn/vim-plug#usage)
|
```vim
|
||||||
2. Edit your .vimrc
|
set rtp+=~/.fzf
|
||||||
|
```
|
||||||
|
|
||||||
call plug#begin()
|
Or you may use any Vim plugin manager, such as
|
||||||
Plug 'junegunn/fzf'
|
[vim-plug](https://github.com/junegunn/vim-plug).
|
||||||
" ...
|
|
||||||
call plug#end()
|
|
||||||
|
|
||||||
3. Run `:PlugInstall`
|
|
||||||
|
|
||||||
Usage
|
Usage
|
||||||
-----
|
-----
|
||||||
@@ -71,12 +60,36 @@ Usage
|
|||||||
```
|
```
|
||||||
usage: fzf [options]
|
usage: fzf [options]
|
||||||
|
|
||||||
-m, --multi Enable multi-select
|
Search
|
||||||
-x, --extended Extended-search mode
|
-x, --extended Extended-search mode
|
||||||
-s, --sort=MAX Maximum number of matched items to sort. Default: 1000
|
-e, --extended-exact Extended-search mode (exact match)
|
||||||
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
-i Case-insensitive match (default: smart-case match)
|
||||||
+i Case-sensitive match
|
+i Case-sensitive match
|
||||||
+c, --no-color Disable colors
|
-n, --nth=[-]N[,..] Comma-separated list of field indexes for limiting
|
||||||
|
search scope (positive or negative integers)
|
||||||
|
-d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)
|
||||||
|
|
||||||
|
Search result
|
||||||
|
-s, --sort=MAX Maximum number of matched items to sort (default: 1000)
|
||||||
|
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
||||||
|
|
||||||
|
Interface
|
||||||
|
-m, --multi Enable multi-select with tab/shift-tab
|
||||||
|
--no-mouse Disable mouse
|
||||||
|
+c, --no-color Disable colors
|
||||||
|
+2, --no-256 Disable 256-color
|
||||||
|
--black Use black background
|
||||||
|
--reverse Reverse orientation
|
||||||
|
|
||||||
|
Scripting
|
||||||
|
-q, --query=STR Start the finder with the given query
|
||||||
|
-1, --select-1 Automatically select the only match
|
||||||
|
-0, --exit-0 Exit immediately when there's no match
|
||||||
|
-f, --filter=STR Filter mode. Do not start interactive finder.
|
||||||
|
|
||||||
|
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
|
||||||
@@ -101,21 +114,25 @@ If you want to preserve the exact sequence of the input, provide `--no-sort` (or
|
|||||||
history | fzf +s
|
history | fzf +s
|
||||||
```
|
```
|
||||||
|
|
||||||
### Key binding
|
### Keys
|
||||||
|
|
||||||
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 / CTRL-Y
|
||||||
- ALT-B / ALT-F
|
- 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.
|
||||||
|
|
||||||
|
You can also use mouse. Double-click on an item to select it or shift-click (or
|
||||||
|
ctrl-click) to select multiple items. Use mouse wheel to move the cursor up and
|
||||||
|
down.
|
||||||
|
|
||||||
### Extended-search mode
|
### Extended-search mode
|
||||||
|
|
||||||
With `-x` or `--extended` option, fzf will start in "extended-search mode".
|
With `-x` or `--extended` option, fzf will start in "extended-search mode".
|
||||||
@@ -132,60 +149,39 @@ such as: `^music .mp3$ sbtrkt !rmx`
|
|||||||
| `'wild` | Items that include `wild` | exact-match (quoted) |
|
| `'wild` | Items that include `wild` | exact-match (quoted) |
|
||||||
| `!'fire` | Items that do not include `fire` | inverse-exact-match |
|
| `!'fire` | Items that do not include `fire` | inverse-exact-match |
|
||||||
|
|
||||||
Usage as Vim plugin
|
If you don't need fuzzy matching and do not wish to "quote" every word, start
|
||||||
-------------------
|
fzf with `-e` or `--extended-exact` option.
|
||||||
|
|
||||||
If you install fzf as a Vim plugin, `:FZF` command will be added.
|
Useful examples
|
||||||
|
---------------
|
||||||
```vim
|
|
||||||
:FZF
|
|
||||||
:FZF --no-sort -m
|
|
||||||
```
|
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
Useful bash examples
|
|
||||||
--------------------
|
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
# vimf - Open selected file in Vim
|
# fe [FUZZY PATTERN] - Open the selected file with the default editor
|
||||||
vimf() {
|
# - Bypass fuzzy finder if there's only one match (--select-1)
|
||||||
FILE=$(fzf) && vim "$FILE"
|
# - Exit if there's no match (--exit-0)
|
||||||
|
fe() {
|
||||||
|
local file
|
||||||
|
file=$(fzf --query="$1" --select-1 --exit-0)
|
||||||
|
[ -n "$file" ] && ${EDITOR:-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"
|
local dir
|
||||||
|
dir=$(find ${1:-*} -path '*/\.*' -prune \
|
||||||
|
-o -type d -print 2> /dev/null | fzf +m) &&
|
||||||
|
cd "$dir"
|
||||||
}
|
}
|
||||||
|
|
||||||
# fda - including hidden directories
|
# fda - including hidden directories
|
||||||
fda() {
|
fda() {
|
||||||
DIR=$(find ${1:-*} -type d 2> /dev/null | fzf) && cd "$DIR"
|
local dir
|
||||||
|
dir=$(find ${1:-.} -type d 2> /dev/null | fzf +m) && cd "$dir"
|
||||||
}
|
}
|
||||||
|
|
||||||
# fh - repeat history
|
# fh - repeat history
|
||||||
fh() {
|
fh() {
|
||||||
eval $(history | fzf +s | sed 's/ *[0-9]* *//')
|
eval $(([ -n "$ZSH_NAME" ] && fc -l 1 || history) | fzf +s | sed 's/ *[0-9]* *//')
|
||||||
}
|
}
|
||||||
|
|
||||||
# fkill - kill process
|
# fkill - kill process
|
||||||
@@ -193,80 +189,333 @@ 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)
|
# fbr - checkout git branch
|
||||||
|
fbr() {
|
||||||
|
local branches branch
|
||||||
|
branches=$(git branch) &&
|
||||||
|
branch=$(echo "$branches" | fzf +s +m) &&
|
||||||
|
git checkout $(echo "$branch" | sed "s/.* //")
|
||||||
|
}
|
||||||
|
|
||||||
# CTRL-T - Paste the selected file path into the command line
|
# fco - checkout git commit
|
||||||
bind '"\er": redraw-current-line'
|
fco() {
|
||||||
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"'
|
local commits commit
|
||||||
|
commits=$(git log --pretty=oneline --abbrev-commit --reverse) &&
|
||||||
|
commit=$(echo "$commits" | fzf +s +m -e) &&
|
||||||
|
git checkout $(echo "$commit" | sed "s/ .*//")
|
||||||
|
}
|
||||||
|
|
||||||
# CTRL-R - Paste the selected command from history into the command line
|
# ftags - search ctags
|
||||||
bind '"\C-r": " \C-e\C-u$(history | fzf +s | sed \"s/ *[0-9]* *//\")\e\C-e\er"'
|
ftags() {
|
||||||
|
local line
|
||||||
|
[ -e tags ] &&
|
||||||
|
line=$(
|
||||||
|
awk 'BEGIN { FS="\t" } !/^!/ {print toupper($4)"\t"$1"\t"$2"\t"$3}' tags |
|
||||||
|
cut -c1-80 | fzf --nth=1,2
|
||||||
|
) && $EDITOR $(cut -f3 <<< "$line") -c "set nocst" \
|
||||||
|
-c "silent tag $(cut -f2 <<< "$line")"
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
zsh widgets
|
For more examples, see [the wiki
|
||||||
-----------
|
page](https://github.com/junegunn/fzf/wiki/examples).
|
||||||
|
|
||||||
|
Key bindings for command line
|
||||||
|
-----------------------------
|
||||||
|
|
||||||
|
The install script will setup the following key bindings for bash, zsh, and
|
||||||
|
fish.
|
||||||
|
|
||||||
|
- `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
|
||||||
|
|
||||||
|
If you're on a tmux session, `CTRL-T` will launch fzf in a new split-window. You
|
||||||
|
may disable this tmux integration by setting `FZF_TMUX` to 0, or change the
|
||||||
|
height of the window with `FZF_TMUX_HEIGHT` (e.g. `20`, `50%`).
|
||||||
|
|
||||||
|
If you use vi mode on bash, you need to add `set -o vi` *before* `source
|
||||||
|
~/.fzf.bash` in your .bashrc, so that it correctly sets up key bindings for vi
|
||||||
|
mode.
|
||||||
|
|
||||||
|
If you want to customize the key bindings, consider editing the
|
||||||
|
installer-generated source code: `~/.fzf.bash`, `~/.fzf.zsh`, and
|
||||||
|
`~/.config/fish/functions/fzf_key_bindings.fish`.
|
||||||
|
|
||||||
|
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
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
(fzf is a command-line utility, naturally it is only accessible in terminal Vim)
|
||||||
|
|
||||||
|
### `:FZF[!]`
|
||||||
|
|
||||||
|
If you have set up fzf for Vim, `: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
|
||||||
|
```
|
||||||
|
|
||||||
|
Note that the environment variables `FZF_DEFAULT_COMMAND` and `FZF_DEFAULT_OPTS`
|
||||||
|
also apply here.
|
||||||
|
|
||||||
|
If you're on a tmux session, `:FZF` will launch fzf in a new split-window whose
|
||||||
|
height can be adjusted with `g:fzf_tmux_height` (default: '40%'). However, the
|
||||||
|
bang version (`:FZF!`) will always start in fullscreen.
|
||||||
|
|
||||||
|
### `fzf#run([options])`
|
||||||
|
|
||||||
|
For more advanced uses, you can call `fzf#run()` function which returns the list
|
||||||
|
of the selected items.
|
||||||
|
|
||||||
|
`fzf#run()` may take an options-dictionary:
|
||||||
|
|
||||||
|
| Option name | Type | Description |
|
||||||
|
| ------------- | ------------- | ------------------------------------------------------------------ |
|
||||||
|
| `source` | string | External command to generate input to fzf (e.g. `find .`) |
|
||||||
|
| `source` | list | Vim list as input to fzf |
|
||||||
|
| `sink` | string | Vim command to handle the selected item (e.g. `e`, `tabe`) |
|
||||||
|
| `sink` | funcref | Reference to function to process each selected item |
|
||||||
|
| `options` | string | Options to fzf |
|
||||||
|
| `dir` | string | Working directory |
|
||||||
|
| `tmux_width` | number/string | Use tmux vertical split with the given height (e.g. `20`, `50%`) |
|
||||||
|
| `tmux_height` | number/string | Use tmux horizontal split with the given height (e.g. `20`, `50%`) |
|
||||||
|
|
||||||
|
#### Examples
|
||||||
|
|
||||||
|
If `sink` option is not given, `fzf#run` will simply return the list.
|
||||||
|
|
||||||
|
```vim
|
||||||
|
let items = fzf#run({ 'options': '-m +c', 'dir': '~', 'source': 'ls' })
|
||||||
|
```
|
||||||
|
|
||||||
|
But if `sink` is given as a string, the command will be executed for each
|
||||||
|
selected item.
|
||||||
|
|
||||||
|
```vim
|
||||||
|
" Each selected item will be opened in a new tab
|
||||||
|
let items = fzf#run({ 'sink': 'tabe', 'options': '-m +c', 'dir': '~', 'source': 'ls' })
|
||||||
|
```
|
||||||
|
|
||||||
|
We can also use a Vim list as the source as follows:
|
||||||
|
|
||||||
|
```vim
|
||||||
|
" Choose a color scheme with fzf
|
||||||
|
nnoremap <silent> <Leader>C :call fzf#run({
|
||||||
|
\ 'source':
|
||||||
|
\ map(split(globpath(&rtp, "colors/*.vim"), "\n"),
|
||||||
|
\ "substitute(fnamemodify(v:val, ':t'), '\\..\\{-}$', '', '')"),
|
||||||
|
\ 'sink': 'colo',
|
||||||
|
\ 'options': '+m',
|
||||||
|
\ 'tmux_width': 20
|
||||||
|
\ })<CR>
|
||||||
|
```
|
||||||
|
|
||||||
|
`sink` option can be a function reference. The following example creates a
|
||||||
|
handy mapping that selects an open buffer.
|
||||||
|
|
||||||
|
```vim
|
||||||
|
" List of buffers
|
||||||
|
function! g:buflist()
|
||||||
|
redir => ls
|
||||||
|
silent ls
|
||||||
|
redir END
|
||||||
|
return split(ls, '\n')
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! g:bufopen(e)
|
||||||
|
execute 'buffer '. matchstr(a:e, '^[ 0-9]*')
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
nnoremap <silent> <Leader><Enter> :call fzf#run({
|
||||||
|
\ 'source': reverse(g:buflist()),
|
||||||
|
\ 'sink': function('g:bufopen'),
|
||||||
|
\ 'options': '+m',
|
||||||
|
\ 'tmux_height': '40%'
|
||||||
|
\ })<CR>
|
||||||
|
```
|
||||||
|
|
||||||
|
### Articles
|
||||||
|
|
||||||
|
- [fzf+vim+tmux](http://junegunn.kr/2014/04/fzf+vim+tmux)
|
||||||
|
|
||||||
Tips
|
Tips
|
||||||
----
|
----
|
||||||
|
|
||||||
### Faster startup with `--disable-gems` options
|
### Rendering issues
|
||||||
|
|
||||||
If you're running Ruby 1.9 or above, you can improve the startup time with
|
If you have any rendering issues, check the followings:
|
||||||
`--disable-gems` option to Ruby.
|
|
||||||
|
|
||||||
- `time ruby ~/bin/fzf -h`
|
1. Make sure `$TERM` is correctly set. fzf will use 256-color only if it
|
||||||
- 0.077 sec
|
contains `256` (e.g. `xterm-256color`)
|
||||||
- `time ruby --disable-gems ~/bin/fzf -h`
|
2. If you're on screen or tmux, `$TERM` should be either `screen` or
|
||||||
- 0.025 sec
|
`screen-256color`
|
||||||
|
3. Some terminal emulators (e.g. mintty) have problem displaying default
|
||||||
|
background color and make some text unable to read. In that case, try `--black`
|
||||||
|
option. And if it solves your problem, I recommend including it in
|
||||||
|
`FZF_DEFAULT_OPTS` for further convenience.
|
||||||
|
4. If you still have problem, try `--no-256` option or even `--no-color`.
|
||||||
|
5. Ruby 1.9 or above is required for correctly displaying unicode characters.
|
||||||
|
|
||||||
Define fzf alias with the option as follows:
|
### 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
|
```sh
|
||||||
alias fzf='ruby --disable-gems ~/bin/fzf'
|
export FZF_DEFAULT_OPTS="--sort 20000"
|
||||||
```
|
```
|
||||||
|
|
||||||
### Incorrect display on Ruby 1.8
|
### Respecting `.gitignore`, `.hgignore`, and `svn:ignore`
|
||||||
|
|
||||||
|
[ag](https://github.com/ggreer/the_silver_searcher) or
|
||||||
|
[pt](https://github.com/monochromegane/the_platinum_searcher) will do the
|
||||||
|
filtering:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# Feed the output of ag into fzf
|
||||||
|
ag -l -g "" | fzf
|
||||||
|
|
||||||
|
# Setting ag as the default source for fzf
|
||||||
|
export FZF_DEFAULT_COMMAND='ag -l -g ""'
|
||||||
|
|
||||||
|
# Now fzf (w/o pipe) will use ag instead of find
|
||||||
|
fzf
|
||||||
|
```
|
||||||
|
|
||||||
|
### `git ls-tree` for fast traversal
|
||||||
|
|
||||||
|
If you're running fzf in a large git repository, `git ls-tree` can boost up the
|
||||||
|
speed of the traversal.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# Copy the original fzf function to __fzf
|
||||||
|
declare -f __fzf > /dev/null ||
|
||||||
|
eval "$(echo "__fzf() {"; declare -f fzf | grep -v '^{' | tail -n +2)"
|
||||||
|
|
||||||
|
# Use git ls-tree when possible
|
||||||
|
fzf() {
|
||||||
|
if [ -n "$(git rev-parse HEAD 2> /dev/null)" ]; then
|
||||||
|
FZF_DEFAULT_COMMAND="git ls-tree -r --name-only HEAD" __fzf "$@"
|
||||||
|
else
|
||||||
|
__fzf "$@"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Fish shell
|
||||||
|
|
||||||
|
It's [a known bug of fish](https://github.com/fish-shell/fish-shell/issues/1362)
|
||||||
|
that it doesn't allow reading from STDIN in command substitution, which means
|
||||||
|
simple `vim (fzf)` won't work as expected. The workaround is to store the result
|
||||||
|
of fzf to a temporary file.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
function vimf
|
||||||
|
if fzf > $TMPDIR/fzf.result
|
||||||
|
vim (cat $TMPDIR/fzf.result)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function fe
|
||||||
|
set tmp $TMPDIR/fzf.result
|
||||||
|
fzf --query="$argv[1]" --select-1 --exit-0 > $tmp
|
||||||
|
if [ (cat $tmp | wc -l) -gt 0 ]
|
||||||
|
vim (cat $tmp)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
### Windows
|
||||||
|
|
||||||
|
fzf works on [Cygwin](http://www.cygwin.com/) and
|
||||||
|
[MSYS2](http://sourceforge.net/projects/msys2/). You may need to use `--black`
|
||||||
|
option on MSYS2 to avoid rendering issues.
|
||||||
|
|
||||||
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
|
||||||
-------
|
-------
|
||||||
|
9
ext/mkrf_conf.rb
Normal file
9
ext/mkrf_conf.rb
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
require 'rubygems/dependency_installer'
|
||||||
|
|
||||||
|
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.1.0')
|
||||||
|
Gem::DependencyInstaller.new.install 'curses', '~> 1.0'
|
||||||
|
end
|
||||||
|
|
||||||
|
File.open(File.expand_path('../Rakefile', __FILE__), 'w') do |f|
|
||||||
|
f.puts 'task :default'
|
||||||
|
end
|
753
fzf
753
fzf
@@ -7,12 +7,13 @@
|
|||||||
# / __/ / /_/ __/
|
# / __/ / /_/ __/
|
||||||
# /_/ /___/_/ Fuzzy finder for your shell
|
# /_/ /___/_/ Fuzzy finder for your shell
|
||||||
#
|
#
|
||||||
# URL: https://github.com/junegunn/fzf
|
# Version: 0.8.4 (May 17, 2014)
|
||||||
# Author: Junegunn Choi
|
|
||||||
# License: MIT
|
|
||||||
# Last update: November 17, 2013
|
|
||||||
#
|
#
|
||||||
# Copyright (c) 2013 Junegunn Choi
|
# Author: Junegunn Choi
|
||||||
|
# URL: https://github.com/junegunn/fzf
|
||||||
|
# License: MIT
|
||||||
|
#
|
||||||
|
# Copyright (c) 2014 Junegunn Choi
|
||||||
#
|
#
|
||||||
# MIT License
|
# MIT License
|
||||||
#
|
#
|
||||||
@@ -39,9 +40,18 @@ require 'thread'
|
|||||||
require 'curses'
|
require 'curses'
|
||||||
require 'set'
|
require 'set'
|
||||||
|
|
||||||
|
unless String.method_defined? :force_encoding
|
||||||
|
class String
|
||||||
|
def force_encoding *arg
|
||||||
|
self
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class FZF
|
class FZF
|
||||||
C = Curses
|
C = Curses
|
||||||
attr_reader :rxflag, :sort, :color, :multi
|
attr_reader :rxflag, :sort, :nth, :color, :black, :ansi256, :reverse,
|
||||||
|
:mouse, :multi, :query, :select1, :exit0, :filter, :extended
|
||||||
|
|
||||||
class AtomicVar
|
class AtomicVar
|
||||||
def initialize value
|
def initialize value
|
||||||
@@ -54,10 +64,8 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
def set value = nil
|
def set value = nil
|
||||||
if block_given?
|
@mutex.synchronize do
|
||||||
@mutex.synchronize { @value = yield @value }
|
@value = block_given? ? yield(@value) : value
|
||||||
else
|
|
||||||
@mutex.synchronize { @value = value }
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -67,59 +75,237 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
def initialize argv, source = $stdin
|
def initialize argv, source = $stdin
|
||||||
usage 0 unless (%w[--help -h] & argv).empty?
|
@rxflag = nil
|
||||||
@rxflag = argv.delete('+i') ? 0 : Regexp::IGNORECASE
|
@sort = ENV.fetch('FZF_DEFAULT_SORT', 1000).to_i
|
||||||
@sort = %w[+s --no-sort].map { |e| argv.delete e }.compact.empty? ?
|
@color = true
|
||||||
ENV.fetch('FZF_DEFAULT_SORT', 1000).to_i : nil
|
@ansi256 = true
|
||||||
@color = %w[+c --no-color].map { |e| argv.delete e }.compact.empty?
|
@black = false
|
||||||
@multi = !%w[-m --multi].map { |e| argv.delete e }.compact.empty?
|
@multi = false
|
||||||
@xmode = !%w[-x --extended].map { |e| argv.delete e }.compact.empty?
|
@mouse = true
|
||||||
rest = argv.join ' '
|
@extended = nil
|
||||||
if sort = rest.match(/(-s|--sort=?) ?([0-9]+)/)
|
@select1 = false
|
||||||
usage 1 unless @sort
|
@exit0 = false
|
||||||
@sort = sort[2].to_i
|
@filter = nil
|
||||||
rest = rest.delete sort[0]
|
@nth = nil
|
||||||
end
|
@delim = nil
|
||||||
usage 1 unless rest.empty?
|
@reverse = false
|
||||||
|
|
||||||
@source = source
|
argv =
|
||||||
|
if opts = ENV['FZF_DEFAULT_OPTS']
|
||||||
|
require 'shellwords'
|
||||||
|
Shellwords.shellwords(opts) + argv
|
||||||
|
else
|
||||||
|
argv.dup
|
||||||
|
end
|
||||||
|
while o = argv.shift
|
||||||
|
case o
|
||||||
|
when '--version' then FZF.version
|
||||||
|
when '-h', '--help' then usage 0
|
||||||
|
when '-m', '--multi' then @multi = true
|
||||||
|
when '+m', '--no-multi' then @multi = false
|
||||||
|
when '-x', '--extended' then @extended = :fuzzy
|
||||||
|
when '+x', '--no-extended' then @extended = nil
|
||||||
|
when '-i' then @rxflag = Regexp::IGNORECASE
|
||||||
|
when '+i' then @rxflag = 0
|
||||||
|
when '-c', '--color' then @color = true
|
||||||
|
when '+c', '--no-color' then @color = false
|
||||||
|
when '-2', '--256' then @ansi256 = true
|
||||||
|
when '+2', '--no-256' then @ansi256 = false
|
||||||
|
when '--black' then @black = true
|
||||||
|
when '--no-black' then @black = false
|
||||||
|
when '--mouse' then @mouse = true
|
||||||
|
when '--no-mouse' then @mouse = false
|
||||||
|
when '--reverse' then @reverse = true
|
||||||
|
when '--no-reverse' then @reverse = false
|
||||||
|
when '+s', '--no-sort' then @sort = nil
|
||||||
|
when '-1', '--select-1' then @select1 = true
|
||||||
|
when '+1', '--no-select-1' then @select1 = false
|
||||||
|
when '-0', '--exit-0' then @exit0 = true
|
||||||
|
when '+0', '--no-exit-0' then @exit0 = false
|
||||||
|
when '-q', '--query'
|
||||||
|
usage 1, 'query string required' unless query = argv.shift
|
||||||
|
@query = AtomicVar.new query.dup
|
||||||
|
when /^-q(.*)$/, /^--query=(.*)$/
|
||||||
|
@query = AtomicVar.new($1)
|
||||||
|
when '-f', '--filter'
|
||||||
|
usage 1, 'query string required' unless query = argv.shift
|
||||||
|
@filter = query
|
||||||
|
when /^-f(.*)$/, /^--filter=(.*)$/
|
||||||
|
@filter = $1
|
||||||
|
when '-n', '--nth'
|
||||||
|
usage 1, 'field number required' unless nth = argv.shift
|
||||||
|
usage 1, 'invalid field number' if nth.to_i == 0
|
||||||
|
@nth = parse_nth nth
|
||||||
|
when /^-n([0-9,-]+)$/, /^--nth=([0-9,-]+)$/
|
||||||
|
@nth = parse_nth $1
|
||||||
|
when '-d', '--delimiter'
|
||||||
|
usage 1, 'delimiter required' unless delim = argv.shift
|
||||||
|
@delim = FZF.build_delim_regex delim
|
||||||
|
when /^-d(.+)$/, /^--delimiter=(.+)$/
|
||||||
|
@delim = FZF.build_delim_regex $1
|
||||||
|
when '-s', '--sort'
|
||||||
|
usage 1, 'sort size required' unless sort = argv.shift
|
||||||
|
usage 1, 'invalid sort size' unless sort =~ /^[0-9]+$/
|
||||||
|
@sort = sort.to_i
|
||||||
|
when /^-s([0-9]+)$/, /^--sort=([0-9]+)$/
|
||||||
|
@sort = $1.to_i
|
||||||
|
when '-e', '--extended-exact' then @extended = :exact
|
||||||
|
when '+e', '--no-extended-exact' then @extended = nil
|
||||||
|
else
|
||||||
|
usage 1, "illegal option: #{o}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@source = source.clone
|
||||||
@mtx = Mutex.new
|
@mtx = Mutex.new
|
||||||
@cv = ConditionVariable.new
|
@cv = ConditionVariable.new
|
||||||
@events = {}
|
@events = {}
|
||||||
@new = []
|
@new = []
|
||||||
@queue = Queue.new
|
@queue = Queue.new
|
||||||
@cursor_x = AtomicVar.new(0)
|
@pending = nil
|
||||||
@query = AtomicVar.new('')
|
|
||||||
@matches = AtomicVar.new([])
|
unless @filter
|
||||||
@count = AtomicVar.new(0)
|
@query ||= AtomicVar.new('')
|
||||||
@vcursor = AtomicVar.new(0)
|
@cursor_x = AtomicVar.new(@query.length)
|
||||||
@vcursors = AtomicVar.new(Set.new)
|
@matches = AtomicVar.new([])
|
||||||
@spinner = AtomicVar.new('-\|/-\|/'.split(//))
|
@count = AtomicVar.new(0)
|
||||||
@selects = AtomicVar.new({}) # ordered >= 1.9
|
@vcursor = AtomicVar.new(0)
|
||||||
@main = Thread.current
|
@vcursors = AtomicVar.new(Set.new)
|
||||||
@stdout = $stdout.clone
|
@spinner = AtomicVar.new('-\|/-\|/'.split(//))
|
||||||
@plcount = 0
|
@selects = AtomicVar.new({}) # ordered >= 1.9
|
||||||
|
@main = Thread.current
|
||||||
|
@plcount = 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_nth nth
|
||||||
|
nth.split(',').map { |n|
|
||||||
|
ni = n.to_i
|
||||||
|
usage 1, "invalid field number: #{n}" if ni == 0
|
||||||
|
ni
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def FZF.build_delim_regex delim
|
||||||
|
Regexp.compile(delim) rescue (delim = Regexp.escape(delim))
|
||||||
|
Regexp.compile "(?:.*?#{delim})|(?:.+?$)"
|
||||||
end
|
end
|
||||||
|
|
||||||
def start
|
def start
|
||||||
$stdout.reopen($stderr)
|
if @filter
|
||||||
|
start_reader.join
|
||||||
|
filter_list @new
|
||||||
|
else
|
||||||
|
start_reader
|
||||||
|
emit(:key) { q = @query.get; [q, q.length] } unless empty = @query.empty?
|
||||||
|
if @select1 || @exit0
|
||||||
|
start_search do |loaded, matches|
|
||||||
|
len = empty ? @count.get : matches.length
|
||||||
|
if loaded
|
||||||
|
if @select1 && len == 1
|
||||||
|
puts empty ? matches.first : matches.first.first
|
||||||
|
exit 0
|
||||||
|
elsif @exit0 && len == 0
|
||||||
|
exit 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
init_screen
|
if loaded || len > 1
|
||||||
start_reader
|
start_renderer
|
||||||
start_renderer
|
Thread.new { start_loop }
|
||||||
start_search
|
end
|
||||||
start_loop
|
end
|
||||||
|
|
||||||
|
sleep
|
||||||
|
else
|
||||||
|
start_search
|
||||||
|
start_renderer
|
||||||
|
start_loop
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def usage x
|
def filter_list list
|
||||||
|
matches = matcher.match(list, @filter, '', '')
|
||||||
|
if @sort && matches.length <= @sort
|
||||||
|
matches = FZF.sort(matches)
|
||||||
|
end
|
||||||
|
matches.each { |m| puts m.first }
|
||||||
|
end
|
||||||
|
|
||||||
|
def matcher
|
||||||
|
@matcher ||=
|
||||||
|
if @extended
|
||||||
|
ExtendedFuzzyMatcher.new @rxflag, @extended, @nth, @delim
|
||||||
|
else
|
||||||
|
FuzzyMatcher.new @rxflag, @nth, @delim
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class << self
|
||||||
|
def version
|
||||||
|
File.open(__FILE__, 'r') do |f|
|
||||||
|
f.each_line do |line|
|
||||||
|
if line =~ /Version: (.*)/
|
||||||
|
$stdout.puts 'fzf ' << $1
|
||||||
|
exit
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def sort list
|
||||||
|
list.sort_by { |tuple| rank tuple }
|
||||||
|
end
|
||||||
|
|
||||||
|
def rank tuple
|
||||||
|
line, offsets = tuple
|
||||||
|
matchlen = 0
|
||||||
|
pe = 0
|
||||||
|
offsets.sort.each do |pair|
|
||||||
|
b, e = pair
|
||||||
|
b = pe if pe > b
|
||||||
|
pe = e if e > pe
|
||||||
|
matchlen += e - b if e > b
|
||||||
|
end
|
||||||
|
[matchlen, line.length, line]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def usage x, message = nil
|
||||||
|
$stderr.puts message if message
|
||||||
$stderr.puts %[usage: fzf [options]
|
$stderr.puts %[usage: fzf [options]
|
||||||
|
|
||||||
-m, --multi Enable multi-select
|
Search
|
||||||
-x, --extended Extended-search mode
|
-x, --extended Extended-search mode
|
||||||
-s, --sort=MAX Maximum number of matched items to sort. Default: 1000
|
-e, --extended-exact Extended-search mode (exact match)
|
||||||
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
-i Case-insensitive match (default: smart-case match)
|
||||||
+i Case-sensitive match
|
+i Case-sensitive match
|
||||||
+c, --no-color Disable colors]
|
-n, --nth=[-]N[,..] Comma-separated list of field indexes for limiting
|
||||||
|
search scope (positive or negative integers)
|
||||||
|
-d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)
|
||||||
|
|
||||||
|
Search result
|
||||||
|
-s, --sort=MAX Maximum number of matched items to sort (default: 1000)
|
||||||
|
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
||||||
|
|
||||||
|
Interface
|
||||||
|
-m, --multi Enable multi-select with tab/shift-tab
|
||||||
|
--no-mouse Disable mouse
|
||||||
|
+c, --no-color Disable colors
|
||||||
|
+2, --no-256 Disable 256-color
|
||||||
|
--black Use black background
|
||||||
|
--reverse Reverse orientation
|
||||||
|
|
||||||
|
Scripting
|
||||||
|
-q, --query=STR Start the finder with the given query
|
||||||
|
-1, --select-1 Automatically select the only match
|
||||||
|
-0, --exit-0 Exit immediately when there's no match
|
||||||
|
-f, --filter=STR Filter mode. Do not start interactive finder.
|
||||||
|
|
||||||
|
Environment variables
|
||||||
|
FZF_DEFAULT_COMMAND Default command to use when input is tty
|
||||||
|
FZF_DEFAULT_OPTS Defaults options. (e.g. "-x -m --sort 10000")] + $/ + $/
|
||||||
exit x
|
exit x
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -246,7 +432,11 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
def max_items; C.lines - 2; end
|
def max_items; C.lines - 2; end
|
||||||
def cursor_y; C.lines - 1; end
|
|
||||||
|
def cursor_y offset = 0
|
||||||
|
@reverse ? (offset) : (C.lines - 1 - offset)
|
||||||
|
end
|
||||||
|
|
||||||
def cprint str, col
|
def cprint str, col
|
||||||
C.attron(col) do
|
C.attron(col) do
|
||||||
addstr_safe str
|
addstr_safe str
|
||||||
@@ -266,7 +456,7 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
def print_info msg = nil
|
def print_info msg = nil
|
||||||
C.setpos cursor_y - 1, 0
|
C.setpos cursor_y(1), 0
|
||||||
C.clrtoeol
|
C.clrtoeol
|
||||||
prefix =
|
prefix =
|
||||||
if spinner = @spinner.first
|
if spinner = @spinner.first
|
||||||
@@ -329,7 +519,7 @@ class FZF
|
|||||||
tokens << [line[b...e], true]
|
tokens << [line[b...e], true]
|
||||||
index = e
|
index = e
|
||||||
end
|
end
|
||||||
tokens << [line[index..-1], false]
|
tokens << [line[index..-1], false] if index < line.length
|
||||||
tokens.reject { |pair| pair.first.empty? }
|
tokens.reject { |pair| pair.first.empty? }
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -356,25 +546,12 @@ class FZF
|
|||||||
C.attroff color(:chosen, true) if chosen
|
C.attroff color(:chosen, true) if chosen
|
||||||
end
|
end
|
||||||
|
|
||||||
def sort_by_rank list
|
AFTER_1_9 = RUBY_VERSION.split('.').map { |e| e.rjust(3, '0') }.join >= '001009'
|
||||||
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 AFTER_1_9
|
||||||
@@wrx = Regexp.new '\p{Han}|\p{Katakana}|\p{Hiragana}|\p{Hangul}'
|
@@wrx = Regexp.new '\p{Han}|\p{Katakana}|\p{Hiragana}|\p{Hangul}'
|
||||||
def width str
|
def width str
|
||||||
str.gsub(@@wrx, ' ').length
|
str.gsub(@@wrx, ' ').length rescue str.length
|
||||||
end
|
end
|
||||||
|
|
||||||
def trim str, len, left
|
def trim str, len, left
|
||||||
@@ -415,10 +592,14 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
def init_screen
|
def init_screen
|
||||||
|
@stdout = $stdout.clone
|
||||||
|
$stdout.reopen($stderr)
|
||||||
|
|
||||||
C.init_screen
|
C.init_screen
|
||||||
|
C.mousemask C::ALL_MOUSE_EVENTS if @mouse
|
||||||
C.start_color
|
C.start_color
|
||||||
dbg =
|
dbg =
|
||||||
if C.respond_to?(:use_default_colors)
|
if !@black && C.respond_to?(:use_default_colors)
|
||||||
C.use_default_colors
|
C.use_default_colors
|
||||||
-1
|
-1
|
||||||
else
|
else
|
||||||
@@ -428,7 +609,7 @@ class FZF
|
|||||||
C.noecho
|
C.noecho
|
||||||
|
|
||||||
if @color
|
if @color
|
||||||
if C.can_change_color?
|
if @ansi256 && ENV['TERM'].to_s =~ /256/
|
||||||
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, 254, 236
|
C.init_pair 3, 254, 236
|
||||||
@@ -467,6 +648,8 @@ class FZF
|
|||||||
end | (bold ? C::A_BOLD : 0)
|
end | (bold ? C::A_BOLD : 0)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
C.refresh
|
||||||
end
|
end
|
||||||
|
|
||||||
def start_reader
|
def start_reader
|
||||||
@@ -488,13 +671,12 @@ class FZF
|
|||||||
emit(:new) { @new << line.chomp }
|
emit(:new) { @new << line.chomp }
|
||||||
end
|
end
|
||||||
emit(:loaded) { true }
|
emit(:loaded) { true }
|
||||||
@spinner.clear
|
@spinner.clear if @spinner
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def start_search
|
def start_search &callback
|
||||||
matcher = (@xmode ? ExtendedFuzzyMatcher : FuzzyMatcher).new @rxflag
|
Thread.new do
|
||||||
searcher = Thread.new {
|
|
||||||
lists = []
|
lists = []
|
||||||
events = {}
|
events = {}
|
||||||
fcache = {}
|
fcache = {}
|
||||||
@@ -533,38 +715,41 @@ class FZF
|
|||||||
progress = 0
|
progress = 0
|
||||||
started_at = Time.now
|
started_at = Time.now
|
||||||
|
|
||||||
if new_search && !lists.empty?
|
if updated = new_search && !lists.empty?
|
||||||
q, cx = events.delete(:key) || [q, 0]
|
q, cx = events.delete(:key) || [q, 0]
|
||||||
empty = matcher.empty?(q)
|
empty = matcher.empty?(q)
|
||||||
matches = fcache[q] ||=
|
unless matches = fcache[q]
|
||||||
begin
|
found = []
|
||||||
found = []
|
skip = false
|
||||||
skip = false
|
cnt = 0
|
||||||
cnt = 0
|
lists.each do |list|
|
||||||
lists.each do |list|
|
cnt += list.length
|
||||||
cnt += list.length
|
skip = @mtx.synchronize { @events[:key] }
|
||||||
skip = @mtx.synchronize { @events[:key] }
|
break if skip
|
||||||
break if skip
|
|
||||||
|
|
||||||
if !empty && (progress = 100 * cnt / @count.get) < 100 && Time.now - started_at > 0.5
|
if !empty && (progress = 100 * cnt / @count.get) < 100 && Time.now - started_at > 0.5
|
||||||
render { print_info " (#{progress}%)" }
|
render { print_info " (#{progress}%)" }
|
||||||
end
|
|
||||||
|
|
||||||
found.concat(q.empty? ? list :
|
|
||||||
matcher.match(list, q, q[0, cx], q[cx..-1]))
|
|
||||||
end
|
end
|
||||||
next if skip
|
|
||||||
@sort ? found : found.reverse
|
|
||||||
end
|
|
||||||
|
|
||||||
if !empty && @sort && matches.length <= @sort
|
found.concat(q.empty? ? list :
|
||||||
matches = sort_by_rank(matches)
|
matcher.match(list, q, q[0, cx], q[cx..-1]))
|
||||||
|
end
|
||||||
|
next if skip
|
||||||
|
matches = @sort ? found : found.reverse
|
||||||
|
if !empty && @sort && matches.length <= @sort
|
||||||
|
matches = FZF.sort(matches)
|
||||||
|
end
|
||||||
|
fcache[q] = matches
|
||||||
end
|
end
|
||||||
|
|
||||||
# Atomic update
|
# Atomic update
|
||||||
@matches.set matches
|
@matches.set matches
|
||||||
end#new_search
|
end#new_search
|
||||||
|
|
||||||
|
callback = nil if callback &&
|
||||||
|
(updated || events[:loaded]) &&
|
||||||
|
callback.call(events[:loaded], matches)
|
||||||
|
|
||||||
# This small delay reduces the number of partial lists
|
# This small delay reduces the number of partial lists
|
||||||
sleep((delay = [20, delay + 5].min) * 0.01) unless user_input
|
sleep((delay = [20, delay + 5].min) * 0.01) unless user_input
|
||||||
|
|
||||||
@@ -573,7 +758,7 @@ class FZF
|
|||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
@main.raise e
|
@main.raise e
|
||||||
end
|
end
|
||||||
}
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def pick
|
def pick
|
||||||
@@ -589,7 +774,7 @@ class FZF
|
|||||||
# Wipe
|
# Wipe
|
||||||
if items.length < @plcount
|
if items.length < @plcount
|
||||||
@plcount.downto(items.length) do |idx|
|
@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
|
||||||
@@ -604,7 +789,7 @@ class FZF
|
|||||||
}
|
}
|
||||||
items.each_with_index do |item, idx|
|
items.each_with_index do |item, idx|
|
||||||
next unless wipe || cleanse.include?(idx)
|
next unless wipe || cleanse.include?(idx)
|
||||||
row = cursor_y - idx - 2
|
row = cursor_y(idx + 2)
|
||||||
chosen = idx == vcursor
|
chosen = idx == vcursor
|
||||||
selected = @selects.include?([*item][0])
|
selected = @selects.include?([*item][0])
|
||||||
line, offsets = convert_item item
|
line, offsets = convert_item item
|
||||||
@@ -612,10 +797,13 @@ class FZF
|
|||||||
print_item row, tokens, chosen, selected
|
print_item row, tokens, chosen, selected
|
||||||
end
|
end
|
||||||
print_info
|
print_info
|
||||||
|
print_input
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def start_renderer
|
def start_renderer
|
||||||
|
init_screen
|
||||||
|
|
||||||
Thread.new do
|
Thread.new do
|
||||||
begin
|
begin
|
||||||
while blk = @queue.shift
|
while blk = @queue.shift
|
||||||
@@ -633,97 +821,278 @@ class FZF
|
|||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def vselect &prc
|
||||||
|
@vcursor.set { |v| @vcursors << v; prc.call v }
|
||||||
|
update_list false
|
||||||
|
end
|
||||||
|
|
||||||
|
def num_unicode_bytes chr
|
||||||
|
# http://en.wikipedia.org/wiki/UTF-8
|
||||||
|
if chr & 0b10000000 > 0
|
||||||
|
bytes = 0
|
||||||
|
7.downto(2) do |shift|
|
||||||
|
break if (chr >> shift) & 0x1 == 0
|
||||||
|
bytes += 1
|
||||||
|
end
|
||||||
|
bytes
|
||||||
|
else
|
||||||
|
1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def read_nb chars = 1, default = nil, tries = 10
|
||||||
|
tries.times do |_|
|
||||||
|
begin
|
||||||
|
return @tty.read_nonblock(chars).ord
|
||||||
|
rescue Exception
|
||||||
|
sleep 0.01
|
||||||
|
end
|
||||||
|
end
|
||||||
|
default
|
||||||
|
end
|
||||||
|
|
||||||
|
def read_nbs
|
||||||
|
ords = []
|
||||||
|
while ord = read_nb
|
||||||
|
ords << ord
|
||||||
|
end
|
||||||
|
ords
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_mouse
|
||||||
|
case ord = read_nb
|
||||||
|
when 32, 36, 40, 48, # mouse-down / shift / cmd / ctrl
|
||||||
|
35, 39, 43, 51 # mouse-up / shift / cmd / ctrl
|
||||||
|
x = read_nb - 33
|
||||||
|
y = read_nb - 33
|
||||||
|
{ :event => (ord % 2 == 0 ? :click : :release),
|
||||||
|
:x => x, :y => y, :shift => ord >= 36 }
|
||||||
|
when 96, 100, 104, 112, # scroll-up / shift / cmd / ctrl
|
||||||
|
97, 101, 105, 113 # scroll-down / shift / cmd / ctrl
|
||||||
|
read_nb(2)
|
||||||
|
{ :event => :scroll, :diff => (ord % 2 == 0 ? -1 : 1), :shift => ord >= 100 }
|
||||||
|
else
|
||||||
|
# e.g. 40, 43, 104, 105
|
||||||
|
read_nb(2)
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_input actions
|
||||||
|
@tty ||= IO.open(IO.sysopen('/dev/tty'), 'r')
|
||||||
|
|
||||||
|
if pending = @pending
|
||||||
|
@pending = nil
|
||||||
|
return pending
|
||||||
|
end
|
||||||
|
|
||||||
|
str = ''
|
||||||
|
while true
|
||||||
|
ord =
|
||||||
|
if str.empty?
|
||||||
|
@tty.getc.ord
|
||||||
|
else
|
||||||
|
begin
|
||||||
|
ord = @tty.read_nonblock(1).ord
|
||||||
|
if (nb = num_unicode_bytes(ord)) > 1
|
||||||
|
ords = [ord]
|
||||||
|
(nb - 1).times do |_|
|
||||||
|
ords << @tty.read_nonblock(1).ord
|
||||||
|
end
|
||||||
|
# UTF-8 TODO Ruby 1.8
|
||||||
|
ords.pack('C*').force_encoding('UTF-8')
|
||||||
|
else
|
||||||
|
ord
|
||||||
|
end
|
||||||
|
rescue Exception
|
||||||
|
return str
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
ord =
|
||||||
|
case read_nb(1, :esc)
|
||||||
|
when 91
|
||||||
|
case read_nb(1, nil)
|
||||||
|
when 68 then ctrl(:b)
|
||||||
|
when 67 then ctrl(:f)
|
||||||
|
when 66 then ctrl(:j)
|
||||||
|
when 65 then ctrl(:k)
|
||||||
|
when 90 then :stab
|
||||||
|
when 50 then read_nb; :ins
|
||||||
|
when 51 then read_nb; :del
|
||||||
|
when 53 then read_nb; :pgup
|
||||||
|
when 54 then read_nb; :pgdn
|
||||||
|
when 49
|
||||||
|
case read_nbs
|
||||||
|
when [59, 50, 68] then ctrl(:a)
|
||||||
|
when [59, 50, 67] then ctrl(:e)
|
||||||
|
when [126] then ctrl(:a)
|
||||||
|
end
|
||||||
|
when 52 then read_nb; ctrl(:e)
|
||||||
|
when 72 then ctrl(:a)
|
||||||
|
when 70 then ctrl(:e)
|
||||||
|
when 77
|
||||||
|
get_mouse
|
||||||
|
end
|
||||||
|
when 'b', 98 then :alt_b
|
||||||
|
when 'f', 102 then :alt_f
|
||||||
|
when :esc then :esc
|
||||||
|
else next
|
||||||
|
end if ord == 27
|
||||||
|
|
||||||
|
return ord if ord.nil? || ord.is_a?(Hash)
|
||||||
|
|
||||||
|
if actions.has_key?(ord)
|
||||||
|
if str.empty?
|
||||||
|
return ord
|
||||||
|
else
|
||||||
|
@pending = ord
|
||||||
|
return str
|
||||||
|
end
|
||||||
|
else
|
||||||
|
unless ord.is_a? String
|
||||||
|
ord = [ord].pack('U*')
|
||||||
|
end
|
||||||
|
str << ord if ord =~ /[[:print:]]/
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class MouseEvent
|
||||||
|
DOUBLE_CLICK_INTERVAL = 0.5
|
||||||
|
|
||||||
|
attr_reader :v
|
||||||
|
|
||||||
|
def initialize v = nil
|
||||||
|
@c = 0
|
||||||
|
@v = v
|
||||||
|
@t = Time.at 0
|
||||||
|
end
|
||||||
|
|
||||||
|
def v= v
|
||||||
|
@c = (@v == v && within?) ? @c + 1 : 0
|
||||||
|
@v = v
|
||||||
|
@t = Time.now
|
||||||
|
end
|
||||||
|
|
||||||
|
def double? v
|
||||||
|
@c == 1 && @v == v && within?
|
||||||
|
end
|
||||||
|
|
||||||
|
def within?
|
||||||
|
(Time.now - @t) < DOUBLE_CLICK_INTERVAL
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def start_loop
|
def start_loop
|
||||||
got = nil
|
got = nil
|
||||||
begin
|
begin
|
||||||
tty = IO.open(IO.sysopen('/dev/tty'), 'r')
|
input = @query.get.dup
|
||||||
input = ''
|
cursor = input.length
|
||||||
cursor = 0
|
yanked = ''
|
||||||
|
mouse_event = MouseEvent.new
|
||||||
backword = proc {
|
backword = proc {
|
||||||
cursor = (input[0, cursor].rindex(/\s\S/) || -1) + 1
|
cursor = (input[0, cursor].rindex(/\s\S/) || -1) + 1
|
||||||
}
|
}
|
||||||
actions = {
|
actions = {
|
||||||
:nop => proc { nil },
|
:esc => 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 {
|
||||||
got = pick
|
got = pick
|
||||||
exit 0
|
exit 0
|
||||||
},
|
},
|
||||||
ctrl(:u) => proc { input = input[cursor..-1]; cursor = 0 },
|
ctrl(:u) => proc {
|
||||||
|
yanked = input[0...cursor] if cursor > 0
|
||||||
|
input = input[cursor..-1]
|
||||||
|
cursor = 0
|
||||||
|
},
|
||||||
ctrl(:a) => proc { cursor = 0; nil },
|
ctrl(:a) => proc { cursor = 0; nil },
|
||||||
ctrl(:e) => proc { cursor = input.length; nil },
|
ctrl(:e) => proc { cursor = input.length; nil },
|
||||||
ctrl(:j) => proc { @vcursor.set { |v| @vcursors << v; v - 1 }; update_list false },
|
ctrl(:j) => proc { vselect { |v| v - (@reverse ? -1 : 1) } },
|
||||||
ctrl(:k) => proc { @vcursor.set { |v| @vcursors << v; v + 1 }; update_list false },
|
ctrl(:k) => proc { vselect { |v| v + (@reverse ? -1 : 1) } },
|
||||||
ctrl(:w) => proc {
|
ctrl(:w) => proc {
|
||||||
pcursor = cursor
|
pcursor = cursor
|
||||||
backword.call
|
backword.call
|
||||||
|
yanked = input[cursor...pcursor] if pcursor > cursor
|
||||||
input = input[0...cursor] + input[pcursor..-1]
|
input = input[0...cursor] + input[pcursor..-1]
|
||||||
},
|
},
|
||||||
127 => proc { input[cursor -= 1] = '' if cursor > 0 },
|
ctrl(:y) => proc { actions[:default].call yanked },
|
||||||
9 => proc { |o|
|
ctrl(:h) => proc { input[cursor -= 1] = '' if cursor > 0 },
|
||||||
|
ctrl(:i) => proc { |o|
|
||||||
if @multi && sel = pick
|
if @multi && sel = pick
|
||||||
if @selects.has_key? sel
|
if @selects.has_key? sel
|
||||||
@selects.delete sel
|
@selects.delete sel
|
||||||
else
|
else
|
||||||
@selects[sel] = 1
|
@selects[sel] = 1
|
||||||
end
|
end
|
||||||
@vcursor.set { |v|
|
vselect { |v| v + case o
|
||||||
@vcursors << v
|
when :stab then 1
|
||||||
v + (o == :stab ? 1 : -1)
|
when :sclick then 0
|
||||||
}
|
else -1
|
||||||
update_list false
|
end * (@reverse ? -1 : 1) }
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
:left => proc { cursor = [0, cursor - 1].max; nil },
|
ctrl(:b) => proc { cursor = [0, cursor - 1].max; nil },
|
||||||
:right => proc { cursor = [input.length, cursor + 1].min; nil },
|
ctrl(:f) => proc { cursor = [input.length, cursor + 1].min; nil },
|
||||||
|
ctrl(:l) => proc { render { C.clear; C.refresh }; update_list true },
|
||||||
|
:del => proc { input[cursor] = '' if input.length > cursor },
|
||||||
|
:pgup => proc { vselect { |_| max_items } },
|
||||||
|
:pgdn => proc { vselect { |_| 0 } },
|
||||||
:alt_b => proc { backword.call; nil },
|
:alt_b => proc { backword.call; nil },
|
||||||
:alt_f => proc {
|
:alt_f => proc {
|
||||||
cursor += (input[cursor..-1].index(/(\S\s)|(.$)/) || -1) + 1
|
cursor += (input[cursor..-1].index(/(\S\s)|(.$)/) || -1) + 1
|
||||||
nil
|
nil
|
||||||
},
|
},
|
||||||
|
:default => proc { |val|
|
||||||
|
case val
|
||||||
|
when String
|
||||||
|
input.insert cursor, val
|
||||||
|
cursor += val.length
|
||||||
|
when Hash
|
||||||
|
event = val[:event]
|
||||||
|
case event
|
||||||
|
when :click, :release
|
||||||
|
x, y, shift = val.values_at :x, :y, :shift
|
||||||
|
if y == cursor_y
|
||||||
|
cursor = [0, [input.length, x - 2].min].max
|
||||||
|
elsif x > 1 && y <= max_items
|
||||||
|
tv = max_items - y - 1
|
||||||
|
|
||||||
|
case event
|
||||||
|
when :click
|
||||||
|
vselect { |_| tv }
|
||||||
|
actions[ctrl(:i)].call(:sclick) if shift
|
||||||
|
mouse_event.v = tv
|
||||||
|
when :release
|
||||||
|
if !shift && mouse_event.double?(tv)
|
||||||
|
actions[ctrl(:m)].call
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
when :scroll
|
||||||
|
diff, shift = val.values_at :diff, :shift
|
||||||
|
actions[ctrl(:i)].call(:sclick) if shift
|
||||||
|
actions[ctrl(diff > 0 ? :j : :k)].call
|
||||||
|
end
|
||||||
|
end
|
||||||
|
}
|
||||||
}
|
}
|
||||||
actions[ctrl(:b)] = actions[:left]
|
|
||||||
actions[ctrl(:f)] = actions[:right]
|
|
||||||
actions[ctrl(:h)] = actions[127]
|
|
||||||
actions[ctrl(:n)] = actions[ctrl(:j)]
|
|
||||||
actions[ctrl(:p)] = actions[ctrl(:k)]
|
actions[ctrl(:p)] = actions[ctrl(:k)]
|
||||||
actions[:stab] = actions[9]
|
actions[ctrl(:n)] = actions[ctrl(:j)]
|
||||||
|
actions[:stab] = actions[ctrl(:i)]
|
||||||
|
actions[127] = actions[ctrl(:h)]
|
||||||
|
actions[ctrl(:q)] = actions[ctrl(:g)] = actions[ctrl(:c)] = actions[:esc]
|
||||||
|
|
||||||
while true
|
while true
|
||||||
@cursor_x.set cursor
|
@cursor_x.set cursor
|
||||||
render { print_input }
|
render { print_input }
|
||||||
|
|
||||||
ord = tty.getc.ord
|
if key = get_input(actions)
|
||||||
ord =
|
upd = actions.fetch(key, actions[:default]).call(key)
|
||||||
case ord = tty.getc.ord
|
|
||||||
when 91
|
|
||||||
case tty.getc.ord
|
|
||||||
when 68 then :left
|
|
||||||
when 67 then :right
|
|
||||||
when 66 then ctrl(:j)
|
|
||||||
when 65 then ctrl(:k)
|
|
||||||
when 90 then :stab
|
|
||||||
else :nop
|
|
||||||
end
|
|
||||||
when 'b'.ord
|
|
||||||
:alt_b
|
|
||||||
when 'f'.ord
|
|
||||||
:alt_f
|
|
||||||
else
|
|
||||||
ord
|
|
||||||
end if ord == 27
|
|
||||||
|
|
||||||
upd = actions.fetch(ord, proc { |ord|
|
# Dispatch key event
|
||||||
char = [ord].pack('U*')
|
emit(:key) { [@query.set(input.dup), cursor] } if upd
|
||||||
if char =~ /[[:print:]]/
|
end
|
||||||
input.insert cursor, char
|
|
||||||
cursor += 1
|
|
||||||
end
|
|
||||||
}).call(ord)
|
|
||||||
|
|
||||||
# Dispatch key event
|
|
||||||
emit(:key) { [@query.set(input.dup), cursor] } if upd
|
|
||||||
end
|
end
|
||||||
ensure
|
ensure
|
||||||
C.close_screen
|
C.close_screen
|
||||||
@@ -739,10 +1108,58 @@ class FZF
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Matcher
|
||||||
|
class MatchData
|
||||||
|
def initialize n
|
||||||
|
@n = n
|
||||||
|
end
|
||||||
|
|
||||||
|
def offset _
|
||||||
|
@n
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize nth, delim
|
||||||
|
@nth = nth && nth.map { |n| n > 0 ? n - 1 : n }
|
||||||
|
@delim = delim
|
||||||
|
@tokens_cache = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
def tokenize str
|
||||||
|
@tokens_cache[str] ||=
|
||||||
|
unless @delim
|
||||||
|
# AWK default
|
||||||
|
prefix_length = str[/^\s+/].length rescue 0
|
||||||
|
[prefix_length, (str.strip.scan(/\S+\s*/) rescue [])]
|
||||||
|
else
|
||||||
|
prefix_length = 0
|
||||||
|
[prefix_length, (str.scan(@delim) rescue [])]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def do_match str, pat
|
||||||
|
if @nth
|
||||||
|
prefix_length, tokens = tokenize str
|
||||||
|
|
||||||
|
@nth.each do |n|
|
||||||
|
if (token = tokens[n]) && (md = token.match(pat) rescue nil)
|
||||||
|
prefix_length += (tokens[0...n] || []).join.length
|
||||||
|
offset = md.offset(0).map { |o| o + prefix_length }
|
||||||
|
return MatchData.new(offset)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
nil
|
||||||
|
else
|
||||||
|
str.match(pat) rescue nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class FuzzyMatcher < Matcher
|
class FuzzyMatcher < Matcher
|
||||||
attr_reader :caches, :rxflag
|
attr_reader :caches, :rxflag
|
||||||
|
|
||||||
def initialize rxflag
|
def initialize rxflag, nth = nil, delim = nil
|
||||||
|
super nth, delim
|
||||||
@caches = Hash.new { |h, k| h[k] = {} }
|
@caches = Hash.new { |h, k| h[k] = {} }
|
||||||
@regexp = {}
|
@regexp = {}
|
||||||
@rxflag = rxflag
|
@rxflag = rxflag
|
||||||
@@ -752,14 +1169,18 @@ class FZF
|
|||||||
q.empty?
|
q.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def rxflag_for q
|
||||||
|
@rxflag || (q =~ /[A-Z]/ ? 0 : Regexp::IGNORECASE)
|
||||||
|
end
|
||||||
|
|
||||||
def fuzzy_regex q
|
def fuzzy_regex q
|
||||||
@regexp[q] ||= begin
|
@regexp[q] ||= begin
|
||||||
q = q.downcase if @rxflag != 0
|
q = q.downcase if @rxflag == Regexp::IGNORECASE
|
||||||
Regexp.new(query_chars(q).inject('') { |sum, e|
|
Regexp.new(query_chars(q).inject('') { |sum, e|
|
||||||
e = Regexp.escape e
|
e = Regexp.escape e
|
||||||
sum << (e.length > 1 ? "(?:#{e}).*?" : # FIXME: not equivalent
|
sum << (e.length > 1 ? "(?:#{e}).*?" : # FIXME: not equivalent
|
||||||
"#{e}[^#{e}]*?")
|
"#{e}[^#{e}]*?")
|
||||||
}, @rxflag)
|
}, rxflag_for(q))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -782,16 +1203,17 @@ class FZF
|
|||||||
cache[q] ||= (partial_cache ?
|
cache[q] ||= (partial_cache ?
|
||||||
partial_cache.map { |e| e.first } : list).map { |line|
|
partial_cache.map { |e| e.first } : list).map { |line|
|
||||||
# Ignore errors: e.g. invalid byte sequence in UTF-8
|
# Ignore errors: e.g. invalid byte sequence in UTF-8
|
||||||
md = line.match(regexp) rescue nil
|
md = do_match(line, regexp)
|
||||||
md && [line, [md.offset(0)]]
|
md && [line, [md.offset(0)]]
|
||||||
}.compact
|
}.compact
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class ExtendedFuzzyMatcher < FuzzyMatcher
|
class ExtendedFuzzyMatcher < FuzzyMatcher
|
||||||
def initialize rxflag
|
def initialize rxflag, mode = :fuzzy, nth = nil, delim = nil
|
||||||
super
|
super rxflag, nth, delim
|
||||||
@regexps = {}
|
@regexps = {}
|
||||||
|
@mode = mode
|
||||||
end
|
end
|
||||||
|
|
||||||
def empty? q
|
def empty? q
|
||||||
@@ -811,21 +1233,30 @@ class FZF
|
|||||||
case w
|
case w
|
||||||
when ''
|
when ''
|
||||||
nil
|
nil
|
||||||
|
when /^\^(.*)\$$/
|
||||||
|
Regexp.new('^' << sanitize(Regexp.escape($1)) << '$', rxflag_for(w))
|
||||||
when /^'/
|
when /^'/
|
||||||
w.length > 1 ?
|
if @mode == :fuzzy && w.length > 1
|
||||||
Regexp.new(sanitize(Regexp.escape(w[1..-1])), rxflag) : nil
|
exact_regex w[1..-1]
|
||||||
|
elsif @mode == :exact
|
||||||
|
exact_regex w
|
||||||
|
end
|
||||||
when /^\^/
|
when /^\^/
|
||||||
w.length > 1 ?
|
w.length > 1 ?
|
||||||
Regexp.new('^' << sanitize(Regexp.escape(w[1..-1])), rxflag) : nil
|
Regexp.new('^' << sanitize(Regexp.escape(w[1..-1])), rxflag_for(w)) : nil
|
||||||
when /\$$/
|
when /\$$/
|
||||||
w.length > 1 ?
|
w.length > 1 ?
|
||||||
Regexp.new(sanitize(Regexp.escape(w[0..-2])) << '$', rxflag) : nil
|
Regexp.new(sanitize(Regexp.escape(w[0..-2])) << '$', rxflag_for(w)) : nil
|
||||||
else
|
else
|
||||||
fuzzy_regex w
|
@mode == :fuzzy ? fuzzy_regex(w) : exact_regex(w)
|
||||||
end, invert ]
|
end, invert ]
|
||||||
}.select { |pair| pair.first }
|
}.select { |pair| pair.first }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def exact_regex w
|
||||||
|
Regexp.new(sanitize(Regexp.escape(w)), rxflag_for(w))
|
||||||
|
end
|
||||||
|
|
||||||
def match list, q, prefix, suffix
|
def match list, q, prefix, suffix
|
||||||
regexps = parse q
|
regexps = parse q
|
||||||
# Look for prefix cache
|
# Look for prefix cache
|
||||||
@@ -842,7 +1273,7 @@ class FZF
|
|||||||
offsets = []
|
offsets = []
|
||||||
regexps.all? { |pair|
|
regexps.all? { |pair|
|
||||||
regexp, invert = pair
|
regexp, invert = pair
|
||||||
md = line.match(regexp) rescue nil
|
md = do_match(line, regexp)
|
||||||
if md && !invert
|
if md && !invert
|
||||||
offsets << md.offset(0)
|
offsets << md.offset(0)
|
||||||
elsif !md && invert
|
elsif !md && invert
|
||||||
|
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.4.2'
|
spec.version = '0.8.4'
|
||||||
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.extensions += ['ext/mkrf_conf.rb']
|
||||||
end
|
end
|
||||||
|
351
install
351
install
@@ -1,7 +1,352 @@
|
|||||||
#!/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
|
||||||
|
system_ruby=/usr/bin/ruby
|
||||||
|
if [ -x $system_ruby -a $system_ruby != "$ruby" ]; then
|
||||||
|
$system_ruby --disable-gems -rcurses -e0 2> /dev/null
|
||||||
|
[ $? -eq 0 ] && ruby=$system_ruby
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "OK ($ruby)"
|
||||||
|
|
||||||
|
# Curses-support
|
||||||
|
echo -n "Checking Curses support ... "
|
||||||
|
"$ruby" -rcurses -e0 2> /dev/null
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
|
echo "OK"
|
||||||
|
else
|
||||||
|
echo "Not found"
|
||||||
|
echo "Installing 'curses' gem ... "
|
||||||
|
/usr/bin/env gem install curses -v 1.0.0 --user-install
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo
|
||||||
|
echo "Failed to install 'curses' gem."
|
||||||
|
if [[ $(uname -r) =~ 'ARCH' ]]; then
|
||||||
|
echo "Make sure that base-devel package group is installed."
|
||||||
|
fi
|
||||||
|
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 -rcurses -e0 2> /dev/null
|
||||||
|
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="[[ \$- =~ i ]] && 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 << "EOFZF"
|
||||||
|
# Key bindings
|
||||||
|
# ------------
|
||||||
|
__fsel() {
|
||||||
|
find * -path '*/\.*' -prune \
|
||||||
|
-o -type f -print \
|
||||||
|
-o -type d -print \
|
||||||
|
-o -type l -print 2> /dev/null | fzf -m | while read item; do
|
||||||
|
printf '%q ' "$item"
|
||||||
|
done
|
||||||
|
echo
|
||||||
|
}
|
||||||
|
|
||||||
|
if [[ $- =~ i ]]; then
|
||||||
|
|
||||||
|
__fsel_tmux() {
|
||||||
|
local height
|
||||||
|
height=${FZF_TMUX_HEIGHT:-40%}
|
||||||
|
if [[ $height =~ %$ ]]; then
|
||||||
|
height="-p ${height%\%}"
|
||||||
|
else
|
||||||
|
height="-l $height"
|
||||||
|
fi
|
||||||
|
tmux split-window $height "bash -c 'source ~/.fzf.bash; tmux send-keys -t $TMUX_PANE \"\$(__fsel)\"'"
|
||||||
|
}
|
||||||
|
|
||||||
|
__fcd() {
|
||||||
|
local dir
|
||||||
|
dir=$(find ${1:-*} -path '*/\.*' -prune -o -type d -print 2> /dev/null | fzf +m) && printf 'cd %q' "$dir"
|
||||||
|
}
|
||||||
|
|
||||||
|
__use_tmux=0
|
||||||
|
[ -n "$TMUX_PANE" -a ${FZF_TMUX:-1} -ne 0 -a ${LINES:-40} -gt 15 ] && __use_tmux=1
|
||||||
|
|
||||||
|
if [ -z "$(set -o | grep '^vi.*on')" ]; then
|
||||||
|
# Required to refresh the prompt after fzf
|
||||||
|
bind '"\er": redraw-current-line'
|
||||||
|
|
||||||
|
# CTRL-T - Paste the selected file path into the command line
|
||||||
|
if [ $__use_tmux -eq 1 ]; then
|
||||||
|
bind '"\C-t": " \C-u \C-a\C-k$(__fsel_tmux)\e\C-e\C-y\C-a\C-d\C-y\ey\C-h"'
|
||||||
|
else
|
||||||
|
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 \C-h"'
|
||||||
|
fi
|
||||||
|
|
||||||
|
# CTRL-R - Paste the selected command from history into the command line
|
||||||
|
bind '"\C-r": " \C-e\C-u$(HISTTIMEFORMAT= history | fzf +s | sed \"s/ *[0-9]* *//\")\e\C-e\er"'
|
||||||
|
|
||||||
|
# ALT-C - cd into the selected directory
|
||||||
|
bind '"\ec": " \C-e\C-u$(__fcd)\e\C-e\er\C-m"'
|
||||||
|
else
|
||||||
|
bind '"\C-x\C-e": shell-expand-line'
|
||||||
|
bind '"\C-x\C-r": redraw-current-line'
|
||||||
|
|
||||||
|
# CTRL-T - Paste the selected file path into the command line
|
||||||
|
# - FIXME: Selected items are attached to the end regardless of cursor position
|
||||||
|
if [ $__use_tmux -eq 1 ]; then
|
||||||
|
bind '"\C-t": "\e$a \eddi$(__fsel_tmux)\C-x\C-e\e0P$xa"'
|
||||||
|
else
|
||||||
|
bind '"\C-t": "\e$a \eddi$(__fsel)\C-x\C-e\e0Px$a \C-x\C-r\exa "'
|
||||||
|
fi
|
||||||
|
|
||||||
|
# CTRL-R - Paste the selected command from history into the command line
|
||||||
|
bind '"\C-r": "\eddi$(HISTTIMEFORMAT= history | fzf +s | sed \"s/ *[0-9]* *//\")\C-x\C-e\e$a\C-x\C-r"'
|
||||||
|
|
||||||
|
# ALT-C - cd into the selected directory
|
||||||
|
bind '"\ec": "\eddi$(__fcd)\C-x\C-e\C-x\C-r\C-m"'
|
||||||
|
fi
|
||||||
|
|
||||||
|
unset __use_tmux
|
||||||
|
|
||||||
|
fi
|
||||||
|
EOFZF
|
||||||
|
else
|
||||||
|
cat >> $src << "EOFZF"
|
||||||
|
# Key bindings
|
||||||
|
# ------------
|
||||||
|
# CTRL-T - Paste the selected file path(s) into the command line
|
||||||
|
__fsel() {
|
||||||
|
set -o nonomatch
|
||||||
|
find * -path '*/\.*' -prune \
|
||||||
|
-o -type f -print \
|
||||||
|
-o -type d -print \
|
||||||
|
-o -type l -print 2> /dev/null | fzf -m | while read item; do
|
||||||
|
printf '%q ' "$item"
|
||||||
|
done
|
||||||
|
echo
|
||||||
|
}
|
||||||
|
|
||||||
|
if [[ $- =~ i ]]; then
|
||||||
|
|
||||||
|
if [ -n "$TMUX_PANE" -a ${FZF_TMUX:-1} -ne 0 -a ${LINES:-40} -gt 15 ]; then
|
||||||
|
fzf-file-widget() {
|
||||||
|
local height
|
||||||
|
height=${FZF_TMUX_HEIGHT:-40%}
|
||||||
|
if [[ $height =~ %$ ]]; then
|
||||||
|
height="-p ${height%\%}"
|
||||||
|
else
|
||||||
|
height="-l $height"
|
||||||
|
fi
|
||||||
|
tmux split-window $height "zsh -c 'source ~/.fzf.zsh; tmux send-keys -t $TMUX_PANE \"\$(__fsel)\"'"
|
||||||
|
}
|
||||||
|
else
|
||||||
|
fzf-file-widget() {
|
||||||
|
LBUFFER="${LBUFFER%% #}$(__fsel)"
|
||||||
|
zle redisplay
|
||||||
|
}
|
||||||
|
fi
|
||||||
|
zle -N fzf-file-widget
|
||||||
|
bindkey '^T' fzf-file-widget
|
||||||
|
|
||||||
|
# ALT-C - cd into the selected directory
|
||||||
|
fzf-cd-widget() {
|
||||||
|
cd "${$(set -o nonomatch; 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=$(fc -l 1 | fzf +s | sed "s/ *[0-9]* *//")
|
||||||
|
zle redisplay
|
||||||
|
}
|
||||||
|
zle -N fzf-history-widget
|
||||||
|
bindkey '^R' fzf-history-widget
|
||||||
|
|
||||||
|
fi
|
||||||
|
EOFZF
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "OK"
|
||||||
|
done
|
||||||
|
|
||||||
|
# fish
|
||||||
|
has_fish=0
|
||||||
|
if [ -n "$(which fish)" ]; then
|
||||||
|
has_fish=1
|
||||||
|
echo -n "Generate ~/.config/fish/functions/fzf.fish ... "
|
||||||
|
mkdir -p ~/.config/fish/functions
|
||||||
|
cat > ~/.config/fish/functions/fzf.fish << EOFZF
|
||||||
|
function fzf
|
||||||
|
$fzf_cmd \$argv
|
||||||
|
end
|
||||||
|
EOFZF
|
||||||
|
echo "ok"
|
||||||
|
|
||||||
|
if [ $key_bindings -eq 0 ]; then
|
||||||
|
echo -n "Generate ~/.config/fish/functions/fzf_key_bindings.fish ... "
|
||||||
|
cat > ~/.config/fish/functions/fzf_key_bindings.fish << "EOFZF"
|
||||||
|
function fzf_key_bindings
|
||||||
|
function __fzf_select
|
||||||
|
find * -path '*/\.*' -prune \
|
||||||
|
-o -type f -print \
|
||||||
|
-o -type d -print \
|
||||||
|
-o -type l -print 2> /dev/null | fzf -m | while read item
|
||||||
|
echo -n (echo -n "$item" | sed 's/ /\\\\ /g')' '
|
||||||
|
end
|
||||||
|
echo
|
||||||
|
end
|
||||||
|
|
||||||
|
function __fzf_ctrl_t
|
||||||
|
if [ -n "$TMUX_PANE" -a "$FZF_TMUX" != "0" ]
|
||||||
|
tmux split-window (__fzf_tmux_height) "fish -c 'fzf_key_bindings; __fzf_ctrl_t_tmux \\$TMUX_PANE'"
|
||||||
|
else
|
||||||
|
__fzf_select > $TMPDIR/fzf.result
|
||||||
|
and commandline -i (cat $TMPDIR/fzf.result)
|
||||||
|
rm -f $TMPDIR/fzf.result
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function __fzf_ctrl_t_tmux
|
||||||
|
__fzf_select > $TMPDIR/fzf.result
|
||||||
|
and tmux send-keys -t $argv[1] (cat $TMPDIR/fzf.result)
|
||||||
|
rm -f $TMPDIR/fzf.result
|
||||||
|
end
|
||||||
|
|
||||||
|
function __fzf_ctrl_r
|
||||||
|
if history | fzf +s +m > $TMPDIR/fzf.result
|
||||||
|
commandline (cat $TMPDIR/fzf.result)
|
||||||
|
else
|
||||||
|
commandline -f repaint
|
||||||
|
end
|
||||||
|
rm -f $TMPDIR/fzf.result
|
||||||
|
end
|
||||||
|
|
||||||
|
function __fzf_alt_c
|
||||||
|
find * -path '*/\.*' -prune -o -type d -print 2> /dev/null | fzf +m > $TMPDIR/fzf.result
|
||||||
|
if [ (cat $TMPDIR/fzf.result | wc -l) -gt 0 ]
|
||||||
|
cd (cat $TMPDIR/fzf.result)
|
||||||
|
end
|
||||||
|
commandline -f repaint
|
||||||
|
rm -f $TMPDIR/fzf.result
|
||||||
|
end
|
||||||
|
|
||||||
|
function __fzf_tmux_height
|
||||||
|
if set -q FZF_TMUX_HEIGHT
|
||||||
|
set height $FZF_TMUX_HEIGHT
|
||||||
|
else
|
||||||
|
set height 40%
|
||||||
|
end
|
||||||
|
if echo $height | grep -q -E '%$'
|
||||||
|
echo "-p "(echo $height | sed 's/%$//')
|
||||||
|
else
|
||||||
|
echo "-l $height"
|
||||||
|
end
|
||||||
|
set -e height
|
||||||
|
end
|
||||||
|
|
||||||
|
bind \ct '__fzf_ctrl_t'
|
||||||
|
bind \cr '__fzf_ctrl_r'
|
||||||
|
bind \ec '__fzf_alt_c'
|
||||||
|
end
|
||||||
|
EOFZF
|
||||||
|
echo "ok"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
append_line() {
|
||||||
|
echo "Update $2:"
|
||||||
|
echo " - $1"
|
||||||
|
[ -f "$2" ] || touch "$2"
|
||||||
|
line=$(grep -nF "$1" "$2" | sed 's/:.*//')
|
||||||
|
if [ -n "$line" ]; then
|
||||||
|
echo " - Already exists (line #$line)"
|
||||||
|
else
|
||||||
|
echo "$1" >> "$2"
|
||||||
|
echo " - Added"
|
||||||
|
fi
|
||||||
|
echo
|
||||||
|
}
|
||||||
|
|
||||||
|
echo
|
||||||
|
for shell in bash zsh; do
|
||||||
|
append_line "source ~/.fzf.${shell}" ~/.${shell}rc
|
||||||
|
done
|
||||||
|
|
||||||
|
if [ $key_bindings -eq 0 -a $has_fish -eq 1 ]; then
|
||||||
|
bind_file=~/.config/fish/functions/fish_user_key_bindings.fish
|
||||||
|
append_line "fzf_key_bindings" "$bind_file"
|
||||||
|
fi
|
||||||
|
|
||||||
|
cat << EOF
|
||||||
|
Finished. Restart your shell or reload config file.
|
||||||
|
source ~/.bashrc # bash
|
||||||
|
source ~/.zshrc # zsh
|
||||||
|
EOF
|
||||||
|
[ $has_fish -eq 1 ] && echo " fzf_key_bindings # fish"; cat << EOF
|
||||||
|
|
||||||
|
To uninstall fzf, simply remove the added lines.
|
||||||
|
|
||||||
|
For more information, see: https://github.com/junegunn/fzf
|
||||||
|
EOF
|
||||||
|
|
||||||
|
208
plugin/fzf.vim
208
plugin/fzf.vim
@@ -1,4 +1,4 @@
|
|||||||
" Copyright (c) 2013 Junegunn Choi
|
" Copyright (c) 2014 Junegunn Choi
|
||||||
"
|
"
|
||||||
" MIT License
|
" MIT License
|
||||||
"
|
"
|
||||||
@@ -21,27 +21,201 @@
|
|||||||
" 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.
|
||||||
|
|
||||||
let s:exec = expand('<sfile>:h:h').'/fzf'
|
let s:min_tmux_width = 10
|
||||||
|
let s:min_tmux_height = 3
|
||||||
|
let s:default_tmux_height = '40%'
|
||||||
|
|
||||||
function! fzf#run(command, args)
|
let s:cpo_save = &cpo
|
||||||
try
|
set cpo&vim
|
||||||
let tf = tempname()
|
|
||||||
let prefix = exists('g:fzf_source') ? g:fzf_source.'|' : ''
|
call system('type fzf')
|
||||||
let fzf = executable(s:exec) ? s:exec : 'fzf'
|
if v:shell_error
|
||||||
let options = empty(a:args) ? get(g:, 'fzf_options', '') : a:args
|
let s:fzf_rb = expand('<sfile>:h:h').'/fzf'
|
||||||
execute "silent !".prefix.fzf.' '.options." > ".tf
|
if executable(s:fzf_rb)
|
||||||
if !v:shell_error
|
let s:exec = s:fzf_rb
|
||||||
for line in readfile(tf)
|
else
|
||||||
if !empty(line)
|
echoerr 'fzf executable not found'
|
||||||
execute a:command.' '.line
|
finish
|
||||||
|
endif
|
||||||
|
else
|
||||||
|
let s:exec = 'fzf'
|
||||||
|
endif
|
||||||
|
|
||||||
|
function! s:tmux_enabled()
|
||||||
|
if exists('s:tmux')
|
||||||
|
return s:tmux
|
||||||
|
endif
|
||||||
|
|
||||||
|
let s:tmux = 0
|
||||||
|
if exists('$TMUX')
|
||||||
|
let output = system('tmux -V')
|
||||||
|
let s:tmux = !v:shell_error && output >= 'tmux 1.7'
|
||||||
|
endif
|
||||||
|
return s:tmux
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:shellesc(arg)
|
||||||
|
return '"'.substitute(a:arg, '"', '\\"', 'g').'"'
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:escape(path)
|
||||||
|
return substitute(a:path, ' ', '\\ ', 'g')
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! fzf#run(...) abort
|
||||||
|
if has('gui_running')
|
||||||
|
echohl Error
|
||||||
|
echo 'GVim is not supported'
|
||||||
|
return []
|
||||||
|
endif
|
||||||
|
let dict = exists('a:1') ? a:1 : {}
|
||||||
|
let temps = { 'result': tempname() }
|
||||||
|
let optstr = get(dict, 'options', '')
|
||||||
|
|
||||||
|
if has_key(dict, 'source')
|
||||||
|
let source = dict.source
|
||||||
|
let type = type(source)
|
||||||
|
if type == 1
|
||||||
|
let prefix = source.'|'
|
||||||
|
elseif type == 3
|
||||||
|
let temps.input = tempname()
|
||||||
|
call writefile(source, temps.input)
|
||||||
|
let prefix = 'cat '.s:shellesc(temps.input).'|'
|
||||||
|
else
|
||||||
|
throw 'Invalid source type'
|
||||||
|
endif
|
||||||
|
else
|
||||||
|
let prefix = ''
|
||||||
|
endif
|
||||||
|
let command = prefix.s:exec.' '.optstr.' > '.temps.result
|
||||||
|
|
||||||
|
if s:tmux_enabled() && s:tmux_splittable(dict)
|
||||||
|
return s:execute_tmux(dict, command, temps)
|
||||||
|
else
|
||||||
|
return s:execute(dict, command, temps)
|
||||||
|
endif
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:tmux_splittable(dict)
|
||||||
|
return
|
||||||
|
\ min([&columns, get(a:dict, 'tmux_width', 0)]) >= s:min_tmux_width ||
|
||||||
|
\ min([&lines, get(a:dict, 'tmux_height', get(a:dict, 'tmux', 0))]) >= s:min_tmux_height
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:pushd(dict)
|
||||||
|
if has_key(a:dict, 'dir')
|
||||||
|
let a:dict.prev_dir = getcwd()
|
||||||
|
execute 'chdir '.s:escape(a:dict.dir)
|
||||||
|
endif
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:popd(dict)
|
||||||
|
if has_key(a:dict, 'prev_dir')
|
||||||
|
execute 'chdir '.s:escape(remove(a:dict, 'prev_dir'))
|
||||||
|
endif
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:execute(dict, command, temps)
|
||||||
|
call s:pushd(a:dict)
|
||||||
|
silent !clear
|
||||||
|
execute 'silent !'.a:command
|
||||||
|
redraw!
|
||||||
|
if v:shell_error
|
||||||
|
return []
|
||||||
|
else
|
||||||
|
return s:callback(a:dict, a:temps, 0)
|
||||||
|
endif
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:execute_tmux(dict, command, temps)
|
||||||
|
if has_key(a:dict, 'dir')
|
||||||
|
let command = 'cd '.s:escape(a:dict.dir).' && '.a:command
|
||||||
|
else
|
||||||
|
let command = a:command
|
||||||
|
endif
|
||||||
|
|
||||||
|
let splitopt = '-v'
|
||||||
|
if has_key(a:dict, 'tmux_width')
|
||||||
|
let splitopt = '-h'
|
||||||
|
let size = a:dict.tmux_width
|
||||||
|
else
|
||||||
|
let size = get(a:dict, 'tmux_height', get(a:dict, 'tmux'))
|
||||||
|
endif
|
||||||
|
|
||||||
|
if type(size) == 1 && size =~ '%$'
|
||||||
|
let sizeopt = '-p '.size[0:-2]
|
||||||
|
else
|
||||||
|
let sizeopt = '-l '.size
|
||||||
|
endif
|
||||||
|
|
||||||
|
let s:pane = substitute(
|
||||||
|
\ system(
|
||||||
|
\ printf(
|
||||||
|
\ 'tmux split-window %s %s -P -F "#{pane_id}" %s',
|
||||||
|
\ splitopt, sizeopt, s:shellesc(command))), '\n', '', 'g')
|
||||||
|
let s:dict = a:dict
|
||||||
|
let s:temps = a:temps
|
||||||
|
|
||||||
|
augroup fzf_tmux
|
||||||
|
autocmd!
|
||||||
|
autocmd VimResized * nested call s:tmux_check()
|
||||||
|
augroup END
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:tmux_check()
|
||||||
|
let panes = split(system('tmux list-panes -a -F "#{pane_id}"'), '\n')
|
||||||
|
|
||||||
|
if index(panes, s:pane) < 0
|
||||||
|
augroup fzf_tmux
|
||||||
|
autocmd!
|
||||||
|
augroup END
|
||||||
|
|
||||||
|
call s:callback(s:dict, s:temps, 1)
|
||||||
|
redraw
|
||||||
|
endif
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:callback(dict, temps, cd)
|
||||||
|
if !filereadable(a:temps.result)
|
||||||
|
let lines = []
|
||||||
|
else
|
||||||
|
if a:cd | call s:pushd(a:dict) | endif
|
||||||
|
|
||||||
|
let lines = readfile(a:temps.result)
|
||||||
|
if has_key(a:dict, 'sink')
|
||||||
|
for line in lines
|
||||||
|
if type(a:dict.sink) == 2
|
||||||
|
call a:dict.sink(line)
|
||||||
|
else
|
||||||
|
execute a:dict.sink.' '.s:escape(line)
|
||||||
endif
|
endif
|
||||||
endfor
|
endfor
|
||||||
endif
|
endif
|
||||||
finally
|
endif
|
||||||
redraw!
|
|
||||||
|
for tf in values(a:temps)
|
||||||
silent! call delete(tf)
|
silent! call delete(tf)
|
||||||
endtry
|
endfor
|
||||||
|
|
||||||
|
call s:popd(a:dict)
|
||||||
|
|
||||||
|
return lines
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
command! -nargs=* FZF call fzf#run('silent e', <q-args>)
|
function! s:cmd(bang, ...) abort
|
||||||
|
let args = copy(a:000)
|
||||||
|
let opts = {}
|
||||||
|
if len(args) > 0 && isdirectory(expand(args[-1]))
|
||||||
|
let opts.dir = remove(args, -1)
|
||||||
|
endif
|
||||||
|
if !a:bang
|
||||||
|
let opts.tmux = get(g:, 'fzf_tmux_height', s:default_tmux_height)
|
||||||
|
endif
|
||||||
|
call fzf#run(extend({ 'sink': 'e', 'options': join(args) }, opts))
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
command! -nargs=* -complete=dir -bang FZF call s:cmd('<bang>' == '!', <f-args>)
|
||||||
|
|
||||||
|
let &cpo = s:cpo_save
|
||||||
|
unlet s:cpo_save
|
||||||
|
|
||||||
|
37
test/fzf.vader
Normal file
37
test/fzf.vader
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
Execute (Setup):
|
||||||
|
let g:dir = fnamemodify(g:vader_file, ':p:h')
|
||||||
|
Log 'Test directory: ' . g:dir
|
||||||
|
|
||||||
|
Execute (fzf#run with dir option):
|
||||||
|
let result = fzf#run({ 'options': '--filter=vdr', 'dir': g:dir })
|
||||||
|
AssertEqual ['fzf.vader'], result
|
||||||
|
|
||||||
|
let result = sort(fzf#run({ 'options': '--filter e', 'dir': g:dir }))
|
||||||
|
AssertEqual ['fzf.vader', 'test_fzf.rb'], result
|
||||||
|
|
||||||
|
Execute (fzf#run with Funcref command):
|
||||||
|
let g:ret = []
|
||||||
|
function! g:proc(e)
|
||||||
|
call add(g:ret, a:e)
|
||||||
|
endfunction
|
||||||
|
let result = sort(fzf#run({ 'sink': function('g:proc'), 'options': '--filter e', 'dir': g:dir }))
|
||||||
|
AssertEqual ['fzf.vader', 'test_fzf.rb'], result
|
||||||
|
AssertEqual ['fzf.vader', 'test_fzf.rb'], sort(g:ret)
|
||||||
|
|
||||||
|
Execute (fzf#run with string source):
|
||||||
|
let result = sort(fzf#run({ 'source': 'echo hi', 'options': '-f i' }))
|
||||||
|
AssertEqual ['hi'], result
|
||||||
|
|
||||||
|
Execute (fzf#run with list source):
|
||||||
|
let result = sort(fzf#run({ 'source': ['hello', 'world'], 'options': '-f e' }))
|
||||||
|
AssertEqual ['hello'], result
|
||||||
|
let result = sort(fzf#run({ 'source': ['hello', 'world'], 'options': '-f o' }))
|
||||||
|
AssertEqual ['hello', 'world'], result
|
||||||
|
|
||||||
|
Execute (fzf#run with string source):
|
||||||
|
let result = sort(fzf#run({ 'source': 'echo hi', 'options': '-f i' }))
|
||||||
|
AssertEqual ['hi'], result
|
||||||
|
|
||||||
|
Execute (Cleanup):
|
||||||
|
unlet g:dir
|
||||||
|
Restore
|
407
test/test_fzf.rb
407
test/test_fzf.rb
@@ -1,50 +1,156 @@
|
|||||||
#!/usr/bin/env ruby
|
#!/usr/bin/env ruby
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
|
||||||
|
require 'curses'
|
||||||
|
require 'timeout'
|
||||||
|
require 'stringio'
|
||||||
require 'minitest/autorun'
|
require 'minitest/autorun'
|
||||||
$LOAD_PATH.unshift File.expand_path('../..', __FILE__)
|
$LOAD_PATH.unshift File.expand_path('../..', __FILE__)
|
||||||
ENV['FZF_EXECUTABLE'] = '0'
|
ENV['FZF_EXECUTABLE'] = '0'
|
||||||
load 'fzf'
|
load 'fzf'
|
||||||
|
|
||||||
class TestFZF < MiniTest::Unit::TestCase
|
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
|
def test_default_options
|
||||||
fzf = FZF.new []
|
fzf = FZF.new []
|
||||||
assert_equal 1000, fzf.sort
|
assert_equal 1000, fzf.sort
|
||||||
assert_equal false, fzf.multi
|
assert_equal false, fzf.multi
|
||||||
assert_equal true, fzf.color
|
assert_equal true, fzf.color
|
||||||
assert_equal Regexp::IGNORECASE, fzf.rxflag
|
assert_equal nil, fzf.rxflag
|
||||||
|
assert_equal true, fzf.mouse
|
||||||
|
assert_equal nil, fzf.nth
|
||||||
|
assert_equal true, fzf.color
|
||||||
|
assert_equal false, fzf.black
|
||||||
|
assert_equal true, fzf.ansi256
|
||||||
|
assert_equal '', fzf.query.get
|
||||||
|
assert_equal false, fzf.select1
|
||||||
|
assert_equal false, fzf.exit0
|
||||||
|
assert_equal nil, fzf.filter
|
||||||
|
assert_equal nil, fzf.extended
|
||||||
|
assert_equal false, fzf.reverse
|
||||||
|
end
|
||||||
|
|
||||||
begin
|
def test_environment_variables
|
||||||
ENV['FZF_DEFAULT_SORT'] = '1500'
|
# Deprecated
|
||||||
fzf = FZF.new []
|
ENV['FZF_DEFAULT_SORT'] = '20000'
|
||||||
assert_equal 1500, fzf.sort
|
fzf = FZF.new []
|
||||||
ensure
|
assert_equal 20000, fzf.sort
|
||||||
ENV.delete 'FZF_DEFAULT_SORT'
|
assert_equal nil, fzf.nth
|
||||||
end
|
|
||||||
|
ENV['FZF_DEFAULT_OPTS'] =
|
||||||
|
'-x -m -s 10000 -q " hello world " +c +2 --select-1 -0 ' +
|
||||||
|
'--no-mouse -f "goodbye world" --black --nth=3,-1,2 --reverse'
|
||||||
|
fzf = FZF.new []
|
||||||
|
assert_equal 10000, fzf.sort
|
||||||
|
assert_equal ' hello world ',
|
||||||
|
fzf.query.get
|
||||||
|
assert_equal 'goodbye world',
|
||||||
|
fzf.filter
|
||||||
|
assert_equal :fuzzy, fzf.extended
|
||||||
|
assert_equal true, fzf.multi
|
||||||
|
assert_equal false, fzf.color
|
||||||
|
assert_equal false, fzf.ansi256
|
||||||
|
assert_equal true, fzf.black
|
||||||
|
assert_equal false, fzf.mouse
|
||||||
|
assert_equal true, fzf.select1
|
||||||
|
assert_equal true, fzf.exit0
|
||||||
|
assert_equal true, fzf.reverse
|
||||||
|
assert_equal [3, -1, 2], fzf.nth
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_option_parser
|
def test_option_parser
|
||||||
# Long opts
|
# Long opts
|
||||||
fzf = FZF.new %w[--sort=2000 --no-color --multi +i]
|
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello --select-1
|
||||||
assert_equal 2000, fzf.sort
|
--exit-0 --filter=howdy --extended-exact
|
||||||
assert_equal true, fzf.multi
|
--no-mouse --no-256 --nth=1 --reverse]
|
||||||
assert_equal false, fzf.color
|
assert_equal 2000, fzf.sort
|
||||||
assert_equal 0, fzf.rxflag
|
assert_equal true, fzf.multi
|
||||||
|
assert_equal false, fzf.color
|
||||||
|
assert_equal false, fzf.ansi256
|
||||||
|
assert_equal false, fzf.black
|
||||||
|
assert_equal false, fzf.mouse
|
||||||
|
assert_equal 0, fzf.rxflag
|
||||||
|
assert_equal 'hello', fzf.query.get
|
||||||
|
assert_equal true, fzf.select1
|
||||||
|
assert_equal true, fzf.exit0
|
||||||
|
assert_equal 'howdy', fzf.filter
|
||||||
|
assert_equal :exact, fzf.extended
|
||||||
|
assert_equal [1], fzf.nth
|
||||||
|
assert_equal true, fzf.reverse
|
||||||
|
|
||||||
|
# Long opts (left-to-right)
|
||||||
|
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query=hello
|
||||||
|
--filter a --filter b --no-256 --black --nth -1 --nth -2
|
||||||
|
--select-1 --exit-0 --no-select-1 --no-exit-0
|
||||||
|
--no-sort -i --color --no-multi --256
|
||||||
|
--reverse --no-reverse]
|
||||||
|
assert_equal nil, fzf.sort
|
||||||
|
assert_equal false, fzf.multi
|
||||||
|
assert_equal true, fzf.color
|
||||||
|
assert_equal true, fzf.ansi256
|
||||||
|
assert_equal true, fzf.black
|
||||||
|
assert_equal true, fzf.mouse
|
||||||
|
assert_equal 1, fzf.rxflag
|
||||||
|
assert_equal 'b', fzf.filter
|
||||||
|
assert_equal 'hello', fzf.query.get
|
||||||
|
assert_equal false, fzf.select1
|
||||||
|
assert_equal false, fzf.exit0
|
||||||
|
assert_equal nil, fzf.extended
|
||||||
|
assert_equal [-2], fzf.nth
|
||||||
|
assert_equal false, fzf.reverse
|
||||||
|
|
||||||
# Short opts
|
# Short opts
|
||||||
fzf = FZF.new %w[-s 2000 +c -m +i]
|
fzf = FZF.new %w[-s2000 +c -m +i -qhello -x -fhowdy +2 -n3 -1 -0]
|
||||||
assert_equal 2000, fzf.sort
|
assert_equal 2000, fzf.sort
|
||||||
assert_equal true, fzf.multi
|
assert_equal true, fzf.multi
|
||||||
assert_equal false, fzf.color
|
assert_equal false, fzf.color
|
||||||
assert_equal 0, fzf.rxflag
|
assert_equal false, fzf.ansi256
|
||||||
|
assert_equal 0, fzf.rxflag
|
||||||
|
assert_equal 'hello', fzf.query.get
|
||||||
|
assert_equal 'howdy', fzf.filter
|
||||||
|
assert_equal :fuzzy, fzf.extended
|
||||||
|
assert_equal [3], fzf.nth
|
||||||
|
assert_equal true, fzf.select1
|
||||||
|
assert_equal true, fzf.exit0
|
||||||
|
|
||||||
|
# Left-to-right
|
||||||
|
fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x -fgoodbye +2 -n3 -n4,5
|
||||||
|
-s 3000 -c +m -i -q world +x -fworld -2 --black --no-black
|
||||||
|
-1 -0 +1 +0
|
||||||
|
]
|
||||||
|
assert_equal 3000, fzf.sort
|
||||||
|
assert_equal false, fzf.multi
|
||||||
|
assert_equal true, fzf.color
|
||||||
|
assert_equal true, fzf.ansi256
|
||||||
|
assert_equal false, fzf.black
|
||||||
|
assert_equal 1, fzf.rxflag
|
||||||
|
assert_equal 'world', fzf.query.get
|
||||||
|
assert_equal false, fzf.select1
|
||||||
|
assert_equal false, fzf.exit0
|
||||||
|
assert_equal 'world', fzf.filter
|
||||||
|
assert_equal nil, fzf.extended
|
||||||
|
assert_equal [4, 5], fzf.nth
|
||||||
|
rescue SystemExit => e
|
||||||
|
assert false, "Exited"
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_invalid_option
|
def test_invalid_option
|
||||||
[%w[-s 2000 +s], %w[yo dawg]].each do |argv|
|
[%w[--unknown], %w[yo dawg]].each do |argv|
|
||||||
assert_raises(SystemExit) do
|
assert_raises(SystemExit) do
|
||||||
fzf = FZF.new argv
|
fzf = FZF.new argv
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
assert_raises(SystemExit) do
|
||||||
|
fzf = FZF.new %w[--nth=0]
|
||||||
|
end
|
||||||
|
assert_raises(SystemExit) do
|
||||||
|
fzf = FZF.new %w[-n 0]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# FIXME Only on 1.9 or above
|
# FIXME Only on 1.9 or above
|
||||||
@@ -139,15 +245,59 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
# TODO : partial_cache
|
# TODO : partial_cache
|
||||||
end
|
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
|
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]]]],
|
assert_equal [['Fruit', [[0, 5]]]],
|
||||||
FZF::FuzzyMatcher.new(0).match(%w[Fruit Grapefruit], 'Fruit', '', '').sort
|
FZF::FuzzyMatcher.new(0).match(%w[Fruit Grapefruit], 'Fruit', '', '').sort
|
||||||
|
|
||||||
|
# Case-insensitive match (+i)
|
||||||
assert_equal [["Fruit", [[0, 5]]], ["Grapefruit", [[5, 10]]]],
|
assert_equal [["Fruit", [[0, 5]]], ["Grapefruit", [[5, 10]]]],
|
||||||
FZF::FuzzyMatcher.new(Regexp::IGNORECASE).
|
FZF::FuzzyMatcher.new(Regexp::IGNORECASE).
|
||||||
match(%w[Fruit Grapefruit], 'Fruit', '', '').sort
|
match(%w[Fruit Grapefruit], 'Fruit', '', '').sort
|
||||||
end
|
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
|
def test_extended_fuzzy_matcher
|
||||||
matcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE
|
matcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE
|
||||||
list = %w[
|
list = %w[
|
||||||
@@ -184,6 +334,11 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal list.length, match.call('j', '').length
|
assert_equal list.length, match.call('j', '').length
|
||||||
assert_equal list.length - 1, 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 0, match.call('!j', '').length
|
||||||
|
|
||||||
@@ -250,7 +405,7 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
["0____1", [[0, 6]]],
|
["0____1", [[0, 6]]],
|
||||||
["0_____1", [[0, 7]]],
|
["0_____1", [[0, 7]]],
|
||||||
["0______1", [[0, 8]]]],
|
["0______1", [[0, 8]]]],
|
||||||
FZF.new([]).sort_by_rank(matcher.match(list, '01', '', '')))
|
FZF.sort(matcher.match(list, '01', '', '')))
|
||||||
|
|
||||||
assert_equal(
|
assert_equal(
|
||||||
[["01", [[0, 1], [1, 2]]],
|
[["01", [[0, 1], [1, 2]]],
|
||||||
@@ -261,16 +416,38 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
["____0_1", [[4, 5], [6, 7]]],
|
["____0_1", [[4, 5], [6, 7]]],
|
||||||
["0______1", [[0, 1], [7, 8]]],
|
["0______1", [[0, 1], [7, 8]]],
|
||||||
["___01___", [[3, 4], [4, 5]]]],
|
["___01___", [[3, 4], [4, 5]]]],
|
||||||
FZF.new([]).sort_by_rank(xmatcher.match(list, '0 1', '', '')))
|
FZF.sort(xmatcher.match(list, '0 1', '', '')))
|
||||||
|
|
||||||
assert_equal(
|
assert_equal(
|
||||||
[["_01_", [[1, 3], [0, 4]]],
|
[["_01_", [[1, 3], [0, 4]], [4, 4, "_01_"]],
|
||||||
["0____1", [[0, 6], [1, 3]]],
|
["___01___", [[3, 5], [0, 2]], [4, 8, "___01___"]],
|
||||||
["0_____1", [[0, 7], [1, 3]]],
|
["____0_1", [[4, 7], [0, 2]], [5, 7, "____0_1"]],
|
||||||
["0______1", [[0, 8], [1, 3]]],
|
["0____1", [[0, 6], [1, 3]], [6, 6, "0____1"]],
|
||||||
["___01___", [[3, 5], [0, 2]]],
|
["0_____1", [[0, 7], [1, 3]], [7, 7, "0_____1"]],
|
||||||
["____0_1", [[4, 7], [0, 2]]]],
|
["0______1", [[0, 8], [1, 3]], [8, 8, "0______1"]]],
|
||||||
FZF.new([]).sort_by_rank(xmatcher.match(list, '01 __', '', '')))
|
FZF.sort(xmatcher.match(list, '01 __', '', '')).map { |tuple|
|
||||||
|
tuple << FZF.rank(tuple)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_extended_exact_mode
|
||||||
|
exact = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE, :exact
|
||||||
|
fuzzy = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE, :fuzzy
|
||||||
|
list = %w[
|
||||||
|
extended-exact-mode-not-fuzzy
|
||||||
|
extended'-fuzzy-mode
|
||||||
|
]
|
||||||
|
assert_equal 2, fuzzy.match(list, 'extended', '', '').length
|
||||||
|
assert_equal 2, fuzzy.match(list, 'mode extended', '', '').length
|
||||||
|
assert_equal 2, fuzzy.match(list, 'xtndd', '', '').length
|
||||||
|
assert_equal 2, fuzzy.match(list, "'-fuzzy", '', '').length
|
||||||
|
|
||||||
|
assert_equal 2, exact.match(list, 'extended', '', '').length
|
||||||
|
assert_equal 2, exact.match(list, 'mode extended', '', '').length
|
||||||
|
assert_equal 0, exact.match(list, 'xtndd', '', '').length
|
||||||
|
assert_equal 1, exact.match(list, "'-fuzzy", '', '').length
|
||||||
|
assert_equal 2, exact.match(list, "-fuzzy", '', '').length
|
||||||
end
|
end
|
||||||
|
|
||||||
if RUBY_PLATFORM =~ /darwin/
|
if RUBY_PLATFORM =~ /darwin/
|
||||||
@@ -319,5 +496,175 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal ["a", "b", "c", "\xFF", "d", "e", "f"],
|
assert_equal ["a", "b", "c", "\xFF", "d", "e", "f"],
|
||||||
FZF::UConv.split("abc\xFFdef")
|
FZF::UConv.split("abc\xFFdef")
|
||||||
end
|
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
|
||||||
|
|
||||||
|
def test_mouse_event
|
||||||
|
interval = FZF::MouseEvent::DOUBLE_CLICK_INTERVAL
|
||||||
|
me = FZF::MouseEvent.new nil
|
||||||
|
me.v = 10
|
||||||
|
assert_equal false, me.double?(10)
|
||||||
|
assert_equal false, me.double?(20)
|
||||||
|
me.v = 20
|
||||||
|
assert_equal false, me.double?(10)
|
||||||
|
assert_equal false, me.double?(20)
|
||||||
|
me.v = 20
|
||||||
|
assert_equal false, me.double?(10)
|
||||||
|
assert_equal true, me.double?(20)
|
||||||
|
sleep interval
|
||||||
|
assert_equal false, me.double?(20)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_nth_match
|
||||||
|
list = [
|
||||||
|
' first second third',
|
||||||
|
'fourth fifth sixth',
|
||||||
|
]
|
||||||
|
|
||||||
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE
|
||||||
|
assert_equal list, matcher.match(list, 'f', '', '').map(&:first)
|
||||||
|
assert_equal [
|
||||||
|
[list[0], [[2, 5]]],
|
||||||
|
[list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')
|
||||||
|
|
||||||
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [2]
|
||||||
|
assert_equal [[list[1], [[8, 9]]]], matcher.match(list, 'f', '', '')
|
||||||
|
assert_equal [[list[0], [[8, 9]]]], matcher.match(list, 's', '', '')
|
||||||
|
|
||||||
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [3]
|
||||||
|
assert_equal [[list[0], [[19, 20]]]], matcher.match(list, 'r', '', '')
|
||||||
|
|
||||||
|
# Comma-separated
|
||||||
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [3, 1]
|
||||||
|
assert_equal [[list[0], [[19, 20]]], [list[1], [[3, 4]]]], matcher.match(list, 'r', '', '')
|
||||||
|
|
||||||
|
# Ordered
|
||||||
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1, 3]
|
||||||
|
assert_equal [[list[0], [[3, 4]]], [list[1], [[3, 4]]]], matcher.match(list, 'r', '', '')
|
||||||
|
|
||||||
|
regex = FZF.build_delim_regex "\t"
|
||||||
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1], regex
|
||||||
|
assert_equal [[list[0], [[3, 10]]]], matcher.match(list, 're', '', '')
|
||||||
|
|
||||||
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [2], regex
|
||||||
|
assert_equal [], matcher.match(list, 'r', '', '')
|
||||||
|
assert_equal [[list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')
|
||||||
|
|
||||||
|
# Negative indexing
|
||||||
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [-1], regex
|
||||||
|
assert_equal [[list[0], [[3, 6]]]], matcher.match(list, 'rt', '', '')
|
||||||
|
assert_equal [[list[0], [[2, 5]]], [list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')
|
||||||
|
|
||||||
|
# Regex delimiter
|
||||||
|
regex = FZF.build_delim_regex "[ \t]+"
|
||||||
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1], regex
|
||||||
|
assert_equal [list[1]], matcher.match(list, 'f', '', '').map(&:first)
|
||||||
|
|
||||||
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [2], regex
|
||||||
|
assert_equal [[list[0], [[1, 2]]], [list[1], [[8, 9]]]], matcher.match(list, 'f', '', '')
|
||||||
|
end
|
||||||
|
|
||||||
|
def stream_for str
|
||||||
|
StringIO.new(str).tap do |sio|
|
||||||
|
sio.instance_eval do
|
||||||
|
alias org_gets gets
|
||||||
|
|
||||||
|
def gets
|
||||||
|
org_gets.tap { |e| sleep 0.5 unless e.nil? }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_select_1
|
||||||
|
stream = stream_for "Hello\nWorld"
|
||||||
|
output = StringIO.new
|
||||||
|
|
||||||
|
begin
|
||||||
|
$stdout = output
|
||||||
|
FZF.new(%w[--query=ol --select-1], stream).start
|
||||||
|
rescue SystemExit => e
|
||||||
|
assert_equal 0, e.status
|
||||||
|
assert_equal 'World', output.string.chomp
|
||||||
|
ensure
|
||||||
|
$stdout = STDOUT
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_select_1_without_query
|
||||||
|
stream = stream_for "Hello World"
|
||||||
|
output = StringIO.new
|
||||||
|
|
||||||
|
begin
|
||||||
|
$stdout = output
|
||||||
|
FZF.new(%w[--select-1], stream).start
|
||||||
|
rescue SystemExit => e
|
||||||
|
assert_equal 0, e.status
|
||||||
|
assert_equal 'Hello World', output.string.chomp
|
||||||
|
ensure
|
||||||
|
$stdout = STDOUT
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_select_1_ambiguity
|
||||||
|
stream = stream_for "Hello\nWorld"
|
||||||
|
begin
|
||||||
|
Timeout::timeout(3) do
|
||||||
|
FZF.new(%w[--query=o --select-1], stream).start
|
||||||
|
end
|
||||||
|
flunk 'Should not reach here'
|
||||||
|
rescue Exception => e
|
||||||
|
Curses.close_screen
|
||||||
|
assert_instance_of Timeout::Error, e
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_exit_0
|
||||||
|
stream = stream_for "Hello\nWorld"
|
||||||
|
output = StringIO.new
|
||||||
|
|
||||||
|
begin
|
||||||
|
$stdout = output
|
||||||
|
FZF.new(%w[--query=zz --exit-0], stream).start
|
||||||
|
rescue SystemExit => e
|
||||||
|
assert_equal 0, e.status
|
||||||
|
assert_equal '', output.string
|
||||||
|
ensure
|
||||||
|
$stdout = STDOUT
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_exit_0_without_query
|
||||||
|
stream = stream_for ""
|
||||||
|
output = StringIO.new
|
||||||
|
|
||||||
|
begin
|
||||||
|
$stdout = output
|
||||||
|
FZF.new(%w[--exit-0], stream).start
|
||||||
|
rescue SystemExit => e
|
||||||
|
assert_equal 0, e.status
|
||||||
|
assert_equal '', output.string
|
||||||
|
ensure
|
||||||
|
$stdout = STDOUT
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_ranking_overlap_match_regions
|
||||||
|
list = [
|
||||||
|
'1 3 4 2',
|
||||||
|
'1 2 3 4'
|
||||||
|
]
|
||||||
|
assert_equal [
|
||||||
|
['1 2 3 4', [[0, 13], [16, 22]]],
|
||||||
|
['1 3 4 2', [[0, 24], [12, 17]]],
|
||||||
|
], FZF.sort(FZF::ExtendedFuzzyMatcher.new(nil).match(list, '12 34', '', ''))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user