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 void 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;
51 while(nb_printed_lines < maxy && y < nb_lines) {
52 if(strstr(lines[y], regexp) &&
53 (last_printer_line < 0 || strcmp(lines[y], lines[last_printer_line]))) {
56 while(lines[y][k] && k < buffer_size - 2 && k < maxx - 1) {
57 buffer[k] = lines[y][k];
69 if(nb_printed_lines == *line + 1) {
70 attron(COLOR_PAIR(2));
72 attroff(COLOR_PAIR(2));
77 last_printer_line = y;
83 if(noblink) { // Erase the rest of the window. That's slightly ugly.
90 for(int l = nb_printed_lines; l < maxy; l++) {
98 attron(COLOR_PAIR(1));
99 sprintf(buffer, "%d/%d pattern: %s", nb_printed_lines - 1, nb_lines, regexp);
100 for(int k = strlen(buffer); k < maxx - 1; k++) buffer[k] = ' ';
101 buffer[maxx-1] = '\0';
103 attroff(COLOR_PAIR(1));
105 refresh(); // After doing something on the display, we refresh it
108 int main(int argc, char **argv) {
109 char buffer[buffer_size];
110 char *lines[nb_lines_max];
114 char stdin_name[] = "/dev/stdin";
116 if(argc == 2 && strcmp(argv[1], "-")) {
119 file_name = stdin_name;
122 ifstream file(file_name);
125 cerr << "Can not open \"" << file_name << "\"" << endl;
130 while(nb_lines < nb_lines_max && !file.eof()) {
131 file.getline(buffer, buffer_size);
132 lines[nb_lines] = new char[strlen(buffer) + 1];
133 strcpy(lines[nb_lines], buffer);
137 char regexp[buffer_size]="";
144 cerr << "No colors." << endl;
150 keypad(stdscr, TRUE);
153 init_pair(1, COLOR_WHITE, COLOR_BLACK);
154 init_pair(2, COLOR_BLACK, COLOR_YELLOW);
158 int line = 0, screen_line = 0;
160 refresh_screen(&screen_line, &line, nb_lines, lines, regexp, noblink);
166 if(key >= ' ' && key <= 'z') {
167 regexp[regexp_point++] = key;
168 regexp[regexp_point] = '\0';
171 else if(key == KEY_BACKSPACE || key == KEY_DC) {
172 if(regexp_point > 0) {
174 regexp[regexp_point] = '\0';
178 else if(key == KEY_UP || key == '
\10') {
184 else if(key == KEY_DOWN || key == '
\ e') {
188 refresh_screen(&screen_line, &line, nb_lines, lines, regexp, noblink);
189 } while(key != '\n' && key != KEY_ENTER && key != '
\a');
195 for(int l = 0; l < nb_lines; l++) {