X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=selector.cc;h=cc25520049171e9f67ecd4e6ffb2e954477034f2;hb=fca0fb9a7bfb0c103501aaa8e00f7f8670693774;hp=7d59b2fe1708d4f3c801e4719d85bab13f5a9e0c;hpb=46c00136c5a8684c50f0a518f53fa8af85d18d92;p=selector.git diff --git a/selector.cc b/selector.cc index 7d59b2f..cc25520 100644 --- a/selector.cc +++ b/selector.cc @@ -25,17 +25,84 @@ #include #include #include +#include using namespace std; -const int buffer_size = 1024; -const int nb_lines_max = 100000; +int buffer_size = 1024; +int nb_lines_max = 100000; + +int match(char *string, char *regexp) { + return strstr(string, regexp) != 0; +} + +void check_opt(int argc, char **argv, int n_opt, int n, const char *help) { + if(n_opt + n >= argc) { + cerr << "Missing argument for " << argv[n_opt] << ". Expecting " << help << "." << endl; + exit(1); + } +} + +int previous_visible(int current_line, int nb_lines, char **lines, char *regexp) { + int line = current_line - 1; + while(line >= 0 && !match(lines[line], regexp)) line--; + return line; +} + +int next_visible(int current_line, int nb_lines, char **lines, char *regexp) { + int line = current_line + 1; + while(line < nb_lines && !match(lines[line], regexp)) line++; + + if(line < nb_lines) + return line; + else + return -1; +} + +void update_screen(int *current_line, int motion, + int nb_lines, char **lines, + char *regexp, int noblink) { -void refresh_screen(int *screen_line, int *line, int nb_lines, char **lines, char *regexp, int noblink) { char buffer[buffer_size]; - int maxx = getmaxx(stdscr); - int maxy = min(buffer_size-2, getmaxy(stdscr)); + int console_width = getmaxx(stdscr); + int console_height = getmaxy(stdscr); + + int nb_printed_lines = 1, last_printer_line = -1; + + // First, we find a visible line. In priority: The current, or the + // first visible after it, or the first visible before it. + + int new_line; + if(match(lines[*current_line], regexp)) { + new_line = *current_line; + } else { + new_line = next_visible(*current_line, nb_lines, lines, regexp); + if(new_line < 0) { + new_line = previous_visible(*current_line, nb_lines, lines, regexp); + } + } + + // If we found a visible line and we should move, let's move + + if(new_line >= 0 && motion != 0) { + int l = new_line; + l += motion; + + if(motion > 0) { + // We want to go down, let's find the first visible line below + l = next_visible(new_line, nb_lines, lines, regexp); + if(l >= 0) { + new_line = l; + } + } else { + // We want to go up, let's find the first visible line above + l = previous_visible(new_line, nb_lines, lines, regexp); + if(l >= 0) { + new_line = l; + } + } + } if(!noblink) { clear(); @@ -43,52 +110,83 @@ void refresh_screen(int *screen_line, int *line, int nb_lines, char **lines, cha use_default_colors(); - printw("\n"); + addstr("\n"); - int nb_printed_lines = 1, last_printer_line = -1; - int y = 0; + // Here new_line is either a line number matching the regexp, or -1 + + if(new_line >= 0) { - while(nb_printed_lines < maxy && y < nb_lines) { - if(strstr(lines[y], regexp) && - (last_printer_line < 0 || strcmp(lines[y], lines[last_printer_line]))) { - int k = 0; + int first_line = new_line, last_line = new_line, nb_match = 1; - while(lines[y][k] && k < buffer_size - 2 && k < maxx - 1) { - buffer[k] = lines[y][k]; - k++; + while(nb_match < console_height-1 && (first_line > 0 || last_line < nb_lines - 1)) { + + if(first_line > 0) { + first_line--; + while(first_line > 0 && !match(lines[first_line], regexp)) { + first_line--; + } + if(match(lines[first_line], regexp)) { + nb_match++; + } } - if(noblink) { - while(k < maxx - 1) { - buffer[k++] = ' '; + if(last_line < nb_lines - 1) { + last_line++; + while(last_line < nb_lines - 1 && !match(lines[last_line], regexp)) { + last_line++; + } + + if(match(lines[last_line], regexp)) { + nb_match++; } } - buffer[k++] = '\n'; - buffer[k++] = '\0'; - - if(nb_printed_lines == *line + 1) { - attron(COLOR_PAIR(2)); - printw(buffer); - attroff(COLOR_PAIR(2)); - } else { - printw(buffer); + } + + for(int l = first_line; l <= last_line; l++) { + if(match(lines[l], regexp)) { + int k = 0; + + while(lines[l][k] && k < buffer_size - 2 && k < console_width - 1) { + buffer[k] = lines[l][k]; + k++; + } + + if(noblink) { + while(k < console_width - 1) { + buffer[k++] = ' '; + } + } + + buffer[k++] = '\n'; + buffer[k++] = '\0'; + + if(l == new_line) { + attron(COLOR_PAIR(2)); + addstr(buffer); + attroff(COLOR_PAIR(2)); + } else { + addstr(buffer); + } + + last_printer_line = l; + nb_printed_lines++; } + } - last_printer_line = y; - nb_printed_lines++; + if(motion != 0) { + *current_line = new_line; } - y++; } if(noblink) { // Erase the rest of the window. That's slightly ugly. int k = 0; - while(k < maxx - 1) { + while(k < console_width - 1) { buffer[k++] = ' '; } buffer[k++] = '\n'; buffer[k++] = '\0'; - for(int l = nb_printed_lines; l < maxy; l++) { - printw(buffer); + for(int l = nb_printed_lines; l < console_height; l++) { + addstr(buffer); } } @@ -97,9 +195,9 @@ void refresh_screen(int *screen_line, int *line, int nb_lines, char **lines, cha move(0, 0); attron(COLOR_PAIR(1)); sprintf(buffer, "%d/%d pattern: %s", nb_printed_lines - 1, nb_lines, regexp); - for(int k = strlen(buffer); k < maxx - 1; k++) buffer[k] = ' '; - buffer[maxx-1] = '\0'; - printw(buffer); + for(int k = strlen(buffer); k < console_width - 1; k++) buffer[k] = ' '; + buffer[console_width-1] = '\0'; + addstr(buffer); attroff(COLOR_PAIR(1)); refresh(); // After doing something on the display, we refresh it @@ -110,19 +208,45 @@ int main(int argc, char **argv) { char *lines[nb_lines_max]; int noblink = 1; - char *file_name; - char stdin_name[] = "/dev/stdin"; + char input_filename[buffer_size], output_filename[buffer_size]; + strcpy(input_filename, "/dev/stdin"); + strcpy(output_filename, "/tmp/selector.out"); - if(argc == 2 && strcmp(argv[1], "-")) { - file_name = argv[1]; - } else { - file_name = stdin_name; + int i = 1; + while(i < argc) { + if(strcmp(argv[i], "-o") == 0) { + check_opt(argc, argv, i, 1, ""); + strncpy(output_filename, argv[i+1], buffer_size); + i += 2; + } + + else if(strcmp(argv[i], "-f") == 0) { + check_opt(argc, argv, i, 1, ""); + strncpy(input_filename, argv[i+1], buffer_size); + i += 2; + } + + else if(strcmp(argv[i], "-b") == 0) { + noblink = 1; + i++; + } + + else if(strcmp(argv[i], "-l") == 0) { + check_opt(argc, argv, i, 1, ""); + nb_lines_max = atoi(argv[i+1]); + i += 2; + } + + else { + cerr << argv[0] << " [-o ] [-b] [-l = ' ' && key <= 'z') { regexp[regexp_point++] = key; regexp[regexp_point] = '\0'; } - else if(key == KEY_BACKSPACE || key == KEY_DC) { + else if(key == KEY_BACKSPACE || key == KEY_DC || key == '') { if(regexp_point > 0) { regexp_point--; regexp[regexp_point] = '\0'; @@ -176,26 +304,36 @@ int main(int argc, char **argv) { } else if(key == KEY_UP || key == '') { - if(line > 0) { - line--; - } + motion = -1; } else if(key == KEY_DOWN || key == '') { - line++; + motion = 1; } - refresh_screen(&screen_line, &line, nb_lines, lines, regexp, noblink); + update_screen(&line, motion, nb_lines, lines, regexp, noblink); } while(key != '\n' && key != KEY_ENTER && key != ''); echo(); curs_set(1); endwin(); + ofstream out(output_filename); + if(out.fail()) { + cerr << "Can not open " << output_filename << " for writing." << endl; + exit(1); + } else { + if((key == KEY_ENTER || key == '\n') && line >= 0 && line < nb_lines) { + out << lines[line] << endl; + } else { + out << endl; + } + out.flush(); + } + for(int l = 0; l < nb_lines; l++) { delete[] lines[l]; } - return 0; + exit(0); } -