Add FZF_DEFAULT_OPTS and update command-line options

This commit is contained in:
Junegunn Choi 2014-01-22 12:03:17 +09:00
parent 13cb198b5c
commit 3723829b0a
4 changed files with 97 additions and 47 deletions

View File

@ -51,14 +51,19 @@ Usage
``` ```
usage: fzf [options] usage: fzf [options]
-m, --multi Enable multi-select Options
-x, --extended Extended-search mode -m, --multi Enable multi-select
-q, --query=STR Initial query -x, --extended Extended-search mode
-s, --sort=MAX Maximum number of matched items to sort. Default: 1000 -q, --query=STR Initial query
+s, --no-sort Do not sort the result. Keep the sequence unchanged. -s, --sort=MAX Maximum number of matched items to sort (default: 1000)
-i Case-insensitive match (default: smart-case match) +s, --no-sort Do not sort the result. Keep the sequence unchanged.
+i Case-sensitive match -i Case-insensitive match (default: smart-case match)
+c, --no-color Disable colors +i Case-sensitive match
+c, --no-color Disable colors
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 fzf will launch curses-based finder, read the list from STDIN, and write the
@ -315,10 +320,10 @@ 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. 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 This limit can be adjusted with `-s` option, or with the environment variable
`FZF_DEFAULT_SORT`. `FZF_DEFAULT_OPTS`.
```sh ```sh
export FZF_DEFAULT_SORT=10000 export FZF_DEFAULT_OPTS="--sort 20000"
``` ```
License License

64
fzf
View File

