From 032cc49feacd5deff062691c3f3edd3b460bddba Mon Sep 17 00:00:00 2001 From: josuah Date: Sat, 26 Nov 2016 13:26:37 +0100 Subject: [PATCH] Performance improvement --- README | 2 +- buffer.c | 31 +++++++++++++++++-------------- draw.c | 24 ++++++++++++------------ input.c | 10 +++++----- io-abduco | 2 +- main.c | 1 - main.h | 3 +-- 7 files changed, 37 insertions(+), 36 deletions(-) diff --git a/README b/README index 32b7796..10d1db2 100644 --- a/README +++ b/README @@ -1,4 +1,4 @@ - iomenu +iomenu . , ,--. --.-. ,--. ---. , , diff --git a/buffer.c b/buffer.c index f08c39e..d7b874d 100644 --- a/buffer.c +++ b/buffer.c @@ -27,7 +27,6 @@ fill_buffer(char *separator) /* read the file into a doubly linked list of lines */ while (fgets(string, LINE_SIZE, fp)) { - buffer->total++; last = add_line(buffer, buffer->total, string, separator, last); } @@ -47,12 +46,15 @@ Line * add_line(Buffer *buffer, int number, char *string, char *separator, Line *prev) { /* allocate new line */ - Line *line = malloc(sizeof(Line)); - line = parse_line(string, separator); - line->next = NULL; - line->prev = NULL; + Line *line = malloc(sizeof(Line)); + line = parse_line(string, separator); + line->next = NULL; + line->prev = NULL; + line->number = number; + line->matches = 1; buffer->last = line; - line->number = number; + buffer->matching++; + buffer->total++; /* interlink with previous line if exists */ if (number == 1) { @@ -139,9 +141,11 @@ free_buffer(Buffer *buffer) * Set the line->matching state according to the return value of match_line, * and buffer->matching to number of matching candidates. * + * The incremental parameter sets whether check already matching or + * non-matching lines only. This is for performance concerns. */ void -filter_lines(Buffer *buffer) +filter_lines(Buffer *buffer, int inc) { Line *line = buffer->first; char **tokv = NULL; @@ -154,18 +158,17 @@ filter_lines(Buffer *buffer) if (++tokc > n && !(tokv = realloc(tokv, ++n * sizeof(*tokv)))) die("cannot realloc memory for tokv\n"); - /* len = tokc ? strlen(tokv[0]) : 0; */ - + /* match lines */ buffer->matching = 0; while (line) { - if (!strcmp(buffer->input, line->content) && buffer->input[0]) { - buffer->current = line; + if (buffer->input[0] && !strcmp(buffer->input, line->content)) { line->matches = 1; + buffer->current = line; + } else if ((inc && line->matches) || (!inc && !line->matches)) { + line->matches = match_line(line, tokv, tokc); + buffer->matching += line->matches; } - line->matches = match_line(line, tokv, tokc); - buffer->matching += line->matches; - line = line->next; } } diff --git a/draw.c b/draw.c index 2a7e1f7..081df48 100644 --- a/draw.c +++ b/draw.c @@ -17,17 +17,23 @@ draw_line(Line *line, int current, int cols, Opt *opt) char *content = expand_tabs(line->content); char *comment = expand_tabs(line->comment); - /* clean the line and add a small margin */ + /* clear the line */ fputs("\033[K", stderr); - /* line number if option set */ + if (!line->header) { + fputs(current ? "\033[1;31m>" : " ", stderr); + n++; + } + if (opt->line_numbers) { - fputs(current ? "\033[1m" : "\033[1;30m", stderr); - fprintf(stderr, "%7d\033[0m ", line->number); + fprintf(stderr, current + ? "\033[1;37m%7d\033[0m " + : "\033[1;30m%7d\033[0m ", + line->number); n += 8; } else { - fputs(line->header ? " " : " ", stderr); - n += line->header ? 1 : 2; + fputc(' ', stderr); + n++; } /* highlight current line */ @@ -165,12 +171,6 @@ draw_prompt(Buffer *buffer, int cols, Opt *opt) /* grey */ fputs("\033[1;30m", stderr); - /* suggest without overflowing terminal width */ - if (opt->complete && buffer->matching > 0) { - for (; i < strlen(suggest) && cols > 0; cols--, i++) - fputc(suggest[i], stderr); - } - /* go to the end of the line */ fprintf(stderr, "\033[%dC", cols); diff --git a/input.c b/input.c index 47e9834..6ebce7c 100644 --- a/input.c +++ b/input.c @@ -54,20 +54,20 @@ input_key(FILE *tty_fp, Buffer *buffer, Opt *opt) case CONTROL('U'): buffer->input[0] = '\0'; buffer->current = buffer->first; - filter_lines(buffer); + filter_lines(buffer, 0); action_jump(buffer, 1); action_jump(buffer, -1); break; case CONTROL('W'): action_remove_word_input(buffer); - filter_lines(buffer); + filter_lines(buffer, 0); break; case 127: case CONTROL('H'): /* backspace */ buffer->input[strlen(buffer->input) - 1] = '\0'; - filter_lines(buffer); + filter_lines(buffer, 0); action_jump(buffer, 0); break; @@ -81,7 +81,7 @@ input_key(FILE *tty_fp, Buffer *buffer, Opt *opt) case CONTROL('I'): /* tab */ strcpy(buffer->input, buffer->current->content); - filter_lines(buffer); + filter_lines(buffer, 1); break; case CONTROL('J'): @@ -219,7 +219,7 @@ action_add_character(Buffer *buffer, char key) buffer->input[length + 1] = '\0'; } - filter_lines(buffer); + filter_lines(buffer, 1); action_jump(buffer, 0); } diff --git a/io-abduco b/io-abduco index 9b29df0..d0728f1 100755 --- a/io-abduco +++ b/io-abduco @@ -67,7 +67,7 @@ get_options() do [ "$command" = "$cmd" -a "$options" ] && printf '%s\n' "$options" done < $CACHE/iomenu/history | - iomenu -c -p "$command" | + iomenu -p "$command" | tee -a "$CACHE/iomenu/history" sort -u "$CACHE/iomenu/history" -o "$CACHE/iomenu/history" diff --git a/main.c b/main.c index bce2aba..d13900b 100644 --- a/main.c +++ b/main.c @@ -72,7 +72,6 @@ main(int argc, char *argv[]) buffer = fill_buffer(opt->separator); /* set the interface */ - filter_lines(buffer); draw_screen(buffer, tty_fd, opt); /* listen and interact to input */ diff --git a/main.h b/main.h index 878fe80..e749203 100644 --- a/main.h +++ b/main.h @@ -16,7 +16,6 @@ enum { PREV = 0, NEXT, ANY }; */ typedef struct Opt { int line_numbers; - int complete; int print_number; char validate_key; char *separator; @@ -72,7 +71,7 @@ Line * parse_line(char *, char *); Line * matching_next(Line *); Line * matching_prev(Line *); int match_line(Line *, char *[], int); -void filter_lines(Buffer *); +void filter_lines(Buffer *, int); /* draw */ -- 2.11.4.GIT