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 ///////////////////////////////////////////////////////////////////////////
33 const int buffer_size = 1024;
34 const int nb_lines_max = 100000;
36 int match(char *string, char *regexp) {
37 return strstr(string, regexp) != 0;
40 void update_screen(int *current_line, int motion,
41 int nb_lines, char **lines,
42 char *regexp, int noblink) {
44 char buffer[buffer_size];
46 int console_width = getmaxx(stdscr);
47 int console_height = getmaxy(stdscr);
57 int nb_printed_lines = 1, last_printer_line = -1;
61 new_line = *current_line + motion;
62 while(new_line < nb_lines && !match(lines[new_line], regexp)) {
65 if(new_line == nb_lines) {
66 new_line = *current_line;
67 while(new_line >= 0 && ! match(lines[new_line], regexp)) {
73 new_line = *current_line - 1;
74 while(new_line >= 0 && ! match(lines[new_line], regexp)) {
79 new_line = *current_line;
80 while(new_line < nb_lines && !match(lines[new_line], regexp)) {
83 if(new_line == nb_lines) {
89 // Here new_line is either a line number matching the regexp, or -1
92 int first_line = new_line, last_line = new_line, nb_match = 1;
94 while(nb_match < console_height && (first_line > 0 || last_line < nb_lines - 1)) {
98 while(first_line > 0 && !match(lines[first_line], regexp)) {
101 if(match(lines[first_line], regexp)) {
106 if(last_line < nb_lines - 1) {
108 while(last_line < nb_lines - 1 && !match(lines[last_line], regexp)) {
112 if(match(lines[last_line], regexp)) {
118 for(int l = first_line; l <= last_line; l++) {
119 if(match(lines[l], regexp)) {
122 while(lines[l][k] && k < buffer_size - 2 && k < console_width - 1) {
123 buffer[k] = lines[l][k];
128 while(k < console_width - 1) {
136 attron(COLOR_PAIR(2));
138 attroff(COLOR_PAIR(2));
143 last_printer_line = l;
148 *current_line = new_line;
151 if(noblink) { // Erase the rest of the window. That's slightly ugly.
153 while(k < console_width - 1) {
158 for(int l = nb_printed_lines; l < console_height; l++) {
166 attron(COLOR_PAIR(1));
167 sprintf(buffer, "%d/%d pattern: %s", nb_printed_lines - 1, nb_lines, regexp);
168 for(int k = strlen(buffer); k < console_width - 1; k++) buffer[k] = ' ';
169 buffer[console_width-1] = '\0';
171 attroff(COLOR_PAIR(1));
173 refresh(); // After doing something on the display, we refresh it
176 int main(int argc, char **argv) {
177 char buffer[buffer_size];
178 char *lines[nb_lines_max];
181 log = new fstream("/tmp/selector.log");
184 char stdin_name[] = "/dev/stdin";
186 if(argc == 2 && strcmp(argv[1], "-")) {
189 file_name = stdin_name;
192 ifstream file(file_name);
195 cerr << "Can not open \"" << file_name << "\"" << endl;
200 while(nb_lines < nb_lines_max && !file.eof()) {
201 file.getline(buffer, buffer_size);
202 lines[nb_lines] = new char[strlen(buffer) + 1];
203 strcpy(lines[nb_lines], buffer);
207 char regexp[buffer_size]="";
214 cerr << "No colors." << endl;
220 keypad(stdscr, TRUE);
223 init_pair(1, COLOR_WHITE, COLOR_BLACK);
224 init_pair(2, COLOR_BLACK, COLOR_YELLOW);
230 update_screen(&line, 0,
240 if(key >= ' ' && key <= 'z') {
241 regexp[regexp_point++] = key;
242 regexp[regexp_point] = '\0';
245 else if(key == KEY_BACKSPACE || key == KEY_DC) {
246 if(regexp_point > 0) {
248 regexp[regexp_point] = '\0';
252 else if(key == KEY_UP || key == '
\10') {
256 else if(key == KEY_DOWN || key == '
\ e') {
260 update_screen(&line, motion, nb_lines, lines, regexp, noblink);
261 } while(key != '\n' && key != KEY_ENTER && key != '
\a');
267 // ofstream out("/tmp/selector.out");
268 // if(key == KEY_ENTER || key == '\n') {
269 // out << lines[highlighted_line] << endl;
275 for(int l = 0; l < nb_lines; l++) {