summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2014-04-03 01:06:40 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2014-04-03 01:06:40 +0900
commit02c01c81a092622ebdbe2366b419919c75c1ba0e (patch)
tree743c8b9ac0197d48a81b6748b8a484c17720ecdd
parent22d3929ae3e2489828f48459bac17e1ab36890f8 (diff)
Improve -0 and -1 as suggested in #36
- Make -0 and -1 work without -q - Change exit status to 0 when exiting with -0
-rw-r--r--README.md4
-rwxr-xr-xfzf16
-rw-r--r--test/test_fzf.rb32
3 files changed, 41 insertions, 11 deletions
diff --git a/README.md b/README.md
index 585b5be5..d9ad3990 100644
--- a/README.md
+++ b/README.md
@@ -72,8 +72,8 @@ usage: fzf [options]
Scripting
-q, --query=STR Start the finder with the given query
- -1, --select-1 (with --query) Automatically select the only match
- -0, --exit-0 (with --query) Exit when there's no match
+ -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
diff --git a/fzf b/fzf
index 9a19b4fb..4c69f1b0 100755
--- a/fzf
+++ b/fzf
@@ -7,7 +7,7 @@
# / __/ / /_/ __/
# /_/ /___/_/ Fuzzy finder for your shell
#
-# Version: 0.8.3 (April 2, 2014)
+# Version: 0.8.3 (April 3, 2014)
#
# Author: Junegunn Choi
# URL: https://github.com/junegunn/fzf
@@ -194,16 +194,16 @@ class FZF
filter_list @new
else
start_reader
- emit(:key) { q = @query.get; [q, q.length] } unless @query.empty?
- if !@query.empty? && (@select1 || @exit0)
+ emit(:key) { q = @query.get; [q, q.length] } unless empty = @query.empty?
+ if @select1 || @exit0
start_search do |loaded, matches|
- len = matches.length
+ len = empty ? @count.get : matches.length
if loaded
if @select1 && len == 1
- puts matches.first.first
+ puts empty ? matches.first : matches.first.first
exit 0
elsif @exit0 && len == 0
- exit 1
+ exit 0
end
end
@@ -276,8 +276,8 @@ class FZF
Scripting
-q, --query=STR Start the finder with the given query
- -1, --select-1 (with --query) Automatically select the only match
- -0, --exit-0 (with --query) Exit when there's no match
+ -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
diff --git a/test/test_fzf.rb b/test/test_fzf.rb
index 94b80d89..ea2ecd96 100644
--- a/test/test_fzf.rb
+++ b/test/test_fzf.rb
@@ -590,6 +590,21 @@ class TestFZF < MiniTest::Unit::TestCase
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
@@ -611,7 +626,22 @@ class TestFZF < MiniTest::Unit::TestCase
$stdout = output
FZF.new(%w[--query=zz --exit-0], stream).start
rescue SystemExit => e
- assert_equal 1, e.status
+ 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