mirror of
https://github.com/Llewellynvdm/fzf.git
synced 2025-01-24 15:48:27 +00:00
Add --with-nth option (#102)
This commit is contained in:
parent
6fd6fff3a6
commit
76a3ef8c37
9
.travis.yml
Normal file
9
.travis.yml
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
language: ruby
|
||||||
|
rvm:
|
||||||
|
- "1.8.7"
|
||||||
|
- "1.9.3"
|
||||||
|
- "2.0.0"
|
||||||
|
- "2.1.1"
|
||||||
|
|
||||||
|
install: gem install curses minitest
|
||||||
|
|
49
README.md
49
README.md
@ -65,38 +65,39 @@ Usage
|
|||||||
usage: fzf [options]
|
usage: fzf [options]
|
||||||
|
|
||||||
Search
|
Search
|
||||||
-x, --extended Extended-search mode
|
-x, --extended Extended-search mode
|
||||||
-e, --extended-exact Extended-search mode (exact match)
|
-e, --extended-exact Extended-search mode (exact match)
|
||||||
-i Case-insensitive match (default: smart-case match)
|
-i Case-insensitive match (default: smart-case match)
|
||||||
+i Case-sensitive match
|
+i Case-sensitive match
|
||||||
-n, --nth=N[,..] Comma-separated list of field index expressions
|
-n, --nth=N[,..] Comma-separated list of field index expressions
|
||||||
for limiting search scope. Each can be a non-zero
|
for limiting search scope. Each can be a non-zero
|
||||||
integer or a range expression ([BEGIN]..[END])
|
integer or a range expression ([BEGIN]..[END])
|
||||||
-d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)
|
--with-nth=N[,..] Transform the item using index expressions for search
|
||||||
|
-d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)
|
||||||
|
|
||||||
Search result
|
Search result
|
||||||
-s, --sort=MAX Maximum number of matched items to sort (default: 1000)
|
-s, --sort=MAX Maximum number of matched items to sort (default: 1000)
|
||||||
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
||||||
|
|
||||||
Interface
|
Interface
|
||||||
-m, --multi Enable multi-select with tab/shift-tab
|
-m, --multi Enable multi-select with tab/shift-tab
|
||||||
--no-mouse Disable mouse
|
--no-mouse Disable mouse
|
||||||
+c, --no-color Disable colors
|
+c, --no-color Disable colors
|
||||||
+2, --no-256 Disable 256-color
|
+2, --no-256 Disable 256-color
|
||||||
--black Use black background
|
--black Use black background
|
||||||
--reverse Reverse orientation
|
--reverse Reverse orientation
|
||||||
--prompt=STR Input prompt (default: '> ')
|
--prompt=STR Input prompt (default: '> ')
|
||||||
|
|
||||||
Scripting
|
Scripting
|
||||||
-q, --query=STR Start the finder with the given query
|
-q, --query=STR Start the finder with the given query
|
||||||
-1, --select-1 Automatically select the only match
|
-1, --select-1 Automatically select the only match
|
||||||
-0, --exit-0 Exit immediately when there's no match
|
-0, --exit-0 Exit immediately when there's no match
|
||||||
-f, --filter=STR Filter mode. Do not start interactive finder.
|
-f, --filter=STR Filter mode. Do not start interactive finder.
|
||||||
--print-query Print query as the first line
|
--print-query Print query as the first line
|
||||||
|
|
||||||
Environment variables
|
Environment variables
|
||||||
FZF_DEFAULT_COMMAND Default command to use when input is tty
|
FZF_DEFAULT_COMMAND Default command to use when input is tty
|
||||||
FZF_DEFAULT_OPTS Defaults options. (e.g. "-x -m --sort 10000")
|
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
|
fzf will launch curses-based finder, read the list from STDIN, and write the
|
||||||
|
1
Rakefile
1
Rakefile
@ -6,3 +6,4 @@ Rake::TestTask.new(:test) do |test|
|
|||||||
test.verbose = true
|
test.verbose = true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
task :default => :test
|
||||||
|
137
fzf
137
fzf
@ -7,7 +7,7 @@
|
|||||||
# / __/ / /_/ __/
|
# / __/ / /_/ __/
|
||||||
# /_/ /___/_/ Fuzzy finder for your shell
|
# /_/ /___/_/ Fuzzy finder for your shell
|
||||||
#
|
#
|
||||||
# Version: 0.8.7 (Aug 17, 2014)
|
# Version: 0.8.8 (Nov 1, 2014)
|
||||||
#
|
#
|
||||||
# Author: Junegunn Choi
|
# Author: Junegunn Choi
|
||||||
# URL: https://github.com/junegunn/fzf
|
# URL: https://github.com/junegunn/fzf
|
||||||
@ -53,11 +53,34 @@ unless String.method_defined? :force_encoding
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class String
|
||||||
|
attr_accessor :orig
|
||||||
|
|
||||||
|
def tokenize delim, nth
|
||||||
|
unless delim
|
||||||
|
# AWK default
|
||||||
|
prefix_length = (index(/\S/) || 0) rescue 0
|
||||||
|
tokens = scan(/\S+\s*/) rescue []
|
||||||
|
else
|
||||||
|
prefix_length = 0
|
||||||
|
tokens = scan(delim) rescue []
|
||||||
|
end
|
||||||
|
nth.map { |n|
|
||||||
|
if n.begin == 0 && n.end == -1
|
||||||
|
[prefix_length, tokens.join]
|
||||||
|
elsif part = tokens[n]
|
||||||
|
[prefix_length + (tokens[0...(n.begin)] || []).join.length,
|
||||||
|
part.join]
|
||||||
|
end
|
||||||
|
}.compact
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class FZF
|
class FZF
|
||||||
C = Curses
|
C = Curses
|
||||||
attr_reader :rxflag, :sort, :nth, :color, :black, :ansi256, :reverse, :prompt,
|
attr_reader :rxflag, :sort, :nth, :color, :black, :ansi256, :reverse, :prompt,
|
||||||
:mouse, :multi, :query, :select1, :exit0, :filter, :extended,
|
:mouse, :multi, :query, :select1, :exit0, :filter, :extended,
|
||||||
:print_query
|
:print_query, :with_nth
|
||||||
|
|
||||||
def sync
|
def sync
|
||||||
@shr_mtx.synchronize { yield }
|
@shr_mtx.synchronize { yield }
|
||||||
@ -95,6 +118,7 @@ class FZF
|
|||||||
@exit0 = false
|
@exit0 = false
|
||||||
@filter = nil
|
@filter = nil
|
||||||
@nth = nil
|
@nth = nil
|
||||||
|
@with_nth = nil
|
||||||
@delim = nil
|
@delim = nil
|
||||||
@reverse = false
|
@reverse = false
|
||||||
@prompt = '> '
|
@prompt = '> '
|
||||||
@ -148,6 +172,11 @@ class FZF
|
|||||||
@nth = parse_nth nth
|
@nth = parse_nth nth
|
||||||
when /^-n([0-9,-\.]+)$/, /^--nth=([0-9,-\.]+)$/
|
when /^-n([0-9,-\.]+)$/, /^--nth=([0-9,-\.]+)$/
|
||||||
@nth = parse_nth $1
|
@nth = parse_nth $1
|
||||||
|
when '--with-nth'
|
||||||
|
usage 1, 'field expression required' unless nth = argv.shift
|
||||||
|
@with_nth = parse_nth nth
|
||||||
|
when /^--with-nth=([0-9,-\.]+)$/
|
||||||
|
@with_nth = parse_nth $1
|
||||||
when '-d', '--delimiter'
|
when '-d', '--delimiter'
|
||||||
usage 1, 'delimiter required' unless delim = argv.shift
|
usage 1, 'delimiter required' unless delim = argv.shift
|
||||||
@delim = FZF.build_delim_regex delim
|
@delim = FZF.build_delim_regex delim
|
||||||
@ -181,6 +210,7 @@ class FZF
|
|||||||
@queue = Queue.new
|
@queue = Queue.new
|
||||||
@pending = nil
|
@pending = nil
|
||||||
@rev_dir = @reverse ? -1 : 1
|
@rev_dir = @reverse ? -1 : 1
|
||||||
|
@stdout = $stdout.clone
|
||||||
|
|
||||||
unless @filter
|
unless @filter
|
||||||
# Shared variables: needs protection
|
# Shared variables: needs protection
|
||||||
@ -200,7 +230,7 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
def parse_nth nth
|
def parse_nth nth
|
||||||
nth.split(',').map { |expr|
|
ranges = nth.split(',').map { |expr|
|
||||||
x = proc { usage 1, "invalid field expression: #{expr}" }
|
x = proc { usage 1, "invalid field expression: #{expr}" }
|
||||||
first, second = expr.split('..', 2)
|
first, second = expr.split('..', 2)
|
||||||
x.call if !first.empty? && first.to_i == 0 ||
|
x.call if !first.empty? && first.to_i == 0 ||
|
||||||
@ -215,6 +245,7 @@ class FZF
|
|||||||
|
|
||||||
Range.new(*[first, second].map { |e| e > 0 ? e - 1 : e })
|
Range.new(*[first, second].map { |e| e > 0 ? e - 1 : e })
|
||||||
}
|
}
|
||||||
|
ranges == [0..-1] ? nil : ranges
|
||||||
end
|
end
|
||||||
|
|
||||||
def FZF.build_delim_regex delim
|
def FZF.build_delim_regex delim
|
||||||
@ -222,6 +253,10 @@ class FZF
|
|||||||
Regexp.compile "(?:.*?#{delim})|(?:.+?$)"
|
Regexp.compile "(?:.*?#{delim})|(?:.+?$)"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def burp string
|
||||||
|
@stdout.puts(string.orig || string)
|
||||||
|
end
|
||||||
|
|
||||||
def start
|
def start
|
||||||
if @filter
|
if @filter
|
||||||
start_reader.join
|
start_reader.join
|
||||||
@ -236,7 +271,7 @@ class FZF
|
|||||||
if loaded
|
if loaded
|
||||||
if @select1 && len == 1
|
if @select1 && len == 1
|
||||||
puts @query if @print_query
|
puts @query if @print_query
|
||||||
puts empty ? matches.first : matches.first.first
|
burp(empty ? matches.first : matches.first.first)
|
||||||
exit 0
|
exit 0
|
||||||
elsif @exit0 && len == 0
|
elsif @exit0 && len == 0
|
||||||
puts @query if @print_query
|
puts @query if @print_query
|
||||||
@ -312,39 +347,40 @@ class FZF
|
|||||||
$stderr.puts %[usage: fzf [options]
|
$stderr.puts %[usage: fzf [options]
|
||||||
|
|
||||||
Search
|
Search
|
||||||
-x, --extended Extended-search mode
|
-x, --extended Extended-search mode
|
||||||
-e, --extended-exact Extended-search mode (exact match)
|
-e, --extended-exact Extended-search mode (exact match)
|
||||||
-i Case-insensitive match (default: smart-case match)
|
-i Case-insensitive match (default: smart-case match)
|
||||||
+i Case-sensitive match
|
+i Case-sensitive match
|
||||||
-n, --nth=N[,..] Comma-separated list of field index expressions
|
-n, --nth=N[,..] Comma-separated list of field index expressions
|
||||||
for limiting search scope. Each can be a non-zero
|
for limiting search scope. Each can be a non-zero
|
||||||
integer or a range expression ([BEGIN]..[END])
|
integer or a range expression ([BEGIN]..[END])
|
||||||
-d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)
|
--with-nth=N[,..] Transform the item using index expressions for search
|
||||||
|
-d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)
|
||||||
|
|
||||||
Search result
|
Search result
|
||||||
-s, --sort=MAX Maximum number of matched items to sort (default: 1000)
|
-s, --sort=MAX Maximum number of matched items to sort (default: 1000)
|
||||||
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
||||||
|
|
||||||
Interface
|
Interface
|
||||||
-m, --multi Enable multi-select with tab/shift-tab
|
-m, --multi Enable multi-select with tab/shift-tab
|
||||||
--no-mouse Disable mouse
|
--no-mouse Disable mouse
|
||||||
+c, --no-color Disable colors
|
+c, --no-color Disable colors
|
||||||
+2, --no-256 Disable 256-color
|
+2, --no-256 Disable 256-color
|
||||||
--black Use black background
|
--black Use black background
|
||||||
--reverse Reverse orientation
|
--reverse Reverse orientation
|
||||||
--prompt=STR Input prompt (default: '> ')
|
--prompt=STR Input prompt (default: '> ')
|
||||||
|
|
||||||
Scripting
|
Scripting
|
||||||
-q, --query=STR Start the finder with the given query
|
-q, --query=STR Start the finder with the given query
|
||||||
-1, --select-1 Automatically select the only match
|
-1, --select-1 Automatically select the only match
|
||||||
-0, --exit-0 Exit immediately when there's no match
|
-0, --exit-0 Exit immediately when there's no match
|
||||||
-f, --filter=STR Filter mode. Do not start interactive finder.
|
-f, --filter=STR Filter mode. Do not start interactive finder.
|
||||||
--print-query Print query as the first line
|
--print-query Print query as the first line
|
||||||
|
|
||||||
Environment variables
|
Environment variables
|
||||||
FZF_DEFAULT_COMMAND Default command to use when input is tty
|
FZF_DEFAULT_COMMAND Default command to use when input is tty
|
||||||
FZF_DEFAULT_OPTS Defaults options. (e.g. "-x -m --sort 10000")] + $/ + $/
|
FZF_DEFAULT_OPTS Defaults options. (e.g. "-x -m --sort 10000")] + $/ + $/
|
||||||
exit x
|
exit x
|
||||||
end
|
end
|
||||||
|
|
||||||
def emit event
|
def emit event
|
||||||
@ -520,7 +556,6 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
def init_screen
|
def init_screen
|
||||||
@stdout = $stdout.clone
|
|
||||||
$stdout.reopen($stderr)
|
$stdout.reopen($stderr)
|
||||||
|
|
||||||
C.init_screen
|
C.init_screen
|
||||||
@ -595,14 +630,28 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
Thread.new do
|
Thread.new do
|
||||||
while line = stream.gets
|
if @with_nth
|
||||||
emit(:new) { @new << line.chomp }
|
while line = stream.gets
|
||||||
|
emit(:new) { @new << transform(line) }
|
||||||
|
end
|
||||||
|
else
|
||||||
|
while line = stream.gets
|
||||||
|
emit(:new) { @new << line.chomp }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
emit(:loaded) { true }
|
emit(:loaded) { true }
|
||||||
@spinner.clear if @spinner
|
@spinner.clear if @spinner
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def transform line
|
||||||
|
line = line.chomp
|
||||||
|
mut = (line =~ / $/ ? line : line + ' ').
|
||||||
|
tokenize(@delim, @with_nth).map { |e| e.last }.join('').sub(/ *$/, '')
|
||||||
|
mut.orig = line
|
||||||
|
mut
|
||||||
|
end
|
||||||
|
|
||||||
def start_search &callback
|
def start_search &callback
|
||||||
Thread.new do
|
Thread.new do
|
||||||
lists = []
|
lists = []
|
||||||
@ -1080,10 +1129,10 @@ class FZF
|
|||||||
@stdout.puts q if @print_query
|
@stdout.puts q if @print_query
|
||||||
if got
|
if got
|
||||||
if selects.empty?
|
if selects.empty?
|
||||||
@stdout.puts got
|
burp got
|
||||||
else
|
else
|
||||||
selects.each do |sel, _|
|
selects.each do |sel, _|
|
||||||
@stdout.puts sel
|
burp sel
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -1108,25 +1157,7 @@ class FZF
|
|||||||
end
|
end
|
||||||
|
|
||||||
def tokenize str
|
def tokenize str
|
||||||
@tokens_cache[str] ||=
|
@tokens_cache[str] ||= str.tokenize(@delim, @nth)
|
||||||
begin
|
|
||||||
unless @delim
|
|
||||||
# AWK default
|
|
||||||
prefix_length = (str.index(/\S/) || 0) rescue 0
|
|
||||||
tokens = str.scan(/\S+\s*/) rescue []
|
|
||||||
else
|
|
||||||
prefix_length = 0
|
|
||||||
tokens = str.scan(@delim) rescue []
|
|
||||||
end
|
|
||||||
@nth.map { |n|
|
|
||||||
if n.begin == 0 && n.end == -1
|
|
||||||
[prefix_length, tokens.join]
|
|
||||||
elsif part = tokens[n]
|
|
||||||
[prefix_length + (tokens[0...(n.begin)] || []).join.length,
|
|
||||||
part.join]
|
|
||||||
end
|
|
||||||
}.compact
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def do_match str, pat
|
def do_match str, pat
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#!/usr/bin/env ruby
|
#!/usr/bin/env ruby
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
|
||||||
|
require 'rubygems'
|
||||||
require 'curses'
|
require 'curses'
|
||||||
require 'timeout'
|
require 'timeout'
|
||||||
require 'stringio'
|
require 'stringio'
|
||||||
@ -25,6 +26,7 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal nil, fzf.rxflag
|
assert_equal nil, fzf.rxflag
|
||||||
assert_equal true, fzf.mouse
|
assert_equal true, fzf.mouse
|
||||||
assert_equal nil, fzf.nth
|
assert_equal nil, fzf.nth
|
||||||
|
assert_equal nil, fzf.with_nth
|
||||||
assert_equal true, fzf.color
|
assert_equal true, fzf.color
|
||||||
assert_equal false, fzf.black
|
assert_equal false, fzf.black
|
||||||
assert_equal true, fzf.ansi256
|
assert_equal true, fzf.ansi256
|
||||||
@ -47,7 +49,7 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
|
|
||||||
ENV['FZF_DEFAULT_OPTS'] =
|
ENV['FZF_DEFAULT_OPTS'] =
|
||||||
'-x -m -s 10000 -q " hello world " +c +2 --select-1 -0 ' <<
|
'-x -m -s 10000 -q " hello world " +c +2 --select-1 -0 ' <<
|
||||||
'--no-mouse -f "goodbye world" --black --nth=3,-1,2 --reverse --print-query'
|
'--no-mouse -f "goodbye world" --black --with-nth=3,-3..,2 --nth=3,-1,2 --reverse --print-query'
|
||||||
fzf = FZF.new []
|
fzf = FZF.new []
|
||||||
assert_equal 10000, fzf.sort
|
assert_equal 10000, fzf.sort
|
||||||
assert_equal ' hello world ',
|
assert_equal ' hello world ',
|
||||||
@ -65,13 +67,14 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal true, fzf.reverse
|
assert_equal true, fzf.reverse
|
||||||
assert_equal true, fzf.print_query
|
assert_equal true, fzf.print_query
|
||||||
assert_equal [2..2, -1..-1, 1..1], fzf.nth
|
assert_equal [2..2, -1..-1, 1..1], fzf.nth
|
||||||
|
assert_equal [2..2, -3..-1, 1..1], fzf.with_nth
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_option_parser
|
def test_option_parser
|
||||||
# Long opts
|
# Long opts
|
||||||
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello --select-1
|
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello --select-1
|
||||||
--exit-0 --filter=howdy --extended-exact
|
--exit-0 --filter=howdy --extended-exact
|
||||||
--no-mouse --no-256 --nth=1 --reverse --prompt (hi)
|
--no-mouse --no-256 --nth=1 --with-nth=.. --reverse --prompt (hi)
|
||||||
--print-query]
|
--print-query]
|
||||||
assert_equal 2000, fzf.sort
|
assert_equal 2000, fzf.sort
|
||||||
assert_equal true, fzf.multi
|
assert_equal true, fzf.multi
|
||||||
@ -86,6 +89,7 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_equal 'howdy', fzf.filter
|
assert_equal 'howdy', fzf.filter
|
||||||
assert_equal :exact, fzf.extended
|
assert_equal :exact, fzf.extended
|
||||||
assert_equal [0..0], fzf.nth
|
assert_equal [0..0], fzf.nth
|
||||||
|
assert_equal nil, fzf.with_nth
|
||||||
assert_equal true, fzf.reverse
|
assert_equal true, fzf.reverse
|
||||||
assert_equal '(hi)', fzf.prompt
|
assert_equal '(hi)', fzf.prompt
|
||||||
assert_equal true, fzf.print_query
|
assert_equal true, fzf.print_query
|
||||||
@ -638,6 +642,32 @@ class TestFZF < MiniTest::Unit::TestCase
|
|||||||
assert_fzf_output %w[--exit-0], '', ''
|
assert_fzf_output %w[--exit-0], '', ''
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_with_nth
|
||||||
|
source = "hello world\nbatman"
|
||||||
|
assert_fzf_output %w[-0 -1 --with-nth=2,1 -x -q ^worl],
|
||||||
|
source, 'hello world'
|
||||||
|
assert_fzf_output %w[-0 -1 --with-nth=2,1 -x -q llo$],
|
||||||
|
source, 'hello world'
|
||||||
|
assert_fzf_output %w[-0 -1 --with-nth=.. -x -q llo$],
|
||||||
|
source, ''
|
||||||
|
assert_fzf_output %w[-0 -1 --with-nth=2,2,2,..,1 -x -q worlworlworlhellworlhell],
|
||||||
|
source, 'hello world'
|
||||||
|
assert_fzf_output %w[-0 -1 --with-nth=1,1,-1,1 -x -q batbatbatbat],
|
||||||
|
source, 'batman'
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_with_nth_transform
|
||||||
|
fzf = FZF.new %w[--with-nth 2..,1]
|
||||||
|
assert_equal 'my world hello', fzf.transform('hello my world')
|
||||||
|
assert_equal 'my world hello', fzf.transform('hello my world')
|
||||||
|
assert_equal 'my world hello', fzf.transform('hello my world ')
|
||||||
|
|
||||||
|
fzf = FZF.new %w[--with-nth 2,-1,2]
|
||||||
|
assert_equal 'my world my', fzf.transform('hello my world')
|
||||||
|
assert_equal 'world world world', fzf.transform('hello world')
|
||||||
|
assert_equal 'world world world', fzf.transform('hello world ')
|
||||||
|
end
|
||||||
|
|
||||||
def test_ranking_overlap_match_regions
|
def test_ranking_overlap_match_regions
|
||||||
list = [
|
list = [
|
||||||
'1 3 4 2',
|
'1 3 4 2',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user