X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=selector.cc;h=0878f8252cbfce7af72a85055c18ccb49472a8fb;hb=6d645205fcd54e4b61d1bf62a09aebee0a01dec9;hp=0d5a30e4fd6fddbd694b1d759b0df39258e74724;hpb=e709c3d6192cd94251e827c430e81cc933bf56e5;p=selector.git diff --git a/selector.cc b/selector.cc index 0d5a30e..0878f82 100644 --- a/selector.cc +++ b/selector.cc @@ -29,48 +29,104 @@ using namespace std; const int buffer_size = 1024; -const int nb_lines_max = 1000; +const int nb_lines_max = 100000; -void build_display(int nb_lines, char **lines, char *regexp) { +int refresh_screen(int *screen_line, int *line, int nb_lines, char **lines, char *regexp, int noblink) { char buffer[buffer_size]; - clear(); // Cleaning the window - refresh(); // After doing something on the display, we refresh it + int maxx = getmaxx(stdscr); - int maxy = getmaxy(stdscr); - printw("maxx %d maxy %d\n", maxx, maxy); - printw("nb_lines %d\n", nb_lines); - int nb_printed_lines = 2; - for(int y = 0; nb_printed_lines < maxy && y < nb_lines; y++) { + int maxy = min(buffer_size-2, getmaxy(stdscr)); + + if(!noblink) { + clear(); + } + + use_default_colors(); + + printw("\n"); + + int nb_printed_lines = 1, last_printer_line = -1; + int y = 0; + int current_line = -1; + + 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; + + while(lines[y][k] && k < buffer_size - 2 && k < maxx - 1) { + buffer[k] = lines[y][k]; + k++; + } + + if(noblink) { + while(k < maxx - 1) { + buffer[k++] = ' '; + } + } + buffer[k++] = '\n'; + buffer[k++] = '\0'; + + if(nb_printed_lines == *line + 1) { + attron(COLOR_PAIR(2)); + printw(buffer); + attroff(COLOR_PAIR(2)); + current_line = y; + } else { + printw(buffer); + } + + last_printer_line = y; + nb_printed_lines++; + } + y++; + } + + if(noblink) { // Erase the rest of the window. That's slightly ugly. int k = 0; - while(lines[y][k] && k < buffer_size - 2 && k < maxx) { - buffer[k] = lines[y][k]; - k++; + while(k < maxx - 1) { + buffer[k++] = ' '; } buffer[k++] = '\n'; buffer[k++] = '\0'; - printw(buffer); - nb_printed_lines++; + for(int l = nb_printed_lines; l < maxy; l++) { + printw(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 < maxx - 1; k++) buffer[k] = ' '; + buffer[maxx-1] = '\0'; + printw(buffer); + attroff(COLOR_PAIR(1)); + + refresh(); // After doing something on the display, we refresh it + + return current_line; } int main(int argc, char **argv) { - int dummy, xpos, ypos; - 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; } @@ -86,40 +142,65 @@ int main(int argc, char **argv) { 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 + noecho(); + curs_set(0); + keypad(stdscr, TRUE); - start_color(); // We will use colors - init_pair(1, COLOR_RED, COLOR_BLACK); // red on black for error messages + start_color(); + init_pair(1, COLOR_WHITE, COLOR_BLACK); + init_pair(2, COLOR_BLACK, COLOR_YELLOW); - // 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! - - printw("Press a key to contine\n"); int key; + int line = 0, screen_line = 0; + + refresh_screen(&screen_line, &line, nb_lines, lines, regexp, noblink); + + int current_line = -1; + do { - build_display(nb_lines, lines, regexp); + key = getch(); - if(key >= 'a' && key <= 'z') { + + if(key >= ' ' && key <= 'z') { 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 + else if(key == KEY_BACKSPACE || key == KEY_DC) { + if(regexp_point > 0) { + regexp_point--; + regexp[regexp_point] = '\0'; + } + } + + else if(key == KEY_UP || key == '') { + if(line > 0) { + line--; + } + } + + else if(key == KEY_DOWN || key == '') { + line++; + } + + current_line = refresh_screen(&screen_line, &line, nb_lines, lines, regexp, noblink); + } while(key != '\n' && key != KEY_ENTER && key != ''); + + echo(); + curs_set(1); + endwin(); + + ofstream out("/tmp/selector.out"); + out << lines[current_line] << endl; + out.flush(); for(int l = 0; l < nb_lines; l++) { delete[] lines[l];