mirror of
https://github.com/junegunn/fzf.git
synced 2025-08-01 20:52:06 -07:00
Compare commits
73 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
7280e8ebc2 | ||
|
c7e86ad4f1 | ||
|
f2b2c022be | ||
|
7747daa9ec | ||
|
c2943e7681 | ||
|
d5fc03d867 | ||
|
b0eca20dc2 | ||
|
aad335475c | ||
|
c3676bf986 | ||
|
6fb4b6d097 | ||
|
6aa168833b | ||
|
0d83cae2ec | ||
|
773d9976a0 | ||
|
3723829b0a | ||
|
13cb198b5c | ||
|
79f645aa6c | ||
|
42d479d071 | ||
|
d7f50b1e41 | ||
|
39eb85596c | ||
|
bff7e9edf5 | ||
|
98ccc03a21 | ||
|
3b668ed448 | ||
|
33b28be941 | ||
|
76fe23b928 | ||
|
622c54f4a3 | ||
|
e09993f919 | ||
|
7ee6fd1f6d | ||
|
2dca6f0cb2 | ||
|
159dd7f069 | ||
|
b30f21e074 | ||
|
636c86cf6f | ||
|
5483e41b2a | ||
|
1c89994c94 | ||
|
e1bc4b983e | ||
|
cb3645ea95 | ||
|
04ebaddf5e | ||
|
45e1f1ae57 | ||
|
c1d5f7cef7 | ||
|
df663c4e41 | ||
|
d3742782f3 | ||
|
faff17b2a9 | ||
|
9a3cddc92e | ||
|
bd2763d863 | ||
|
b2bb22d883 | ||
|
ad8ec7f387 | ||
|
cf0ca8578c | ||
|
07aee79bd8 | ||
|
344b57fe33 | ||
|
18a2fbf54a | ||
|
39af56cf8f | ||
|
2d3a0a1034 | ||
|
655fa5d9aa | ||
|
9a49a29c7f | ||
|
89ae45cda4 | ||
|
f660ad35b2 | ||
|
c61738ae43 | ||
|
c4dec4d34b | ||
|
a797604255 | ||
|
25840d3bc7 | ||
|
4745d50931 | ||
|
04bf3abe99 | ||
|
57f7963eee | ||
|
2fa21e5dd6 | ||
|
9c4c37aa36 | ||
|
2540c9062f | ||
|
f28274109f | ||
|
724724bd8c | ||
|
64541cb5f8 | ||
|
179b00ed6c | ||
|
a9fd496691 | ||
|
b14c57e656 | ||
|
fa5617e076 | ||
|
e52a1d5fad |
282
README.md
282
README.md
@@ -16,39 +16,19 @@ fzf requires Ruby (>= 1.8.5).
|
||||
Installation
|
||||
------------
|
||||
|
||||
Download [fzf executable](https://raw.github.com/junegunn/fzf/master/fzf) and
|
||||
put it somewhere in your search $PATH.
|
||||
|
||||
```sh
|
||||
mkdir -p ~/bin
|
||||
wget https://raw.github.com/junegunn/fzf/master/fzf -O ~/bin/fzf
|
||||
chmod +x ~/bin/fzf
|
||||
```
|
||||
|
||||
Or you can just clone this repository and run
|
||||
Clone this repository and run
|
||||
[install](https://github.com/junegunn/fzf/blob/master/install) script.
|
||||
|
||||
```sh
|
||||
git clone https://github.com/junegunn/fzf.git
|
||||
fzf/install
|
||||
git clone https://github.com/junegunn/fzf.git ~/.fzf
|
||||
~/.fzf/install
|
||||
```
|
||||
|
||||
Make sure that ~/bin is included in $PATH.
|
||||
The script will setup:
|
||||
|
||||
```sh
|
||||
export PATH=$PATH:~/bin
|
||||
```
|
||||
|
||||
### Install as Ruby gem
|
||||
|
||||
fzf can be installed as a Ruby gem
|
||||
|
||||
```
|
||||
gem install fzf
|
||||
```
|
||||
|
||||
It's a bit easier to install and update the script but the Ruby gem version
|
||||
takes slightly longer to start.
|
||||
- `fzf` executable
|
||||
- Key bindings (`CTRL-T`, `CTRL-R`, etc.)
|
||||
- Fuzzy auto-completion for bash
|
||||
|
||||
### Install as Vim plugin
|
||||
|
||||
@@ -71,12 +51,20 @@ Usage
|
||||
```
|
||||
usage: fzf [options]
|
||||
|
||||
Options
|
||||
-m, --multi Enable multi-select
|
||||
-x, --extended Extended-search mode
|
||||
-s, --sort=MAX Maximum number of matched items to sort. Default: 1000
|
||||
-q, --query=STR Initial query
|
||||
-s, --sort=MAX Maximum number of matched items to sort (default: 1000)
|
||||
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
||||
-i Case-insensitive match (default: smart-case match)
|
||||
+i Case-sensitive match
|
||||
+c, --no-color Disable colors
|
||||
--no-mouse Disable mouse
|
||||
|
||||
Environment variables
|
||||
FZF_DEFAULT_COMMAND Default command to use when input is tty
|
||||
FZF_DEFAULT_OPTS Defaults options. (e.g. "-x -m --sort 10000")
|
||||
```
|
||||
|
||||
fzf will launch curses-based finder, read the list from STDIN, and write the
|
||||
@@ -104,7 +92,7 @@ history | fzf +s
|
||||
### Key binding
|
||||
|
||||
Use CTRL-J and CTRL-K (or CTRL-N and CTRL-P) to change the selection, press
|
||||
enter key to select the item. CTRL-C will terminate the finder.
|
||||
enter key to select the item. CTRL-C, CTRL-G, or ESC will terminate the finder.
|
||||
|
||||
The following readline key bindings should also work as expected.
|
||||
|
||||
@@ -116,6 +104,9 @@ The following readline key bindings should also work as expected.
|
||||
If you enable multi-select mode with `-m` option, you can select multiple items
|
||||
with TAB or Shift-TAB key.
|
||||
|
||||
You can also use mouse. Click on an item to select it or shift-click to select
|
||||
multiple items. Use mouse wheel to move the cursor up and down.
|
||||
|
||||
### Extended-search mode
|
||||
|
||||
With `-x` or `--extended` option, fzf will start in "extended-search mode".
|
||||
@@ -132,14 +123,143 @@ such as: `^music .mp3$ sbtrkt !rmx`
|
||||
| `'wild` | Items that include `wild` | exact-match (quoted) |
|
||||
| `!'fire` | Items that do not include `fire` | inverse-exact-match |
|
||||
|
||||
Useful examples
|
||||
---------------
|
||||
|
||||
```sh
|
||||
# vimf - Open selected file in Vim
|
||||
vimf() {
|
||||
FILE=$(fzf) && vim "$FILE"
|
||||
}
|
||||
|
||||
# fd - cd to selected directory
|
||||
fd() {
|
||||
DIR=$(find ${1:-*} -path '*/\.*' -prune -o -type d -print 2> /dev/null | fzf) && cd "$DIR"
|
||||
}
|
||||
|
||||
# fda - including hidden directories
|
||||
fda() {
|
||||
DIR=$(find ${1:-.} -type d 2> /dev/null | fzf) && cd "$DIR"
|
||||
}
|
||||
|
||||
# fh - repeat history
|
||||
fh() {
|
||||
eval $(history | fzf +s | sed 's/ *[0-9]* *//')
|
||||
}
|
||||
|
||||
# fkill - kill process
|
||||
fkill() {
|
||||
ps -ef | sed 1d | fzf -m | awk '{print $2}' | xargs kill -${1:-9}
|
||||
}
|
||||
```
|
||||
|
||||
Key bindings for command line
|
||||
-----------------------------
|
||||
|
||||
The install script will setup the following key bindings.
|
||||
|
||||
### bash
|
||||
|
||||
- `CTRL-T` - Paste the selected file path(s) into the command line
|
||||
- `CTRL-R` - Paste the selected command from history into the command line
|
||||
|
||||
The source code can be found in `~/.fzf.bash`.
|
||||
|
||||
### zsh
|
||||
|
||||
- `CTRL-T` - Paste the selected file path(s) into the command line
|
||||
- `CTRL-R` - Paste the selected command from history into the command line
|
||||
- `ALT-C` - cd into the selected directory
|
||||
|
||||
The source code can be found in `~/.fzf.zsh`.
|
||||
|
||||
Auto-completion
|
||||
---------------
|
||||
|
||||
Disclaimer: *Auto-completion feature is currently experimental, it can change
|
||||
over time*
|
||||
|
||||
### bash
|
||||
|
||||
#### Files and directories
|
||||
|
||||
Fuzzy completion for files and directories can be triggered if the word before
|
||||
the cursor ends with the trigger sequence which is by default `**`.
|
||||
|
||||
- `COMMAND [DIRECTORY/][FUZZY_PATTERN]**<TAB>`
|
||||
|
||||
```sh
|
||||
# Files under current directory
|
||||
# - You can select multiple items with TAB key
|
||||
vim **<TAB>
|
||||
|
||||
# Files under parent directory
|
||||
vim ../**<TAB>
|
||||
|
||||
# Files under parent directory that match `fzf`
|
||||
vim ../fzf**<TAB>
|
||||
|
||||
# Files under your home directory
|
||||
vim ~/**<TAB>
|
||||
|
||||
|
||||
# Directories under current directory (single-selection)
|
||||
cd **<TAB>
|
||||
|
||||
# Directories under ~/github that match `fzf`
|
||||
cd ~/github/fzf**<TAB>
|
||||
```
|
||||
|
||||
#### Process IDs
|
||||
|
||||
Fuzzy completion for PIDs is provided for kill command. In this case
|
||||
there is no trigger sequence, just press tab key after kill command.
|
||||
|
||||
```sh
|
||||
# Can select multiple processes with <TAB> or <Shift-TAB> keys
|
||||
kill -9 <TAB>
|
||||
```
|
||||
|
||||
#### Host names
|
||||
|
||||
For ssh and telnet commands, fuzzy completion for host names is provided. The
|
||||
names are extracted from /etc/hosts and ~/.ssh/config.
|
||||
|
||||
```sh
|
||||
ssh **<TAB>
|
||||
telnet **<TAB>
|
||||
```
|
||||
|
||||
#### Settings
|
||||
|
||||
```sh
|
||||
# Use ~~ as the trigger sequence instead of the default **
|
||||
export FZF_COMPLETION_TRIGGER='~~'
|
||||
|
||||
# Options to fzf command
|
||||
export FZF_COMPLETION_OPTS='+c -x'
|
||||
```
|
||||
|
||||
### zsh
|
||||
|
||||
TODO :smiley:
|
||||
|
||||
(Pull requests are appreciated.)
|
||||
|
||||
Usage as Vim plugin
|
||||
-------------------
|
||||
|
||||
If you install fzf as a Vim plugin, `:FZF` command will be added.
|
||||
|
||||
```vim
|
||||
" Look for files under current directory
|
||||
:FZF
|
||||
:FZF --no-sort -m
|
||||
|
||||
" Look for files under your home directory
|
||||
:FZF ~
|
||||
|
||||
" With options
|
||||
:FZF --no-sort -m /tmp
|
||||
```
|
||||
|
||||
You can override the source command which produces input to fzf.
|
||||
@@ -164,84 +284,6 @@ Most of the time, you will prefer native Vim plugins with better integration
|
||||
with Vim. The only reason one might consider using fzf in Vim is its speed. For
|
||||
a very large list of files, fzf is significantly faster and it does not block.
|
||||
|
||||
Useful bash examples
|
||||
--------------------
|
||||
|
||||
```sh
|
||||
# vimf - Open selected file in Vim
|
||||
vimf() {
|
||||
FILE=$(fzf) && vim "$FILE"
|
||||
}
|
||||
|
||||
# fd - cd to selected directory
|
||||
fd() {
|
||||
DIR=$(find ${1:-*} -path '*/\.*' -prune -o -type d -print 2> /dev/null | fzf) && cd "$DIR"
|
||||
}
|
||||
|
||||
# fda - including hidden directories
|
||||
fda() {
|
||||
DIR=$(find ${1:-*} -type d 2> /dev/null | fzf) && cd "$DIR"
|
||||
}
|
||||
|
||||
# fh - repeat history
|
||||
fh() {
|
||||
eval $(history | fzf +s | sed 's/ *[0-9]* *//')
|
||||
}
|
||||
|
||||
# fkill - kill process
|
||||
fkill() {
|
||||
ps -ef | sed 1d | fzf -m | awk '{print $2}' | xargs kill -${1:-9}
|
||||
}
|
||||
|
||||
# (Assuming you don't use the default CTRL-T and CTRL-R)
|
||||
|
||||
# CTRL-T - Paste the selected file path into the command line
|
||||
bind '"\er": redraw-current-line'
|
||||
bind '"\C-t": " \C-u \C-a\C-k$(fzf)\e\C-e\C-y\C-a\C-y\ey\C-h\C-e\er"'
|
||||
|
||||
# CTRL-R - Paste the selected command from history into the command line
|
||||
bind '"\C-r": " \C-e\C-u$(history | fzf +s | sed \"s/ *[0-9]* *//\")\e\C-e\er"'
|
||||
```
|
||||
|
||||
zsh widgets
|
||||
-----------
|
||||
|
||||
```sh
|
||||
# CTRL-T - Paste the selected file(s) path into the command line
|
||||
fzf-file-widget() {
|
||||
local FILES
|
||||
local IFS="
|
||||
"
|
||||
FILES=($(
|
||||
find * -path '*/\.*' -prune \
|
||||
-o -type f -print \
|
||||
-o -type l -print 2> /dev/null | fzf -m))
|
||||
unset IFS
|
||||
FILES=$FILES:q
|
||||
LBUFFER="${LBUFFER%% #} $FILES"
|
||||
zle redisplay
|
||||
}
|
||||
zle -N fzf-file-widget
|
||||
bindkey '^T' fzf-file-widget
|
||||
|
||||
# ALT-C - cd into the selected directory
|
||||
fzf-cd-widget() {
|
||||
cd "${$(find * -path '*/\.*' -prune \
|
||||
-o -type d -print 2> /dev/null | fzf):-.}"
|
||||
zle reset-prompt
|
||||
}
|
||||
zle -N fzf-cd-widget
|
||||
bindkey '\ec' fzf-cd-widget
|
||||
|
||||
# CTRL-R - Paste the selected command from history into the command line
|
||||
fzf-history-widget() {
|
||||
LBUFFER=$(history | fzf +s | sed "s/ *[0-9]* *//")
|
||||
zle redisplay
|
||||
}
|
||||
zle -N fzf-history-widget
|
||||
bindkey '^R' fzf-history-widget
|
||||
```
|
||||
|
||||
Tips
|
||||
----
|
||||
|
||||
@@ -255,12 +297,18 @@ If you're running Ruby 1.9 or above, you can improve the startup time with
|
||||
- `time ruby --disable-gems ~/bin/fzf -h`
|
||||
- 0.025 sec
|
||||
|
||||
Define fzf alias with the option as follows:
|
||||
You can define fzf function with the option as follows:
|
||||
|
||||
```sh
|
||||
alias fzf='ruby --disable-gems ~/bin/fzf'
|
||||
fzf() {
|
||||
ruby --disable-gems ~/bin/fzf "$@"
|
||||
}
|
||||
export -f fzf
|
||||
```
|
||||
|
||||
However, this is automatically set up in your .bashrc and .zshrc if you use the
|
||||
bundled [install](https://github.com/junegunn/fzf/blob/master/install) script.
|
||||
|
||||
### Incorrect display on Ruby 1.8
|
||||
|
||||
It is reported that the output of fzf can become unreadable on some terminals
|
||||
@@ -268,6 +316,20 @@ when it's running on Ruby 1.8. If you experience the problem, upgrade your Ruby
|
||||
to 1.9 or above. Ruby 1.9 or above is also required for displaying Unicode
|
||||
characters.
|
||||
|
||||
### Ranking algorithm
|
||||
|
||||
fzf sorts the result first by the length of the matched substring, then by the
|
||||
length of the whole string. However it only does so when the number of matches
|
||||
is less than the limit which is by default 1000, in order to avoid the cost of
|
||||
sorting a large list and limit the response time of the query.
|
||||
|
||||
This limit can be adjusted with `-s` option, or with the environment variable
|
||||
`FZF_DEFAULT_OPTS`.
|
||||
|
||||
```sh
|
||||
export FZF_DEFAULT_OPTS="--sort 20000"
|
||||
```
|
||||
|
||||
License
|
||||
-------
|
||||
|
||||
|
294
fzf
294
fzf
@@ -7,10 +7,11 @@
|
||||
# / __/ / /_/ __/
|
||||
# /_/ /___/_/ Fuzzy finder for your shell
|
||||
#
|
||||
# URL: https://github.com/junegunn/fzf
|
||||
# Version: 0.7.0 (January 30, 2014)
|
||||
#
|
||||
# Author: Junegunn Choi
|
||||
# URL: https://github.com/junegunn/fzf
|
||||
# License: MIT
|
||||
# Last update: November 17, 2013
|
||||
#
|
||||
# Copyright (c) 2013 Junegunn Choi
|
||||
#
|
||||
@@ -39,9 +40,17 @@ require 'thread'
|
||||
require 'curses'
|
||||
require 'set'
|
||||
|
||||
unless String.method_defined? :force_encoding
|
||||
class String
|
||||
def force_encoding *arg
|
||||
self
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class FZF
|
||||
C = Curses
|
||||
attr_reader :rxflag, :sort, :color, :multi
|
||||
attr_reader :rxflag, :sort, :color, :mouse, :multi, :query, :extended
|
||||
|
||||
class AtomicVar
|
||||
def initialize value
|
||||
@@ -67,29 +76,58 @@ class FZF
|
||||
end
|
||||
|
||||
def initialize argv, source = $stdin
|
||||
usage 0 unless (%w[--help -h] & argv).empty?
|
||||
@rxflag = argv.delete('+i') ? 0 : Regexp::IGNORECASE
|
||||
@sort = %w[+s --no-sort].map { |e| argv.delete e }.compact.empty? ?
|
||||
ENV.fetch('FZF_DEFAULT_SORT', 1000).to_i : nil
|
||||
@color = %w[+c --no-color].map { |e| argv.delete e }.compact.empty?
|
||||
@multi = !%w[-m --multi].map { |e| argv.delete e }.compact.empty?
|
||||
@xmode = !%w[-x --extended].map { |e| argv.delete e }.compact.empty?
|
||||
rest = argv.join ' '
|
||||
if sort = rest.match(/(-s|--sort=?) ?([0-9]+)/)
|
||||
usage 1 unless @sort
|
||||
@sort = sort[2].to_i
|
||||
rest = rest.delete sort[0]
|
||||
end
|
||||
usage 1 unless rest.empty?
|
||||
@rxflag = nil
|
||||
@sort = ENV.fetch('FZF_DEFAULT_SORT', 1000).to_i
|
||||
@color = true
|
||||
@multi = false
|
||||
@extended = false
|
||||
@mouse = true
|
||||
|
||||
@source = source
|
||||
argv =
|
||||
if opts = ENV['FZF_DEFAULT_OPTS']
|
||||
require 'shellwords'
|
||||
Shellwords.shellwords(opts) + argv
|
||||
else
|
||||
argv.dup
|
||||
end
|
||||
while o = argv.shift
|
||||
case o
|
||||
when '--version' then version
|
||||
when '-h', '--help' then usage 0
|
||||
when '-m', '--multi' then @multi = true
|
||||
when '+m', '--no-multi' then @multi = false
|
||||
when '-x', '--extended' then @extended = true
|
||||
when '+x', '--no-extended' then @extended = false
|
||||
when '-i' then @rxflag = Regexp::IGNORECASE
|
||||
when '+i' then @rxflag = 0
|
||||
when '-c', '--color' then @color = true
|
||||
when '+c', '--no-color' then @color = false
|
||||
when '--no-mouse' then @mouse = false
|
||||
when '+s', '--no-sort' then @sort = nil
|
||||
when '-q', '--query'
|
||||
usage 1, 'query string required' unless query = argv.shift
|
||||
@query = AtomicVar.new query.dup
|
||||
when /^-q(.*)$/, /^--query=(.*)$/
|
||||
@query = AtomicVar.new($1)
|
||||
when '-s', '--sort'
|
||||
usage 1, 'sort size required' unless sort = argv.shift
|
||||
usage 1, 'invalid sort size' unless sort =~ /^[0-9]+$/
|
||||
@sort = sort.to_i
|
||||
when /^-s([0-9]+)$/, /^--sort=([0-9]+)$/
|
||||
@sort = $1.to_i
|
||||
else
|
||||
usage 1, "illegal option: #{o}"
|
||||
end
|
||||
end
|
||||
|
||||
@source = source.clone
|
||||
@mtx = Mutex.new
|
||||
@cv = ConditionVariable.new
|
||||
@events = {}
|
||||
@new = []
|
||||
@queue = Queue.new
|
||||
@cursor_x = AtomicVar.new(0)
|
||||
@query = AtomicVar.new('')
|
||||
@query ||= AtomicVar.new('')
|
||||
@cursor_x = AtomicVar.new(@query.length)
|
||||
@matches = AtomicVar.new([])
|
||||
@count = AtomicVar.new(0)
|
||||
@vcursor = AtomicVar.new(0)
|
||||
@@ -111,15 +149,35 @@ class FZF
|
||||
start_loop
|
||||
end
|
||||
|
||||
def usage x
|
||||
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 usage x, message = nil
|
||||
$stderr.puts message if message
|
||||
$stderr.puts %[usage: fzf [options]
|
||||
|
||||
Options
|
||||
-m, --multi Enable multi-select
|
||||
-x, --extended Extended-search mode
|
||||
-s, --sort=MAX Maximum number of matched items to sort. Default: 1000
|
||||
-q, --query=STR Initial query
|
||||
-s, --sort=MAX Maximum number of matched items to sort (default: 1000)
|
||||
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
||||
-i Case-insensitive match (default: smart-case match)
|
||||
+i Case-sensitive match
|
||||
+c, --no-color Disable colors]
|
||||
+c, --no-color Disable colors
|
||||
--no-mouse Disable mouse
|
||||
|
||||
Environment variables
|
||||
FZF_DEFAULT_COMMAND Default command to use when input is tty
|
||||
FZF_DEFAULT_OPTS Defaults options. (e.g. "-x -m --sort 10000")] + $/ + $/
|
||||
exit x
|
||||
end
|
||||
|
||||
@@ -329,7 +387,7 @@ class FZF
|
||||
tokens << [line[b...e], true]
|
||||
index = e
|
||||
end
|
||||
tokens << [line[index..-1], false]
|
||||
tokens << [line[index..-1], false] if index < line.length
|
||||
tokens.reject { |pair| pair.first.empty? }
|
||||
end
|
||||
|
||||
@@ -374,7 +432,7 @@ class FZF
|
||||
if RUBY_VERSION.split('.').map { |e| e.rjust(3, '0') }.join > '001009'
|
||||
@@wrx = Regexp.new '\p{Han}|\p{Katakana}|\p{Hiragana}|\p{Hangul}'
|
||||
def width str
|
||||
str.gsub(@@wrx, ' ').length
|
||||
str.gsub(@@wrx, ' ').length rescue str.length
|
||||
end
|
||||
|
||||
def trim str, len, left
|
||||
@@ -416,6 +474,11 @@ class FZF
|
||||
|
||||
def init_screen
|
||||
C.init_screen
|
||||
if @mouse
|
||||
C.mouseinterval 0
|
||||
C.mousemask C::ALL_MOUSE_EVENTS
|
||||
end
|
||||
C.stdscr.keypad(true)
|
||||
C.start_color
|
||||
dbg =
|
||||
if C.respond_to?(:use_default_colors)
|
||||
@@ -425,10 +488,11 @@ class FZF
|
||||
C::COLOR_BLACK
|
||||
end
|
||||
C.raw
|
||||
C.nonl
|
||||
C.noecho
|
||||
|
||||
if @color
|
||||
if C.can_change_color?
|
||||
if C.can_change_color? && ENV['TERM'].to_s =~ /256/
|
||||
C.init_pair 1, 110, dbg
|
||||
C.init_pair 2, 108, dbg
|
||||
C.init_pair 3, 254, 236
|
||||
@@ -480,6 +544,7 @@ class FZF
|
||||
exit 1
|
||||
end
|
||||
else
|
||||
$stdin.reopen IO.open(IO.sysopen('/dev/tty'), 'r')
|
||||
@source
|
||||
end
|
||||
|
||||
@@ -493,7 +558,7 @@ class FZF
|
||||
end
|
||||
|
||||
def start_search
|
||||
matcher = (@xmode ? ExtendedFuzzyMatcher : FuzzyMatcher).new @rxflag
|
||||
matcher = (@extended ? ExtendedFuzzyMatcher : FuzzyMatcher).new @rxflag
|
||||
searcher = Thread.new {
|
||||
lists = []
|
||||
events = {}
|
||||
@@ -536,8 +601,7 @@ class FZF
|
||||
if new_search && !lists.empty?
|
||||
q, cx = events.delete(:key) || [q, 0]
|
||||
empty = matcher.empty?(q)
|
||||
matches = fcache[q] ||=
|
||||
begin
|
||||
unless matches = fcache[q]
|
||||
found = []
|
||||
skip = false
|
||||
cnt = 0
|
||||
@@ -554,7 +618,7 @@ class FZF
|
||||
matcher.match(list, q, q[0, cx], q[cx..-1]))
|
||||
end
|
||||
next if skip
|
||||
@sort ? found : found.reverse
|
||||
matches = fcache[q] = @sort ? found : found.reverse
|
||||
end
|
||||
|
||||
if !empty && @sort && matches.length <= @sort
|
||||
@@ -612,12 +676,14 @@ class FZF
|
||||
print_item row, tokens, chosen, selected
|
||||
end
|
||||
print_info
|
||||
print_input
|
||||
end
|
||||
end
|
||||
|
||||
def start_renderer
|
||||
Thread.new do
|
||||
begin
|
||||
refresh
|
||||
while blk = @queue.shift
|
||||
blk.call
|
||||
refresh
|
||||
@@ -633,18 +699,39 @@ class FZF
|
||||
nil
|
||||
end
|
||||
|
||||
def vselect &prc
|
||||
@vcursor.set { |v| @vcursors << v; prc.call v }
|
||||
update_list false
|
||||
end
|
||||
|
||||
def num_unicode_bytes chr
|
||||
# http://en.wikipedia.org/wiki/UTF-8
|
||||
if chr & 0b10000000 > 0
|
||||
bytes = 0
|
||||
7.downto(2) do |shift|
|
||||
break if (chr >> shift) & 0x1 == 0
|
||||
bytes += 1
|
||||
end
|
||||
bytes
|
||||
else
|
||||
1
|
||||
end
|
||||
end
|
||||
|
||||
def test_mouse st, *states
|
||||
states.any? { |s| s & st > 0 }
|
||||
end
|
||||
|
||||
def start_loop
|
||||
got = nil
|
||||
begin
|
||||
tty = IO.open(IO.sysopen('/dev/tty'), 'r')
|
||||
input = ''
|
||||
cursor = 0
|
||||
input = @query.get.dup
|
||||
cursor = input.length
|
||||
backword = proc {
|
||||
cursor = (input[0, cursor].rindex(/\s\S/) || -1) + 1
|
||||
}
|
||||
actions = {
|
||||
:nop => proc { nil },
|
||||
ctrl(:c) => proc { exit 1 },
|
||||
:esc => proc { exit 1 },
|
||||
ctrl(:d) => proc { exit 1 if input.empty? },
|
||||
ctrl(:m) => proc {
|
||||
got = pick
|
||||
@@ -653,74 +740,115 @@ class FZF
|
||||
ctrl(:u) => proc { input = input[cursor..-1]; cursor = 0 },
|
||||
ctrl(:a) => proc { cursor = 0; nil },
|
||||
ctrl(:e) => proc { cursor = input.length; nil },
|
||||
ctrl(:j) => proc { @vcursor.set { |v| @vcursors << v; v - 1 }; update_list false },
|
||||
ctrl(:k) => proc { @vcursor.set { |v| @vcursors << v; v + 1 }; update_list false },
|
||||
ctrl(:j) => proc { vselect { |v| v - 1 } },
|
||||
ctrl(:k) => proc { vselect { |v| v + 1 } },
|
||||
ctrl(:w) => proc {
|
||||
pcursor = cursor
|
||||
backword.call
|
||||
input = input[0...cursor] + input[pcursor..-1]
|
||||
},
|
||||
127 => proc { input[cursor -= 1] = '' if cursor > 0 },
|
||||
9 => proc { |o|
|
||||
ctrl(:h) => proc { input[cursor -= 1] = '' if cursor > 0 },
|
||||
ctrl(:i) => proc { |o|
|
||||
if @multi && sel = pick
|
||||
if @selects.has_key? sel
|
||||
@selects.delete sel
|
||||
else
|
||||
@selects[sel] = 1
|
||||
end
|
||||
@vcursor.set { |v|
|
||||
@vcursors << v
|
||||
v + (o == :stab ? 1 : -1)
|
||||
vselect { |v|
|
||||
v + case o
|
||||
when :select then 0
|
||||
when C::KEY_BTAB then 1
|
||||
else -1
|
||||
end
|
||||
}
|
||||
update_list false
|
||||
end
|
||||
},
|
||||
:left => proc { cursor = [0, cursor - 1].max; nil },
|
||||
:right => proc { cursor = [input.length, cursor + 1].min; nil },
|
||||
ctrl(:b) => proc { cursor = [0, cursor - 1].max; nil },
|
||||
ctrl(:f) => proc { cursor = [input.length, cursor + 1].min; nil },
|
||||
:alt_b => proc { backword.call; nil },
|
||||
:alt_f => proc {
|
||||
cursor += (input[cursor..-1].index(/(\S\s)|(.$)/) || -1) + 1
|
||||
nil
|
||||
},
|
||||
}
|
||||
actions[ctrl(:b)] = actions[:left]
|
||||
actions[ctrl(:f)] = actions[:right]
|
||||
actions[ctrl(:h)] = actions[127]
|
||||
actions[ctrl(:n)] = actions[ctrl(:j)]
|
||||
actions[ctrl(:p)] = actions[ctrl(:k)]
|
||||
actions[:stab] = actions[9]
|
||||
actions[C::KEY_UP] = actions[ctrl(:p)] = actions[ctrl(:k)]
|
||||
actions[C::KEY_DOWN] = actions[ctrl(:n)] = actions[ctrl(:j)]
|
||||
actions[C::KEY_LEFT] = actions[ctrl(:b)]
|
||||
actions[C::KEY_RIGHT] = actions[ctrl(:f)]
|
||||
actions[C::KEY_BTAB] = actions[:select] = actions[ctrl(:i)]
|
||||
actions[C::KEY_BACKSPACE] = actions[127] = actions[ctrl(:h)]
|
||||
actions[ctrl(:q)] = actions[ctrl(:g)] = actions[ctrl(:c)] = actions[:esc]
|
||||
|
||||
emit(:key) { [@query.get, cursor] } unless @query.empty?
|
||||
pmv = nil
|
||||
while true
|
||||
@cursor_x.set cursor
|
||||
render { print_input }
|
||||
|
||||
ord = tty.getc.ord
|
||||
ord =
|
||||
case ord = tty.getc.ord
|
||||
when 91
|
||||
case tty.getc.ord
|
||||
when 68 then :left
|
||||
when 67 then :right
|
||||
when 66 then ctrl(:j)
|
||||
when 65 then ctrl(:k)
|
||||
when 90 then :stab
|
||||
else :nop
|
||||
end
|
||||
when 'b'.ord
|
||||
:alt_b
|
||||
when 'f'.ord
|
||||
:alt_f
|
||||
else
|
||||
ord
|
||||
end if ord == 27
|
||||
C.stdscr.timeout = -1
|
||||
ch = C.getch
|
||||
|
||||
upd = actions.fetch(ord, proc { |ord|
|
||||
char = [ord].pack('U*')
|
||||
if char =~ /[[:print:]]/
|
||||
input.insert cursor, char
|
||||
case ch
|
||||
when C::KEY_MOUSE
|
||||
if m = C.getmouse
|
||||
st = m.bstate
|
||||
if test_mouse(st, C::BUTTON1_PRESSED, C::BUTTON1_RELEASED)
|
||||
if m.y == cursor_y
|
||||
# TODO Wide-characters
|
||||
cursor = [0, [input.length, m.x - 2].min].max
|
||||
elsif m.x > 1 && m.y <= max_items
|
||||
vselect { |v|
|
||||
tv = max_items - m.y - 1
|
||||
if test_mouse(st, C::BUTTON1_RELEASED)
|
||||
if test_mouse(st, C::BUTTON_SHIFT)
|
||||
ch = :select
|
||||
elsif pmv == tv
|
||||
ch = ctrl(:m)
|
||||
end
|
||||
pmv = tv
|
||||
end
|
||||
tv
|
||||
}
|
||||
end
|
||||
elsif test_mouse(st, 0x8000000, C::BUTTON2_PRESSED)
|
||||
ch = C::KEY_DOWN
|
||||
elsif test_mouse(st, C::BUTTON4_PRESSED)
|
||||
ch = C::KEY_UP
|
||||
end
|
||||
end
|
||||
when 27
|
||||
C.stdscr.timeout = 0
|
||||
ch =
|
||||
case ch2 = C.getch
|
||||
when 'b' then :alt_b
|
||||
when 'f' then :alt_f
|
||||
when nil then :esc
|
||||
else
|
||||
ch2
|
||||
end
|
||||
end
|
||||
|
||||
upd = actions.fetch(ch, proc { |ch|
|
||||
if ch.is_a?(Fixnum)
|
||||
# Ruby 1.8
|
||||
if (ch.chr rescue '') =~ /[[:print:]]/
|
||||
ch = ch.chr
|
||||
elsif (nch = num_unicode_bytes(ch)) > 1
|
||||
chs = [ch]
|
||||
(nch - 1).times do |i|
|
||||
chs << C.getch
|
||||
end
|
||||
# UTF-8 TODO Ruby 1.8
|
||||
ch = chs.pack('C*').force_encoding('UTF-8')
|
||||
end
|
||||
end
|
||||
|
||||
if ch.is_a?(String) && ch =~ /[[:print:]]/
|
||||
input.insert cursor, ch
|
||||
cursor += 1
|
||||
end
|
||||
}).call(ord)
|
||||
}).call(ch)
|
||||
|
||||
# Dispatch key event
|
||||
emit(:key) { [@query.set(input.dup), cursor] } if upd
|
||||
@@ -752,14 +880,18 @@ class FZF
|
||||
q.empty?
|
||||
end
|
||||
|
||||
def rxflag_for q
|
||||
@rxflag || (q =~ /[A-Z]/ ? 0 : Regexp::IGNORECASE)
|
||||
end
|
||||
|
||||
def fuzzy_regex q
|
||||
@regexp[q] ||= begin
|
||||
q = q.downcase if @rxflag != 0
|
||||
q = q.downcase if @rxflag == Regexp::IGNORECASE
|
||||
Regexp.new(query_chars(q).inject('') { |sum, e|
|
||||
e = Regexp.escape e
|
||||
sum << (e.length > 1 ? "(?:#{e}).*?" : # FIXME: not equivalent
|
||||
"#{e}[^#{e}]*?")
|
||||
}, @rxflag)
|
||||
}, rxflag_for(q))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -811,15 +943,17 @@ class FZF
|
||||
case w
|
||||
when ''
|
||||
nil
|
||||
when /^\^(.*)\$$/
|
||||
Regexp.new('^' << sanitize(Regexp.escape($1)) << '$', rxflag_for(w))
|
||||
when /^'/
|
||||
w.length > 1 ?
|
||||
Regexp.new(sanitize(Regexp.escape(w[1..-1])), rxflag) : nil
|
||||
Regexp.new(sanitize(Regexp.escape(w[1..-1])), rxflag_for(w)) : nil
|
||||
when /^\^/
|
||||
w.length > 1 ?
|
||||
Regexp.new('^' << sanitize(Regexp.escape(w[1..-1])), rxflag) : nil
|
||||
Regexp.new('^' << sanitize(Regexp.escape(w[1..-1])), rxflag_for(w)) : nil
|
||||
when /\$$/
|
||||
w.length > 1 ?
|
||||
Regexp.new(sanitize(Regexp.escape(w[0..-2])) << '$', rxflag) : nil
|
||||
Regexp.new(sanitize(Regexp.escape(w[0..-2])) << '$', rxflag_for(w)) : nil
|
||||
else
|
||||
fuzzy_regex w
|
||||
end, invert ]
|
||||
|
167
fzf-completion.bash
Normal file
167
fzf-completion.bash
Normal file
@@ -0,0 +1,167 @@
|
||||
#!/bin/bash
|
||||
# ____ ____
|
||||
# / __/___ / __/
|
||||
# / /_/_ / / /_
|
||||
# / __/ / /_/ __/
|
||||
# /_/ /___/_/-completion.bash
|
||||
#
|
||||
# - $FZF_COMPLETION_TRIGGER (default: '**')
|
||||
# - $FZF_COMPLETION_OPTS (default: empty)
|
||||
|
||||
_fzf_opts_completion() {
|
||||
local cur prev opts
|
||||
COMPREPLY=()
|
||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
||||
opts="-m --multi -x --extended -s --sort +s +i +c --no-color"
|
||||
|
||||
case "${prev}" in
|
||||
--sort|-s)
|
||||
COMPREPLY=( $(compgen -W "$(seq 2000 1000 10000)" -- ${cur}) )
|
||||
return 0
|
||||
;;
|
||||
esac
|
||||
|
||||
if [[ ${cur} =~ ^-|\+ ]]; then
|
||||
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
||||
return 0
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
_fzf_generic_completion() {
|
||||
local cur base dir leftover matches trigger
|
||||
COMPREPLY=()
|
||||
trigger=${FZF_COMPLETION_TRIGGER:-**}
|
||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||
if [[ ${cur} == *"$trigger" ]]; then
|
||||
base=${cur:0:${#cur}-${#trigger}}
|
||||
eval base=$base
|
||||
|
||||
dir="$base"
|
||||
while [ 1 ]; do
|
||||
if [ -z "$dir" -o -d "$dir" ]; then
|
||||
leftover=${base/#"$dir"}
|
||||
leftover=${leftover/#\/}
|
||||
[ "$dir" = './' ] && dir=''
|
||||
tput sc
|
||||
matches=$(find "$dir"* $1 2> /dev/null | fzf $FZF_COMPLETION_OPTS $2 -q "$leftover" | while read item; do
|
||||
printf '%q ' "$item"
|
||||
done)
|
||||
matches=${matches% }
|
||||
if [ -n "$matches" ]; then
|
||||
COMPREPLY=( "$matches" )
|
||||
else
|
||||
COMPREPLY=( "$cur" )
|
||||
fi
|
||||
tput rc
|
||||
return 0
|
||||
fi
|
||||
dir=$(dirname "$dir")
|
||||
[[ "$dir" =~ /$ ]] || dir="$dir"/
|
||||
done
|
||||
fi
|
||||
}
|
||||
|
||||
_fzf_all_completion() {
|
||||
_fzf_generic_completion \
|
||||
"-name .git -prune -o -name .svn -prune -o -type d -print -o -type f -print -o -type l -print" \
|
||||
"-m"
|
||||
}
|
||||
|
||||
_fzf_file_completion() {
|
||||
_fzf_generic_completion \
|
||||
"-name .git -prune -o -name .svn -prune -o -type f -print -o -type l -print" \
|
||||
"-m"
|
||||
}
|
||||
|
||||
_fzf_dir_completion() {
|
||||
_fzf_generic_completion \
|
||||
"-name .git -prune -o -name .svn -prune -o -type d -print" \
|
||||
""
|
||||
}
|
||||
|
||||
_fzf_kill_completion() {
|
||||
[ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1
|
||||
|
||||
local selected
|
||||
tput sc
|
||||
selected=$(ps -ef | sed 1d | fzf -m $FZF_COMPLETION_OPTS | awk '{print $2}' | tr '\n' ' ')
|
||||
tput rc
|
||||
|
||||
if [ -n "$selected" ]; then
|
||||
COMPREPLY=( "$selected" )
|
||||
return 0
|
||||
fi
|
||||
}
|
||||
|
||||
_fzf_telnet_completion() {
|
||||
local cur selected trigger
|
||||
trigger=${FZF_COMPLETION_TRIGGER:-**}
|
||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||
[[ ${cur} == *"$trigger" ]] || return 1
|
||||
cur=${cur:0:${#cur}-${#trigger}}
|
||||
|
||||
tput sc
|
||||
selected=$(grep -v '^\s*\(#\|$\)' /etc/hosts | awk '{print $2}' | sort -u | fzf $FZF_COMPLETION_OPTS -q "$cur")
|
||||
tput rc
|
||||
|
||||
if [ -n "$selected" ]; then
|
||||
COMPREPLY=("$selected")
|
||||
return 0
|
||||
fi
|
||||
}
|
||||
|
||||
_fzf_ssh_completion() {
|
||||
local cur selected trigger
|
||||
trigger=${FZF_COMPLETION_TRIGGER:-**}
|
||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||
[[ ${cur} == *"$trigger" ]] || return 1
|
||||
cur=${cur:0:${#cur}-${#trigger}}
|
||||
|
||||
tput sc
|
||||
selected=$(cat \
|
||||
<(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | grep -i ^host) \
|
||||
<(grep -v '^\s*\(#\|$\)' /etc/hosts) | \
|
||||
awk '{print $2}' | sort -u | fzf $FZF_COMPLETION_OPTS -q "$cur")
|
||||
tput rc
|
||||
|
||||
if [ -n "$selected" ]; then
|
||||
COMPREPLY=("$selected")
|
||||
return 0
|
||||
fi
|
||||
}
|
||||
|
||||
complete -F _fzf_opts_completion fzf
|
||||
|
||||
# Directory
|
||||
for cmd in "cd pushd rmdir"; do
|
||||
complete -F _fzf_dir_completion -o default -o bashdefault $cmd
|
||||
done
|
||||
|
||||
# File
|
||||
for cmd in "
|
||||
awk cat diff diff3
|
||||
emacs ex file ftp g++ gcc gvim head hg java
|
||||
javac ld less more mvim patch perl python ruby
|
||||
sed sftp sort source tail tee uniq vi view vim wc"; do
|
||||
complete -F _fzf_file_completion -o default -o bashdefault $cmd
|
||||
done
|
||||
|
||||
# Anything
|
||||
for cmd in "
|
||||
basename bunzip2 bzip2 chmod chown curl cp dirname du
|
||||
find git grep gunzip gzip hg jar
|
||||
ln ls mv open rm rsync scp
|
||||
svn tar unzip zip"; do
|
||||
complete -F _fzf_all_completion -o default -o bashdefault $cmd
|
||||
done
|
||||
|
||||
# Kill completion
|
||||
complete -F _fzf_kill_completion -o nospace -o default -o bashdefault kill
|
||||
|
||||
# Host completion
|
||||
complete -F _fzf_ssh_completion -o default -o bashdefault ssh
|
||||
complete -F _fzf_telnet_completion -o default -o bashdefault telnet
|
||||
|
9
fzf-completion.zsh
Normal file
9
fzf-completion.zsh
Normal file
@@ -0,0 +1,9 @@
|
||||
#!/bin/zsh
|
||||
# ____ ____
|
||||
# / __/___ / __/
|
||||
# / /_/_ / / /_
|
||||
# / __/ / /_/ __/
|
||||
# /_/ /___/_/-completion.zsh
|
||||
#
|
||||
|
||||
# TODO
|
@@ -1,7 +1,7 @@
|
||||
# coding: utf-8
|
||||
Gem::Specification.new do |spec|
|
||||
spec.name = 'fzf'
|
||||
spec.version = '0.4.2'
|
||||
spec.version = '0.7.0'
|
||||
spec.authors = ['Junegunn Choi']
|
||||
spec.email = ['junegunn.c@gmail.com']
|
||||
spec.description = %q{Fuzzy finder for your shell}
|
||||
@@ -12,4 +12,6 @@ Gem::Specification.new do |spec|
|
||||
spec.bindir = '.'
|
||||
spec.files = %w[fzf.gemspec]
|
||||
spec.executables = 'fzf'
|
||||
|
||||
spec.add_runtime_dependency 'curses', '~> 1.0.0'
|
||||
end
|
||||
|
183
install
183
install
@@ -1,7 +1,184 @@
|
||||
#!/bin/bash
|
||||
|
||||
cd `dirname $BASH_SOURCE`
|
||||
mkdir -p ~/bin
|
||||
ln -sf `pwd`/fzf ~/bin/fzf
|
||||
chmod +x ~/bin/fzf
|
||||
fzf_base=`pwd`
|
||||
|
||||
# ruby executable
|
||||
echo -n "Checking Ruby executable ... "
|
||||
ruby=`which ruby`
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "ruby executable not found!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# System ruby is preferred
|
||||
curses_check="begin; require 'curses'; rescue Exception; exit 1; end"
|
||||
system_ruby=/usr/bin/ruby
|
||||
if [ -x $system_ruby -a $system_ruby != "$ruby" ]; then
|
||||
$system_ruby --disable-gems -e "$curses_check" 2> /dev/null
|
||||
[ $? -eq 0 ] && ruby=$system_ruby
|
||||
fi
|
||||
|
||||
echo "OK ($ruby)"
|
||||
|
||||
# Curses-support
|
||||
echo -n "Checking Curses support ... "
|
||||
"$ruby" -e "$curses_check"
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "OK"
|
||||
else
|
||||
echo "Not found"
|
||||
echo "Installing 'curses' gem ... "
|
||||
/usr/bin/env gem install curses
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Failed to install 'curses' gem."
|
||||
echo "Try installing it as root: sudo gem install curses"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# Ruby version
|
||||
echo -n "Checking Ruby version ... "
|
||||
"$ruby" -e 'exit RUBY_VERSION >= "1.9"'
|
||||
if [ $? -eq 0 ]; then
|
||||
echo ">= 1.9"
|
||||
"$ruby" --disable-gems -e "$curses_check"
|
||||
if [ $? -eq 0 ]; then
|
||||
fzf_cmd="$ruby --disable-gems $fzf_base/fzf"
|
||||
else
|
||||
fzf_cmd="$ruby $fzf_base/fzf"
|
||||
fi
|
||||
else
|
||||
echo "< 1.9"
|
||||
fzf_cmd="$ruby $fzf_base/fzf"
|
||||
fi
|
||||
|
||||
# Auto-completion
|
||||
read -p "Do you want to add auto-completion support? ([y]/n) " -n 1 -r
|
||||
echo
|
||||
[[ ! $REPLY =~ ^[Nn]$ ]]
|
||||
auto_completion=$?
|
||||
|
||||
# Key-bindings
|
||||
read -p "Do you want to add key bindings? ([y]/n) " -n 1 -r
|
||||
echo
|
||||
[[ ! $REPLY =~ ^[Nn]$ ]]
|
||||
key_bindings=$?
|
||||
|
||||
echo
|
||||
for shell in bash zsh; do
|
||||
echo -n "Generate ~/.fzf.$shell ... "
|
||||
src=~/.fzf.${shell}
|
||||
|
||||
fzf_completion="source $fzf_base/fzf-completion.${shell}"
|
||||
if [ $auto_completion -ne 0 ]; then
|
||||
fzf_completion="# $fzf_completion"
|
||||
fi
|
||||
|
||||
cat > $src << EOF
|
||||
# Setup fzf function
|
||||
# ------------------
|
||||
unalias fzf 2> /dev/null
|
||||
fzf() {
|
||||
$fzf_cmd "\$@"
|
||||
}
|
||||
export -f fzf > /dev/null
|
||||
|
||||
# Auto-completion
|
||||
# ---------------
|
||||
$fzf_completion
|
||||
|
||||
EOF
|
||||
|
||||
if [ $key_bindings -eq 0 ]; then
|
||||
if [ $shell = bash ]; then
|
||||
cat >> $src << "EOF"
|
||||
# Key bindings
|
||||
# ------------
|
||||
# Required to refresh the prompt after fzf
|
||||
bind '"\er": redraw-current-line'
|
||||
|
||||
# CTRL-T - Paste the selected file path into the command line
|
||||
__fsel() {
|
||||
find * -path '*/\.*' -prune \
|
||||
-o -type f -print \
|
||||
-o -type l -print 2> /dev/null | fzf -m | while read item; do
|
||||
printf '%q ' "$item"
|
||||
done
|
||||
echo
|
||||
}
|
||||
bind '"\C-t": " \C-u \C-a\C-k$(__fsel)\e\C-e\C-y\C-a\C-y\ey\C-h\C-e\er"'
|
||||
|
||||
# CTRL-R - Paste the selected command from history into the command line
|
||||
bind '"\C-r": " \C-e\C-u$(history | fzf +s | sed \"s/ *[0-9]* *//\")\e\C-e\er"'
|
||||
|
||||
EOF
|
||||
else
|
||||
cat >> $src << "EOF"
|
||||
# Key bindings
|
||||
# ------------
|
||||
# CTRL-T - Paste the selected file path(s) into the command line
|
||||
fzf-file-widget() {
|
||||
local FILES
|
||||
local IFS="
|
||||
"
|
||||
FILES=($(
|
||||
find * -path '*/\.*' -prune \
|
||||
-o -type f -print \
|
||||
-o -type l -print 2> /dev/null | fzf -m))
|
||||
unset IFS
|
||||
FILES=$FILES:q
|
||||
LBUFFER="${LBUFFER%% #} $FILES"
|
||||
zle redisplay
|
||||
}
|
||||
zle -N fzf-file-widget
|
||||
bindkey '^T' fzf-file-widget
|
||||
|
||||
# ALT-C - cd into the selected directory
|
||||
fzf-cd-widget() {
|
||||
cd "${$(find * -path '*/\.*' -prune \
|
||||
-o -type d -print 2> /dev/null | fzf):-.}"
|
||||
zle reset-prompt
|
||||
}
|
||||
zle -N fzf-cd-widget
|
||||
bindkey '\ec' fzf-cd-widget
|
||||
|
||||
# CTRL-R - Paste the selected command from history into the command line
|
||||
fzf-history-widget() {
|
||||
LBUFFER=$(history | fzf +s | sed "s/ *[0-9]* *//")
|
||||
zle redisplay
|
||||
}
|
||||
zle -N fzf-history-widget
|
||||
bindkey '^R' fzf-history-widget
|
||||
|
||||
EOF
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "OK"
|
||||
done
|
||||
|
||||
echo
|
||||
for shell in bash zsh; do
|
||||
rc=~/.${shell}rc
|
||||
src="source ~/.fzf.${shell}"
|
||||
|
||||
echo "Update $rc:"
|
||||
echo " - $src"
|
||||
if [ $(grep -F "$src" $rc | wc -l) -gt 0 ]; then
|
||||
echo " - Not added (already being sourced)"
|
||||
else
|
||||
echo $src >> $rc
|
||||
echo " - Added"
|
||||
fi
|
||||
echo
|
||||
done
|
||||
|
||||
cat << EOF
|
||||
Finished. Reload your .bashrc or .zshrc.
|
||||
source ~/.bashrc # bash
|
||||
source ~/.zshrc # zsh
|
||||
|
||||
To uninstall fzf, simply remove the added line.
|
||||
EOF
|
||||
|
||||
|
@@ -23,25 +23,37 @@
|
||||
|
||||
let s:exec = expand('<sfile>:h:h').'/fzf'
|
||||
|
||||
function! fzf#run(command, args)
|
||||
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)
|
||||
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(a:args) ? get(g:, 'fzf_options', '') : a:args
|
||||
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.' '.line
|
||||
execute a:command.' '.s:escape(line)
|
||||
endif
|
||||
endfor
|
||||
endif
|
||||
finally
|
||||
execute 'chdir '.s:escape(cwd)
|
||||
redraw!
|
||||
silent! call delete(tf)
|
||||
endtry
|
||||
endfunction
|
||||
|
||||
command! -nargs=* FZF call fzf#run('silent e', <q-args>)
|
||||
command! -nargs=* -complete=dir FZF call fzf#run('silent e', <f-args>)
|
||||
|
||||
|
128
test/test_fzf.rb
128
test/test_fzf.rb
@@ -7,40 +7,88 @@ ENV['FZF_EXECUTABLE'] = '0'
|
||||
load 'fzf'
|
||||
|
||||
class TestFZF < MiniTest::Unit::TestCase
|
||||
|
||||
def setup
|
||||
ENV.delete 'FZF_DEFAULT_SORT'
|
||||
ENV.delete 'FZF_DEFAULT_OPTS'
|
||||
ENV.delete 'FZF_DEFAULT_COMMAND'
|
||||
end
|
||||
|
||||
def test_default_options
|
||||
fzf = FZF.new []
|
||||
assert_equal 1000, fzf.sort
|
||||
assert_equal false, fzf.multi
|
||||
assert_equal true, fzf.color
|
||||
assert_equal Regexp::IGNORECASE, fzf.rxflag
|
||||
|
||||
begin
|
||||
ENV['FZF_DEFAULT_SORT'] = '1500'
|
||||
fzf = FZF.new []
|
||||
assert_equal 1500, fzf.sort
|
||||
ensure
|
||||
ENV.delete 'FZF_DEFAULT_SORT'
|
||||
assert_equal nil, fzf.rxflag
|
||||
assert_equal true, fzf.mouse
|
||||
end
|
||||
|
||||
def test_environment_variables
|
||||
# Deprecated
|
||||
ENV['FZF_DEFAULT_SORT'] = '20000'
|
||||
fzf = FZF.new []
|
||||
assert_equal 20000, fzf.sort
|
||||
|
||||
ENV['FZF_DEFAULT_OPTS'] = '-x -m -s 10000 -q " hello world " +c --no-mouse'
|
||||
fzf = FZF.new []
|
||||
assert_equal 10000, fzf.sort
|
||||
assert_equal ' hello world ',
|
||||
fzf.query.get
|
||||
assert_equal true, fzf.extended
|
||||
assert_equal true, fzf.multi
|
||||
assert_equal false, fzf.color
|
||||
assert_equal false, fzf.mouse
|
||||
end
|
||||
|
||||
def test_option_parser
|
||||
# Long opts
|
||||
fzf = FZF.new %w[--sort=2000 --no-color --multi +i]
|
||||
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello --extended --no-mouse]
|
||||
assert_equal 2000, fzf.sort
|
||||
assert_equal true, fzf.multi
|
||||
assert_equal false, fzf.color
|
||||
assert_equal false, fzf.mouse
|
||||
assert_equal 0, fzf.rxflag
|
||||
assert_equal 'hello', fzf.query.get
|
||||
assert_equal true, fzf.extended
|
||||
|
||||
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello
|
||||
--no-sort -i --color --no-multi]
|
||||
assert_equal nil, fzf.sort
|
||||
assert_equal false, fzf.multi
|
||||
assert_equal true, fzf.color
|
||||
assert_equal true, fzf.mouse
|
||||
assert_equal 1, fzf.rxflag
|
||||
assert_equal 'hello', fzf.query.get
|
||||
assert_equal false, fzf.extended
|
||||
|
||||
# Short opts
|
||||
fzf = FZF.new %w[-s 2000 +c -m +i]
|
||||
fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x]
|
||||
assert_equal 2000, fzf.sort
|
||||
assert_equal true, fzf.multi
|
||||
assert_equal false, fzf.color
|
||||
assert_equal 0, fzf.rxflag
|
||||
assert_equal 'hello', fzf.query.get
|
||||
assert_equal true, fzf.extended
|
||||
|
||||
# Left-to-right
|
||||
fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x
|
||||
-s 3000 -c +m -i -q world +x]
|
||||
assert_equal 3000, fzf.sort
|
||||
assert_equal false, fzf.multi
|
||||
assert_equal true, fzf.color
|
||||
assert_equal 1, fzf.rxflag
|
||||
assert_equal 'world', fzf.query.get
|
||||
assert_equal false, fzf.extended
|
||||
|
||||
fzf = FZF.new %w[--query hello +s -s 2000 --query=world]
|
||||
assert_equal 2000, fzf.sort
|
||||
assert_equal 'world', fzf.query.get
|
||||
rescue SystemExit => e
|
||||
assert false, "Exited"
|
||||
end
|
||||
|
||||
def test_invalid_option
|
||||
[%w[-s 2000 +s], %w[yo dawg]].each do |argv|
|
||||
[%w[--unknown], %w[yo dawg]].each do |argv|
|
||||
assert_raises(SystemExit) do
|
||||
fzf = FZF.new argv
|
||||
end
|
||||
@@ -139,15 +187,59 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
# TODO : partial_cache
|
||||
end
|
||||
|
||||
def test_fuzzy_matcher_rxflag
|
||||
assert_equal nil, FZF::FuzzyMatcher.new(nil).rxflag
|
||||
assert_equal 0, FZF::FuzzyMatcher.new(0).rxflag
|
||||
assert_equal 1, FZF::FuzzyMatcher.new(1).rxflag
|
||||
|
||||
assert_equal 1, FZF::FuzzyMatcher.new(nil).rxflag_for('abc')
|
||||
assert_equal 0, FZF::FuzzyMatcher.new(nil).rxflag_for('Abc')
|
||||
assert_equal 0, FZF::FuzzyMatcher.new(0).rxflag_for('abc')
|
||||
assert_equal 0, FZF::FuzzyMatcher.new(0).rxflag_for('Abc')
|
||||
assert_equal 1, FZF::FuzzyMatcher.new(1).rxflag_for('abc')
|
||||
assert_equal 1, FZF::FuzzyMatcher.new(1).rxflag_for('Abc')
|
||||
end
|
||||
|
||||
def test_fuzzy_matcher_case_sensitive
|
||||
# Smart-case match (Uppercase found)
|
||||
assert_equal [['Fruit', [[0, 5]]]],
|
||||
FZF::FuzzyMatcher.new(nil).match(%w[Fruit Grapefruit], 'Fruit', '', '').sort
|
||||
|
||||
# Smart-case match (Uppercase not-found)
|
||||
assert_equal [["Fruit", [[0, 5]]], ["Grapefruit", [[5, 10]]]],
|
||||
FZF::FuzzyMatcher.new(nil).match(%w[Fruit Grapefruit], 'fruit', '', '').sort
|
||||
|
||||
# Case-sensitive match (-i)
|
||||
assert_equal [['Fruit', [[0, 5]]]],
|
||||
FZF::FuzzyMatcher.new(0).match(%w[Fruit Grapefruit], 'Fruit', '', '').sort
|
||||
|
||||
# Case-insensitive match (+i)
|
||||
assert_equal [["Fruit", [[0, 5]]], ["Grapefruit", [[5, 10]]]],
|
||||
FZF::FuzzyMatcher.new(Regexp::IGNORECASE).
|
||||
match(%w[Fruit Grapefruit], 'Fruit', '', '').sort
|
||||
end
|
||||
|
||||
def test_extended_fuzzy_matcher_case_sensitive
|
||||
%w['Fruit Fruit$].each do |q|
|
||||
# Smart-case match (Uppercase found)
|
||||
assert_equal [['Fruit', [[0, 5]]]],
|
||||
FZF::ExtendedFuzzyMatcher.new(nil).match(%w[Fruit Grapefruit], q, '', '').sort
|
||||
|
||||
# Smart-case match (Uppercase not-found)
|
||||
assert_equal [["Fruit", [[0, 5]]], ["Grapefruit", [[5, 10]]]],
|
||||
FZF::ExtendedFuzzyMatcher.new(nil).match(%w[Fruit Grapefruit], q.downcase, '', '').sort
|
||||
|
||||
# Case-sensitive match (-i)
|
||||
assert_equal [['Fruit', [[0, 5]]]],
|
||||
FZF::ExtendedFuzzyMatcher.new(0).match(%w[Fruit Grapefruit], q, '', '').sort
|
||||
|
||||
# Case-insensitive match (+i)
|
||||
assert_equal [["Fruit", [[0, 5]]], ["Grapefruit", [[5, 10]]]],
|
||||
FZF::ExtendedFuzzyMatcher.new(Regexp::IGNORECASE).
|
||||
match(%w[Fruit Grapefruit], q, '', '').sort
|
||||
end
|
||||
end
|
||||
|
||||
def test_extended_fuzzy_matcher
|
||||
matcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE
|
||||
list = %w[
|
||||
@@ -184,6 +276,11 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
assert_equal list.length, match.call('j', '').length
|
||||
assert_equal list.length - 1, match.call('^j', '').length
|
||||
|
||||
# ^ + $
|
||||
assert_equal 0, match.call('^juici$', '').length
|
||||
assert_equal 1, match.call('^juice$', '').length
|
||||
assert_equal 0, match.call('^.*$', '').length
|
||||
|
||||
# !
|
||||
assert_equal 0, match.call('!j', '').length
|
||||
|
||||
@@ -319,5 +416,14 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
assert_equal ["a", "b", "c", "\xFF", "d", "e", "f"],
|
||||
FZF::UConv.split("abc\xFFdef")
|
||||
end
|
||||
|
||||
# ^$ -> matches empty item
|
||||
def test_format_empty_item
|
||||
fzf = FZF.new []
|
||||
item = ['', [[0, 0]]]
|
||||
line, offsets = fzf.convert_item item
|
||||
tokens = fzf.format line, 80, offsets
|
||||
assert_equal [], tokens
|
||||
end
|
||||
end
|
||||
|
||||
|
Reference in New Issue
Block a user