mirror of
https://github.com/junegunn/fzf.git
synced 2025-08-01 12:42:01 -07:00
Compare commits
36 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
ae84d8c7a4 | ||
|
dbd627c38a | ||
|
d172c3ce03 | ||
|
9904f5354e | ||
|
f345bf7983 | ||
|
875f9b6534 | ||
|
871dfb709d | ||
|
19e24bd644 | ||
|
457a240457 | ||
|
bbf4567dd8 | ||
|
27d3b52843 | ||
|
dcb4694ec1 | ||
|
2fb8ae010f | ||
|
65ae6cabb5 | ||
|
86a66da04d | ||
|
d66b02b0cd | ||
|
b3182c3304 | ||
|
2dbca00bfb | ||
|
b22fd6de6d | ||
|
245ee42763 | ||
|
98bef4600c | ||
|
f5d53b94fe | ||
|
00c8a68430 | ||
|
c1be834ff9 | ||
|
2c0dc2f3b1 | ||
|
1c94fef720 | ||
|
b711d76b8e | ||
|
4396ab7548 | ||
|
2b8c2b9f2a | ||
|
426284c87e | ||
|
089691faaf | ||
|
301290663d | ||
|
1155da7e1c | ||
|
eca0a99fb4 | ||
|
96215c4619 | ||
|
b2d2be55ef |
66
README.md
66
README.md
@@ -32,18 +32,14 @@ The script will setup:
|
||||
|
||||
### Install as Vim plugin
|
||||
|
||||
You can use any Vim plugin manager to install fzf for Vim. If you don't use one,
|
||||
I recommend you try [vim-plug](https://github.com/junegunn/vim-plug).
|
||||
Once you have cloned the repository, add the following line to your .vimrc.
|
||||
|
||||
1. [Install vim-plug](https://github.com/junegunn/vim-plug#usage)
|
||||
2. Edit your .vimrc
|
||||
```vim
|
||||
set rtp+=~/.fzf
|
||||
```
|
||||
|
||||
call plug#begin()
|
||||
Plug 'junegunn/fzf'
|
||||
" ...
|
||||
call plug#end()
|
||||
|
||||
3. Run `:PlugInstall`
|
||||
Or you may use any Vim plugin manager, such as
|
||||
[vim-plug](https://github.com/junegunn/vim-plug).
|
||||
|
||||
Usage
|
||||
-----
|
||||
@@ -54,12 +50,16 @@ usage: fzf [options]
|
||||
Options
|
||||
-m, --multi Enable multi-select
|
||||
-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
|
||||
+c, --no-color Disable colors
|
||||
+2, --no-256 Disable 256-color
|
||||
--black Use black background
|
||||
--no-mouse Disable mouse
|
||||
|
||||
Environment variables
|
||||
@@ -104,8 +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.
|
||||
You can also use mouse. Double-click on an item to select it or shift-click (or
|
||||
ctrl-click) to select multiple items. Use mouse wheel to move the cursor up and
|
||||
down.
|
||||
|
||||
### Extended-search mode
|
||||
|
||||
@@ -123,6 +124,9 @@ such as: `^music .mp3$ sbtrkt !rmx`
|
||||
| `'wild` | Items that include `wild` | exact-match (quoted) |
|
||||
| `!'fire` | Items that do not include `fire` | inverse-exact-match |
|
||||
|
||||
If you don't need fuzzy matching and do not wish to "quote" every word, start
|
||||
fzf with `-e` or `--extended-exact` option.
|
||||
|
||||
Useful examples
|
||||
---------------
|
||||
|
||||
@@ -287,34 +291,20 @@ a very large list of files, fzf is significantly faster and it does not block.
|
||||
Tips
|
||||
----
|
||||
|
||||
### Faster startup with `--disable-gems` options
|
||||
### Rendering issues
|
||||
|
||||
If you're running Ruby 1.9 or above, you can improve the startup time with
|
||||
`--disable-gems` option to Ruby.
|
||||
If you have any rendering issues, check the followings:
|
||||
|
||||
- `time ruby ~/bin/fzf -h`
|
||||
- 0.077 sec
|
||||
- `time ruby --disable-gems ~/bin/fzf -h`
|
||||
- 0.025 sec
|
||||
|
||||
You can define fzf function with the option as follows:
|
||||
|
||||
```sh
|
||||
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
|
||||
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.
|
||||
1. Make sure `$TERM` is correctly set. fzf will use 256-color only if it
|
||||
contains `256` (e.g. `xterm-256color`)
|
||||
2. If you're on screen or tmux, `$TERM` should be either `screen` or
|
||||
`screen-256color`
|
||||
3. Some terminal emulators (e.g. mintty) have problem displaying default
|
||||
background color and make some text unable to read. In that case, try `--black`
|
||||
option. And if it solves your problem, I recommend including it in
|
||||
`FZF_DEFAULT_OPTS` for further convenience.
|
||||
4. If you still have problem, try `--no-256` option or even `--no-color`.
|
||||
5. Ruby 1.9 or above is required for correctly displaying unicode characters.
|
||||
|
||||
### Ranking algorithm
|
||||
|
||||
|
413
fzf
413
fzf
@@ -7,13 +7,13 @@
|
||||
# / __/ / /_/ __/
|
||||
# /_/ /___/_/ Fuzzy finder for your shell
|
||||
#
|
||||
# Version: 0.7.0 (January 30, 2014)
|
||||
# Version: 0.8.1 (March 9, 2014)
|
||||
#
|
||||
# Author: Junegunn Choi
|
||||
# URL: https://github.com/junegunn/fzf
|
||||
# License: MIT
|
||||
#
|
||||
# Copyright (c) 2013 Junegunn Choi
|
||||
# Copyright (c) 2014 Junegunn Choi
|
||||
#
|
||||
# MIT License
|
||||
#
|
||||
@@ -50,7 +50,7 @@ end
|
||||
|
||||
class FZF
|
||||
C = Curses
|
||||
attr_reader :rxflag, :sort, :color, :mouse, :multi, :query, :extended
|
||||
attr_reader :rxflag, :sort, :color, :black, :ansi256, :mouse, :multi, :query, :filter, :extended
|
||||
|
||||
class AtomicVar
|
||||
def initialize value
|
||||
@@ -63,10 +63,8 @@ class FZF
|
||||
end
|
||||
|
||||
def set value = nil
|
||||
if block_given?
|
||||
@mutex.synchronize { @value = yield @value }
|
||||
else
|
||||
@mutex.synchronize { @value = value }
|
||||
@mutex.synchronize do
|
||||
@value = block_given? ? yield(@value) : value
|
||||
end
|
||||
end
|
||||
|
||||
@@ -79,9 +77,12 @@ class FZF
|
||||
@rxflag = nil
|
||||
@sort = ENV.fetch('FZF_DEFAULT_SORT', 1000).to_i
|
||||
@color = true
|
||||
@ansi256 = true
|
||||
@black = false
|
||||
@multi = false
|
||||
@extended = false
|
||||
@mouse = true
|
||||
@extended = nil
|
||||
@filter = nil
|
||||
|
||||
argv =
|
||||
if opts = ENV['FZF_DEFAULT_OPTS']
|
||||
@@ -96,12 +97,17 @@ class FZF
|
||||
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 '-x', '--extended' then @extended = :fuzzy
|
||||
when '+x', '--no-extended' then @extended = nil
|
||||
when '-i' then @rxflag = Regexp::IGNORECASE
|
||||
when '+i' then @rxflag = 0
|
||||
when '-c', '--color' then @color = true
|
||||
when '+c', '--no-color' then @color = false
|
||||
when '-2', '--256' then @ansi256 = true
|
||||
when '+2', '--no-256' then @ansi256 = false
|
||||
when '--black' then @black = true
|
||||
when '--no-black' then @black = false
|
||||
when '--mouse' then @mouse = true
|
||||
when '--no-mouse' then @mouse = false
|
||||
when '+s', '--no-sort' then @sort = nil
|
||||
when '-q', '--query'
|
||||
@@ -109,12 +115,19 @@ class FZF
|
||||
@query = AtomicVar.new query.dup
|
||||
when /^-q(.*)$/, /^--query=(.*)$/
|
||||
@query = AtomicVar.new($1)
|
||||
when '-f', '--filter'
|
||||
usage 1, 'query string required' unless query = argv.shift
|
||||
@filter = query
|
||||
when /^-f(.*)$/, /^--filter=(.*)$/
|
||||
@filter = $1
|
||||
when '-s', '--sort'
|
||||
usage 1, 'sort size required' unless sort = argv.shift
|
||||
usage 1, 'invalid sort size' unless sort =~ /^[0-9]+$/
|
||||
@sort = sort.to_i
|
||||
when /^-s([0-9]+)$/, /^--sort=([0-9]+)$/
|
||||
@sort = $1.to_i
|
||||
when '-e', '--extended-exact' then @extended = :exact
|
||||
when '+e', '--no-extended-exact' then @extended = nil
|
||||
else
|
||||
usage 1, "illegal option: #{o}"
|
||||
end
|
||||
@@ -126,27 +139,52 @@ class FZF
|
||||
@events = {}
|
||||
@new = []
|
||||
@queue = Queue.new
|
||||
@query ||= AtomicVar.new('')
|
||||
@cursor_x = AtomicVar.new(@query.length)
|
||||
@matches = AtomicVar.new([])
|
||||
@count = AtomicVar.new(0)
|
||||
@vcursor = AtomicVar.new(0)
|
||||
@vcursors = AtomicVar.new(Set.new)
|
||||
@spinner = AtomicVar.new('-\|/-\|/'.split(//))
|
||||
@selects = AtomicVar.new({}) # ordered >= 1.9
|
||||
@main = Thread.current
|
||||
@stdout = $stdout.clone
|
||||
@plcount = 0
|
||||
@pending = nil
|
||||
|
||||
unless @filter
|
||||
@query ||= AtomicVar.new('')
|
||||
@cursor_x = AtomicVar.new(@query.length)
|
||||
@matches = AtomicVar.new([])
|
||||
@count = AtomicVar.new(0)
|
||||
@vcursor = AtomicVar.new(0)
|
||||
@vcursors = AtomicVar.new(Set.new)
|
||||
@spinner = AtomicVar.new('-\|/-\|/'.split(//))
|
||||
@selects = AtomicVar.new({}) # ordered >= 1.9
|
||||
@main = Thread.current
|
||||
@plcount = 0
|
||||
end
|
||||
end
|
||||
|
||||
def start
|
||||
$stdout.reopen($stderr)
|
||||
if @filter
|
||||
start_reader(false).join
|
||||
filter_list @new
|
||||
else
|
||||
@stdout = $stdout.clone
|
||||
$stdout.reopen($stderr)
|
||||
|
||||
init_screen
|
||||
start_reader
|
||||
start_renderer
|
||||
start_search
|
||||
start_loop
|
||||
start_reader true
|
||||
init_screen
|
||||
start_renderer
|
||||
start_search
|
||||
start_loop
|
||||
end
|
||||
end
|
||||
|
||||
def filter_list list
|
||||
matches = get_matcher.match(list, @filter, '', '')
|
||||
if @sort && matches.length <= @sort
|
||||
matches = sort_by_rank(matches)
|
||||
end
|
||||
matches.each { |m| puts m.first }
|
||||
end
|
||||
|
||||
def get_matcher
|
||||
if @extended
|
||||
ExtendedFuzzyMatcher.new @rxflag, @extended
|
||||
else
|
||||
FuzzyMatcher.new @rxflag
|
||||
end
|
||||
end
|
||||
|
||||
def version
|
||||
@@ -167,12 +205,16 @@ class FZF
|
||||
Options
|
||||
-m, --multi Enable multi-select
|
||||
-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
|
||||
+c, --no-color Disable colors
|
||||
+2, --no-256 Disable 256-color
|
||||
--black Use black background
|
||||
--no-mouse Disable mouse
|
||||
|
||||
Environment variables
|
||||
@@ -429,7 +471,9 @@ class FZF
|
||||
}
|
||||
end
|
||||
|
||||
if RUBY_VERSION.split('.').map { |e| e.rjust(3, '0') }.join > '001009'
|
||||
AFTER_1_9 = RUBY_VERSION.split('.').map { |e| e.rjust(3, '0') }.join >= '001009'
|
||||
|
||||
if AFTER_1_9
|
||||
@@wrx = Regexp.new '\p{Han}|\p{Katakana}|\p{Hiragana}|\p{Hangul}'
|
||||
def width str
|
||||
str.gsub(@@wrx, ' ').length rescue str.length
|
||||
@@ -474,25 +518,20 @@ 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.mousemask C::ALL_MOUSE_EVENTS if @mouse
|
||||
C.start_color
|
||||
dbg =
|
||||
if C.respond_to?(:use_default_colors)
|
||||
if !@black && C.respond_to?(:use_default_colors)
|
||||
C.use_default_colors
|
||||
-1
|
||||
else
|
||||
C::COLOR_BLACK
|
||||
end
|
||||
C.raw
|
||||
C.nonl
|
||||
C.noecho
|
||||
|
||||
if @color
|
||||
if C.can_change_color? && ENV['TERM'].to_s =~ /256/
|
||||
if @ansi256 && ENV['TERM'].to_s =~ /256/
|
||||
C.init_pair 1, 110, dbg
|
||||
C.init_pair 2, 108, dbg
|
||||
C.init_pair 3, 254, 236
|
||||
@@ -531,9 +570,11 @@ class FZF
|
||||
end | (bold ? C::A_BOLD : 0)
|
||||
end
|
||||
end
|
||||
|
||||
C.refresh
|
||||
end
|
||||
|
||||
def start_reader
|
||||
def start_reader curses
|
||||
stream =
|
||||
if @source.tty?
|
||||
if default_command = ENV['FZF_DEFAULT_COMMAND']
|
||||
@@ -544,7 +585,6 @@ class FZF
|
||||
exit 1
|
||||
end
|
||||
else
|
||||
$stdin.reopen IO.open(IO.sysopen('/dev/tty'), 'r')
|
||||
@source
|
||||
end
|
||||
|
||||
@@ -553,12 +593,12 @@ class FZF
|
||||
emit(:new) { @new << line.chomp }
|
||||
end
|
||||
emit(:loaded) { true }
|
||||
@spinner.clear
|
||||
@spinner.clear if curses
|
||||
end
|
||||
end
|
||||
|
||||
def start_search
|
||||
matcher = (@extended ? ExtendedFuzzyMatcher : FuzzyMatcher).new @rxflag
|
||||
matcher = get_matcher
|
||||
searcher = Thread.new {
|
||||
lists = []
|
||||
events = {}
|
||||
@@ -618,11 +658,11 @@ class FZF
|
||||
matcher.match(list, q, q[0, cx], q[cx..-1]))
|
||||
end
|
||||
next if skip
|
||||
matches = fcache[q] = @sort ? found : found.reverse
|
||||
end
|
||||
|
||||
if !empty && @sort && matches.length <= @sort
|
||||
matches = sort_by_rank(matches)
|
||||
matches = @sort ? found : found.reverse
|
||||
if !empty && @sort && matches.length <= @sort
|
||||
matches = sort_by_rank(matches)
|
||||
end
|
||||
fcache[q] = matches
|
||||
end
|
||||
|
||||
# Atomic update
|
||||
@@ -683,7 +723,6 @@ class FZF
|
||||
def start_renderer
|
||||
Thread.new do
|
||||
begin
|
||||
refresh
|
||||
while blk = @queue.shift
|
||||
blk.call
|
||||
refresh
|
||||
@@ -718,8 +757,148 @@ class FZF
|
||||
end
|
||||
end
|
||||
|
||||
def test_mouse st, *states
|
||||
states.any? { |s| s & st > 0 }
|
||||
def read_nb chars = 1, default = nil, tries = 10
|
||||
tries.times do |_|
|
||||
begin
|
||||
return @tty.read_nonblock(chars).ord
|
||||
rescue Exception
|
||||
sleep 0.01
|
||||
end
|
||||
end
|
||||
default
|
||||
end
|
||||
|
||||
def read_nbs
|
||||
ords = []
|
||||
while ord = read_nb
|
||||
ords << ord
|
||||
end
|
||||
ords
|
||||
end
|
||||
|
||||
def get_mouse
|
||||
case ord = read_nb
|
||||
when 32, 36, 40, 48, # mouse-down / shift / cmd / ctrl
|
||||
35, 39, 43, 51 # mouse-up / shift / cmd / ctrl
|
||||
x = read_nb - 33
|
||||
y = read_nb - 33
|
||||
{ :event => (ord % 2 == 0 ? :click : :release),
|
||||
:x => x, :y => y, :shift => ord >= 36 }
|
||||
when 96, 100, 104, 112, # scroll-up / shift / cmd / ctrl
|
||||
97, 101, 105, 113 # scroll-down / shift / cmd / ctrl
|
||||
read_nb(2)
|
||||
{ :event => :scroll, :diff => (ord % 2 == 0 ? -1 : 1), :shift => ord >= 100 }
|
||||
else
|
||||
# e.g. 40, 43, 104, 105
|
||||
read_nb(2)
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def get_input actions
|
||||
@tty ||= IO.open(IO.sysopen('/dev/tty'), 'r')
|
||||
|
||||
if pending = @pending
|
||||
@pending = nil
|
||||
return pending
|
||||
end
|
||||
|
||||
str = ''
|
||||
while true
|
||||
ord =
|
||||
if str.empty?
|
||||
@tty.getc.ord
|
||||
else
|
||||
begin
|
||||
ord = @tty.read_nonblock(1).ord
|
||||
if (nb = num_unicode_bytes(ord)) > 1
|
||||
ords = [ord]
|
||||
(nb - 1).times do |_|
|
||||
ords << @tty.read_nonblock(1).ord
|
||||
end
|
||||
# UTF-8 TODO Ruby 1.8
|
||||
ords.pack('C*').force_encoding('UTF-8')
|
||||
else
|
||||
ord
|
||||
end
|
||||
rescue Exception
|
||||
return str
|
||||
end
|
||||
end
|
||||
|
||||
ord =
|
||||
case read_nb(1, :esc)
|
||||
when 91
|
||||
case read_nb(1, nil)
|
||||
when 68 then ctrl(:b)
|
||||
when 67 then ctrl(:f)
|
||||
when 66 then ctrl(:j)
|
||||
when 65 then ctrl(:k)
|
||||
when 90 then :stab
|
||||
when 50 then read_nb; :ins
|
||||
when 51 then read_nb; :del
|
||||
when 53 then read_nb; :pgup
|
||||
when 54 then read_nb; :pgdn
|
||||
when 49
|
||||
case read_nbs
|
||||
when [59, 50, 68] then ctrl(:a)
|
||||
when [59, 50, 67] then ctrl(:e)
|
||||
when [126] then ctrl(:a)
|
||||
end
|
||||
when 52 then read_nb; ctrl(:e)
|
||||
when 72 then ctrl(:a)
|
||||
when 70 then ctrl(:e)
|
||||
when 77
|
||||
get_mouse
|
||||
end
|
||||
when 'b', 98 then :alt_b
|
||||
when 'f', 102 then :alt_f
|
||||
when :esc then :esc
|
||||
else next
|
||||
end if ord == 27
|
||||
|
||||
return ord if ord.nil? || ord.is_a?(Hash)
|
||||
|
||||
if actions.has_key?(ord)
|
||||
if str.empty?
|
||||
return ord
|
||||
else
|
||||
@pending = ord
|
||||
return str
|
||||
end
|
||||
else
|
||||
unless ord.is_a? String
|
||||
ord = [ord].pack('U*')
|
||||
end
|
||||
str << ord if ord =~ /[[:print:]]/
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class MouseEvent
|
||||
DOUBLE_CLICK_INTERVAL = 0.5
|
||||
|
||||
attr_reader :v
|
||||
|
||||
def initialize v = nil
|
||||
@c = 0
|
||||
@v = v
|
||||
@t = Time.at 0
|
||||
end
|
||||
|
||||
def v= v
|
||||
@c = (@v == v && within?) ? @c + 1 : 0
|
||||
@v = v
|
||||
@t = Time.now
|
||||
end
|
||||
|
||||
def double? v
|
||||
@c == 1 && @v == v && within?
|
||||
end
|
||||
|
||||
def within?
|
||||
(Time.now - @t) < DOUBLE_CLICK_INTERVAL
|
||||
end
|
||||
end
|
||||
|
||||
def start_loop
|
||||
@@ -755,103 +934,75 @@ class FZF
|
||||
else
|
||||
@selects[sel] = 1
|
||||
end
|
||||
vselect { |v|
|
||||
v + case o
|
||||
when :select then 0
|
||||
when C::KEY_BTAB then 1
|
||||
else -1
|
||||
end
|
||||
}
|
||||
vselect { |v| v + case o
|
||||
when :stab then 1
|
||||
when :sclick then 0
|
||||
else -1
|
||||
end }
|
||||
end
|
||||
},
|
||||
ctrl(:b) => proc { cursor = [0, cursor - 1].max; nil },
|
||||
ctrl(:f) => proc { cursor = [input.length, cursor + 1].min; nil },
|
||||
ctrl(:l) => proc { render { C.clear; C.refresh }; update_list true },
|
||||
:del => proc { input[cursor] = '' if input.length > cursor },
|
||||
:pgup => proc { vselect { |_| max_items } },
|
||||
:pgdn => proc { vselect { |_| 0 } },
|
||||
:alt_b => proc { backword.call; nil },
|
||||
:alt_f => proc {
|
||||
cursor += (input[cursor..-1].index(/(\S\s)|(.$)/) || -1) + 1
|
||||
nil
|
||||
},
|
||||
}
|
||||
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(:p)] = actions[ctrl(:k)]
|
||||
actions[ctrl(:n)] = actions[ctrl(:j)]
|
||||
actions[:stab] = actions[ctrl(:i)]
|
||||
actions[127] = actions[ctrl(:h)]
|
||||
actions[ctrl(:q)] = actions[ctrl(:g)] = actions[ctrl(:c)] = actions[:esc]
|
||||
|
||||
emit(:key) { [@query.get, cursor] } unless @query.empty?
|
||||
pmv = nil
|
||||
mouse = MouseEvent.new
|
||||
while true
|
||||
@cursor_x.set cursor
|
||||
render { print_input }
|
||||
|
||||
C.stdscr.timeout = -1
|
||||
ch = C.getch
|
||||
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 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)
|
||||
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
|
||||
pmv = tv
|
||||
end
|
||||
tv
|
||||
}
|
||||
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
|
||||
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
|
||||
}).call(key)
|
||||
|
||||
# Dispatch key event
|
||||
emit(:key) { [@query.set(input.dup), cursor] } if upd
|
||||
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(ch)
|
||||
|
||||
# Dispatch key event
|
||||
emit(:key) { [@query.set(input.dup), cursor] } if upd
|
||||
end
|
||||
ensure
|
||||
C.close_screen
|
||||
@@ -921,9 +1072,10 @@ class FZF
|
||||
end
|
||||
|
||||
class ExtendedFuzzyMatcher < FuzzyMatcher
|
||||
def initialize rxflag
|
||||
super
|
||||
def initialize rxflag, mode = :fuzzy
|
||||
super rxflag
|
||||
@regexps = {}
|
||||
@mode = mode
|
||||
end
|
||||
|
||||
def empty? q
|
||||
@@ -946,8 +1098,11 @@ class FZF
|
||||
when /^\^(.*)\$$/
|
||||
Regexp.new('^' << sanitize(Regexp.escape($1)) << '$', rxflag_for(w))
|
||||
when /^'/
|
||||
w.length > 1 ?
|
||||
Regexp.new(sanitize(Regexp.escape(w[1..-1])), rxflag_for(w)) : nil
|
||||
if @mode == :fuzzy && w.length > 1
|
||||
exact_regex w[1..-1]
|
||||
elsif @mode == :exact
|
||||
exact_regex w
|
||||
end
|
||||
when /^\^/
|
||||
w.length > 1 ?
|
||||
Regexp.new('^' << sanitize(Regexp.escape(w[1..-1])), rxflag_for(w)) : nil
|
||||
@@ -955,11 +1110,15 @@ class FZF
|
||||
w.length > 1 ?
|
||||
Regexp.new(sanitize(Regexp.escape(w[0..-2])) << '$', rxflag_for(w)) : nil
|
||||
else
|
||||
fuzzy_regex w
|
||||
@mode == :fuzzy ? fuzzy_regex(w) : exact_regex(w)
|
||||
end, invert ]
|
||||
}.select { |pair| pair.first }
|
||||
end
|
||||
|
||||
def exact_regex w
|
||||
Regexp.new(sanitize(Regexp.escape(w)), rxflag_for(w))
|
||||
end
|
||||
|
||||
def match list, q, prefix, suffix
|
||||
regexps = parse q
|
||||
# Look for prefix cache
|
||||
|
@@ -1,7 +1,7 @@
|
||||
# coding: utf-8
|
||||
Gem::Specification.new do |spec|
|
||||
spec.name = 'fzf'
|
||||
spec.version = '0.7.0'
|
||||
spec.version = '0.8.1'
|
||||
spec.authors = ['Junegunn Choi']
|
||||
spec.email = ['junegunn.c@gmail.com']
|
||||
spec.description = %q{Fuzzy finder for your shell}
|
||||
|
34
install
34
install
@@ -29,7 +29,7 @@ if [ $? -eq 0 ]; then
|
||||
else
|
||||
echo "Not found"
|
||||
echo "Installing 'curses' gem ... "
|
||||
/usr/bin/env gem install curses
|
||||
/usr/bin/env gem install curses -v 1.0.0
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Failed to install 'curses' gem."
|
||||
echo "Try installing it as root: sudo gem install curses"
|
||||
@@ -95,23 +95,40 @@ EOF
|
||||
cat >> $src << "EOF"
|
||||
# Key bindings
|
||||
# ------------
|
||||
# Required to refresh the prompt after fzf
|
||||
bind '"\er": redraw-current-line'
|
||||
if [[ $- =~ i ]]; then
|
||||
|
||||
# CTRL-T - Paste the selected file path into the command line
|
||||
__fsel() {
|
||||
find * -path '*/\.*' -prune \
|
||||
-o -type f -print \
|
||||
-o -type d -print \
|
||||
-o -type l -print 2> /dev/null | fzf -m | while read item; do
|
||||
printf '%q ' "$item"
|
||||
done
|
||||
echo
|
||||
}
|
||||
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"'
|
||||
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"'
|
||||
|
||||
# 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"'
|
||||
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"'
|
||||
|
||||
# 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"'
|
||||
fi
|
||||
|
||||
fi
|
||||
EOF
|
||||
else
|
||||
cat >> $src << "EOF"
|
||||
@@ -125,6 +142,7 @@ fzf-file-widget() {
|
||||
FILES=($(
|
||||
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
|
||||
@@ -145,7 +163,7 @@ 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]* *//")
|
||||
LBUFFER=$(fc -l 1 | fzf +s | sed "s/ *[0-9]* *//")
|
||||
zle redisplay
|
||||
}
|
||||
zle -N fzf-history-widget
|
||||
|
@@ -7,7 +7,6 @@ ENV['FZF_EXECUTABLE'] = '0'
|
||||
load 'fzf'
|
||||
|
||||
class TestFZF < MiniTest::Unit::TestCase
|
||||
|
||||
def setup
|
||||
ENV.delete 'FZF_DEFAULT_SORT'
|
||||
ENV.delete 'FZF_DEFAULT_OPTS'
|
||||
@@ -29,56 +28,73 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
fzf = FZF.new []
|
||||
assert_equal 20000, fzf.sort
|
||||
|
||||
ENV['FZF_DEFAULT_OPTS'] = '-x -m -s 10000 -q " hello world " +c --no-mouse'
|
||||
ENV['FZF_DEFAULT_OPTS'] = '-x -m -s 10000 -q " hello world " +c +2 --no-mouse -f "goodbye world" --black'
|
||||
fzf = FZF.new []
|
||||
assert_equal 10000, fzf.sort
|
||||
assert_equal ' hello world ',
|
||||
fzf.query.get
|
||||
assert_equal true, fzf.extended
|
||||
assert_equal 'goodbye world',
|
||||
fzf.filter
|
||||
assert_equal :fuzzy, fzf.extended
|
||||
assert_equal true, fzf.multi
|
||||
assert_equal false, fzf.color
|
||||
assert_equal false, fzf.ansi256
|
||||
assert_equal true, fzf.black
|
||||
assert_equal false, fzf.mouse
|
||||
end
|
||||
|
||||
def test_option_parser
|
||||
# Long opts
|
||||
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello --extended --no-mouse]
|
||||
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello
|
||||
--filter=howdy --extended-exact --no-mouse --no-256]
|
||||
assert_equal 2000, fzf.sort
|
||||
assert_equal true, fzf.multi
|
||||
assert_equal false, fzf.color
|
||||
assert_equal false, fzf.ansi256
|
||||
assert_equal false, fzf.black
|
||||
assert_equal false, fzf.mouse
|
||||
assert_equal 0, fzf.rxflag
|
||||
assert_equal 'hello', fzf.query.get
|
||||
assert_equal true, fzf.extended
|
||||
assert_equal 'howdy', fzf.filter
|
||||
assert_equal :exact, fzf.extended
|
||||
|
||||
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello
|
||||
--no-sort -i --color --no-multi]
|
||||
--filter a --filter b --no-256 --black
|
||||
--no-sort -i --color --no-multi --256]
|
||||
assert_equal nil, fzf.sort
|
||||
assert_equal false, fzf.multi
|
||||
assert_equal true, fzf.color
|
||||
assert_equal true, fzf.ansi256
|
||||
assert_equal true, fzf.black
|
||||
assert_equal true, fzf.mouse
|
||||
assert_equal 1, fzf.rxflag
|
||||
assert_equal 'b', fzf.filter
|
||||
assert_equal 'hello', fzf.query.get
|
||||
assert_equal false, fzf.extended
|
||||
assert_equal nil, fzf.extended
|
||||
|
||||
# Short opts
|
||||
fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x]
|
||||
fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x -fhowdy +2]
|
||||
assert_equal 2000, fzf.sort
|
||||
assert_equal true, fzf.multi
|
||||
assert_equal false, fzf.color
|
||||
assert_equal false, fzf.ansi256
|
||||
assert_equal 0, fzf.rxflag
|
||||
assert_equal 'hello', fzf.query.get
|
||||
assert_equal true, fzf.extended
|
||||
assert_equal 'howdy', fzf.filter
|
||||
assert_equal :fuzzy, fzf.extended
|
||||
|
||||
# Left-to-right
|
||||
fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x
|
||||
-s 3000 -c +m -i -q world +x]
|
||||
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]
|
||||
assert_equal 3000, fzf.sort
|
||||
assert_equal false, fzf.multi
|
||||
assert_equal true, fzf.color
|
||||
assert_equal true, fzf.ansi256
|
||||
assert_equal false, fzf.black
|
||||
assert_equal 1, fzf.rxflag
|
||||
assert_equal 'world', fzf.query.get
|
||||
assert_equal false, fzf.extended
|
||||
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
|
||||
@@ -370,6 +386,25 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
FZF.new([]).sort_by_rank(xmatcher.match(list, '01 __', '', '')))
|
||||
end
|
||||
|
||||
def test_extended_exact_mode
|
||||
exact = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE, :exact
|
||||
fuzzy = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE, :fuzzy
|
||||
list = %w[
|
||||
extended-exact-mode-not-fuzzy
|
||||
extended'-fuzzy-mode
|
||||
]
|
||||
assert_equal 2, fuzzy.match(list, 'extended', '', '').length
|
||||
assert_equal 2, fuzzy.match(list, 'mode extended', '', '').length
|
||||
assert_equal 2, fuzzy.match(list, 'xtndd', '', '').length
|
||||
assert_equal 2, fuzzy.match(list, "'-fuzzy", '', '').length
|
||||
|
||||
assert_equal 2, exact.match(list, 'extended', '', '').length
|
||||
assert_equal 2, exact.match(list, 'mode extended', '', '').length
|
||||
assert_equal 0, exact.match(list, 'xtndd', '', '').length
|
||||
assert_equal 1, exact.match(list, "'-fuzzy", '', '').length
|
||||
assert_equal 2, exact.match(list, "-fuzzy", '', '').length
|
||||
end
|
||||
|
||||
if RUBY_PLATFORM =~ /darwin/
|
||||
NFD = '한글'
|
||||
def test_nfc
|
||||
@@ -425,5 +460,21 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
tokens = fzf.format line, 80, offsets
|
||||
assert_equal [], tokens
|
||||
end
|
||||
|
||||
def test_mouse_event
|
||||
interval = FZF::MouseEvent::DOUBLE_CLICK_INTERVAL
|
||||
me = FZF::MouseEvent.new nil
|
||||
me.v = 10
|
||||
assert_equal false, me.double?(10)
|
||||
assert_equal false, me.double?(20)
|
||||
me.v = 20
|
||||
assert_equal false, me.double?(10)
|
||||
assert_equal false, me.double?(20)
|
||||
me.v = 20
|
||||
assert_equal false, me.double?(10)
|
||||
assert_equal true, me.double?(20)
|
||||
sleep interval
|
||||
assert_equal false, me.double?(20)
|
||||
end
|
||||
end
|
||||
|
||||
|
Reference in New Issue
Block a user