mirror of
https://github.com/junegunn/fzf.git
synced 2025-07-31 04:02:01 -07:00
Compare commits
48 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
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 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
||||
pkg
|
||||
Gemfile.lock
|
||||
.DS_Store
|
||||
|
178
README.md
178
README.md
@@ -5,6 +5,8 @@ fzf is a general-purpose fuzzy finder for your shell.
|
||||
|
||||

|
||||
|
||||
([tmux integration!](https://cloud.githubusercontent.com/assets/700826/2593609/3ec13962-ba83-11e3-88d3-f9f95bd8a64b.gif))
|
||||
|
||||
It was heavily inspired by [ctrlp.vim](https://github.com/kien/ctrlp.vim) and
|
||||
the likes.
|
||||
|
||||
@@ -27,9 +29,12 @@ git clone https://github.com/junegunn/fzf.git ~/.fzf
|
||||
The script will setup:
|
||||
|
||||
- `fzf` executable
|
||||
- Key bindings (`CTRL-T`, `CTRL-R`, etc.)
|
||||
- Key bindings (`CTRL-T`, `CTRL-R`, and `ALT-C`) for bash and zsh
|
||||
- Fuzzy auto-completion for bash
|
||||
|
||||
If you don't use bash or zsh, you have to manually place fzf executable in a
|
||||
directory included in `$PATH`. Key bindings are not yet supported.
|
||||
|
||||
### Install as Vim plugin
|
||||
|
||||
Once you have cloned the repository, add the following line to your .vimrc.
|
||||
@@ -47,20 +52,31 @@ Usage
|
||||
```
|
||||
usage: fzf [options]
|
||||
|
||||
Options
|
||||
-m, --multi Enable multi-select
|
||||
Search
|
||||
-x, --extended Extended-search mode
|
||||
-e, --extended-exact Extended-search mode (exact match)
|
||||
-q, --query=STR Initial query
|
||||
-f, --filter=STR Filter mode. Do not start interactive finder.
|
||||
-s, --sort=MAX Maximum number of matched items to sort (default: 1000)
|
||||
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
||||
-i Case-insensitive match (default: smart-case match)
|
||||
+i Case-sensitive match
|
||||
-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
|
||||
--no-mouse Disable mouse
|
||||
|
||||
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
|
||||
@@ -89,7 +105,7 @@ If you want to preserve the exact sequence of the input, provide `--no-sort` (or
|
||||
history | fzf +s
|
||||
```
|
||||
|
||||
### Key binding
|
||||
### Keys
|
||||
|
||||
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, CTRL-G, or ESC will terminate the finder.
|
||||
@@ -98,7 +114,7 @@ The following readline key bindings should also work as expected.
|
||||
|
||||
- CTRL-A / CTRL-E
|
||||
- CTRL-B / CTRL-F
|
||||
- CTRL-W / CTRL-U
|
||||
- CTRL-W / CTRL-U / CTRL-Y
|
||||
- ALT-B / ALT-F
|
||||
|
||||
If you enable multi-select mode with `-m` option, you can select multiple items
|
||||
@@ -131,30 +147,65 @@ Useful examples
|
||||
---------------
|
||||
|
||||
```sh
|
||||
# vimf - Open selected file in Vim
|
||||
vimf() {
|
||||
FILE=$(fzf) && vim "$FILE"
|
||||
# fe [FUZZY PATTERN] - Open the selected file with the default editor
|
||||
# - Bypass fuzzy finder if there's only one match (--select-1)
|
||||
# - 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() {
|
||||
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() {
|
||||
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() {
|
||||
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() {
|
||||
ps -ef | sed 1d | fzf -m | awk '{print $2}' | xargs kill -${1:-9}
|
||||
}
|
||||
|
||||
# fbr - checkout git branch
|
||||
fbr() {
|
||||
local branches branch
|
||||
branches=$(git branch) &&
|
||||
branch=$(echo "$branches" | fzf +s +m) &&
|
||||
git checkout $(echo "$branch" | sed "s/.* //")
|
||||
}
|
||||
|
||||
# fco - checkout git commit
|
||||
fco() {
|
||||
local commits commit
|
||||
commits=$(git log --pretty=oneline --abbrev-commit --reverse) &&
|
||||
commit=$(echo "$commits" | fzf +s +m -e) &&
|
||||
git checkout $(echo "$commit" | sed "s/ .*//")
|
||||
}
|
||||
|
||||
# ftags - search ctags
|
||||
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")"
|
||||
}
|
||||
```
|
||||
|
||||
Key bindings for command line
|
||||
@@ -162,20 +213,17 @@ Key bindings for command line
|
||||
|
||||
The install script will setup the following key bindings.
|
||||
|
||||
### bash
|
||||
|
||||
- `CTRL-T` - Paste the selected file path(s) into the command line
|
||||
- `CTRL-R` - Paste the selected command from history into the command line
|
||||
|
||||
The source code can be found in `~/.fzf.bash`.
|
||||
|
||||
### zsh
|
||||
### bash/zsh
|
||||
|
||||
- `CTRL-T` - Paste the selected file path(s) into the command line
|
||||
- `CTRL-R` - Paste the selected command from history into the command line
|
||||
- `ALT-C` - cd into the selected directory
|
||||
|
||||
The source code can be found in `~/.fzf.zsh`.
|
||||
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%`).
|
||||
|
||||
The source code can be found in `~/.fzf.bash` and in `~/.fzf.zsh`.
|
||||
|
||||
Auto-completion
|
||||
---------------
|
||||
@@ -253,7 +301,11 @@ TODO :smiley:
|
||||
Usage as Vim plugin
|
||||
-------------------
|
||||
|
||||
If you install fzf as a Vim plugin, `:FZF` command will be added.
|
||||
(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
|
||||
@@ -266,27 +318,83 @@ If you install fzf as a Vim plugin, `:FZF` command will be added.
|
||||
:FZF --no-sort -m /tmp
|
||||
```
|
||||
|
||||
You can override the source command which produces input to fzf.
|
||||
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` | number/string | Use tmux split if possible with the given height (e.g. `20`, `50%`) |
|
||||
|
||||
#### Examples
|
||||
|
||||
If `sink` option is not given, `fzf#run` will simply return the list.
|
||||
|
||||
```vim
|
||||
let g:fzf_source = 'find . -type f'
|
||||
let items = fzf#run({ 'options': '-m +c', 'dir': '~', 'source': 'ls' })
|
||||
```
|
||||
|
||||
And you can predefine default options to fzf command.
|
||||
But if `sink` is given as a string, the command will be executed for each
|
||||
selected item.
|
||||
|
||||
```vim
|
||||
let g:fzf_options = '--no-color --extended'
|
||||
" Each selected item will be opened in a new tab
|
||||
let items = fzf#run({ 'sink': 'tabe', 'options': '-m +c', 'dir': '~', 'source': 'ls' })
|
||||
```
|
||||
|
||||
For more advanced uses, you can call `fzf#run` function as follows.
|
||||
We can also use a Vim list as the source as follows:
|
||||
|
||||
```vim
|
||||
:call fzf#run('tabedit', '-m +c')
|
||||
" 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': 15
|
||||
\ })<CR>
|
||||
```
|
||||
|
||||
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.
|
||||
`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': g:buflist(),
|
||||
\ 'sink': function('g:bufopen'),
|
||||
\ 'options': '+m +s',
|
||||
\ 'tmux': 15
|
||||
\ })<CR>
|
||||
```
|
||||
|
||||
Tips
|
||||
----
|
||||
|
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
|
326
fzf
326
fzf
@@ -7,7 +7,7 @@
|
||||
# / __/ / /_/ __/
|
||||
# /_/ /___/_/ Fuzzy finder for your shell
|
||||
#
|
||||
# Version: 0.8.1 (March 9, 2014)
|
||||
# Version: 0.8.3 (April 3, 2014)
|
||||
#
|
||||
# Author: Junegunn Choi
|
||||
# URL: https://github.com/junegunn/fzf
|
||||
@@ -50,7 +50,8 @@ end
|
||||
|
||||
class FZF
|
||||
C = Curses
|
||||
attr_reader :rxflag, :sort, :color, :black, :ansi256, :mouse, :multi, :query, :filter, :extended
|
||||
attr_reader :rxflag, :sort, :nth, :color, :black, :ansi256,
|
||||
:mouse, :multi, :query, :select1, :exit0, :filter, :extended
|
||||
|
||||
class AtomicVar
|
||||
def initialize value
|
||||
@@ -82,7 +83,11 @@ class FZF
|
||||
@multi = false
|
||||
@mouse = true
|
||||
@extended = nil
|
||||
@select1 = false
|
||||
@exit0 = false
|
||||
@filter = nil
|
||||
@nth = nil
|
||||
@delim = nil
|
||||
|
||||
argv =
|
||||
if opts = ENV['FZF_DEFAULT_OPTS']
|
||||
@@ -93,7 +98,7 @@ class FZF
|
||||
end
|
||||
while o = argv.shift
|
||||
case o
|
||||
when '--version' then version
|
||||
when '--version' then FZF.version
|
||||
when '-h', '--help' then usage 0
|
||||
when '-m', '--multi' then @multi = true
|
||||
when '+m', '--no-multi' then @multi = false
|
||||
@@ -110,6 +115,10 @@ class FZF
|
||||
when '--mouse' then @mouse = true
|
||||
when '--no-mouse' then @mouse = 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
|
||||
@@ -120,6 +129,17 @@ class FZF
|
||||
@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]+$/
|
||||
@@ -155,67 +175,129 @@ class FZF
|
||||
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
|
||||
|
||||
def start
|
||||
if @filter
|
||||
start_reader(false).join
|
||||
start_reader.join
|
||||
filter_list @new
|
||||
else
|
||||
@stdout = $stdout.clone
|
||||
$stdout.reopen($stderr)
|
||||
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
|
||||
|
||||
start_reader true
|
||||
init_screen
|
||||
start_renderer
|
||||
start_search
|
||||
start_loop
|
||||
if loaded || len > 1
|
||||
start_renderer
|
||||
Thread.new { start_loop }
|
||||
end
|
||||
end
|
||||
|
||||
sleep
|
||||
else
|
||||
start_search
|
||||
start_renderer
|
||||
start_loop
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def filter_list list
|
||||
matches = get_matcher.match(list, @filter, '', '')
|
||||
matches = matcher.match(list, @filter, '', '')
|
||||
if @sort && matches.length <= @sort
|
||||
matches = sort_by_rank(matches)
|
||||
matches = FZF.sort(matches)
|
||||
end
|
||||
matches.each { |m| puts m.first }
|
||||
end
|
||||
|
||||
def get_matcher
|
||||
if @extended
|
||||
ExtendedFuzzyMatcher.new @rxflag, @extended
|
||||
else
|
||||
FuzzyMatcher.new @rxflag
|
||||
end
|
||||
def matcher
|
||||
@matcher ||=
|
||||
if @extended
|
||||
ExtendedFuzzyMatcher.new @rxflag, @extended, @nth, @delim
|
||||
else
|
||||
FuzzyMatcher.new @rxflag, @nth, @delim
|
||||
end
|
||||
end
|
||||
|
||||
def version
|
||||
File.open(__FILE__, 'r') do |f|
|
||||
f.each_line do |line|
|
||||
if line =~ /Version: (.*)/
|
||||
$stdout.puts "fzf " << $1
|
||||
exit
|
||||
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]
|
||||
|
||||
Options
|
||||
-m, --multi Enable multi-select
|
||||
Search
|
||||
-x, --extended Extended-search mode
|
||||
-e, --extended-exact Extended-search mode (exact match)
|
||||
-q, --query=STR Initial query
|
||||
-f, --filter=STR Filter mode. Do not start interactive finder.
|
||||
-s, --sort=MAX Maximum number of matched items to sort (default: 1000)
|
||||
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
||||
-i Case-insensitive match (default: smart-case match)
|
||||
+i Case-sensitive match
|
||||
-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
|
||||
--no-mouse Disable mouse
|
||||
|
||||
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
|
||||
@@ -456,21 +538,6 @@ class FZF
|
||||
C.attroff color(:chosen, true) if chosen
|
||||
end
|
||||
|
||||
def sort_by_rank list
|
||||
list.sort_by { |tuple|
|
||||
line, offsets = tuple
|
||||
matchlen = 0
|
||||
pe = nil
|
||||
offsets.sort.each do |pair|
|
||||
b, e = pair
|
||||
b = pe if pe && pe > b
|
||||
pe = e
|
||||
matchlen += e - b
|
||||
end
|
||||
[matchlen, line.length, line]
|
||||
}
|
||||
end
|
||||
|
||||
AFTER_1_9 = RUBY_VERSION.split('.').map { |e| e.rjust(3, '0') }.join >= '001009'
|
||||
|
||||
if AFTER_1_9
|
||||
@@ -517,6 +584,9 @@ class FZF
|
||||
end
|
||||
|
||||
def init_screen
|
||||
@stdout = $stdout.clone
|
||||
$stdout.reopen($stderr)
|
||||
|
||||
C.init_screen
|
||||
C.mousemask C::ALL_MOUSE_EVENTS if @mouse
|
||||
C.start_color
|
||||
@@ -574,7 +644,7 @@ class FZF
|
||||
C.refresh
|
||||
end
|
||||
|
||||
def start_reader curses
|
||||
def start_reader
|
||||
stream =
|
||||
if @source.tty?
|
||||
if default_command = ENV['FZF_DEFAULT_COMMAND']
|
||||
@@ -593,13 +663,12 @@ class FZF
|
||||
emit(:new) { @new << line.chomp }
|
||||
end
|
||||
emit(:loaded) { true }
|
||||
@spinner.clear if curses
|
||||
@spinner.clear if @spinner
|
||||
end
|
||||
end
|
||||
|
||||
def start_search
|
||||
matcher = get_matcher
|
||||
searcher = Thread.new {
|
||||
def start_search &callback
|
||||
Thread.new do
|
||||
lists = []
|
||||
events = {}
|
||||
fcache = {}
|
||||
@@ -638,7 +707,7 @@ class FZF
|
||||
progress = 0
|
||||
started_at = Time.now
|
||||
|
||||
if new_search && !lists.empty?
|
||||
if updated = new_search && !lists.empty?
|
||||
q, cx = events.delete(:key) || [q, 0]
|
||||
empty = matcher.empty?(q)
|
||||
unless matches = fcache[q]
|
||||
@@ -660,7 +729,7 @@ class FZF
|
||||
next if skip
|
||||
matches = @sort ? found : found.reverse
|
||||
if !empty && @sort && matches.length <= @sort
|
||||
matches = sort_by_rank(matches)
|
||||
matches = FZF.sort(matches)
|
||||
end
|
||||
fcache[q] = matches
|
||||
end
|
||||
@@ -669,6 +738,10 @@ class FZF
|
||||
@matches.set matches
|
||||
end#new_search
|
||||
|
||||
callback = nil if callback &&
|
||||
(updated || events[:loaded]) &&
|
||||
callback.call(events[:loaded], matches)
|
||||
|
||||
# This small delay reduces the number of partial lists
|
||||
sleep((delay = [20, delay + 5].min) * 0.01) unless user_input
|
||||
|
||||
@@ -677,7 +750,7 @@ class FZF
|
||||
rescue Exception => e
|
||||
@main.raise e
|
||||
end
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
def pick
|
||||
@@ -721,6 +794,8 @@ class FZF
|
||||
end
|
||||
|
||||
def start_renderer
|
||||
init_screen
|
||||
|
||||
Thread.new do
|
||||
begin
|
||||
while blk = @queue.shift
|
||||
@@ -904,8 +979,10 @@ class FZF
|
||||
def start_loop
|
||||
got = nil
|
||||
begin
|
||||
input = @query.get.dup
|
||||
cursor = input.length
|
||||
input = @query.get.dup
|
||||
cursor = input.length
|
||||
yanked = ''
|
||||
mouse_event = MouseEvent.new
|
||||
backword = proc {
|
||||
cursor = (input[0, cursor].rindex(/\s\S/) || -1) + 1
|
||||
}
|
||||
@@ -916,7 +993,11 @@ class FZF
|
||||
got = pick
|
||||
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(:e) => proc { cursor = input.length; nil },
|
||||
ctrl(:j) => proc { vselect { |v| v - 1 } },
|
||||
@@ -924,8 +1005,10 @@ class FZF
|
||||
ctrl(:w) => proc {
|
||||
pcursor = cursor
|
||||
backword.call
|
||||
yanked = input[cursor...pcursor] if pcursor > cursor
|
||||
input = input[0...cursor] + input[pcursor..-1]
|
||||
},
|
||||
ctrl(:y) => proc { actions[:default].call yanked },
|
||||
ctrl(:h) => proc { input[cursor -= 1] = '' if cursor > 0 },
|
||||
ctrl(:i) => proc { |o|
|
||||
if @multi && sel = pick
|
||||
@@ -952,6 +1035,39 @@ class FZF
|
||||
cursor += (input[cursor..-1].index(/(\S\s)|(.$)/) || -1) + 1
|
||||
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(:p)] = actions[ctrl(:k)]
|
||||
actions[ctrl(:n)] = actions[ctrl(:j)]
|
||||
@@ -959,46 +1075,12 @@ class FZF
|
||||
actions[127] = actions[ctrl(:h)]
|
||||
actions[ctrl(:q)] = actions[ctrl(:g)] = actions[ctrl(:c)] = actions[:esc]
|
||||
|
||||
emit(:key) { [@query.get, cursor] } unless @query.empty?
|
||||
mouse = MouseEvent.new
|
||||
while true
|
||||
@cursor_x.set cursor
|
||||
render { print_input }
|
||||
|
||||
if key = get_input(actions)
|
||||
upd = actions.fetch(key, 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.v = tv
|
||||
when :release
|
||||
if !shift && mouse.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
|
||||
}).call(key)
|
||||
upd = actions.fetch(key, actions[:default]).call(key)
|
||||
|
||||
# Dispatch key event
|
||||
emit(:key) { [@query.set(input.dup), cursor] } if upd
|
||||
@@ -1018,10 +1100,58 @@ class FZF
|
||||
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
|
||||
attr_reader :caches, :rxflag
|
||||
|
||||
def initialize rxflag
|
||||
def initialize rxflag, nth = nil, delim = nil
|
||||
super nth, delim
|
||||
@caches = Hash.new { |h, k| h[k] = {} }
|
||||
@regexp = {}
|
||||
@rxflag = rxflag
|
||||
@@ -1065,15 +1195,15 @@ class FZF
|
||||
cache[q] ||= (partial_cache ?
|
||||
partial_cache.map { |e| e.first } : list).map { |line|
|
||||
# Ignore errors: e.g. invalid byte sequence in UTF-8
|
||||
md = line.match(regexp) rescue nil
|
||||
md = do_match(line, regexp)
|
||||
md && [line, [md.offset(0)]]
|
||||
}.compact
|
||||
end
|
||||
end
|
||||
|
||||
class ExtendedFuzzyMatcher < FuzzyMatcher
|
||||
def initialize rxflag, mode = :fuzzy
|
||||
super rxflag
|
||||
def initialize rxflag, mode = :fuzzy, nth = nil, delim = nil
|
||||
super rxflag, nth, delim
|
||||
@regexps = {}
|
||||
@mode = mode
|
||||
end
|
||||
@@ -1135,7 +1265,7 @@ class FZF
|
||||
offsets = []
|
||||
regexps.all? { |pair|
|
||||
regexp, invert = pair
|
||||
md = line.match(regexp) rescue nil
|
||||
md = do_match(line, regexp)
|
||||
if md && !invert
|
||||
offsets << md.offset(0)
|
||||
elsif !md && invert
|
||||
|
@@ -1,7 +1,7 @@
|
||||
# coding: utf-8
|
||||
Gem::Specification.new do |spec|
|
||||
spec.name = 'fzf'
|
||||
spec.version = '0.8.1'
|
||||
spec.version = '0.8.3'
|
||||
spec.authors = ['Junegunn Choi']
|
||||
spec.email = ['junegunn.c@gmail.com']
|
||||
spec.description = %q{Fuzzy finder for your shell}
|
||||
@@ -13,5 +13,5 @@ Gem::Specification.new do |spec|
|
||||
spec.files = %w[fzf.gemspec]
|
||||
spec.executables = 'fzf'
|
||||
|
||||
spec.add_runtime_dependency 'curses', '~> 1.0.0'
|
||||
spec.extensions += ['ext/mkrf_conf.rb']
|
||||
end
|
||||
|
108
install
108
install
@@ -29,10 +29,13 @@ if [ $? -eq 0 ]; then
|
||||
else
|
||||
echo "Not found"
|
||||
echo "Installing 'curses' gem ... "
|
||||
/usr/bin/env gem install curses -v 1.0.0
|
||||
/usr/bin/env gem install curses -v 1.0.0 --user-install
|
||||
if [ $? -ne 0 ]; then
|
||||
echo
|
||||
echo "Failed to install 'curses' gem."
|
||||
echo "Try installing it as root: sudo gem install curses"
|
||||
if [[ $(uname -r) =~ 'ARCH' ]]; then
|
||||
echo "Make sure that base-devel package group is installed."
|
||||
fi
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
@@ -86,17 +89,15 @@ export -f fzf > /dev/null
|
||||
|
||||
# Auto-completion
|
||||
# ---------------
|
||||
$fzf_completion
|
||||
[[ \$- =~ i ]] && $fzf_completion
|
||||
|
||||
EOF
|
||||
|
||||
if [ $key_bindings -eq 0 ]; then
|
||||
if [ $shell = bash ]; then
|
||||
cat >> $src << "EOF"
|
||||
cat >> $src << "EOFZF"
|
||||
# Key bindings
|
||||
# ------------
|
||||
if [[ $- =~ i ]]; then
|
||||
|
||||
__fsel() {
|
||||
find * -path '*/\.*' -prune \
|
||||
-o -type f -print \
|
||||
@@ -107,54 +108,107 @@ __fsel() {
|
||||
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
|
||||
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"'
|
||||
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"'
|
||||
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
|
||||
bind '"\C-t": "\eddi$(__fsel)\C-x\C-e\e0P$a \C-x\C-r"'
|
||||
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"'
|
||||
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
|
||||
EOF
|
||||
EOFZF
|
||||
else
|
||||
cat >> $src << "EOF"
|
||||
cat >> $src << "EOFZF"
|
||||
# Key bindings
|
||||
# ------------
|
||||
# CTRL-T - Paste the selected file path(s) into the command line
|
||||
fzf-file-widget() {
|
||||
local FILES
|
||||
local IFS="
|
||||
"
|
||||
FILES=($(
|
||||
find * -path '*/\.*' -prune \
|
||||
__fsel() {
|
||||
set -o nonomatch
|
||||
find * -path '*/\.*' -prune \
|
||||
-o -type f -print \
|
||||
-o -type d -print \
|
||||
-o -type l -print 2> /dev/null | fzf -m))
|
||||
unset IFS
|
||||
FILES=$FILES:q
|
||||
LBUFFER="${LBUFFER%% #} $FILES"
|
||||
zle redisplay
|
||||
-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 "${$(find * -path '*/\.*' -prune \
|
||||
cd "${$(set -o nonomatch; find * -path '*/\.*' -prune \
|
||||
-o -type d -print 2> /dev/null | fzf):-.}"
|
||||
zle reset-prompt
|
||||
}
|
||||
@@ -169,7 +223,8 @@ fzf-history-widget() {
|
||||
zle -N fzf-history-widget
|
||||
bindkey '^R' fzf-history-widget
|
||||
|
||||
EOF
|
||||
fi
|
||||
EOFZF
|
||||
fi
|
||||
fi
|
||||
|
||||
@@ -183,8 +238,9 @@ for shell in bash zsh; do
|
||||
|
||||
echo "Update $rc:"
|
||||
echo " - $src"
|
||||
if [ $(grep -F "$src" $rc | wc -l) -gt 0 ]; then
|
||||
echo " - Not added (already being sourced)"
|
||||
line=$(grep -nF "$src" $rc | sed 's/:.*//')
|
||||
if [ -n "$line" ]; then
|
||||
echo " - Already exists (line #$line)"
|
||||
else
|
||||
echo $src >> $rc
|
||||
echo " - Added"
|
||||
@@ -198,5 +254,7 @@ Finished. Reload your .bashrc or .zshrc.
|
||||
source ~/.zshrc # zsh
|
||||
|
||||
To uninstall fzf, simply remove the added line.
|
||||
|
||||
For more information, see: https://github.com/junegunn/fzf
|
||||
EOF
|
||||
|
||||
|
196
plugin/fzf.vim
196
plugin/fzf.vim
@@ -1,4 +1,4 @@
|
||||
" Copyright (c) 2013 Junegunn Choi
|
||||
" Copyright (c) 2014 Junegunn Choi
|
||||
"
|
||||
" MIT License
|
||||
"
|
||||
@@ -21,39 +21,187 @@
|
||||
" OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
" WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
let s:exec = expand('<sfile>:h:h').'/fzf'
|
||||
let s:min_tmux_height = 3
|
||||
let s:default_tmux_height = '40%'
|
||||
|
||||
let s:cpo_save = &cpo
|
||||
set cpo&vim
|
||||
|
||||
call system('type fzf')
|
||||
if v:shell_error
|
||||
let s:fzf_rb = expand('<sfile>:h:h').'/fzf'
|
||||
if executable(s:fzf_rb)
|
||||
let s:exec = s:fzf_rb
|
||||
else
|
||||
echoerr 'fzf executable not found'
|
||||
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(command, ...)
|
||||
let cwd = getcwd()
|
||||
try
|
||||
let args = copy(a:000)
|
||||
if len(args) > 0 && isdirectory(expand(args[-1]))
|
||||
let dir = remove(args, -1)
|
||||
execute 'chdir '.s:escape(dir)
|
||||
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
|
||||
let argstr = join(args)
|
||||
let tf = tempname()
|
||||
let prefix = exists('g:fzf_source') ? g:fzf_source.'|' : ''
|
||||
let fzf = executable(s:exec) ? s:exec : 'fzf'
|
||||
let options = empty(argstr) ? get(g:, 'fzf_options', '') : argstr
|
||||
execute "silent !".prefix.fzf.' '.options." > ".tf
|
||||
if !v:shell_error
|
||||
for line in readfile(tf)
|
||||
if !empty(line)
|
||||
execute a:command.' '.s:escape(line)
|
||||
else
|
||||
let prefix = ''
|
||||
endif
|
||||
let command = prefix.s:exec.' '.optstr.' > '.temps.result
|
||||
|
||||
if s:tmux_enabled() && has_key(dict, 'tmux') &&
|
||||
\ dict.tmux > 0 && winheight(0) >= s:min_tmux_height
|
||||
return s:execute_tmux(dict, command, temps)
|
||||
else
|
||||
return s:execute(dict, command, temps)
|
||||
endif
|
||||
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
|
||||
|
||||
if type(a:dict.tmux) == 1 && a:dict.tmux =~ '%$'
|
||||
let height = '-p '.a:dict.tmux[0:-2]
|
||||
else
|
||||
let height = '-l '.a:dict.tmux
|
||||
endif
|
||||
|
||||
let s:pane = substitute(
|
||||
\ system(
|
||||
\ printf(
|
||||
\ 'tmux split-window %s -P -F "#{pane_id}" %s',
|
||||
\ height, 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
|
||||
endfor
|
||||
endif
|
||||
finally
|
||||
execute 'chdir '.s:escape(cwd)
|
||||
redraw!
|
||||
endif
|
||||
|
||||
for tf in values(a:temps)
|
||||
silent! call delete(tf)
|
||||
endtry
|
||||
endfor
|
||||
|
||||
call s:popd(a:dict)
|
||||
|
||||
return lines
|
||||
endfunction
|
||||
|
||||
command! -nargs=* -complete=dir FZF call fzf#run('silent e', <f-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
|
227
test/test_fzf.rb
227
test/test_fzf.rb
@@ -1,6 +1,9 @@
|
||||
#!/usr/bin/env ruby
|
||||
# encoding: utf-8
|
||||
|
||||
require 'curses'
|
||||
require 'timeout'
|
||||
require 'stringio'
|
||||
require 'minitest/autorun'
|
||||
$LOAD_PATH.unshift File.expand_path('../..', __FILE__)
|
||||
ENV['FZF_EXECUTABLE'] = '0'
|
||||
@@ -20,6 +23,15 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
assert_equal true, fzf.color
|
||||
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
|
||||
end
|
||||
|
||||
def test_environment_variables
|
||||
@@ -27,8 +39,11 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
ENV['FZF_DEFAULT_SORT'] = '20000'
|
||||
fzf = FZF.new []
|
||||
assert_equal 20000, fzf.sort
|
||||
assert_equal nil, fzf.nth
|
||||
|
||||
ENV['FZF_DEFAULT_OPTS'] = '-x -m -s 10000 -q " hello world " +c +2 --no-mouse -f "goodbye world" --black'
|
||||
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'
|
||||
fzf = FZF.new []
|
||||
assert_equal 10000, fzf.sort
|
||||
assert_equal ' hello world ',
|
||||
@@ -41,12 +56,16 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
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 [3, -1, 2], fzf.nth
|
||||
end
|
||||
|
||||
def test_option_parser
|
||||
# Long opts
|
||||
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello
|
||||
--filter=howdy --extended-exact --no-mouse --no-256]
|
||||
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello --select-1
|
||||
--exit-0 --filter=howdy --extended-exact
|
||||
--no-mouse --no-256 --nth=1]
|
||||
assert_equal 2000, fzf.sort
|
||||
assert_equal true, fzf.multi
|
||||
assert_equal false, fzf.color
|
||||
@@ -55,11 +74,16 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
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
|
||||
|
||||
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello
|
||||
--filter a --filter b --no-256 --black
|
||||
# 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]
|
||||
assert_equal nil, fzf.sort
|
||||
assert_equal false, fzf.multi
|
||||
@@ -70,10 +94,13 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
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
|
||||
|
||||
# Short opts
|
||||
fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x -fhowdy +2]
|
||||
fzf = FZF.new %w[-s2000 +c -m +i -qhello -x -fhowdy +2 -n3 -1 -0]
|
||||
assert_equal 2000, fzf.sort
|
||||
assert_equal true, fzf.multi
|
||||
assert_equal false, fzf.color
|
||||
@@ -82,10 +109,15 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
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
|
||||
-s 3000 -c +m -i -q world +x -fworld -2 --black --no-black]
|
||||
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
|
||||
@@ -93,12 +125,11 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
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
|
||||
|
||||
fzf = FZF.new %w[--query hello +s -s 2000 --query=world]
|
||||
assert_equal 2000, fzf.sort
|
||||
assert_equal 'world', fzf.query.get
|
||||
assert_equal [4, 5], fzf.nth
|
||||
rescue SystemExit => e
|
||||
assert false, "Exited"
|
||||
end
|
||||
@@ -109,6 +140,12 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
fzf = FZF.new argv
|
||||
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
|
||||
|
||||
# FIXME Only on 1.9 or above
|
||||
@@ -363,7 +400,7 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
["0____1", [[0, 6]]],
|
||||
["0_____1", [[0, 7]]],
|
||||
["0______1", [[0, 8]]]],
|
||||
FZF.new([]).sort_by_rank(matcher.match(list, '01', '', '')))
|
||||
FZF.sort(matcher.match(list, '01', '', '')))
|
||||
|
||||
assert_equal(
|
||||
[["01", [[0, 1], [1, 2]]],
|
||||
@@ -374,16 +411,19 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
["____0_1", [[4, 5], [6, 7]]],
|
||||
["0______1", [[0, 1], [7, 8]]],
|
||||
["___01___", [[3, 4], [4, 5]]]],
|
||||
FZF.new([]).sort_by_rank(xmatcher.match(list, '0 1', '', '')))
|
||||
FZF.sort(xmatcher.match(list, '0 1', '', '')))
|
||||
|
||||
assert_equal(
|
||||
[["_01_", [[1, 3], [0, 4]]],
|
||||
["0____1", [[0, 6], [1, 3]]],
|
||||
["0_____1", [[0, 7], [1, 3]]],
|
||||
["0______1", [[0, 8], [1, 3]]],
|
||||
["___01___", [[3, 5], [0, 2]]],
|
||||
["____0_1", [[4, 7], [0, 2]]]],
|
||||
FZF.new([]).sort_by_rank(xmatcher.match(list, '01 __', '', '')))
|
||||
[["_01_", [[1, 3], [0, 4]], [4, 4, "_01_"]],
|
||||
["___01___", [[3, 5], [0, 2]], [4, 8, "___01___"]],
|
||||
["____0_1", [[4, 7], [0, 2]], [5, 7, "____0_1"]],
|
||||
["0____1", [[0, 6], [1, 3]], [6, 6, "0____1"]],
|
||||
["0_____1", [[0, 7], [1, 3]], [7, 7, "0_____1"]],
|
||||
["0______1", [[0, 8], [1, 3]], [8, 8, "0______1"]]],
|
||||
FZF.sort(xmatcher.match(list, '01 __', '', '')).map { |tuple|
|
||||
tuple << FZF.rank(tuple)
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
def test_extended_exact_mode
|
||||
@@ -476,5 +516,150 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
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
|
||||
|
||||
|
Reference in New Issue
Block a user