Automatic commit
[selector.git] / selector.cc
1
2 ///////////////////////////////////////////////////////////////////////////
3 // START_IP_HEADER                                                       //
4 //                                                                       //
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.                         //
8 //                                                                       //
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.                              //
13 //                                                                       //
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/>.  //
16 //                                                                       //
17 // Written by and Copyright (C) Francois Fleuret                         //
18 // Contact <francois.fleuret@idiap.ch> for comments & bug reports        //
19 //                                                                       //
20 // END_IP_HEADER                                                         //
21 ///////////////////////////////////////////////////////////////////////////
22
23 #include <stdio.h>
24 #include <ncurses.h>
25 #include <iostream>
26 #include <fstream>
27 #include <string.h>
28 #include <stdlib.h>
29
30 using namespace std;
31
32 int buffer_size = 1024;
33 int nb_lines_max = 100000;
34
35 int match(char *string, char *regexp) {
36   return strstr(string, regexp) != 0;
37 }
38
39 void check_opt(int argc, char **argv, int n_opt, int n, const char *help) {
40   if(n_opt + n >= argc) {
41     cerr << "Missing argument for " << argv[n_opt] << ". Expecting " << help << "." << endl;
42     exit(1);
43   }
44 }
45
46 int previous_visible(int current_line, int nb_lines, char **lines, char *regexp) {
47   int line = current_line - 1;
48   while(line >= 0 && !match(lines[line], regexp)) line--;
49   return line;
50 }
51
52 int next_visible(int current_line, int nb_lines, char **lines, char *regexp) {
53   int line = current_line + 1;
54   while(line < nb_lines && !match(lines[line], regexp)) line++;
55
56   if(line < nb_lines)
57     return line;
58   else
59     return -1;
60 }
61
62 void update_screen(int *current_line, int motion,
63                    int nb_lines, char **lines,
64                    char *regexp, int noblink) {
65
66   char buffer[buffer_size];
67
68   int console_width = getmaxx(stdscr);
69   int console_height = getmaxy(stdscr);
70
71   int nb_printed_lines = 1, last_printer_line = -1;
72
73   // First, we find a visible line. In priority: The current, or the
74   // first visible after it, or the first visible before it.
75
76   int new_line;
77   if(match(lines[*current_line], regexp)) {
78     new_line = *current_line;
79   } else {
80     new_line = next_visible(*current_line, nb_lines, lines, regexp);
81     if(new_line < 0) {
82       new_line = previous_visible(*current_line, nb_lines, lines, regexp);
83     }
84   }
85
86   // If we found a visible line and we should move, let's move
87
88   if(new_line >= 0 && motion != 0) {
89     int l = new_line;
90     l += motion;
91
92     if(motion > 0) {
93       // We want to go down, let's find the first visible line below
94       l = next_visible(new_line, nb_lines, lines, regexp);
95       if(l >= 0) {
96         new_line = l;
97       }
98     } else {
99       // We want to go up, let's find the first visible line above
100       l = previous_visible(new_line, nb_lines, lines, regexp);
101       if(l >= 0) {
102         new_line = l;
103       }
104     }
105   }
106
107   if(!noblink) {
108     clear();
109   }
110
111   use_default_colors();
112
113   addstr("\n");
114
115   // Here new_line is either a line number matching the regexp, or -1
116
117   if(new_line >= 0) {
118
119     int first_line = new_line, last_line = new_line, nb_match = 1;
120
121     while(nb_match < console_height-1 && (first_line > 0 || last_line < nb_lines - 1)) {
122
123       if(first_line > 0) {
124         first_line--;
125         while(first_line > 0 && !match(lines[first_line], regexp)) {
126           first_line--;
127         }
128         if(match(lines[first_line], regexp)) {
129           nb_match++;
130         }
131       }
132
133       if(last_line < nb_lines - 1) {
134         last_line++;
135         while(last_line < nb_lines - 1 && !match(lines[last_line], regexp)) {
136           last_line++;
137         }
138
139         if(match(lines[last_line], regexp)) {
140           nb_match++;
141         }
142       }
143     }
144
145     for(int l = first_line; l <= last_line; l++) {
146       if(match(lines[l], regexp)) {
147         int k = 0;
148
149         while(lines[l][k] && k < buffer_size - 2 && k < console_width - 1) {
150           buffer[k] = lines[l][k];
151           k++;
152         }
153
154         if(noblink) {
155           while(k < console_width - 1) {
156             buffer[k++] = ' ';
157           }
158         }
159
160         buffer[k++] = '\n';
161         buffer[k++] = '\0';
162
163         if(l == new_line) {
164           attron(COLOR_PAIR(2));
165           addstr(buffer);
166           attroff(COLOR_PAIR(2));
167         } else {
168           addstr(buffer);
169         }
170
171         last_printer_line = l;
172         nb_printed_lines++;
173       }
174     }
175
176     if(motion != 0) {
177       *current_line = new_line;
178     }
179   }
180
181   if(noblink) { // Erase the rest of the window. That's slightly ugly.
182     int k = 0;
183     while(k < console_width - 1) {
184       buffer[k++] = ' ';
185     }
186     buffer[k++] = '\n';
187     buffer[k++] = '\0';
188     for(int l = nb_printed_lines; l < console_height; l++) {
189       addstr(buffer);
190     }
191   }
192
193   // Draw the modeline
194
195   move(0, 0);
196   attron(COLOR_PAIR(1));
197   sprintf(buffer, "%d/%d pattern: %s", nb_printed_lines - 1, nb_lines, regexp);
198   for(int k = strlen(buffer); k < console_width - 1; k++) buffer[k] = ' ';
199   buffer[console_width-1] = '\0';
200   addstr(buffer);
201   attroff(COLOR_PAIR(1));
202
203   refresh();       // After doing something on the display, we refresh it
204 }
205
206 int main(int argc, char **argv) {
207   char buffer[buffer_size];
208   char *lines[nb_lines_max];
209   int noblink = 1;
210
211   char input_filename[buffer_size], output_filename[buffer_size];
212   strcpy(input_filename, "/dev/stdin");
213   strcpy(output_filename, "/tmp/selector.out");
214
215   int i = 1;
216   while(i < argc) {
217     if(strcmp(argv[i], "-o") == 0) {
218       check_opt(argc, argv, i, 1, "<output filename>");
219       strncpy(output_filename, argv[i+1], buffer_size);
220       i += 2;
221     }
222
223     else if(strcmp(argv[i], "-f") == 0) {
224       check_opt(argc, argv, i, 1, "<input filename>");
225       strncpy(input_filename, argv[i+1], buffer_size);
226       i += 2;
227     }
228
229     else if(strcmp(argv[i], "-b") == 0) {
230       noblink = 1;
231       i++;
232     }
233
234     else if(strcmp(argv[i], "-l") == 0) {
235       check_opt(argc, argv, i, 1, "<maximum number of lines>");
236       nb_lines_max = atoi(argv[i+1]);
237       i += 2;
238     }
239
240     else {
241       cerr << argv[0] << " [-o <output filename>] [-b] [-l <max number of lines]" << endl;
242       exit(1);
243     }
244   }
245
246   ifstream file(input_filename);
247
248   if(file.fail()) {
249     cerr << "Can not open \"" << input_filename << "\"" << endl;
250     return 1;
251   }
252
253   int nb_lines = 0;
254   while(nb_lines < nb_lines_max && !file.eof()) {
255     file.getline(buffer, buffer_size);
256     lines[nb_lines] = new char[strlen(buffer) + 1];
257     strcpy(lines[nb_lines], buffer);
258     nb_lines++;
259   }
260
261   char regexp[buffer_size];
262   regexp[0] = '\0';
263   int regexp_point;
264   regexp_point = 0;
265
266   initscr();
267
268   if(!has_colors()) {
269     cerr << "No colors." << endl;
270     return 1;
271   }
272
273   noecho();
274   curs_set(0);
275   keypad(stdscr, TRUE);
276
277   start_color();
278   // init_pair(1, COLOR_WHITE, COLOR_BLACK);
279   init_pair(1, COLOR_WHITE, COLOR_GREEN);
280   init_pair(2, COLOR_BLACK, COLOR_YELLOW);
281
282   int key;
283
284   int line = 0;
285
286   update_screen(&line, 0, nb_lines, lines, regexp, noblink);
287
288   do {
289
290     key = getch();
291
292     int motion = 0;
293
294     if(key >= ' ' && key <= 'z') {
295       regexp[regexp_point++] = key;
296       regexp[regexp_point] = '\0';
297     }
298
299     else if(key == KEY_BACKSPACE || key == KEY_DC || key == '\b') {
300       if(regexp_point > 0) {
301         regexp_point--;
302         regexp[regexp_point] = '\0';
303       }
304     }
305
306     else if(key == KEY_UP || key == '\10') {
307       motion = -1;
308     }
309
310     else if(key == KEY_DOWN || key == '\ e') {
311       motion = 1;
312     }
313
314     update_screen(&line, motion, nb_lines, lines, regexp, noblink);
315   } while(key != '\n' && key != KEY_ENTER && key != '\a');
316
317   echo();
318   curs_set(1);
319   endwin();
320
321   ofstream out(output_filename);
322   if(out.fail()) {
323     cerr << "Can not open " << output_filename << " for writing." << endl;
324     exit(1);
325   } else {
326     if((key == KEY_ENTER || key == '\n') && line >= 0 && line < nb_lines) {
327       out << lines[line] << endl;
328     } else {
329       out << endl;
330     }
331     out.flush();
332   }
333
334   for(int l = 0; l < nb_lines; l++) {
335     delete[] lines[l];
336   }
337
338   exit(0);
339 }