X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;ds=inline;f=selector.cc;h=c9360291b24b021766904489bc9f8f3a3d731d6f;hb=9ac8b5e29aa7d08129e37cd21fea844ba916f64d;hp=0bb2d3f95ced54cd5fde0dddc68172f7b0204872;hpb=af3bbcdf9a629c4fb333709dedd435b72bdc5213;p=selector.git diff --git a/selector.cc b/selector.cc index 0bb2d3f..c936029 100644 --- a/selector.cc +++ b/selector.cc @@ -54,6 +54,7 @@ int inverse_order = 0; int remove_duplicates = 0; int use_regexp = 0; int case_sensitive = 0; +char *title = 0; ////////////////////////////////////////////////////////////////////// @@ -86,6 +87,10 @@ void check_opt(int argc, char **argv, int n_opt, int n, const char *help) { ////////////////////////////////////////////////////////////////////// // A quick and dirty hash table +// The table itself stores index of the strings in a char +// **table. When a string is added, if it was already in the table, +// the new index replaces the previous one. + int *new_hash_table(int hash_table_size) { int *result; result = new int[hash_table_size]; @@ -95,6 +100,10 @@ int *new_hash_table(int hash_table_size) { return result; } +// Adds new_string in the table, associated to new_index. If this +// string was not already in the table, returns -1. Otherwise, returns +// the previous index it had. + int test_and_add(char *new_string, int new_index, char **strings, int *hash_table, int hash_table_size) { unsigned int code = 0; @@ -109,16 +118,23 @@ int test_and_add(char *new_string, int new_index, code = code % hash_table_size; while(hash_table[code] >= 0) { + // There is a string with that code if(strcmp(new_string, strings[hash_table[code]]) == 0) { + // It is the same string, we keep a copy of the stored index int result = hash_table[code]; + // Put the new one hash_table[code] = new_index; + // And return the previous one return result; } + // This collision was not the same string, let's move to the next + // in the table code = (code + 1) % hash_table_size; } + // This string was not already in there, store the index in the + // table and return -1 hash_table[code] = new_index; - return -1; } @@ -160,7 +176,9 @@ void free_matcher(matcher_t *matcher) { } } -void initialize_matcher(int use_regexp, int case_sensitive, matcher_t *matcher, const char *pattern) { +void initialize_matcher(int use_regexp, int case_sensitive, + matcher_t *matcher, const char *pattern) { + if(use_regexp) { matcher->nb_patterns = -1; matcher->regexp_error = regcomp(&matcher->preg, pattern, case_sensitive ? 0 : REG_ICASE); @@ -214,6 +232,7 @@ int next_visible(int current_line, int nb_lines, char **lines, matcher_t *matche void update_screen(int *current_line, int *temporary_line, int motion, int nb_lines, char **lines, + int cursor_position, char *pattern) { char buffer[buffer_size]; @@ -363,23 +382,64 @@ void update_screen(int *current_line, int *temporary_line, int motion, // Draw the modeline - sprintf(buffer, "%d/%d pattern: %s%s", - nb_printed_lines, - nb_lines, - pattern, - use_regexp ? " [regexp]" : ""); + move(0, 0); + if(with_colors) { + attron(COLOR_PAIR(1)); + } else { + attron(A_REVERSE); + } - for(int k = strlen(buffer); k < console_width; k++) buffer[k] = ' '; + for(int k = 0; k < console_width; k++) buffer[k] = ' '; buffer[console_width] = '\0'; + addnstr(buffer, console_width); move(0, 0); + + if(title) { + addstr(title); + addstr(" "); + } + + printw("%d/%d ", nb_printed_lines, nb_lines); + + addnstr(pattern, cursor_position); + + // Now we print the cursor. All that mess to have reverse video with + // and without color. + if(with_colors) { - attron(COLOR_PAIR(1)); - addnstr(buffer, console_width); attroff(COLOR_PAIR(1)); + attron(COLOR_PAIR(3)); + } else { + attroff(A_REVERSE); + } + + if(pattern[cursor_position]) { + addnstr(&pattern[cursor_position], 1); + } else { + addstr(" "); + } + + if(with_colors) { + attroff(COLOR_PAIR(3)); + attron(COLOR_PAIR(1)); } else { attron(A_REVERSE); - addnstr(buffer, console_width); + } + + if(pattern[cursor_position]) { + addstr(pattern + cursor_position + 1); + } + + // Finished printing the cursor + + if(use_regexp) { + addstr(" [regexp]"); + } + + if(with_colors) { + attroff(COLOR_PAIR(1)); + } else { attroff(A_REVERSE); } @@ -477,6 +537,14 @@ int main(int argc, char **argv) { i++; } + else if(strcmp(argv[i], "-t") == 0) { + check_opt(argc, argv, i, 1, "