X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;ds=inline;f=selector.cc;h=50620b69f7a75e9b46f9830801028ccd4edca310;hb=49a8d9563a444aeaa5e732ef658e8ddd900d4294;hp=ab793b0476d84b74096b252f0e1cc3299b210d30;hpb=888ddd157bc2aab66fcab17b28eb6ba243dfe3ec;p=selector.git diff --git a/selector.cc b/selector.cc index ab793b0..50620b6 100644 --- a/selector.cc +++ b/selector.cc @@ -28,35 +28,206 @@ using namespace std; -void build_display(char **choices, char *regexp) { - clear(); // Cleaning the window - refresh(); // After doing something on the display, we refresh it - for(int y = 0; y < 10; y++) { - printw("y = %d\n", y); +int buffer_size = 1024; +int nb_lines_max = 100000; + +int match(char *string, char *regexp) { + return strstr(string, regexp) != 0; +} + +void check(int condition, const char *message) { + if(!condition) { + echo(); + curs_set(1); + endwin(); + cout << message << endl; } - printw("regexp = \"%s\"", regexp); } -int main(int argc, char **argv) { - int dummy, xpos, ypos; - const int buffer_size = 1024; - const int nb_lines_max = 1000; +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) { char buffer[buffer_size]; + + 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(); + } + + use_default_colors(); + + addstr("\n"); + + check(new_line < nb_lines, "Ouch!"); + + // Here new_line is either a line number matching the regexp, or -1 + + if(new_line >= 0) { + + int first_line = new_line, last_line = new_line, nb_match = 1; + + 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(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++; + } + } + } + + check(first_line >= 0 && last_line < nb_lines, "ouch2"); + + 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++; + } + } + + check(nb_printed_lines != nb_match, "ouch3"); + + if(motion != 0) { + *current_line = new_line; + } + } + + if(noblink) { // Erase the rest of the window. That's slightly ugly. + int k = 0; + while(k < console_width - 1) { + buffer[k++] = ' '; + } + buffer[k++] = '\n'; + buffer[k++] = '\0'; + for(int l = nb_printed_lines; l < console_height; l++) { + addstr(buffer); + } + } + + // Draw the modeline + + 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 < 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 +} + +int main(int argc, char **argv) { + char buffer[buffer_size]; char *lines[nb_lines_max]; + int noblink = 1; - ifstream file(argv[1]); + char *file_name; + char stdin_name[] = "/dev/stdin"; - if(argc != 2) { - cerr << argv[0] << " " << endl; - return 1; + if(argc == 2 && strcmp(argv[1], "-")) { + file_name = argv[1]; + } else { + file_name = stdin_name; } + ifstream file(file_name); + if(file.fail()) { - cerr << "Can not open \"" - << argv[1] - << "\"" - << endl; + cerr << "Can not open \"" << file_name << "\"" << endl; return 1; } @@ -65,44 +236,75 @@ int main(int argc, char **argv) { file.getline(buffer, buffer_size); lines[nb_lines] = new char[strlen(buffer) + 1]; strcpy(lines[nb_lines], buffer); + nb_lines++; } - char regexp[buffer_size]=""; + char regexp[buffer_size]; + regexp[0] = '\0'; int regexp_point; regexp_point = 0; - initscr(); // Necessary to start a curses session + initscr(); - if (has_colors()) { - cout << "You can use color on this terminal" << endl; - } else { - cout << "No colors." << endl; + if(!has_colors()) { + cerr << "No colors." << endl; return 1; } - noecho(); // I don't want echo when I press a key - curs_set(0); // I don't want to see the cursor - - start_color(); // We will use colors - init_pair(1, COLOR_RED, COLOR_BLACK); // red on black for error messages + noecho(); + curs_set(0); + keypad(stdscr, TRUE); - // attron(COLOR_PAIR(1)); // Let's print something in red on black - // printw("Hello world\n"); // That's how we print something! - // attroff(COLOR_PAIR(1)); // Let's get back to default colors! + start_color(); + init_pair(1, COLOR_WHITE, COLOR_BLACK); + init_pair(2, COLOR_BLACK, COLOR_YELLOW); - printw("Press a key to contine\n"); int key; + int line = 0; + + update_screen(&line, 0, nb_lines, lines, regexp, noblink); + do { - build_display(0, regexp); + key = getch(); - regexp[regexp_point++] = key; - regexp[regexp_point] = '\0'; - } while(key != 'q'); - echo(); // We want to have echo - curs_set(1); // We want to see the cursor again - endwin(); // Back to normal + int motion = 0; + + if(key >= ' ' && key <= 'z') { + regexp[regexp_point++] = key; + regexp[regexp_point] = '\0'; + } + + else if(key == KEY_BACKSPACE || key == KEY_DC || key == '') { + if(regexp_point > 0) { + regexp_point--; + regexp[regexp_point] = '\0'; + } + } + + else if(key == KEY_UP || key == '') { + motion = -1; + } + + else if(key == KEY_DOWN || key == '') { + motion = 1; + } + + update_screen(&line, motion, nb_lines, lines, regexp, noblink); + } while(key != '\n' && key != KEY_ENTER && key != ''); + + echo(); + curs_set(1); + endwin(); + + ofstream out("/tmp/selector.out"); + 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];