@ -7,7 +7,7 @@
# / __/ / /_/ __/ # / __/ / /_/ __/
# /_/ /___/_/ Fuzzy finder for your shell # /_/ /___/_/ Fuzzy finder for your shell
# #
# Version: 0.6.1 (December 28, 2013) # Version: 0.6.2-devel (January 22, 2014)
# #
# Author: Junegunn Choi # Author: Junegunn Choi
# URL: https://github.com/junegunn/fzf # URL: https://github.com/junegunn/fzf
@ -42,7 +42,7 @@ require 'set'
class FZF class FZF
C = Curses C = Curses
attr_reader :rxflag, :sort, :color, :multi, :query attr_reader :rxflag, :sort, :color, :multi, :query, :extended
class AtomicVar class AtomicVar
def initialize value def initialize value
@ -68,23 +68,32 @@ class FZF
end end
def initialize argv, source = $stdin def initialize argv, source = $stdin
@rxflag = nil @rxflag = nil
@sort = ENV.fetch('FZF_DEFAULT_SORT', 1000).to_i @sort = ENV.fetch('FZF_DEFAULT_SORT', 1000).to_i
@color = true @color = true
@multi = false @multi = false
@xmode = false @extended = false
argv = argv.dup argv =
if opts = ENV['FZF_DEFAULT_OPTS']
require 'shellwords'
Shellwords.shellwords(opts) + argv
else
argv.dup
end
while o = argv.shift while o = argv.shift
case o case o
when '--version' then version when '--version' then version
when '-h', '--help' then usage 0 when '-h', '--help' then usage 0
when '-m', '--multi' then @multi = true when '-m', '--multi' then @multi = true
when '-x', '--extended' then @xmode = true when '+m', '--no-multi' then @multi = false
when '-i' then @rxflag = Regexp::IGNORECASE when '-x', '--extended' then @extended = true
when '+i' then @rxflag = 0 when '+x', '--no-extended' then @extended = false
when '+s', '--no-sort' then @sort = nil when '-i' then @rxflag = Regexp::IGNORECASE
when '+c', '--no-color' then @color = false when '+i' then @rxflag = 0
when '-c', '--color' then @color = true
when '+c', '--no-color' then @color = false
when '+s', '--no-sort' then @sort = nil
when '-q', '--query' when '-q', '--query'
usage 1, 'query string required' unless query = argv.shift usage 1, 'query string required' unless query = argv.shift
@query = AtomicVar.new query.dup @query = AtomicVar.new query.dup
@ -145,14 +154,19 @@ class FZF
$stderr.puts message if message $stderr.puts message if message
$stderr.puts %[usage: fzf [options] $stderr.puts %[usage: fzf [options]
-m, --multi Enable multi-select Options
-x, --extended Extended-search mode -m, --multi Enable multi-select
-q, --query=STR Initial query -x, --extended Extended-search mode
-s, --sort=MAX Maximum number of matched items to sort. Default: 1000 -q, --query=STR Initial query
+s, --no-sort Do not sort the result. Keep the sequence unchanged. -s, --sort=MAX Maximum number of matched items to sort (default: 1000)
-i Case-insensitive match (default: smart-case match) +s, --no-sort Do not sort the result. Keep the sequence unchanged.
+i Case-sensitive match -i Case-insensitive match (default: smart-case match)
+c, --no-color Disable colors] +i Case-sensitive match
+c, --no-color Disable colors
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 exit x
end end
@ -526,7 +540,7 @@ class FZF
end end
def start_search def start_search
matcher = (@xmode ? ExtendedFuzzyMatcher : FuzzyMatcher).new @rxflag matcher = (@extended ? ExtendedFuzzyMatcher : FuzzyMatcher).new @rxflag
searcher = Thread.new { searcher = Thread.new {
lists = [] lists = []
events = {} events = {}

View File

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

View File

@ -7,43 +7,74 @@ ENV['FZF_EXECUTABLE'] = '0'
load 'fzf' load 'fzf'
class TestFZF < MiniTest::Unit::TestCase 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 def test_default_options
fzf = FZF.new [] fzf = FZF.new []
assert_equal 1000, fzf.sort assert_equal 1000, fzf.sort
assert_equal false, fzf.multi assert_equal false, fzf.multi
assert_equal true, fzf.color assert_equal true, fzf.color
assert_equal nil, fzf.rxflag assert_equal nil, fzf.rxflag
end
begin def test_environment_variables
ENV['FZF_DEFAULT_SORT'] = '1500' # Deprecated
fzf = FZF.new [] ENV['FZF_DEFAULT_SORT'] = '20000'
assert_equal 1500, fzf.sort fzf = FZF.new []
ensure assert_equal 20000, fzf.sort
ENV.delete 'FZF_DEFAULT_SORT'
end ENV['FZF_DEFAULT_OPTS'] = '-x -m -s 10000 -q " hello world " +c'
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
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] fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello --extended]
assert_equal 2000, fzf.sort assert_equal 2000, fzf.sort
assert_equal true, fzf.multi assert_equal true, fzf.multi
assert_equal false, fzf.color assert_equal false, fzf.color
assert_equal 0, fzf.rxflag assert_equal 0, fzf.rxflag
assert_equal 'hello', fzf.query.get 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 1, fzf.rxflag
assert_equal 'hello', fzf.query.get
assert_equal false, fzf.extended
# Short opts # Short opts
fzf = FZF.new %w[-s 2000 +c -m +i -qhello] fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x]
assert_equal 2000, fzf.sort assert_equal 2000, fzf.sort
assert_equal true, fzf.multi assert_equal true, fzf.multi
assert_equal false, fzf.color assert_equal false, fzf.color
assert_equal 0, fzf.rxflag assert_equal 0, fzf.rxflag
assert_equal 'hello', fzf.query.get assert_equal 'hello', fzf.query.get
assert_equal true, fzf.extended
# Left-to-right # Left-to-right
fzf = FZF.new %w[-qhello -s 2000 --no-sort -q world] fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x
assert_equal nil, fzf.sort -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 'world', fzf.query.get
assert_equal false, fzf.extended
fzf = FZF.new %w[--query hello +s -s 2000 --query=world] fzf = FZF.new %w[--query hello +s -s 2000 --query=world]
assert_equal 2000, fzf.sort assert_equal 2000, fzf.sort