Browse code

Merge pull request #85 from jhawthorn/fast_tty_init

Initialize tty before reading choices

John Hawthorn authored on 18/06/2018 18:18:50 • GitHub committed on 18/06/2018 18:18:50
Showing 2 changed files

... ...
@@ -3,6 +3,7 @@
3 3
 #include <stdlib.h>
4 4
 #include <ctype.h>
5 5
 #include <limits.h>
6
+#include <unistd.h>
6 7
 
7 8
 #include "match.h"
8 9
 #include "tty.h"
... ...
@@ -20,16 +21,17 @@ int main(int argc, char *argv[]) {
20 21
 
21 22
 	choices_t choices;
22 23
 	choices_init(&choices, &options);
23
-	choices_fread(&choices, stdin);
24 24
 
25 25
 	if (options.benchmark) {
26 26
 		if (!options.filter) {
27 27
 			fprintf(stderr, "Must specify -e/--show-matches with --benchmark\n");
28 28
 			exit(EXIT_FAILURE);
29 29
 		}
30
+		choices_fread(&choices, stdin);
30 31
 		for (int i = 0; i < options.benchmark; i++)
31 32
 			choices_search(&choices, options.filter);
32 33
 	} else if (options.filter) {
34
+		choices_fread(&choices, stdin);
33 35
 		choices_search(&choices, options.filter);
34 36
 		for (size_t i = 0; i < choices_available(&choices); i++) {
35 37
 			if (options.show_scores)
... ...
@@ -38,9 +40,16 @@ int main(int argc, char *argv[]) {
38 40
 		}
39 41
 	} else {
40 42
 		/* interactive */
43
+
44
+		if (isatty(STDIN_FILENO))
45
+			choices_fread(&choices, stdin);
46
+
41 47
 		tty_t tty;
42 48
 		tty_init(&tty, options.tty_filename);
43 49
 
50
+		if (!isatty(STDIN_FILENO))
51
+			choices_fread(&choices, stdin);
52
+
44 53
 		if (options.num_lines > choices.size)
45 54
 			options.num_lines = choices.size;
46 55
 
... ...
@@ -320,6 +320,13 @@ class FzyTest < Minitest::Test
320 320
     @tty.assert_matches "> foo\nfoo"
321 321
   end
322 322
 
323
+  # https://github.com/jhawthorn/fzy/issues/81
324
+  def test_slow_stdin_fast_user
325
+    @tty = TTYtest.new_terminal(%{(echo aa; echo bc; echo bd; sleep 0.5) | #{FZY_PATH}})
326
+    @tty.send_keys("b\r")
327
+    @tty.assert_matches "bc"
328
+  end
329
+
323 330
   def test_help
324 331
     @tty = TTYtest.new_terminal(%{#{FZY_PATH} --help})
325 332
     @tty.assert_matches <<TTY