From 6c7cfa71ebddbc9a8c9fe3e4ec8242a32d7dd4d5 Mon Sep 17 00:00:00 2001 From: josuah Date: Fri, 21 Oct 2016 16:46:26 +0200 Subject: [PATCH] Further more cleanup --- main.c | 93 ++++++++++++++++++++++++++++++++++++++++-------------------------- main.h | 13 +++++++++- 2 files changed, 69 insertions(+), 37 deletions(-) diff --git a/main.c b/main.c index 9d1f3b1..11e5e9b 100644 --- a/main.c +++ b/main.c @@ -12,7 +12,7 @@ /* add abstraction at no cost, but may add more complexity as well? */ enum { FALSE = 0, TRUE = 1 }; -enum { NEXT = 0, PREV = 1, MATCH = 2 }; +enum { NEXT = 0, PREV = 1, BOTH = 2 }; /* preprocessor macros */ #define LENGTH(x) (sizeof(x) / sizeof(*x)) @@ -146,23 +146,47 @@ filter_lines(Buffer *buffer) } /* - * Check if line matches and return TRUE if so + * Check if line matches and return TRUE or FALSE */ int line_match_input(Line *line, char *input) { if (opt_complete_mode) { - if (!strncmp(input, line->content, strlen(input))) - return TRUE; + if (!strncmp(input, line->content, strlen(input))) { + return TRUE; + } } else { - if (strstr(line->content, input)) + if (strstr(line->content, input)) { return TRUE; + } } return FALSE; } /* + * Seek the previous matching line, or NULL if none matches. + */ +Line * +matching_prev(Line *line) +{ + while ((line = line->prev) && !line->matches) + ; + return line; +} + +/* + * Seek the next matching line, or NULL if none matches. + */ +Line * +matching_next(Line *line) +{ + while ((line = line->next) && !line->matches) + ; + return line; +} + +/* * Replace tab as a multiple of 8 spaces in a line. */ char * @@ -393,9 +417,7 @@ do_key(char key, Buffer *buffer) case CONTROL('W'): do_remove_word_input(buffer); - filter_lines(buffer); - break; case 127: @@ -404,25 +426,21 @@ do_key(char key, Buffer *buffer) filter_lines(buffer); if (!buffer->current->matches) { - do_go_to(buffer, MATCH); + do_jump(buffer, BOTH); } break; case CONTROL('N'): - do_go_to(buffer, NEXT); + do_jump(buffer, NEXT); break; case CONTROL('P'): - do_go_to(buffer, PREV); + do_jump(buffer, PREV); break; case CONTROL('I'): /* tab */ - if (opt_complete_mode) { - strcpy(buffer->input, buffer->current->content); - filter_lines(buffer); - } else { - do_go_to(buffer, NEXT); - } + strcpy(buffer->input, buffer->current->content); + filter_lines(buffer); break; case CONTROL('M'): @@ -441,22 +459,21 @@ do_key(char key, Buffer *buffer) * Set the current line to the next/previous/closest matching line. */ void -do_go_to(Buffer *buffer, int to) +do_jump(Buffer *buffer, int direction) { Line * line = buffer->current; - if (to == MATCH) { - do_go_to(buffer, NEXT); - do_go_to(buffer, PREV); - } else { - while ((line = (to == PREV ) ? line->prev : line->next)) { - if (line->matches) { - buffer->current = line; - break; - } - } + if (direction == BOTH) { + line = matching_next(buffer->current); + line = (line) ? line : matching_prev(buffer->current); + } else if (direction == NEXT) { + line = matching_next(line); + } else if (direction == PREV) { + line = matching_prev(line); } + buffer->current = (line) ? line : buffer->current; + if (opt_print_numbers) do_print_selection(buffer); } @@ -515,7 +532,7 @@ do_add_character(Buffer *buffer, char key) filter_lines(buffer); if (!buffer->current->matches) { - do_go_to(buffer, MATCH); + do_jump(buffer, BOTH); } if (!buffer->current->matches) { @@ -531,7 +548,6 @@ void print_prompt(Buffer *buffer, int cols) { size_t i; - int digits = 0; int matching = buffer->matching; int total = buffer->total; char *input = expand_tabs(buffer->input); @@ -540,16 +556,21 @@ print_prompt(Buffer *buffer, int cols) /* for the '/' separator between the numbers */ cols--; - /* count the number of digits */ - for (i = matching; i; i /= 10, digits++); - for (i = total; i; i /= 10, digits++); + /* number of digits */ + for (i = matching; i; i /= 10, cols--) + ; + for (i = total; i; i /= 10, cols--) + ; + + /* 0 also has one digit*/ + cols -= !matching ? 1 : 0; /* actual prompt */ fprintf(stderr, "\r%s\033[K> ", opt_prompt); cols -= 2 + strlen(opt_prompt); /* input without overflowing terminal width */ - for (i = 0; i < strlen(input) && cols > digits; cols--, i++) { + for (i = 0; i < strlen(input) && cols > 0; cols--, i++) { fputc(input[i], stderr); } @@ -561,13 +582,13 @@ print_prompt(Buffer *buffer, int cols) /* suggest without overflowing terminal width */ if (opt_complete_mode) { - for (; i < strlen(suggest) && cols > digits; cols--, i++) { + for (; i < strlen(suggest) && cols > 0; cols--, i++) { fputc(suggest[i], stderr); } } /* go to the end of the line */ - for (i = 0; cols > digits; cols--, i++) { + for (i = 0; cols > 0; cols--, i++) { fputc(' ', stderr); } @@ -575,7 +596,7 @@ print_prompt(Buffer *buffer, int cols) fprintf(stderr, "%d/%d", matching, total); /* restore cursor position at the end of the input */ - fputs("\033[u", stderr); + fputs("\033[0m\033[u", stderr); free(input); free(suggest); diff --git a/main.h b/main.h index 2fb482a..b38fe3e 100644 --- a/main.h +++ b/main.h @@ -54,20 +54,31 @@ typedef struct Buffer { void die(const char *); + +/* buffer */ Buffer * fill_buffer(char *); Line * parse_line(char *, char *); Line * add_line(Buffer *, int, char *, char *, Line *); +Line * matching_next(Line *); + +/* matching */ int line_match_input(Line *, char *); void filter_lines(Buffer *); + +/* drawing */ char * expand_tabs(char *); void print_line(Line *, int, int); void print_header(); void print_lines(Buffer *, int, int, int); void update_screen(Buffer *, int, int, int); + +/* tty */ struct termios terminal_set(int); + +/* input */ void get_input(Buffer *, int, int, int); int do_key(char, Buffer *); -void do_go_to(Buffer *, int); +void do_jump(Buffer *, int); void do_print_selection(Buffer *); void do_remove_word_input(Buffer *); void do_add_character(Buffer *, char); -- 2.11.4.GIT