Browse code

Disable line wrap when printing candidates

This solves the line wrapping issue with much simpler code, which also
works better with Unicode characters and when the terminal is resized.

John Hawthorn authored on 09/09/2018 20:05:12
Showing 2 changed files

... ...
@@ -42,15 +42,12 @@ static void draw_match(tty_interface_t *state, const char *choice, int selected)
42 42
 
43 43
 	score_t score = match_positions(search, choice, &positions[0]);
44 44
 
45
-	size_t maxwidth = tty_getwidth(tty);
46
-
47
-	if (options->show_scores && maxwidth >= 9) {
45
+	if (options->show_scores) {
48 46
 		if (score == SCORE_MIN) {
49 47
 			tty_printf(tty, "(     ) ");
50 48
 		} else {
51 49
 			tty_printf(tty, "(%5.2f) ", score);
52 50
 		}
53
-		maxwidth -= 8;
54 51
 	}
55 52
 
56 53
 	if (selected)
... ...
@@ -60,20 +57,17 @@ static void draw_match(tty_interface_t *state, const char *choice, int selected)
60 57
 		tty_setinvert(tty);
61 58
 #endif
62 59
 
60
+	tty_setnowrap(tty);
63 61
 	for (size_t i = 0, p = 0; choice[i] != '\0'; i++) {
64
-		if (i + 1 < maxwidth) {
65
-			if (positions[p] == i) {
66
-				tty_setfg(tty, TTY_COLOR_HIGHLIGHT);
67
-				p++;
68
-			} else {
69
-				tty_setfg(tty, TTY_COLOR_NORMAL);
70
-			}
71
-			tty_printf(tty, "%c", choice[i]);
62
+		if (positions[p] == i) {
63
+			tty_setfg(tty, TTY_COLOR_HIGHLIGHT);
64
+			p++;
72 65
 		} else {
73
-			tty_printf(tty, "$");
74
-			break;
66
+			tty_setfg(tty, TTY_COLOR_NORMAL);
75 67
 		}
68
+		tty_printf(tty, "%c", choice[i]);
76 69
 	}
70
+	tty_setwrap(tty);
77 71
 	tty_setnormal(tty);
78 72
 }
79 73
 
... ...
@@ -430,6 +430,18 @@ class FzyTest < Minitest::Test
430 430
     @tty.assert_cursor_position(y: 0, x: 8)
431 431
   end
432 432
 
433
+  def test_long_strings
434
+    ascii = "LongStringOfText" * 6
435
+    unicode = "LongStringOfText" * 3
436
+
437
+    @tty = interactive_fzy(input: [ascii, unicode])
438
+    @tty.assert_matches <<~TTY
439
+      >
440
+      LongStringOfTextLongStringOfTextLongStringOfTextLongStringOfTextLongStringOfText
441
+      LongStringOfTextLongStringOfTextLongStri
442
+    TTY
443
+  end
444
+
433 445
   def test_help
434 446
     @tty = TTYtest.new_terminal(%{#{FZY_PATH} --help})
435 447
     @tty.assert_matches <<TTY