diff options
author | John Hawthorn <john.hawthorn@gmail.com> | 2014-09-15 00:07:18 -0700 |
---|---|---|
committer | John Hawthorn <john.hawthorn@gmail.com> | 2014-09-15 00:07:18 -0700 |
commit | 8ddf546d8e36fca6b147b78621df31800b081e60 (patch) | |
tree | 430dd95a4731445c7cf931abaa7d26f21c907686 | |
parent | 6bace05aece734fd1efbfa7c46040f4e6fad2d86 (diff) |
Truncate matches at terminal width
-rw-r--r-- | fzy.c | 22 | ||||
-rw-r--r-- | tty.c | 17 | ||||
-rw-r--r-- | tty.h | 4 |
3 files changed, 36 insertions, 7 deletions
@@ -55,6 +55,8 @@ void draw_match(tty_t *tty, const char *choice, int selected){ double score = match_positions(search, choice, &positions[0]); + size_t maxwidth = tty_getwidth(tty); + if(flag_show_scores) tty_printf(tty, "(%5.2f) ", score); @@ -62,13 +64,18 @@ void draw_match(tty_t *tty, const char *choice, int selected){ tty_setinvert(tty); for(size_t i = 0, p = 0; choice[i] != '\0'; i++){ - if(positions[p] == i){ - tty_setfg(tty, TTY_COLOR_HIGHLIGHT); - p++; + if(i+1 < maxwidth){ + if(positions[p] == i){ + tty_setfg(tty, TTY_COLOR_HIGHLIGHT); + p++; + }else{ + tty_setfg(tty, TTY_COLOR_NORMAL); + } + tty_printf(tty, "%c", choice[i]); }else{ - tty_setfg(tty, TTY_COLOR_NORMAL); + tty_printf(tty, "$"); + break; } - tty_printf(tty, "%c", choice[i]); } tty_setnormal(tty); } @@ -85,14 +92,15 @@ void draw(tty_t *tty, choices_t *choices){ const char *prompt = "> "; tty_setcol(tty, 0); tty_printf(tty, "%s%s", prompt, search); + tty_clearline(tty); for(size_t i = start; i < start + num_lines; i++){ - tty_newline(tty); + tty_printf(tty, "\n"); + tty_clearline(tty); const char *choice = choices_get(choices, i); if(choice){ draw_match(tty, choice, i == choices->selection); } } - tty_clearline(tty); tty_moveup(tty, num_lines); tty_setcol(tty, strlen(prompt) + strlen(search)); tty_flush(tty); @@ -1,8 +1,11 @@ +#define _GNU_SOURCE #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <stdlib.h> #include <stdarg.h> +#include <termios.h> +#include <sys/ioctl.h> #include "tty.h" @@ -28,9 +31,20 @@ void tty_init(tty_t *tty, const char *tty_filename){ tcsetattr(tty->fdin, TCSANOW, &new_termios); + tty_getwinsz(tty); + tty_setnormal(tty); } +void tty_getwinsz(tty_t *tty){ + struct winsize ws; + if(ioctl(fileno(tty->fout), TIOCGWINSZ, &ws) == -1){ + tty->maxwidth = 80; + }else{ + tty->maxwidth = ws.ws_col; + } +} + char tty_getchar(tty_t *tty){ char ch; int size = read(tty->fdin, &ch, 1); @@ -92,3 +106,6 @@ void tty_flush(tty_t *tty){ fflush(tty->fout); } +size_t tty_getwidth(tty_t *tty){ + return tty->maxwidth; +} @@ -8,10 +8,12 @@ typedef struct{ FILE *fout; struct termios original_termios; int fgcolor; + size_t maxwidth; } tty_t; void tty_reset(tty_t *tty); void tty_init(tty_t *tty, const char *tty_filename); +void tty_getwinsz(tty_t *tty); char tty_getchar(tty_t *tty); void tty_setfg(tty_t *tty, int fg); @@ -45,4 +47,6 @@ void tty_setcol(tty_t *tty, int col); void tty_printf(tty_t *tty, const char *fmt, ...); void tty_flush(tty_t *tty); +size_t tty_getwidth(tty_t *tty); + #endif |