From b23c297ee92f45b6bae1ef07e0e479c93a49d078 Mon Sep 17 00:00:00 2001 From: josuah Date: Sat, 26 Nov 2016 21:06:58 +0100 Subject: [PATCH] Faster drawing with only one flush --- TODO | 8 +++----- draw.c | 43 ++++++++++++++++++------------------------- io-grep | 15 ++++----------- 3 files changed, 25 insertions(+), 41 deletions(-) diff --git a/TODO b/TODO index 2bd0a1a..b25671b 100644 --- a/TODO +++ b/TODO @@ -5,14 +5,12 @@ - Prettier prompt? -- Support arrows keys +- Fix the input shifting the line count by 1. -- Async line input, with fork(): I finally understood how it basically +- Async line input? With fork(): I finally understood how it basically works. mmap the buffer and fill it in one fork while the other continues with the keyboard interaction? - - Merge file, buffer, command list in a same iomenu list - - Option to output header title rather than the line itself, with lines counted relatively from the beginning of the header. This way, it can acts as a swiper-all: grep through multiple files, and @@ -21,4 +19,4 @@ - Use this for io-grep -- Remove as many options as possible. +- Case insensitive match. diff --git a/draw.c b/draw.c index 081df48..9f741d6 100644 --- a/draw.c +++ b/draw.c @@ -12,53 +12,46 @@ void draw_line(Line *line, int current, int cols, Opt *opt) { - size_t i; - int n = 0; char *content = expand_tabs(line->content); char *comment = expand_tabs(line->comment); + char output[LINE_SIZE * sizeof(char)] = ""; /* clear the line */ - fputs("\033[K", stderr); + strncat(output, "\033[K", cols); - if (!line->header) { - fputs(current ? "\033[1;31m>" : " ", stderr); - n++; - } + if (!line->header) + strncat(output, current ? "\033[1;31m>" : " ", cols--); if (opt->line_numbers) { - fprintf(stderr, current - ? "\033[1;37m%7d\033[0m " - : "\033[1;30m%7d\033[0m ", - line->number); - n += 8; + strcat(output, current ? "\033[1;37m" : "\033[1;30m"); + sprintf(output, "%7d \033[0m", line->number); + cols -= 8; } else { - fputc(' ', stderr); - n++; + strncat(output, " ", cols--); } /* highlight current line */ if (current) - fputs("\033[1;33m", stderr); + strcat(output, "\033[1;33m"); /* print content without overflowing terminal width */ - for (i = 0; i < strlen(content) && n < cols; n++, i++) - fputc(content[i], stderr); + strncat(output, content, cols); + cols -= strlen(content); /* shift without overflowing terminal width */ if (!line->header && line->comment[0] != '\0') { /* MAX with '1' as \033[0C still move 1 to the right */ - fprintf(stderr, "\033[%dC", MAX(40 - n, 1)); - n += MAX(40 - n, 1); + sprintf(content, "\033[%dC\033[1;30m", MIN(40, cols)); + cols -= MIN(40, cols); } - /* comments in grey */ - fputs("\033[1;30m", stderr); - /* print comment without overflowing terminal width */ - for (i = 0; i < strlen(comment) && n < cols; n++, i++) - fputc(comment[i], stderr); + strncat(output, comment, cols); + cols -= strlen(comment); + + strcat(output, "\033[0m\n"); - fputs("\033[0m\n", stderr); + fputs(output, stderr); free(content); free(comment); diff --git a/io-grep b/io-grep index 7fe4d0f..ca4b8b5 100755 --- a/io-grep +++ b/io-grep @@ -3,20 +3,13 @@ # Directory to be searched can be set in ~/.config/iomenu/grep. They are # relative to "$HOME" -# requires: iomenu - -CONFIG="${XDG_CONFIG_HOME:-$HOME/.config}/iomenu" - mkdir -p "$CONFIG" reference="$( - while IFS='' read -r directory - do - find "$HOME/$directory" ! -path '*/.git/*' -type f | - xargs grep -n "$*" | - cut -c $((${#HOME} + 2))- - done < "$CONFIG/grep" | - iomenu + find "$1" ! -path '*/.git/*' -type f | + xargs -n 1 grep -n | + cut -c $((${#HOME} + 2))- | + iomenu -n )" path="${reference%%:*}" -- 2.11.4.GIT