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 int buffer_size = 1024;
32 int nb_lines_max = 100000;
34 int match(char *string, char *regexp) {
35 return strstr(string, regexp) != 0;
38 void check(int condition, const char *message) {
43 cout << message << endl;
47 int previous_visible(int current_line, int nb_lines, char **lines, char *regexp) {
48 int line = current_line - 1;
49 while(line >= 0 && !match(lines[line], regexp)) line--;
53 int next_visible(int current_line, int nb_lines, char **lines, char *regexp) {
54 int line = current_line + 1;
55 while(line < nb_lines && !match(lines[line], regexp)) line++;
63 void update_screen(int *current_line, int motion,
64 int nb_lines, char **lines,
65 char *regexp, int noblink) {
67 char buffer[buffer_size];
69 int console_width = getmaxx(stdscr);
70 int console_height = getmaxy(stdscr);
72 int nb_printed_lines = 1, last_printer_line = -1;
74 // First, we find a visible line. In priority: The current, or the
75 // first visible after it, or the first visible before it.
78 if(match(lines[*current_line], regexp)) {
79 new_line = *current_line;
81 new_line = next_visible(*current_line, nb_lines, lines, regexp);
83 new_line = previous_visible(*current_line, nb_lines, lines, regexp);
87 // If we found a visible line and we should move, let's move
89 if(new_line >= 0 && motion != 0) {
94 // We want to go down, let's find the first visible line below
95 l = next_visible(new_line, nb_lines, lines, regexp);
100 // We want to go up, let's find the first visible line above
101 l = previous_visible(new_line, nb_lines, lines, regexp);
112 use_default_colors();
116 check(new_line < nb_lines, "Ouch!");
118 // Here new_line is either a line number matching the regexp, or -1
122 int first_line = new_line, last_line = new_line, nb_match = 1;
124 while(nb_match < console_height-1 && (first_line > 0 || last_line < nb_lines - 1)) {
128 while(first_line > 0 && !match(lines[first_line], regexp)) {
131 if(match(lines[first_line], regexp)) {
136 if(last_line < nb_lines - 1) {
138 while(last_line < nb_lines - 1 && !match(lines[last_line], regexp)) {
142 if(match(lines[last_line], regexp)) {
148 check(first_line >= 0 && last_line < nb_lines, "ouch2");
150 for(int l = first_line; l <= last_line; l++) {
151 if(match(lines[l], regexp)) {
154 while(lines[l][k] && k < buffer_size - 2 && k < console_width - 1) {
155 buffer[k] = lines[l][k];
160 while(k < console_width - 1) {
169 attron(COLOR_PAIR(2));
171 attroff(COLOR_PAIR(2));
176 last_printer_line = l;
181 check(nb_printed_lines != nb_match, "ouch3");
184 *current_line = new_line;
188 if(noblink) { // Erase the rest of the window. That's slightly ugly.
190 while(k < console_width - 1) {
195 for(int l = nb_printed_lines; l < console_height; l++) {
203 attron(COLOR_PAIR(1));
204 sprintf(buffer, "%d/%d pattern: %s", nb_printed_lines - 1, nb_lines, regexp);
205 for(int k = strlen(buffer); k < console_width - 1; k++) buffer[k] = ' ';
206 buffer[console_width-1] = '\0';
208 attroff(COLOR_PAIR(1));
210 refresh(); // After doing something on the display, we refresh it
213 int main(int argc, char **argv) {
214 char buffer[buffer_size];
215 char *lines[nb_lines_max];
219 char stdin_name[] = "/dev/stdin";
221 if(argc == 2 && strcmp(argv[1], "-")) {
224 file_name = stdin_name;
227 ifstream file(file_name);
230 cerr << "Can not open \"" << file_name << "\"" << endl;
235 while(nb_lines < nb_lines_max && !file.eof()) {
236 file.getline(buffer, buffer_size);
237 lines[nb_lines] = new char[strlen(buffer) + 1];
238 strcpy(lines[nb_lines], buffer);
242 char regexp[buffer_size];
250 cerr << "No colors." << endl;
256 keypad(stdscr, TRUE);
259 init_pair(1, COLOR_WHITE, COLOR_BLACK);
260 init_pair(2, COLOR_BLACK, COLOR_YELLOW);
266 update_screen(&line, 0, nb_lines, lines, regexp, noblink);
274 if(key >= ' ' && key <= 'z') {
275 regexp[regexp_point++] = key;
276 regexp[regexp_point] = '\0';
279 else if(key == KEY_BACKSPACE || key == KEY_DC || key == '
\b') {
280 if(regexp_point > 0) {
282 regexp[regexp_point] = '\0';
286 else if(key == KEY_UP || key == '
\10') {
290 else if(key == KEY_DOWN || key == '
\ e') {
294 update_screen(&line, motion, nb_lines, lines, regexp, noblink);
295 } while(key != '\n' && key != KEY_ENTER && key != '
\a');
301 ofstream out("/tmp/selector.out");
302 if((key == KEY_ENTER || key == '\n') && line >= 0 && line < nb_lines) {
303 out << lines[line] << endl;
309 for(int l = 0; l < nb_lines; l++) {