m/fzf
1
0
mirror of https://github.com/junegunn/fzf.git synced 2025-11-13 22:03:47 -05:00

Compare commits

..

22 Commits
0.8.2 ... 0.8.3

Author SHA1 Message Date
Junegunn Choi
d82e38adc1 0.8.3 2014-04-05 12:56:10 +09:00
Junegunn Choi
af677e7e35 Vim plugin: do not enable tmux-integration if version < 1.7 2014-04-04 12:43:29 +09:00
Junegunn Choi
6ad38bdad3 Update example: suppress error message from fc on bash (#37)
`'fc' -l 1` generated an error message on bash
2014-04-04 10:26:38 +09:00
Junegunn Choi
8b80136a87 Merge pull request #37 from wellle/zsh-history
Feed all zsh history into fzf
2014-04-03 23:14:28 +09:00
Christian Wellenbrock
97de919152 Feed all zsh history into fzf 2014-04-03 16:11:51 +02:00
Junegunn Choi
0eafa725b9 Fix test code indentation 2014-04-03 14:53:47 +09:00
Junegunn Choi
fa212efe5f Fix ranking when multiple regions overlap
e.g.
  Match region #1: [-----------]
  Match region #2:       [---]
  Match region #3:         [------]
2014-04-03 14:51:01 +09:00
Junegunn Choi
a9056ce90c Add gif showing tmux integration 2014-04-03 01:29:21 +09:00
Junegunn Choi
16682a3f92 Update fe example as the exit status from -0 has changed (#36) 2014-04-03 01:20:22 +09:00
Junegunn Choi
02c01c81a0 Improve -0 and -1 as suggested in #36
- Make -0 and -1 work without -q
- Change exit status to 0 when exiting with -0
2014-04-03 01:06:40 +09:00
Junegunn Choi
22d3929ae3 Implement --select-1 and --exit-0 (#27, #36) 2014-04-02 21:41:57 +09:00
Junegunn Choi
ab9fbf1967 Allow --nth option to take multiple indexes (comma-separated) 2014-04-02 01:49:07 +09:00
Junegunn Choi
608ec2b806 set -o nonomatch for zsh (#34)
Avoid error message in an empty directory
2014-04-01 21:39:40 +09:00
Junegunn Choi
e5ae4f0ef6 Do not load interactive parts when not required (#34) 2014-04-01 20:55:26 +09:00
Junegunn Choi
67ba87d390 Avoid CTRL-T error when default shell != zsh (#34) 2014-04-01 20:49:54 +09:00
Junegunn Choi
77d45cb173 Avoid starting interactive bash (#34) 2014-04-01 20:48:15 +09:00
Junegunn Choi
d83febea46 Merge pull request #35 from junegunn/fix-tmux-on-linux
Fix #34: tmux integration on Linux
2014-04-01 17:58:19 +09:00
Junegunn Choi
546a315884 Fix #34: tmux integration on Linux 2014-04-01 08:55:16 +00:00
Junegunn Choi
af616457e3 Use -p option of split-window instead of manual calculation 2014-03-31 13:48:53 +09:00
Junegunn Choi
1a100a2919 No need for screenrow() 2014-03-31 13:36:58 +09:00
Junegunn Choi
a85bb93b69 Fix use of screenrow when tmux height is given in % 2014-03-31 13:22:52 +09:00
Junegunn Choi
057eda060c Installation on other shells 2014-03-31 10:15:56 +09:00
6 changed files with 362 additions and 169 deletions

View File

@@ -5,6 +5,8 @@ fzf is a general-purpose fuzzy finder for your shell.
![](https://raw.github.com/junegunn/i/master/fzf.gif)
([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.
@@ -30,6 +32,9 @@ The script will setup:
- 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,22 +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.
-n, --nth=[-]N Match only in the N-th token of the item
-d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)
-s, --sort=MAX Maximum number of matched items to sort (default: 1000)
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
-i Case-insensitive match (default: smart-case match)
+i Case-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
@@ -133,10 +147,13 @@ Useful examples
---------------
```sh
# vimf - Open selected file in Vim
vimf() {
# 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") && vim "$file"
file=$(fzf --query="$1" --select-1 --exit-0)
[ -n "$file" ] && ${EDITOR:-vim} "$file"
}
# fd - cd to selected directory
@@ -155,7 +172,7 @@ fda() {
# 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
@@ -183,31 +200,11 @@ fco() {
ftags() {
local line
[ -e tags ] &&
line=$(grep -v "^!" tags | cut -f1-3 | cut -c1-80 | fzf --nth=1) &&
$EDITOR $(cut -f2 <<< "$line")
}
# fq1 [QUERY]
# - Immediately select the file when there's only one match.
# If not, start the fuzzy finder as usual.
fq1() {
local lines
lines=$(fzf --filter="$1" --no-sort)
if [ -z "$lines" ]; then
return 1
elif [ $(wc -l <<< "$lines") -eq 1 ]; then
echo "$lines"
else
echo "$lines" | fzf --query="$1"
fi
}
# fe [QUERY]
# - Open the selected file with the default editor
# (Bypass fuzzy finder when there's only one match)
fe() {
local file
file=$(fq1 "$1") && ${EDITOR:-vim} "$file"
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")"
}
```

188
fzf
View File

@@ -7,7 +7,7 @@
# / __/ / /_/ __/
# /_/ /___/_/ Fuzzy finder for your shell
#
# Version: 0.8.2 (March 30, 2014)
# Version: 0.8.3 (April 3, 2014)
#
# Author: Junegunn Choi
# URL: https://github.com/junegunn/fzf
@@ -51,7 +51,7 @@ end
class FZF
C = Curses
attr_reader :rxflag, :sort, :nth, :color, :black, :ansi256,
:mouse, :multi, :query, :filter, :extended
:mouse, :multi, :query, :select1, :exit0, :filter, :extended
class AtomicVar
def initialize value
@@ -83,6 +83,8 @@ class FZF
@multi = false
@mouse = true
@extended = nil
@select1 = false
@exit0 = false
@filter = nil
@nth = nil
@delim = nil
@@ -96,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
@@ -113,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
@@ -126,9 +132,9 @@ class FZF
when '-n', '--nth'
usage 1, 'field number required' unless nth = argv.shift
usage 1, 'invalid field number' if nth.to_i == 0
@nth = nth.to_i
when /^-n(-?[1-9][0-9]*)$/, /^--nth=(-?[1-9][0-9]*)$/
@nth = $1.to_i
@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
@@ -169,6 +175,14 @@ 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})|(?:.+?$)"
@@ -176,67 +190,114 @@ class FZF
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, @nth, @delim
else
FuzzyMatcher.new @rxflag, @nth, @delim
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.
-n, --nth=[-]N Match only in the N-th token of the item
-d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)
-s, --sort=MAX Maximum number of matched items to sort (default: 1000)
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
-i Case-insensitive match (default: smart-case match)
+i Case-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
@@ -477,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
@@ -538,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
@@ -595,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']
@@ -614,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 = {}
@@ -659,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]
@@ -681,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
@@ -690,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
@@ -698,7 +750,7 @@ class FZF
rescue Exception => e
@main.raise e
end
}
end
end
def pick
@@ -742,6 +794,8 @@ class FZF
end
def start_renderer
init_screen
Thread.new do
begin
while blk = @queue.shift
@@ -1021,7 +1075,6 @@ 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?
while true
@cursor_x.set cursor
render { print_input }
@@ -1059,7 +1112,7 @@ class FZF
end
def initialize nth, delim
@nth = nth && (nth > 0 ? nth - 1 : nth)
@nth = nth && nth.map { |n| n > 0 ? n - 1 : n }
@delim = delim
@tokens_cache = {}
end
@@ -1080,11 +1133,14 @@ class FZF
if @nth
prefix_length, tokens = tokenize str
if (token = tokens[@nth]) && (md = token.match(pat) rescue nil)
prefix_length += (tokens[0...@nth] || []).join.length
offset = md.offset(0).map { |o| o + prefix_length }
MatchData.new offset
@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

View File

@@ -1,7 +1,7 @@
# coding: utf-8
Gem::Specification.new do |spec|
spec.name = 'fzf'
spec.version = '0.8.2'
spec.version = '0.8.3'
spec.authors = ['Junegunn Choi']
spec.email = ['junegunn.c@gmail.com']
spec.description = %q{Fuzzy finder for your shell}

44
install
View File

@@ -89,7 +89,7 @@ export -f fzf > /dev/null
# Auto-completion
# ---------------
$fzf_completion
[[ \$- =~ i ]] && $fzf_completion
EOF
@@ -98,9 +98,7 @@ EOF
cat >> $src << "EOFZF"
# Key bindings
# ------------
if [[ $- =~ i ]]; then
read -r -d '' __fsel <<'EOF'
__fsel() {
find * -path '*/\.*' -prune \
-o -type f -print \
-o -type d -print \
@@ -108,21 +106,19 @@ read -r -d '' __fsel <<'EOF'
printf '%q ' "$item"
done
echo
EOF
__fsel() {
eval "$__fsel"
}
if [[ $- =~ i ]]; then
__fsel_tmux() {
local height lines
local height
height=${FZF_TMUX_HEIGHT:-40%}
lines=${LINES:-40}
if [[ $height =~ %$ ]]; then
height=${height:0:${#height}-1}
height=$(( height * lines / 100 ))
height="-p ${height%\%}"
else
height="-l $height"
fi
tmux split-window -l $height "tmux send-keys -t $TMUX_PANE \"\$($__fsel)\""
tmux split-window $height "bash -c 'source ~/.fzf.bash; tmux send-keys -t $TMUX_PANE \"\$(__fsel)\"'"
}
__fcd() {
@@ -177,7 +173,8 @@ EOFZF
# Key bindings
# ------------
# CTRL-T - Paste the selected file path(s) into the command line
read -r -d '' __fsel <<'EOF'
__fsel() {
set -o nonomatch
find * -path '*/\.*' -prune \
-o -type f -print \
-o -type d -print \
@@ -185,22 +182,24 @@ read -r -d '' __fsel <<'EOF'
printf '%q ' "$item"
done
echo
EOF
}
if [[ $- =~ i ]]; then
if [ -n "$TMUX_PANE" -a ${FZF_TMUX:-1} -ne 0 -a ${LINES:-40} -gt 15 ]; then
fzf-file-widget() {
local height lines
local height
height=${FZF_TMUX_HEIGHT:-40%}
lines=${LINES:-40}
if [[ $height =~ %$ ]]; then
height=${height:0:${#height}-1}
height=$(( height * lines / 100 ))
height="-p ${height%\%}"
else
height="-l $height"
fi
tmux split-window -l $height "tmux send-keys -t $TMUX_PANE \"\$($__fsel)\""
tmux split-window $height "zsh -c 'source ~/.fzf.zsh; tmux send-keys -t $TMUX_PANE \"\$(__fsel)\"'"
}
else
fzf-file-widget() {
LBUFFER="${LBUFFER%% #}$(eval "$__fsel")"
LBUFFER="${LBUFFER%% #}$(__fsel)"
zle redisplay
}
fi
@@ -209,7 +208,7 @@ 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
}
@@ -224,6 +223,7 @@ fzf-history-widget() {
zle -N fzf-history-widget
bindkey '^R' fzf-history-widget
fi
EOFZF
fi
fi

View File

@@ -40,6 +40,19 @@ 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
@@ -75,7 +88,7 @@ function! fzf#run(...) abort
endif
let command = prefix.s:exec.' '.optstr.' > '.temps.result
if exists('$TMUX') && has_key(dict, 'tmux') &&
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
@@ -115,20 +128,16 @@ function! s:execute_tmux(dict, command, temps)
let command = a:command
endif
if type(a:dict.tmux) == 1
if a:dict.tmux =~ '%$'
let height = screenrow() * str2nr(a:dict.tmux[0:-2]) / 100
else
let height = str2nr(a:dict.tmux)
endif
if type(a:dict.tmux) == 1 && a:dict.tmux =~ '%$'
let height = '-p '.a:dict.tmux[0:-2]
else
let height = a:dict.tmux
let height = '-l '.a:dict.tmux
endif
let s:pane = substitute(
\ system(
\ printf(
\ 'tmux split-window -l %d -P -F "#{pane_id}" %s',
\ '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

View File

@@ -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
@@ -30,7 +42,8 @@ class TestFZF < MiniTest::Unit::TestCase
assert_equal nil, fzf.nth
ENV['FZF_DEFAULT_OPTS'] =
'-x -m -s 10000 -q " hello world " +c +2 --no-mouse -f "goodbye world" --black --nth=3'
'-x -m -s 10000 -q " hello world " +c +2 --select-1 -0 ' +
'--no-mouse -f "goodbye world" --black --nth=3,-1,2'
fzf = FZF.new []
assert_equal 10000, fzf.sort
assert_equal ' hello world ',
@@ -43,13 +56,16 @@ class TestFZF < MiniTest::Unit::TestCase
assert_equal false, fzf.ansi256
assert_equal true, fzf.black
assert_equal false, fzf.mouse
assert_equal 3, fzf.nth
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 --nth=1]
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
@@ -58,12 +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
assert_equal [1], fzf.nth
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello
--filter a --filter b --no-256 --black --nth 2
# 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
@@ -74,11 +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
assert_equal [-2], fzf.nth
# Short opts
fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x -fhowdy +2 -n3]
fzf = FZF.new %w[-s2000 +c -m +i -qhello -x -fhowdy +2 -n3 -1 -0]
assert_equal 2000, fzf.sort
assert_equal true, fzf.multi
assert_equal false, fzf.color
@@ -87,11 +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 [3], fzf.nth
assert_equal true, fzf.select1
assert_equal true, fzf.exit0
# Left-to-right
fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x -fgoodbye +2 -n3 -n4
-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
@@ -99,13 +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
assert_equal 4, fzf.nth
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
@@ -376,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]]],
@@ -387,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
@@ -502,33 +529,137 @@ class TestFZF < MiniTest::Unit::TestCase
[list[0], [[2, 5]]],
[list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, 2
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [2]
assert_equal [[list[1], [[8, 9]]]], matcher.match(list, 'f', '', '')
assert_equal [[list[0], [[8, 9]]]], matcher.match(list, 's', '', '')
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, 3
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [3]
assert_equal [[list[0], [[19, 20]]]], matcher.match(list, 'r', '', '')
# 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
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
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
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
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
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