mirror of
https://github.com/junegunn/fzf.git
synced 2025-11-15 06:43:47 -05:00
Compare commits
40 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 |
208
README.md
208
README.md
@@ -24,11 +24,24 @@ git clone https://github.com/junegunn/fzf.git ~/.fzf
|
|||||||
~/.fzf/install
|
~/.fzf/install
|
||||||
```
|
```
|
||||||
|
|
||||||
|
In case you don't have git installed:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
mkdir -p ~/.fzf
|
||||||
|
curl -L https://github.com/junegunn/fzf/archive/master.tar.gz |
|
||||||
|
tar xz --strip-components 1 -C ~/.fzf
|
||||||
|
~/.fzf/install
|
||||||
|
```
|
||||||
|
|
||||||
The script will setup:
|
The script will setup:
|
||||||
|
|
||||||
- `fzf` executable
|
- `fzf` function (bash, zsh, fish)
|
||||||
- Key bindings (`CTRL-T`, `CTRL-R`, and `ALT-C`) for bash and zsh
|
- Key bindings (`CTRL-T`, `CTRL-R`, and `ALT-C`) (bash, zsh, fish)
|
||||||
- Fuzzy auto-completion for bash
|
- Fuzzy auto-completion (bash)
|
||||||
|
|
||||||
|
If you don't use any of the aforementioned shells, you have to manually place
|
||||||
|
fzf executable in a directory included in `$PATH`. Key bindings and
|
||||||
|
auto-completion will not be available in that case.
|
||||||
|
|
||||||
### Install as Vim plugin
|
### Install as Vim plugin
|
||||||
|
|
||||||
@@ -47,22 +60,32 @@ Usage
|
|||||||
```
|
```
|
||||||
usage: fzf [options]
|
usage: fzf [options]
|
||||||
|
|
||||||
Options
|
Search
|
||||||
-m, --multi Enable multi-select
|
|
||||||
-x, --extended Extended-search mode
|
-x, --extended Extended-search mode
|
||||||
-e, --extended-exact Extended-search mode (exact match)
|
-e, --extended-exact Extended-search mode (exact match)
|
||||||
-q, --query=STR Initial query
|
|
||||||
-f, --filter=STR Filter mode. Do not start interactive finder.
|
|
||||||
-n, --nth=[-]N Match only in the N-th token of the item
|
|
||||||
-d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)
|
|
||||||
-s, --sort=MAX Maximum number of matched items to sort (default: 1000)
|
|
||||||
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
|
||||||
-i Case-insensitive match (default: smart-case match)
|
-i Case-insensitive match (default: smart-case match)
|
||||||
+i Case-sensitive match
|
+i Case-sensitive match
|
||||||
|
-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
|
+c, --no-color Disable colors
|
||||||
+2, --no-256 Disable 256-color
|
+2, --no-256 Disable 256-color
|
||||||
--black Use black background
|
--black Use black background
|
||||||
--no-mouse Disable mouse
|
--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
|
Environment variables
|
||||||
FZF_DEFAULT_COMMAND Default command to use when input is tty
|
FZF_DEFAULT_COMMAND Default command to use when input is tty
|
||||||
@@ -133,10 +156,13 @@ Useful 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)
|
||||||
|
# - Exit if there's no match (--exit-0)
|
||||||
|
fe() {
|
||||||
local file
|
local file
|
||||||
file=$(fzf --query="$1") && vim "$file"
|
file=$(fzf --query="$1" --select-1 --exit-0)
|
||||||
|
[ -n "$file" ] && ${EDITOR:-vim} "$file"
|
||||||
}
|
}
|
||||||
|
|
||||||
# fd - cd to selected directory
|
# fd - cd to selected directory
|
||||||
@@ -155,7 +181,7 @@ fda() {
|
|||||||
|
|
||||||
# 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
|
||||||
@@ -183,40 +209,22 @@ fco() {
|
|||||||
ftags() {
|
ftags() {
|
||||||
local line
|
local line
|
||||||
[ -e tags ] &&
|
[ -e tags ] &&
|
||||||
line=$(grep -v "^!" tags | cut -f1-3 | cut -c1-80 | fzf --nth=1) &&
|
line=$(
|
||||||
$EDITOR $(cut -f2 <<< "$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" \
|
||||||
# fq1 [QUERY]
|
-c "silent tag $(cut -f2 <<< "$line")"
|
||||||
# - Immediately select the file when there's only one match.
|
|
||||||
# If not, start the fuzzy finder as usual.
|
|
||||||
fq1() {
|
|
||||||
local lines
|
|
||||||
lines=$(fzf --filter="$1" --no-sort)
|
|
||||||
if [ -z "$lines" ]; then
|
|
||||||
return 1
|
|
||||||
elif [ $(wc -l <<< "$lines") -eq 1 ]; then
|
|
||||||
echo "$lines"
|
|
||||||
else
|
|
||||||
echo "$lines" | fzf --query="$1"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# fe [QUERY]
|
|
||||||
# - Open the selected file with the default editor
|
|
||||||
# (Bypass fuzzy finder when there's only one match)
|
|
||||||
fe() {
|
|
||||||
local file
|
|
||||||
file=$(fq1 "$1") && ${EDITOR:-vim} "$file"
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
For more examples, see [the wiki
|
||||||
|
page](https://github.com/junegunn/fzf/wiki/examples).
|
||||||
|
|
||||||
Key bindings for command line
|
Key bindings for command line
|
||||||
-----------------------------
|
-----------------------------
|
||||||
|
|
||||||
The install script will setup the following key bindings.
|
The install script will setup the following key bindings for bash, zsh, and
|
||||||
|
fish.
|
||||||
### bash/zsh
|
|
||||||
|
|
||||||
- `CTRL-T` - Paste the selected file path(s) into the command line
|
- `CTRL-T` - Paste the selected file path(s) into the command line
|
||||||
- `CTRL-R` - Paste the selected command from history into the command line
|
- `CTRL-R` - Paste the selected command from history into the command line
|
||||||
@@ -226,7 +234,13 @@ 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
|
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%`).
|
height of the window with `FZF_TMUX_HEIGHT` (e.g. `20`, `50%`).
|
||||||
|
|
||||||
The source code can be found in `~/.fzf.bash` and in `~/.fzf.zsh`.
|
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
|
Auto-completion
|
||||||
---------------
|
---------------
|
||||||
@@ -335,15 +349,16 @@ of the selected items.
|
|||||||
|
|
||||||
`fzf#run()` may take an options-dictionary:
|
`fzf#run()` may take an options-dictionary:
|
||||||
|
|
||||||
| Option name | Type | Description |
|
| Option name | Type | Description |
|
||||||
| ----------- | ------------- | ------------------------------------------------------------------- |
|
| ------------- | ------------- | ------------------------------------------------------------------ |
|
||||||
| `source` | string | External command to generate input to fzf (e.g. `find .`) |
|
| `source` | string | External command to generate input to fzf (e.g. `find .`) |
|
||||||
| `source` | list | Vim list as input to fzf |
|
| `source` | list | Vim list as input to fzf |
|
||||||
| `sink` | string | Vim command to handle the selected item (e.g. `e`, `tabe`) |
|
| `sink` | string | Vim command to handle the selected item (e.g. `e`, `tabe`) |
|
||||||
| `sink` | funcref | Reference to function to process each selected item |
|
| `sink` | funcref | Reference to function to process each selected item |
|
||||||
| `options` | string | Options to fzf |
|
| `options` | string | Options to fzf |
|
||||||
| `dir` | string | Working directory |
|
| `dir` | string | Working directory |
|
||||||
| `tmux` | number/string | Use tmux split if possible with the given height (e.g. `20`, `50%`) |
|
| `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
|
#### Examples
|
||||||
|
|
||||||
@@ -369,9 +384,9 @@ nnoremap <silent> <Leader>C :call fzf#run({
|
|||||||
\ 'source':
|
\ 'source':
|
||||||
\ map(split(globpath(&rtp, "colors/*.vim"), "\n"),
|
\ map(split(globpath(&rtp, "colors/*.vim"), "\n"),
|
||||||
\ "substitute(fnamemodify(v:val, ':t'), '\\..\\{-}$', '', '')"),
|
\ "substitute(fnamemodify(v:val, ':t'), '\\..\\{-}$', '', '')"),
|
||||||
\ 'sink': 'colo',
|
\ 'sink': 'colo',
|
||||||
\ 'options': '+m',
|
\ 'options': '+m',
|
||||||
\ 'tmux': 15
|
\ 'tmux_width': 20
|
||||||
\ })<CR>
|
\ })<CR>
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -392,13 +407,17 @@ function! g:bufopen(e)
|
|||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
nnoremap <silent> <Leader><Enter> :call fzf#run({
|
nnoremap <silent> <Leader><Enter> :call fzf#run({
|
||||||
\ 'source': g:buflist(),
|
\ 'source': reverse(g:buflist()),
|
||||||
\ 'sink': function('g:bufopen'),
|
\ 'sink': function('g:bufopen'),
|
||||||
\ 'options': '+m +s',
|
\ 'options': '+m',
|
||||||
\ 'tmux': 15
|
\ 'tmux_height': '40%'
|
||||||
\ })<CR>
|
\ })<CR>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Articles
|
||||||
|
|
||||||
|
- [fzf+vim+tmux](http://junegunn.kr/2014/04/fzf+vim+tmux)
|
||||||
|
|
||||||
Tips
|
Tips
|
||||||
----
|
----
|
||||||
|
|
||||||
@@ -431,6 +450,73 @@ This limit can be adjusted with `-s` option, or with the environment variable
|
|||||||
export FZF_DEFAULT_OPTS="--sort 20000"
|
export FZF_DEFAULT_OPTS="--sort 20000"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 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.
|
||||||
|
|
||||||
|
|
||||||
License
|
License
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
|||||||
212
fzf
212
fzf
@@ -7,7 +7,7 @@
|
|||||||
# / __/ / /_/ __/
|
# / __/ / /_/ __/
|
||||||
# /_/ /___/_/ Fuzzy finder for your shell
|
# /_/ /___/_/ Fuzzy finder for your shell
|
||||||
#
|
#
|
||||||
# Version: 0.8.2 (March 30, 2014)
|
# Version: 0.8.4 (May 17, 2014)
|
||||||
#
|
#
|
||||||
# Author: Junegunn Choi
|
# Author: Junegunn Choi
|
||||||
# URL: https://github.com/junegunn/fzf
|
# URL: https://github.com/junegunn/fzf
|
||||||
@@ -50,8 +50,8 @@ end
|
|||||||
|
|
||||||
class FZF
|
class FZF
|
||||||
C = Curses
|
C = Curses
|
||||||
attr_reader :rxflag, :sort, :nth, :color, :black, :ansi256,
|
attr_reader :rxflag, :sort, :nth, :color, :black, :ansi256, :reverse,
|
||||||
:mouse, :multi, :query, :filter, :extended
|
:mouse, :multi, :query, :select1, :exit0, :filter, :extended
|
||||||
|
|
||||||
class AtomicVar
|
class AtomicVar
|
||||||
def initialize value
|
def initialize value
|
||||||
@@ -83,9 +83,12 @@ class FZF
|
|||||||
@multi = false
|
@multi = false
|
||||||
@mouse = true
|
@mouse = true
|
||||||
@extended = nil
|
@extended = nil
|
||||||
|
@select1 = false
|
||||||
|
@exit0 = false
|
||||||
@filter = nil
|
@filter = nil
|
||||||
@nth = nil
|
@nth = nil
|
||||||
@delim = nil
|
@delim = nil
|
||||||
|
@reverse = false
|
||||||
|
|
||||||
argv =
|
argv =
|
||||||
if opts = ENV['FZF_DEFAULT_OPTS']
|
if opts = ENV['FZF_DEFAULT_OPTS']
|
||||||
@@ -96,7 +99,7 @@ class FZF
|
|||||||
end
|
end
|
||||||
while o = argv.shift
|
while o = argv.shift
|
||||||
case o
|
case o
|
||||||
when '--version' then version
|
when '--version' then FZF.version
|
||||||
when '-h', '--help' then usage 0
|
when '-h', '--help' then usage 0
|
||||||
when '-m', '--multi' then @multi = true
|
when '-m', '--multi' then @multi = true
|
||||||
when '+m', '--no-multi' then @multi = false
|
when '+m', '--no-multi' then @multi = false
|
||||||
@@ -112,7 +115,13 @@ class FZF
|
|||||||
when '--no-black' then @black = false
|
when '--no-black' then @black = false
|
||||||
when '--mouse' then @mouse = true
|
when '--mouse' then @mouse = true
|
||||||
when '--no-mouse' then @mouse = false
|
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 '+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'
|
when '-q', '--query'
|
||||||
usage 1, 'query string required' unless query = argv.shift
|
usage 1, 'query string required' unless query = argv.shift
|
||||||
@query = AtomicVar.new query.dup
|
@query = AtomicVar.new query.dup
|
||||||
@@ -126,9 +135,9 @@ class FZF
|
|||||||
when '-n', '--nth'
|
when '-n', '--nth'
|
||||||
usage 1, 'field number required' unless nth = argv.shift
|
usage 1, 'field number required' unless nth = argv.shift
|
||||||
usage 1, 'invalid field number' if nth.to_i == 0
|
usage 1, 'invalid field number' if nth.to_i == 0
|
||||||
@nth = nth.to_i
|
@nth = parse_nth nth
|
||||||
when /^-n(-?[1-9][0-9]*)$/, /^--nth=(-?[1-9][0-9]*)$/
|
when /^-n([0-9,-]+)$/, /^--nth=([0-9,-]+)$/
|
||||||
@nth = $1.to_i
|
@nth = parse_nth $1
|
||||||
when '-d', '--delimiter'
|
when '-d', '--delimiter'
|
||||||
usage 1, 'delimiter required' unless delim = argv.shift
|
usage 1, 'delimiter required' unless delim = argv.shift
|
||||||
@delim = FZF.build_delim_regex delim
|
@delim = FZF.build_delim_regex delim
|
||||||
@@ -169,6 +178,14 @@ class FZF
|
|||||||
end
|
end
|
||||||
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
|
def FZF.build_delim_regex delim
|
||||||
Regexp.compile(delim) rescue (delim = Regexp.escape(delim))
|
Regexp.compile(delim) rescue (delim = Regexp.escape(delim))
|
||||||
Regexp.compile "(?:.*?#{delim})|(?:.+?$)"
|
Regexp.compile "(?:.*?#{delim})|(?:.+?$)"
|
||||||
@@ -176,67 +193,115 @@ class FZF
|
|||||||
|
|
||||||
def start
|
def start
|
||||||
if @filter
|
if @filter
|
||||||
start_reader(false).join
|
start_reader.join
|
||||||
filter_list @new
|
filter_list @new
|
||||||
else
|
else
|
||||||
@stdout = $stdout.clone
|
start_reader
|
||||||
$stdout.reopen($stderr)
|
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
|
||||||
|
|
||||||
start_reader true
|
if loaded || len > 1
|
||||||
init_screen
|
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
|
end
|
||||||
|
|
||||||
def filter_list list
|
def filter_list list
|
||||||
matches = get_matcher.match(list, @filter, '', '')
|
matches = matcher.match(list, @filter, '', '')
|
||||||
if @sort && matches.length <= @sort
|
if @sort && matches.length <= @sort
|
||||||
matches = sort_by_rank(matches)
|
matches = FZF.sort(matches)
|
||||||
end
|
end
|
||||||
matches.each { |m| puts m.first }
|
matches.each { |m| puts m.first }
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_matcher
|
def matcher
|
||||||
if @extended
|
@matcher ||=
|
||||||
ExtendedFuzzyMatcher.new @rxflag, @extended, @nth, @delim
|
if @extended
|
||||||
else
|
ExtendedFuzzyMatcher.new @rxflag, @extended, @nth, @delim
|
||||||
FuzzyMatcher.new @rxflag, @nth, @delim
|
else
|
||||||
end
|
FuzzyMatcher.new @rxflag, @nth, @delim
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def version
|
class << self
|
||||||
File.open(__FILE__, 'r') do |f|
|
def version
|
||||||
f.each_line do |line|
|
File.open(__FILE__, 'r') do |f|
|
||||||
if line =~ /Version: (.*)/
|
f.each_line do |line|
|
||||||
$stdout.puts "fzf " << $1
|
if line =~ /Version: (.*)/
|
||||||
exit
|
$stdout.puts 'fzf ' << $1
|
||||||
|
exit
|
||||||
|
end
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
def usage x, message = nil
|
def usage x, message = nil
|
||||||
$stderr.puts message if message
|
$stderr.puts message if message
|
||||||
$stderr.puts %[usage: fzf [options]
|
$stderr.puts %[usage: fzf [options]
|
||||||
|
|
||||||
Options
|
Search
|
||||||
-m, --multi Enable multi-select
|
|
||||||
-x, --extended Extended-search mode
|
-x, --extended Extended-search mode
|
||||||
-e, --extended-exact Extended-search mode (exact match)
|
-e, --extended-exact Extended-search mode (exact match)
|
||||||
-q, --query=STR Initial query
|
|
||||||
-f, --filter=STR Filter mode. Do not start interactive finder.
|
|
||||||
-n, --nth=[-]N Match only in the N-th token of the item
|
|
||||||
-d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)
|
|
||||||
-s, --sort=MAX Maximum number of matched items to sort (default: 1000)
|
|
||||||
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
|
||||||
-i Case-insensitive match (default: smart-case match)
|
-i Case-insensitive match (default: smart-case match)
|
||||||
+i Case-sensitive match
|
+i Case-sensitive match
|
||||||
|
-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
|
+c, --no-color Disable colors
|
||||||
+2, --no-256 Disable 256-color
|
+2, --no-256 Disable 256-color
|
||||||
--black Use black background
|
--black Use black background
|
||||||
--no-mouse Disable mouse
|
--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
|
Environment variables
|
||||||
FZF_DEFAULT_COMMAND Default command to use when input is tty
|
FZF_DEFAULT_COMMAND Default command to use when input is tty
|
||||||
@@ -367,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
|
||||||
@@ -387,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
|
||||||
@@ -477,21 +546,6 @@ class FZF
|
|||||||
C.attroff color(:chosen, true) if chosen
|
C.attroff color(:chosen, true) if chosen
|
||||||
end
|
end
|
||||||
|
|
||||||
def sort_by_rank list
|
|
||||||
list.sort_by { |tuple|
|
|
||||||
line, offsets = tuple
|
|
||||||
matchlen = 0
|
|
||||||
pe = nil
|
|
||||||
offsets.sort.each do |pair|
|
|
||||||
b, e = pair
|
|
||||||
b = pe if pe && pe > b
|
|
||||||
pe = e
|
|
||||||
matchlen += e - b
|
|
||||||
end
|
|
||||||
[matchlen, line.length, line]
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
AFTER_1_9 = RUBY_VERSION.split('.').map { |e| e.rjust(3, '0') }.join >= '001009'
|
AFTER_1_9 = RUBY_VERSION.split('.').map { |e| e.rjust(3, '0') }.join >= '001009'
|
||||||
|
|
||||||
if AFTER_1_9
|
if AFTER_1_9
|
||||||
@@ -538,6 +592,9 @@ 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.mousemask C::ALL_MOUSE_EVENTS if @mouse
|
||||||
C.start_color
|
C.start_color
|
||||||
@@ -595,7 +652,7 @@ class FZF
|
|||||||
C.refresh
|
C.refresh
|
||||||
end
|
end
|
||||||
|
|
||||||
def start_reader curses
|
def start_reader
|
||||||
stream =
|
stream =
|
||||||
if @source.tty?
|
if @source.tty?
|
||||||
if default_command = ENV['FZF_DEFAULT_COMMAND']
|
if default_command = ENV['FZF_DEFAULT_COMMAND']
|
||||||
@@ -614,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 if curses
|
@spinner.clear if @spinner
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def start_search
|
def start_search &callback
|
||||||
matcher = get_matcher
|
Thread.new do
|
||||||
searcher = Thread.new {
|
|
||||||
lists = []
|
lists = []
|
||||||
events = {}
|
events = {}
|
||||||
fcache = {}
|
fcache = {}
|
||||||
@@ -659,7 +715,7 @@ 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)
|
||||||
unless matches = fcache[q]
|
unless matches = fcache[q]
|
||||||
@@ -681,7 +737,7 @@ class FZF
|
|||||||
next if skip
|
next if skip
|
||||||
matches = @sort ? found : found.reverse
|
matches = @sort ? found : found.reverse
|
||||||
if !empty && @sort && matches.length <= @sort
|
if !empty && @sort && matches.length <= @sort
|
||||||
matches = sort_by_rank(matches)
|
matches = FZF.sort(matches)
|
||||||
end
|
end
|
||||||
fcache[q] = matches
|
fcache[q] = matches
|
||||||
end
|
end
|
||||||
@@ -690,6 +746,10 @@ class FZF
|
|||||||
@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
|
||||||
|
|
||||||
@@ -698,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
|
||||||
@@ -714,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
|
||||||
@@ -729,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
|
||||||
@@ -742,6 +802,8 @@ class FZF
|
|||||||
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
|
||||||
@@ -946,8 +1008,8 @@ class FZF
|
|||||||
},
|
},
|
||||||
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 { vselect { |v| v - 1 } },
|
ctrl(:j) => proc { vselect { |v| v - (@reverse ? -1 : 1) } },
|
||||||
ctrl(:k) => proc { vselect { |v| v + 1 } },
|
ctrl(:k) => proc { vselect { |v| v + (@reverse ? -1 : 1) } },
|
||||||
ctrl(:w) => proc {
|
ctrl(:w) => proc {
|
||||||
pcursor = cursor
|
pcursor = cursor
|
||||||
backword.call
|
backword.call
|
||||||
@@ -967,7 +1029,7 @@ class FZF
|
|||||||
when :stab then 1
|
when :stab then 1
|
||||||
when :sclick then 0
|
when :sclick then 0
|
||||||
else -1
|
else -1
|
||||||
end }
|
end * (@reverse ? -1 : 1) }
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
ctrl(:b) => proc { cursor = [0, cursor - 1].max; nil },
|
ctrl(:b) => proc { cursor = [0, cursor - 1].max; nil },
|
||||||
@@ -1021,7 +1083,6 @@ class FZF
|
|||||||
actions[127] = actions[ctrl(:h)]
|
actions[127] = actions[ctrl(:h)]
|
||||||
actions[ctrl(:q)] = actions[ctrl(:g)] = actions[ctrl(:c)] = actions[:esc]
|
actions[ctrl(:q)] = actions[ctrl(:g)] = actions[ctrl(:c)] = actions[:esc]
|
||||||
|
|
||||||
emit(:key) { [@query.get, cursor] } unless @query.empty?
|
|
||||||
while true
|
while true
|
||||||
@cursor_x.set cursor
|
@cursor_x.set cursor
|
||||||
render { print_input }
|
render { print_input }
|
||||||
@@ -1059,7 +1120,7 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
def initialize nth, delim
|
def initialize nth, delim
|
||||||
@nth = nth && (nth > 0 ? nth - 1 : nth)
|
@nth = nth && nth.map { |n| n > 0 ? n - 1 : n }
|
||||||
@delim = delim
|
@delim = delim
|
||||||
@tokens_cache = {}
|
@tokens_cache = {}
|
||||||
end
|
end
|
||||||
@@ -1080,11 +1141,14 @@ class FZF
|
|||||||
if @nth
|
if @nth
|
||||||
prefix_length, tokens = tokenize str
|
prefix_length, tokens = tokenize str
|
||||||
|
|
||||||
if (token = tokens[@nth]) && (md = token.match(pat) rescue nil)
|
@nth.each do |n|
|
||||||
prefix_length += (tokens[0...@nth] || []).join.length
|
if (token = tokens[n]) && (md = token.match(pat) rescue nil)
|
||||||
offset = md.offset(0).map { |o| o + prefix_length }
|
prefix_length += (tokens[0...n] || []).join.length
|
||||||
MatchData.new offset
|
offset = md.offset(0).map { |o| o + prefix_length }
|
||||||
|
return MatchData.new(offset)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
nil
|
||||||
else
|
else
|
||||||
str.match(pat) rescue nil
|
str.match(pat) rescue nil
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -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.8.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}
|
||||||
|
|||||||
168
install
168
install
@@ -12,10 +12,9 @@ if [ $? -ne 0 ]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# System ruby is preferred
|
# System ruby is preferred
|
||||||
curses_check="begin; require 'curses'; rescue Exception; exit 1; end"
|
|
||||||
system_ruby=/usr/bin/ruby
|
system_ruby=/usr/bin/ruby
|
||||||
if [ -x $system_ruby -a $system_ruby != "$ruby" ]; then
|
if [ -x $system_ruby -a $system_ruby != "$ruby" ]; then
|
||||||
$system_ruby --disable-gems -e "$curses_check" 2> /dev/null
|
$system_ruby --disable-gems -rcurses -e0 2> /dev/null
|
||||||
[ $? -eq 0 ] && ruby=$system_ruby
|
[ $? -eq 0 ] && ruby=$system_ruby
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -23,7 +22,7 @@ echo "OK ($ruby)"
|
|||||||
|
|
||||||
# Curses-support
|
# Curses-support
|
||||||
echo -n "Checking Curses support ... "
|
echo -n "Checking Curses support ... "
|
||||||
"$ruby" -e "$curses_check"
|
"$ruby" -rcurses -e0 2> /dev/null
|
||||||
if [ $? -eq 0 ]; then
|
if [ $? -eq 0 ]; then
|
||||||
echo "OK"
|
echo "OK"
|
||||||
else
|
else
|
||||||
@@ -45,7 +44,7 @@ echo -n "Checking Ruby version ... "
|
|||||||
"$ruby" -e 'exit RUBY_VERSION >= "1.9"'
|
"$ruby" -e 'exit RUBY_VERSION >= "1.9"'
|
||||||
if [ $? -eq 0 ]; then
|
if [ $? -eq 0 ]; then
|
||||||
echo ">= 1.9"
|
echo ">= 1.9"
|
||||||
"$ruby" --disable-gems -e "$curses_check"
|
"$ruby" --disable-gems -rcurses -e0 2> /dev/null
|
||||||
if [ $? -eq 0 ]; then
|
if [ $? -eq 0 ]; then
|
||||||
fzf_cmd="$ruby --disable-gems $fzf_base/fzf"
|
fzf_cmd="$ruby --disable-gems $fzf_base/fzf"
|
||||||
else
|
else
|
||||||
@@ -73,7 +72,7 @@ for shell in bash zsh; do
|
|||||||
echo -n "Generate ~/.fzf.$shell ... "
|
echo -n "Generate ~/.fzf.$shell ... "
|
||||||
src=~/.fzf.${shell}
|
src=~/.fzf.${shell}
|
||||||
|
|
||||||
fzf_completion="source $fzf_base/fzf-completion.${shell}"
|
fzf_completion="[[ \$- =~ i ]] && source $fzf_base/fzf-completion.${shell}"
|
||||||
if [ $auto_completion -ne 0 ]; then
|
if [ $auto_completion -ne 0 ]; then
|
||||||
fzf_completion="# $fzf_completion"
|
fzf_completion="# $fzf_completion"
|
||||||
fi
|
fi
|
||||||
@@ -98,9 +97,7 @@ EOF
|
|||||||
cat >> $src << "EOFZF"
|
cat >> $src << "EOFZF"
|
||||||
# Key bindings
|
# Key bindings
|
||||||
# ------------
|
# ------------
|
||||||
if [[ $- =~ i ]]; then
|
__fsel() {
|
||||||
|
|
||||||
read -r -d '' __fsel <<'EOF'
|
|
||||||
find * -path '*/\.*' -prune \
|
find * -path '*/\.*' -prune \
|
||||||
-o -type f -print \
|
-o -type f -print \
|
||||||
-o -type d -print \
|
-o -type d -print \
|
||||||
@@ -108,21 +105,19 @@ read -r -d '' __fsel <<'EOF'
|
|||||||
printf '%q ' "$item"
|
printf '%q ' "$item"
|
||||||
done
|
done
|
||||||
echo
|
echo
|
||||||
EOF
|
|
||||||
|
|
||||||
__fsel() {
|
|
||||||
eval "$__fsel"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if [[ $- =~ i ]]; then
|
||||||
|
|
||||||
__fsel_tmux() {
|
__fsel_tmux() {
|
||||||
local height lines
|
local height
|
||||||
height=${FZF_TMUX_HEIGHT:-40%}
|
height=${FZF_TMUX_HEIGHT:-40%}
|
||||||
lines=${LINES:-40}
|
|
||||||
if [[ $height =~ %$ ]]; then
|
if [[ $height =~ %$ ]]; then
|
||||||
height=${height:0:${#height}-1}
|
height="-p ${height%\%}"
|
||||||
height=$(( height * lines / 100 ))
|
else
|
||||||
|
height="-l $height"
|
||||||
fi
|
fi
|
||||||
tmux split-window -l $height "tmux send-keys -t $TMUX_PANE \"\$($__fsel)\""
|
tmux split-window $height "bash -c 'source ~/.fzf.bash; tmux send-keys -t $TMUX_PANE \"\$(__fsel)\"'"
|
||||||
}
|
}
|
||||||
|
|
||||||
__fcd() {
|
__fcd() {
|
||||||
@@ -141,7 +136,7 @@ if [ -z "$(set -o | grep '^vi.*on')" ]; then
|
|||||||
if [ $__use_tmux -eq 1 ]; then
|
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"'
|
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
|
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"'
|
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
|
fi
|
||||||
|
|
||||||
# CTRL-R - Paste the selected command from history into the command line
|
# CTRL-R - Paste the selected command from history into the command line
|
||||||
@@ -158,7 +153,7 @@ else
|
|||||||
if [ $__use_tmux -eq 1 ]; then
|
if [ $__use_tmux -eq 1 ]; then
|
||||||
bind '"\C-t": "\e$a \eddi$(__fsel_tmux)\C-x\C-e\e0P$xa"'
|
bind '"\C-t": "\e$a \eddi$(__fsel_tmux)\C-x\C-e\e0P$xa"'
|
||||||
else
|
else
|
||||||
bind '"\C-t": "\e$a \eddi$(__fsel)\C-x\C-e\e0Px$a \C-x\C-r"'
|
bind '"\C-t": "\e$a \eddi$(__fsel)\C-x\C-e\e0Px$a \C-x\C-r\exa "'
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# CTRL-R - Paste the selected command from history into the command line
|
# CTRL-R - Paste the selected command from history into the command line
|
||||||
@@ -177,7 +172,8 @@ EOFZF
|
|||||||
# Key bindings
|
# Key bindings
|
||||||
# ------------
|
# ------------
|
||||||
# CTRL-T - Paste the selected file path(s) into the command line
|
# CTRL-T - Paste the selected file path(s) into the command line
|
||||||
read -r -d '' __fsel <<'EOF'
|
__fsel() {
|
||||||
|
set -o nonomatch
|
||||||
find * -path '*/\.*' -prune \
|
find * -path '*/\.*' -prune \
|
||||||
-o -type f -print \
|
-o -type f -print \
|
||||||
-o -type d -print \
|
-o -type d -print \
|
||||||
@@ -185,22 +181,24 @@ read -r -d '' __fsel <<'EOF'
|
|||||||
printf '%q ' "$item"
|
printf '%q ' "$item"
|
||||||
done
|
done
|
||||||
echo
|
echo
|
||||||
EOF
|
}
|
||||||
|
|
||||||
|
if [[ $- =~ i ]]; then
|
||||||
|
|
||||||
if [ -n "$TMUX_PANE" -a ${FZF_TMUX:-1} -ne 0 -a ${LINES:-40} -gt 15 ]; then
|
if [ -n "$TMUX_PANE" -a ${FZF_TMUX:-1} -ne 0 -a ${LINES:-40} -gt 15 ]; then
|
||||||
fzf-file-widget() {
|
fzf-file-widget() {
|
||||||
local height lines
|
local height
|
||||||
height=${FZF_TMUX_HEIGHT:-40%}
|
height=${FZF_TMUX_HEIGHT:-40%}
|
||||||
lines=${LINES:-40}
|
|
||||||
if [[ $height =~ %$ ]]; then
|
if [[ $height =~ %$ ]]; then
|
||||||
height=${height:0:${#height}-1}
|
height="-p ${height%\%}"
|
||||||
height=$(( height * lines / 100 ))
|
else
|
||||||
|
height="-l $height"
|
||||||
fi
|
fi
|
||||||
tmux split-window -l $height "tmux send-keys -t $TMUX_PANE \"\$($__fsel)\""
|
tmux split-window $height "zsh -c 'source ~/.fzf.zsh; tmux send-keys -t $TMUX_PANE \"\$(__fsel)\"'"
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
fzf-file-widget() {
|
fzf-file-widget() {
|
||||||
LBUFFER="${LBUFFER%% #}$(eval "$__fsel")"
|
LBUFFER="${LBUFFER%% #}$(__fsel)"
|
||||||
zle redisplay
|
zle redisplay
|
||||||
}
|
}
|
||||||
fi
|
fi
|
||||||
@@ -209,7 +207,7 @@ bindkey '^T' fzf-file-widget
|
|||||||
|
|
||||||
# ALT-C - cd into the selected directory
|
# ALT-C - cd into the selected directory
|
||||||
fzf-cd-widget() {
|
fzf-cd-widget() {
|
||||||
cd "${$(find * -path '*/\.*' -prune \
|
cd "${$(set -o nonomatch; find * -path '*/\.*' -prune \
|
||||||
-o -type d -print 2> /dev/null | fzf):-.}"
|
-o -type d -print 2> /dev/null | fzf):-.}"
|
||||||
zle reset-prompt
|
zle reset-prompt
|
||||||
}
|
}
|
||||||
@@ -224,6 +222,7 @@ fzf-history-widget() {
|
|||||||
zle -N fzf-history-widget
|
zle -N fzf-history-widget
|
||||||
bindkey '^R' fzf-history-widget
|
bindkey '^R' fzf-history-widget
|
||||||
|
|
||||||
|
fi
|
||||||
EOFZF
|
EOFZF
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
@@ -231,29 +230,122 @@ EOFZF
|
|||||||
echo "OK"
|
echo "OK"
|
||||||
done
|
done
|
||||||
|
|
||||||
echo
|
# fish
|
||||||
for shell in bash zsh; do
|
has_fish=0
|
||||||
rc=~/.${shell}rc
|
if [ -n "$(which fish)" ]; then
|
||||||
src="source ~/.fzf.${shell}"
|
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"
|
||||||
|
|
||||||
echo "Update $rc:"
|
if [ $key_bindings -eq 0 ]; then
|
||||||
echo " - $src"
|
echo -n "Generate ~/.config/fish/functions/fzf_key_bindings.fish ... "
|
||||||
line=$(grep -nF "$src" $rc | sed 's/:.*//')
|
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
|
if [ -n "$line" ]; then
|
||||||
echo " - Already exists (line #$line)"
|
echo " - Already exists (line #$line)"
|
||||||
else
|
else
|
||||||
echo $src >> $rc
|
echo "$1" >> "$2"
|
||||||
echo " - Added"
|
echo " - Added"
|
||||||
fi
|
fi
|
||||||
echo
|
echo
|
||||||
|
}
|
||||||
|
|
||||||
|
echo
|
||||||
|
for shell in bash zsh; do
|
||||||
|
append_line "source ~/.fzf.${shell}" ~/.${shell}rc
|
||||||
done
|
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
|
cat << EOF
|
||||||
Finished. Reload your .bashrc or .zshrc.
|
Finished. Restart your shell or reload config file.
|
||||||
source ~/.bashrc # bash
|
source ~/.bashrc # bash
|
||||||
source ~/.zshrc # zsh
|
source ~/.zshrc # zsh
|
||||||
|
EOF
|
||||||
|
[ $has_fish -eq 1 ] && echo " fzf_key_bindings # fish"; cat << EOF
|
||||||
|
|
||||||
To uninstall fzf, simply remove the added line.
|
To uninstall fzf, simply remove the added lines.
|
||||||
|
|
||||||
For more information, see: https://github.com/junegunn/fzf
|
For more information, see: https://github.com/junegunn/fzf
|
||||||
EOF
|
EOF
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
" 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:min_tmux_width = 10
|
||||||
let s:min_tmux_height = 3
|
let s:min_tmux_height = 3
|
||||||
let s:default_tmux_height = '40%'
|
let s:default_tmux_height = '40%'
|
||||||
|
|
||||||
@@ -40,6 +41,19 @@ else
|
|||||||
let s:exec = 'fzf'
|
let s:exec = 'fzf'
|
||||||
endif
|
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)
|
function! s:shellesc(arg)
|
||||||
return '"'.substitute(a:arg, '"', '\\"', 'g').'"'
|
return '"'.substitute(a:arg, '"', '\\"', 'g').'"'
|
||||||
endfunction
|
endfunction
|
||||||
@@ -75,14 +89,19 @@ function! fzf#run(...) abort
|
|||||||
endif
|
endif
|
||||||
let command = prefix.s:exec.' '.optstr.' > '.temps.result
|
let command = prefix.s:exec.' '.optstr.' > '.temps.result
|
||||||
|
|
||||||
if exists('$TMUX') && has_key(dict, 'tmux') &&
|
if s:tmux_enabled() && s:tmux_splittable(dict)
|
||||||
\ dict.tmux > 0 && winheight(0) >= s:min_tmux_height
|
|
||||||
return s:execute_tmux(dict, command, temps)
|
return s:execute_tmux(dict, command, temps)
|
||||||
else
|
else
|
||||||
return s:execute(dict, command, temps)
|
return s:execute(dict, command, temps)
|
||||||
endif
|
endif
|
||||||
endfunction
|
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)
|
function! s:pushd(dict)
|
||||||
if has_key(a:dict, 'dir')
|
if has_key(a:dict, 'dir')
|
||||||
let a:dict.prev_dir = getcwd()
|
let a:dict.prev_dir = getcwd()
|
||||||
@@ -115,21 +134,25 @@ function! s:execute_tmux(dict, command, temps)
|
|||||||
let command = a:command
|
let command = a:command
|
||||||
endif
|
endif
|
||||||
|
|
||||||
if type(a:dict.tmux) == 1
|
let splitopt = '-v'
|
||||||
if a:dict.tmux =~ '%$'
|
if has_key(a:dict, 'tmux_width')
|
||||||
let height = screenrow() * str2nr(a:dict.tmux[0:-2]) / 100
|
let splitopt = '-h'
|
||||||
else
|
let size = a:dict.tmux_width
|
||||||
let height = str2nr(a:dict.tmux)
|
|
||||||
endif
|
|
||||||
else
|
else
|
||||||
let height = a:dict.tmux
|
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
|
endif
|
||||||
|
|
||||||
let s:pane = substitute(
|
let s:pane = substitute(
|
||||||
\ system(
|
\ system(
|
||||||
\ printf(
|
\ printf(
|
||||||
\ 'tmux split-window -l %d -P -F "#{pane_id}" %s',
|
\ 'tmux split-window %s %s -P -F "#{pane_id}" %s',
|
||||||
\ height, s:shellesc(command))), '\n', '', 'g')
|
\ splitopt, sizeopt, s:shellesc(command))), '\n', '', 'g')
|
||||||
let s:dict = a:dict
|
let s:dict = a:dict
|
||||||
let s:temps = a:temps
|
let s:temps = a:temps
|
||||||
|
|
||||||
|
|||||||
204
test/test_fzf.rb
204
test/test_fzf.rb
@@ -1,6 +1,9 @@
|
|||||||
#!/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'
|
||||||
@@ -20,6 +23,16 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal true, fzf.color
|
assert_equal true, fzf.color
|
||||||
assert_equal nil, fzf.rxflag
|
assert_equal nil, fzf.rxflag
|
||||||
assert_equal true, fzf.mouse
|
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
|
end
|
||||||
|
|
||||||
def test_environment_variables
|
def test_environment_variables
|
||||||
@@ -30,7 +43,8 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal nil, fzf.nth
|
assert_equal nil, fzf.nth
|
||||||
|
|
||||||
ENV['FZF_DEFAULT_OPTS'] =
|
ENV['FZF_DEFAULT_OPTS'] =
|
||||||
'-x -m -s 10000 -q " hello world " +c +2 --no-mouse -f "goodbye world" --black --nth=3'
|
'-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 []
|
fzf = FZF.new []
|
||||||
assert_equal 10000, fzf.sort
|
assert_equal 10000, fzf.sort
|
||||||
assert_equal ' hello world ',
|
assert_equal ' hello world ',
|
||||||
@@ -43,13 +57,17 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal false, fzf.ansi256
|
assert_equal false, fzf.ansi256
|
||||||
assert_equal true, fzf.black
|
assert_equal true, fzf.black
|
||||||
assert_equal false, fzf.mouse
|
assert_equal false, fzf.mouse
|
||||||
assert_equal 3, fzf.nth
|
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 --query hello
|
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello --select-1
|
||||||
--filter=howdy --extended-exact --no-mouse --no-256 --nth=1]
|
--exit-0 --filter=howdy --extended-exact
|
||||||
|
--no-mouse --no-256 --nth=1 --reverse]
|
||||||
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
|
||||||
@@ -58,13 +76,19 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal false, fzf.mouse
|
assert_equal false, fzf.mouse
|
||||||
assert_equal 0, fzf.rxflag
|
assert_equal 0, fzf.rxflag
|
||||||
assert_equal 'hello', fzf.query.get
|
assert_equal 'hello', fzf.query.get
|
||||||
|
assert_equal true, fzf.select1
|
||||||
|
assert_equal true, fzf.exit0
|
||||||
assert_equal 'howdy', fzf.filter
|
assert_equal 'howdy', fzf.filter
|
||||||
assert_equal :exact, fzf.extended
|
assert_equal :exact, fzf.extended
|
||||||
assert_equal 1, fzf.nth
|
assert_equal [1], fzf.nth
|
||||||
|
assert_equal true, fzf.reverse
|
||||||
|
|
||||||
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello
|
# Long opts (left-to-right)
|
||||||
--filter a --filter b --no-256 --black --nth 2
|
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query=hello
|
||||||
--no-sort -i --color --no-multi --256]
|
--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 nil, fzf.sort
|
||||||
assert_equal false, fzf.multi
|
assert_equal false, fzf.multi
|
||||||
assert_equal true, fzf.color
|
assert_equal true, fzf.color
|
||||||
@@ -74,11 +98,14 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal 1, fzf.rxflag
|
assert_equal 1, fzf.rxflag
|
||||||
assert_equal 'b', fzf.filter
|
assert_equal 'b', fzf.filter
|
||||||
assert_equal 'hello', fzf.query.get
|
assert_equal 'hello', fzf.query.get
|
||||||
|
assert_equal false, fzf.select1
|
||||||
|
assert_equal false, fzf.exit0
|
||||||
assert_equal nil, fzf.extended
|
assert_equal nil, fzf.extended
|
||||||
assert_equal 2, fzf.nth
|
assert_equal [-2], fzf.nth
|
||||||
|
assert_equal false, fzf.reverse
|
||||||
|
|
||||||
# Short opts
|
# Short opts
|
||||||
fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x -fhowdy +2 -n3]
|
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
|
||||||
@@ -87,11 +114,15 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal 'hello', fzf.query.get
|
assert_equal 'hello', fzf.query.get
|
||||||
assert_equal 'howdy', fzf.filter
|
assert_equal 'howdy', fzf.filter
|
||||||
assert_equal :fuzzy, fzf.extended
|
assert_equal :fuzzy, fzf.extended
|
||||||
assert_equal 3, fzf.nth
|
assert_equal [3], fzf.nth
|
||||||
|
assert_equal true, fzf.select1
|
||||||
|
assert_equal true, fzf.exit0
|
||||||
|
|
||||||
# Left-to-right
|
# Left-to-right
|
||||||
fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x -fgoodbye +2 -n3 -n4
|
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]
|
-s 3000 -c +m -i -q world +x -fworld -2 --black --no-black
|
||||||
|
-1 -0 +1 +0
|
||||||
|
]
|
||||||
assert_equal 3000, fzf.sort
|
assert_equal 3000, fzf.sort
|
||||||
assert_equal false, fzf.multi
|
assert_equal false, fzf.multi
|
||||||
assert_equal true, fzf.color
|
assert_equal true, fzf.color
|
||||||
@@ -99,13 +130,11 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal false, fzf.black
|
assert_equal false, fzf.black
|
||||||
assert_equal 1, fzf.rxflag
|
assert_equal 1, fzf.rxflag
|
||||||
assert_equal 'world', fzf.query.get
|
assert_equal 'world', fzf.query.get
|
||||||
|
assert_equal false, fzf.select1
|
||||||
|
assert_equal false, fzf.exit0
|
||||||
assert_equal 'world', fzf.filter
|
assert_equal 'world', fzf.filter
|
||||||
assert_equal nil, fzf.extended
|
assert_equal nil, fzf.extended
|
||||||
assert_equal 4, fzf.nth
|
assert_equal [4, 5], fzf.nth
|
||||||
|
|
||||||
fzf = FZF.new %w[--query hello +s -s 2000 --query=world]
|
|
||||||
assert_equal 2000, fzf.sort
|
|
||||||
assert_equal 'world', fzf.query.get
|
|
||||||
rescue SystemExit => e
|
rescue SystemExit => e
|
||||||
assert false, "Exited"
|
assert false, "Exited"
|
||||||
end
|
end
|
||||||
@@ -376,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]]],
|
||||||
@@ -387,16 +416,19 @@ 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
|
end
|
||||||
|
|
||||||
def test_extended_exact_mode
|
def test_extended_exact_mode
|
||||||
@@ -502,33 +534,137 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
[list[0], [[2, 5]]],
|
[list[0], [[2, 5]]],
|
||||||
[list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')
|
[list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')
|
||||||
|
|
||||||
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, 2
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [2]
|
||||||
assert_equal [[list[1], [[8, 9]]]], matcher.match(list, 'f', '', '')
|
assert_equal [[list[1], [[8, 9]]]], matcher.match(list, 'f', '', '')
|
||||||
assert_equal [[list[0], [[8, 9]]]], matcher.match(list, 's', '', '')
|
assert_equal [[list[0], [[8, 9]]]], matcher.match(list, 's', '', '')
|
||||||
|
|
||||||
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, 3
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [3]
|
||||||
assert_equal [[list[0], [[19, 20]]]], matcher.match(list, 'r', '', '')
|
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"
|
regex = FZF.build_delim_regex "\t"
|
||||||
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, 1, regex
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1], regex
|
||||||
assert_equal [[list[0], [[3, 10]]]], matcher.match(list, 're', '', '')
|
assert_equal [[list[0], [[3, 10]]]], matcher.match(list, 're', '', '')
|
||||||
|
|
||||||
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, 2, regex
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [2], regex
|
||||||
assert_equal [], matcher.match(list, 'r', '', '')
|
assert_equal [], matcher.match(list, 'r', '', '')
|
||||||
assert_equal [[list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')
|
assert_equal [[list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')
|
||||||
|
|
||||||
# Negative indexing
|
# Negative indexing
|
||||||
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, -1, regex
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [-1], regex
|
||||||
assert_equal [[list[0], [[3, 6]]]], matcher.match(list, 'rt', '', '')
|
assert_equal [[list[0], [[3, 6]]]], matcher.match(list, 'rt', '', '')
|
||||||
assert_equal [[list[0], [[2, 5]]], [list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')
|
assert_equal [[list[0], [[2, 5]]], [list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')
|
||||||
|
|
||||||
# Regex delimiter
|
# Regex delimiter
|
||||||
regex = FZF.build_delim_regex "[ \t]+"
|
regex = FZF.build_delim_regex "[ \t]+"
|
||||||
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, 1, regex
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1], regex
|
||||||
assert_equal [list[1]], matcher.match(list, 'f', '', '').map(&:first)
|
assert_equal [list[1]], matcher.match(list, 'f', '', '').map(&:first)
|
||||||
|
|
||||||
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, 2, regex
|
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [2], regex
|
||||||
assert_equal [[list[0], [[1, 2]]], [list[1], [[8, 9]]]], matcher.match(list, 'f', '', '')
|
assert_equal [[list[0], [[1, 2]]], [list[1], [[8, 9]]]], matcher.match(list, 'f', '', '')
|
||||||
end
|
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