Browse code

Truncate matches at terminal width

John Hawthorn authored on 15/09/2014 07:07:18
Showing 3 changed files

  • fzy.c index 1168b2a..2dcf5aa 100644
  • tty.c index 274f88c..f7032f1 100644
  • tty.h index ad21e8e..0586977 100644
... ...
@@ -55,6 +55,8 @@ void draw_match(tty_t *tty, const char *choice, int selected){
55 55
 
56 56
 	double score = match_positions(search, choice, &positions[0]);
57 57
 
58
+	size_t maxwidth = tty_getwidth(tty);
59
+
58 60
 	if(flag_show_scores)
59 61
 		tty_printf(tty, "(%5.2f) ", score);
60 62
 
... ...
@@ -62,13 +64,18 @@ void draw_match(tty_t *tty, const char *choice, int selected){
62 64
 		tty_setinvert(tty);
63 65
 
64 66
 	for(size_t i = 0, p = 0; choice[i] != '\0'; i++){
65
-		if(positions[p] == i){
66
-			tty_setfg(tty, TTY_COLOR_HIGHLIGHT);
67
-			p++;
67
+		if(i+1 < maxwidth){
68
+			if(positions[p] == i){
69
+				tty_setfg(tty, TTY_COLOR_HIGHLIGHT);
70
+				p++;
71
+			}else{
72
+				tty_setfg(tty, TTY_COLOR_NORMAL);
73
+			}
74
+			tty_printf(tty, "%c", choice[i]);
68 75
 		}else{
69
-			tty_setfg(tty, TTY_COLOR_NORMAL);
76
+			tty_printf(tty, "$");
77
+			break;
70 78
 		}
71
-		tty_printf(tty, "%c", choice[i]);
72 79
 	}
73 80
 	tty_setnormal(tty);
74 81
 }
... ...
@@ -85,14 +92,15 @@ void draw(tty_t *tty, choices_t *choices){
85 92
 	const char *prompt = "> ";
86 93
 	tty_setcol(tty, 0);
87 94
 	tty_printf(tty, "%s%s", prompt, search);
95
+	tty_clearline(tty);
88 96
 	for(size_t i = start; i < start + num_lines; i++){
89
-		tty_newline(tty);
97
+		tty_printf(tty, "\n");
98
+		tty_clearline(tty);
90 99
 		const char *choice = choices_get(choices, i);
91 100
 		if(choice){
92 101
 			draw_match(tty, choice, i == choices->selection);
93 102
 		}
94 103
 	}
95
-	tty_clearline(tty);
96 104
 	tty_moveup(tty, num_lines);
97 105
 	tty_setcol(tty, strlen(prompt) + strlen(search));
98 106
 	tty_flush(tty);
... ...
@@ -1,8 +1,11 @@
1
+#define _GNU_SOURCE
1 2
 #include <stdio.h>
2 3
 #include <unistd.h>
3 4
 #include <fcntl.h>
4 5
 #include <stdlib.h>
5 6
 #include <stdarg.h>
7
+#include <termios.h>
8
+#include <sys/ioctl.h>
6 9
 
7 10
 #include "tty.h"
8 11
 
... ...
@@ -28,9 +31,20 @@ void tty_init(tty_t *tty, const char *tty_filename){
28 31
 
29 32
 	tcsetattr(tty->fdin, TCSANOW, &new_termios);
30 33
 
34
+	tty_getwinsz(tty);
35
+
31 36
 	tty_setnormal(tty);
32 37
 }
33 38
 
39
+void tty_getwinsz(tty_t *tty){
40
+	struct winsize ws;
41
+	if(ioctl(fileno(tty->fout), TIOCGWINSZ, &ws) == -1){
42
+		tty->maxwidth = 80;
43
+	}else{
44
+		tty->maxwidth = ws.ws_col;
45
+	}
46
+}
47
+
34 48
 char tty_getchar(tty_t *tty){
35 49
 	char ch;
36 50
 	int size = read(tty->fdin, &ch, 1);
... ...
@@ -92,3 +106,6 @@ void tty_flush(tty_t *tty){
92 106
 	fflush(tty->fout);
93 107
 }
94 108
 
109
+size_t tty_getwidth(tty_t *tty){
110
+	return tty->maxwidth;
111
+}
... ...
@@ -8,10 +8,12 @@ typedef struct{
8 8
 	FILE *fout;
9 9
 	struct termios original_termios;
10 10
 	int fgcolor;
11
+	size_t maxwidth;
11 12
 } tty_t;
12 13
 
13 14
 void tty_reset(tty_t *tty);
14 15
 void tty_init(tty_t *tty, const char *tty_filename);
16
+void tty_getwinsz(tty_t *tty);
15 17
 char tty_getchar(tty_t *tty);
16 18
 
17 19
 void tty_setfg(tty_t *tty, int fg);
... ...
@@ -45,4 +47,6 @@ void tty_setcol(tty_t *tty, int col);
45 47
 void tty_printf(tty_t *tty, const char *fmt, ...);
46 48
 void tty_flush(tty_t *tty);
47 49
 
50
+size_t tty_getwidth(tty_t *tty);
51
+
48 52
 #endif