2 ///////////////////////////////////////////////////////////////////////////
5 // This program is free software: you can redistribute it and/or modify //
6 // it under the terms of the version 3 of the GNU General Public License //
7 // as published by the Free Software Foundation. //
9 // This program is distributed in the hope that it will be useful, but //
10 // WITHOUT ANY WARRANTY; without even the implied warranty of //
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
12 // General Public License for more details. //
14 // You should have received a copy of the GNU General Public License //
15 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
17 // Written by and Copyright (C) Francois Fleuret //
18 // Contact <francois.fleuret@idiap.ch> for comments & bug reports //
21 ///////////////////////////////////////////////////////////////////////////
31 const int buffer_size = 1024;
32 const int nb_lines_max = 100000;
34 int refresh_screen(int *screen_line, int *line, int nb_lines, char **lines, char *regexp, int noblink) {
35 char buffer[buffer_size];
37 int maxx = getmaxx(stdscr);
38 int maxy = min(buffer_size-2, getmaxy(stdscr));
48 int nb_printed_lines = 1, last_printer_line = -1;
50 int current_line = -1;
52 while(nb_printed_lines < maxy && y < nb_lines) {
53 if(strstr(lines[y], regexp) &&
54 (last_printer_line < 0 || strcmp(lines[y], lines[last_printer_line]))) {
57 while(lines[y][k] && k < buffer_size - 2 && k < maxx - 1) {
58 buffer[k] = lines[y][k];
70 if(nb_printed_lines == *line + 1) {
71 attron(COLOR_PAIR(2));
73 attroff(COLOR_PAIR(2));
79 last_printer_line = y;
85 if(noblink) { // Erase the rest of the window. That's slightly ugly.
92 for(int l = nb_printed_lines; l < maxy; l++) {
100 attron(COLOR_PAIR(1));
101 sprintf(buffer, "%d/%d pattern: %s", nb_printed_lines - 1, nb_lines, regexp);
102 for(int k = strlen(buffer); k < maxx - 1; k++) buffer[k] = ' ';
103 buffer[maxx-1] = '\0';
105 attroff(COLOR_PAIR(1));
107 refresh(); // After doing something on the display, we refresh it
112 int main(int argc, char **argv) {
113 char buffer[buffer_size];
114 char *lines[nb_lines_max];
118 char stdin_name[] = "/dev/stdin";
120 if(argc == 2 && strcmp(argv[1], "-")) {
123 file_name = stdin_name;
126 ifstream file(file_name);
129 cerr << "Can not open \"" << file_name << "\"" << endl;
134 while(nb_lines < nb_lines_max && !file.eof()) {
135 file.getline(buffer, buffer_size);
136 lines[nb_lines] = new char[strlen(buffer) + 1];
137 strcpy(lines[nb_lines], buffer);
141 char regexp[buffer_size]="";
148 cerr << "No colors." << endl;
154 keypad(stdscr, TRUE);
157 init_pair(1, COLOR_WHITE, COLOR_BLACK);
158 init_pair(2, COLOR_BLACK, COLOR_YELLOW);
162 int line = 0, screen_line = 0;
164 refresh_screen(&screen_line, &line, nb_lines, lines, regexp, noblink);
166 int current_line = -1;
172 if(key >= ' ' && key <= 'z') {
173 regexp[regexp_point++] = key;
174 regexp[regexp_point] = '\0';
177 else if(key == KEY_BACKSPACE || key == KEY_DC) {
178 if(regexp_point > 0) {
180 regexp[regexp_point] = '\0';
184 else if(key == KEY_UP || key == '
\10') {
190 else if(key == KEY_DOWN || key == '
\ e') {
194 current_line = refresh_screen(&screen_line, &line, nb_lines, lines, regexp, noblink);
195 } while(key != '\n' && key != KEY_ENTER && key != '
\a');
201 ofstream out("/tmp/selector.out");
202 out << lines[current_line] << endl;
205 for(int l = 0; l < nb_lines; l++) {