From 3723829b0a0fa18b7c897e7663ffd1172f0ac859 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Wed, 22 Jan 2014 12:03:17 +0900 Subject: [PATCH] Add FZF_DEFAULT_OPTS and update command-line options --- README.md | 25 +++++++++++-------- fzf | 64 +++++++++++++++++++++++++++++------------------- fzf.gemspec | 2 +- test/test_fzf.rb | 53 ++++++++++++++++++++++++++++++--------- 4 files changed, 97 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index 997e7da..0faed38 100644 --- a/README.md +++ b/README.md @@ -51,14 +51,19 @@ Usage ``` usage: fzf [options] - -m, --multi Enable multi-select - -x, --extended Extended-search mode - -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 + Options + -m, --multi Enable multi-select + -x, --extended Extended-search mode + -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 + + 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 @@ -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. This limit can be adjusted with `-s` option, or with the environment variable -`FZF_DEFAULT_SORT`. +`FZF_DEFAULT_OPTS`. ```sh -export FZF_DEFAULT_SORT=10000 +export FZF_DEFAULT_OPTS="--sort 20000" ``` License diff --git a/fzf b/fzf index e61d613..aa6e81e 100755 --- a/fzf +++ b/fzf @@ -7,7 +7,7 @@ # / __/ / /_/ __/ # /_/ /___/_/ Fuzzy finder for your shell # -# Version: 0.6.1 (December 28, 2013) +# Version: 0.6.2-devel (January 22, 2014) # # Author: Junegunn Choi # URL: https://github.com/junegunn/fzf @@ -42,7 +42,7 @@ require 'set' class FZF C = Curses - attr_reader :rxflag, :sort, :color, :multi, :query + attr_reader :rxflag, :sort, :color, :multi, :query, :extended class AtomicVar def initialize value @@ -68,23 +68,32 @@ class FZF end def initialize argv, source = $stdin - @rxflag = nil - @sort = ENV.fetch('FZF_DEFAULT_SORT', 1000).to_i - @color = true - @multi = false - @xmode = false + @rxflag = nil + @sort = ENV.fetch('FZF_DEFAULT_SORT', 1000).to_i + @color = true + @multi = 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 case o - when '--version' then version - when '-h', '--help' then usage 0 - when '-m', '--multi' then @multi = true - when '-x', '--extended' then @xmode = true - when '-i' then @rxflag = Regexp::IGNORECASE - when '+i' then @rxflag = 0 - when '+s', '--no-sort' then @sort = nil - when '+c', '--no-color' then @color = false + 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 '+s', '--no-sort' then @sort = nil when '-q', '--query' usage 1, 'query string required' unless query = argv.shift @query = AtomicVar.new query.dup @@ -145,14 +154,19 @@ class FZF $stderr.puts message if message $stderr.puts %[usage: fzf [options] - -m, --multi Enable multi-select - -x, --extended Extended-search mode - -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] + Options + -m, --multi Enable multi-select + -x, --extended Extended-search mode + -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 + + 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 @@ -526,7 +540,7 @@ class FZF end def start_search - matcher = (@xmode ? ExtendedFuzzyMatcher : FuzzyMatcher).new @rxflag + matcher = (@extended ? ExtendedFuzzyMatcher : FuzzyMatcher).new @rxflag searcher = Thread.new { lists = [] events = {} diff --git a/fzf.gemspec b/fzf.gemspec index cc682e5..c61b9e9 100644 --- a/fzf.gemspec +++ b/fzf.gemspec @@ -1,7 +1,7 @@ # coding: utf-8 Gem::Specification.new do |spec| spec.name = 'fzf' - spec.version = '0.6.1' + spec.version = '0.6.2' spec.authors = ['Junegunn Choi'] spec.email = ['junegunn.c@gmail.com'] spec.description = %q{Fuzzy finder for your shell} diff --git a/test/test_fzf.rb b/test/test_fzf.rb index 8133c22..7310735 100644 --- a/test/test_fzf.rb +++ b/test/test_fzf.rb @@ -7,43 +7,74 @@ 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 nil, fzf.rxflag + end - begin - ENV['FZF_DEFAULT_SORT'] = '1500' - fzf = FZF.new [] - assert_equal 1500, fzf.sort - ensure - ENV.delete 'FZF_DEFAULT_SORT' - 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' + 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 def test_option_parser # 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 true, fzf.multi assert_equal false, fzf.color 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 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 -qhello] + 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[-qhello -s 2000 --no-sort -q world] - assert_equal nil, fzf.sort + 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