From a0e259f93664a6ee8030da8f2b57027753793d3e Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Sun, 17 Jun 2018 11:30:11 -0700 Subject: Initialize tty before reading choices It's possible for user input to arrive while fzy was still reading choices from stdin. Previously, this would happen before the correct termios were set, causing fzy to misinterpret Enter as Ctrl-J for example. Fixes #81 --- src/fzy.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/fzy.c b/src/fzy.c index 461f022..23f5673 100644 --- a/src/fzy.c +++ b/src/fzy.c @@ -20,16 +20,17 @@ int main(int argc, char *argv[]) { choices_t choices; choices_init(&choices, &options); - choices_fread(&choices, stdin); if (options.benchmark) { if (!options.filter) { fprintf(stderr, "Must specify -e/--show-matches with --benchmark\n"); exit(EXIT_FAILURE); } + choices_fread(&choices, stdin); for (int i = 0; i < options.benchmark; i++) choices_search(&choices, options.filter); } else if (options.filter) { + choices_fread(&choices, stdin); choices_search(&choices, options.filter); for (size_t i = 0; i < choices_available(&choices); i++) { if (options.show_scores) @@ -41,6 +42,8 @@ int main(int argc, char *argv[]) { tty_t tty; tty_init(&tty, options.tty_filename); + choices_fread(&choices, stdin); + if (options.num_lines > choices.size) options.num_lines = choices.size; -- cgit v1.2.3 From 794dc0c06cfc1ea60cf97d98c65bb14b47aa3a69 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Sun, 17 Jun 2018 11:48:06 -0700 Subject: Add test for user input coming before choices read --- test/acceptance/acceptance_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/acceptance/acceptance_test.rb b/test/acceptance/acceptance_test.rb index 4a11cac..cac4c26 100644 --- a/test/acceptance/acceptance_test.rb +++ b/test/acceptance/acceptance_test.rb @@ -320,6 +320,13 @@ class FzyTest < Minitest::Test @tty.assert_matches "> foo\nfoo" end + # https://github.com/jhawthorn/fzy/issues/81 + def test_slow_stdin_fast_user + @tty = TTYtest.new_terminal(%{(echo aa; echo bc; echo bd; sleep 0.5) | #{FZY_PATH}}) + @tty.send_keys("b\r") + @tty.assert_matches "bc" + end + def test_help @tty = TTYtest.new_terminal(%{#{FZY_PATH} --help}) @tty.assert_matches < Date: Sun, 17 Jun 2018 11:58:09 -0700 Subject: Fix reading choices if stdin is a tty Previously we deferred reading choices to after initializing the tty. This makes sense only when stdin and our tty aren't the same. --- src/fzy.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/fzy.c b/src/fzy.c index 23f5673..6b9aa5a 100644 --- a/src/fzy.c +++ b/src/fzy.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "match.h" #include "tty.h" @@ -39,10 +40,15 @@ int main(int argc, char *argv[]) { } } else { /* interactive */ + + if (isatty(STDIN_FILENO)) + choices_fread(&choices, stdin); + tty_t tty; tty_init(&tty, options.tty_filename); - choices_fread(&choices, stdin); + if (!isatty(STDIN_FILENO)) + choices_fread(&choices, stdin); if (options.num_lines > choices.size) options.num_lines = choices.size; -- cgit v1.2.3