fzy

terminal fuzzy finder picker

git clone https://9o.is/git/fzy.git

commit 7aba3bf7e9bd667e120350420cd18233c4427433
parent 62e14a8bc37a7352d1d6357aa5edf2ae465c92c7
Author: John Hawthorn <john.hawthorn@gmail.com>
Date:   Mon, 15 Sep 2014 00:07:18 -0700

Truncate matches at terminal width

Diffstat:
Mfzy.c | 22+++++++++++++++-------
Mtty.c | 17+++++++++++++++++
Mtty.h | 4++++
3 files changed, 36 insertions(+), 7 deletions(-)

diff --git a/fzy.c b/fzy.c @@ -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); diff --git a/tty.c b/tty.c @@ -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; +} diff --git a/tty.h b/tty.h @@ -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