Browse code

Cap max lines at terminal height

John Hawthorn authored on 17/09/2014 02:13:41
Showing 3 changed files

  • fzy.c index 86c4de7..c97829d 100644
  • tty.c index cbf0a85..63ea698 100644
  • tty.h index 0586977..1f3a4f7 100644
... ...
@@ -246,6 +246,9 @@ int main(int argc, char *argv[]){
246 246
 	choices_init(&choices);
247 247
 	read_choices(&choices);
248 248
 
249
+	if(num_lines > choices.size)
250
+		num_lines = choices.size;
251
+
249 252
 	if(benchmark){
250 253
 		if(!initial_query){
251 254
 			fprintf(stderr, "Must specify -e/--show-matches with --benchmark\n");
... ...
@@ -265,6 +268,9 @@ int main(int argc, char *argv[]){
265 268
 		tty_t tty;
266 269
 		tty_init(&tty, tty_filename);
267 270
 
271
+		if(num_lines + 1 > tty_getheight(&tty))
272
+			num_lines = tty_getheight(&tty) - 1;
273
+
268 274
 		run(&tty, &choices);
269 275
 	}
270 276
 
... ...
@@ -43,8 +43,10 @@ void tty_getwinsz(tty_t *tty){
43 43
 	struct winsize ws;
44 44
 	if(ioctl(fileno(tty->fout), TIOCGWINSZ, &ws) == -1){
45 45
 		tty->maxwidth = 80;
46
+		tty->maxheight = 25;
46 47
 	}else{
47 48
 		tty->maxwidth = ws.ws_col;
49
+		tty->maxheight = ws.ws_row;
48 50
 	}
49 51
 }
50 52
 
... ...
@@ -112,3 +114,7 @@ void tty_flush(tty_t *tty){
112 114
 size_t tty_getwidth(tty_t *tty){
113 115
 	return tty->maxwidth;
114 116
 }
117
+
118
+size_t tty_getheight(tty_t *tty){
119
+	return tty->maxheight;
120
+}
... ...
@@ -9,6 +9,7 @@ typedef struct{
9 9
 	struct termios original_termios;
10 10
 	int fgcolor;
11 11
 	size_t maxwidth;
12
+	size_t maxheight;
12 13
 } tty_t;
13 14
 
14 15
 void tty_reset(tty_t *tty);
... ...
@@ -48,5 +49,6 @@ void tty_printf(tty_t *tty, const char *fmt, ...);
48 49
 void tty_flush(tty_t *tty);
49 50
 
50 51
 size_t tty_getwidth(tty_t *tty);
52
+size_t tty_getheight(tty_t *tty);
51 53
 
52 54
 #endif