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 build_display(int *screen_line, int *line, int nb_lines, char **lines, char *regexp) {
35 char buffer[buffer_size];
37 int maxx = getmaxx(stdscr);
38 int maxy = min(buffer_size-2, getmaxy(stdscr));
40 // clear(); // Cleaning the window
46 int nb_printed_lines = 1, last_printer_line = -1;
49 while(nb_printed_lines < maxy && y < nb_lines) {
50 if(strstr(lines[y], regexp) &&
51 (last_printer_line < 0 || strcmp(lines[y], lines[last_printer_line]))) {
54 while(lines[y][k] && k < buffer_size - 2 && k < maxx - 1) {
55 buffer[k] = lines[y][k];
65 if(nb_printed_lines == *line + 1) {
66 attron(COLOR_PAIR(2));
68 attroff(COLOR_PAIR(2));
73 last_printer_line = y;
79 { // Erase the rest of the window. That's slightly ugly.
86 for(int l = nb_printed_lines; l < maxy; l++) {
94 attron(COLOR_PAIR(1));
95 sprintf(buffer, "%d/%d pattern: %s", nb_printed_lines - 1, nb_lines, regexp);
96 for(int k = strlen(buffer); k < maxx - 1; k++) buffer[k] = ' ';
97 buffer[maxx-1] = '\0';
99 attroff(COLOR_PAIR(1));
101 refresh(); // After doing something on the display, we refresh it
104 int main(int argc, char **argv) {
105 char buffer[buffer_size];
106 char *lines[nb_lines_max];
109 char stdin_name[] = "/dev/stdin";
111 if(argc == 2 && strcmp(argv[1], "-")) {
114 file_name = stdin_name;
117 ifstream file(file_name);
120 cerr << "Can not open \"" << file_name << "\"" << endl;
125 while(nb_lines < nb_lines_max && !file.eof()) {
126 file.getline(buffer, buffer_size);
127 lines[nb_lines] = new char[strlen(buffer) + 1];
128 strcpy(lines[nb_lines], buffer);
132 char regexp[buffer_size]="";
139 cerr << "No colors." << endl;
145 keypad(stdscr, TRUE);
148 init_pair(1, COLOR_WHITE, COLOR_BLACK);
149 init_pair(2, COLOR_BLACK, COLOR_YELLOW);
153 int line = 0, screen_line = 0;
155 build_display(&screen_line, &line, nb_lines, lines, regexp);
161 if(key >= ' ' && key <= 'z') {
162 regexp[regexp_point++] = key;
163 regexp[regexp_point] = '\0';
166 else if(key == KEY_BACKSPACE || key == KEY_DC) {
167 if(regexp_point > 0) {
169 regexp[regexp_point] = '\0';
173 else if(key == KEY_UP || key == '
\10') {
179 else if(key == KEY_DOWN || key == '
\ e') {
183 build_display(&screen_line, &line, nb_lines, lines, regexp);
184 } while(key != '\n' && key != KEY_ENTER && key != '
\a');
190 for(int l = 0; l < nb_lines; l++) {