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 ///////////////////////////////////////////////////////////////////////////
32 int buffer_size = 1024;
33 int nb_lines_max = 100000;
35 int match(char *string, char *regexp) {
36 return strstr(string, regexp) != 0;
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;
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--;
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++;
62 void update_screen(int *current_line, int motion,
63 int nb_lines, char **lines,
64 char *regexp, int noblink) {
66 char buffer[buffer_size];
68 int console_width = getmaxx(stdscr);
69 int console_height = getmaxy(stdscr);
71 int nb_printed_lines = 1, last_printer_line = -1;
73 // First, we find a visible line. In priority: The current, or the
74 // first visible after it, or the first visible before it.
77 if(match(lines[*current_line], regexp)) {
78 new_line = *current_line;
80 new_line = next_visible(*current_line, nb_lines, lines, regexp);
82 new_line = previous_visible(*current_line, nb_lines, lines, regexp);
86 // If we found a visible line and we should move, let's move
88 if(new_line >= 0 && 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);
99 // We want to go up, let's find the first visible line above
100 l = previous_visible(new_line, nb_lines, lines, regexp);
111 use_default_colors();
115 // Here new_line is either a line number matching the regexp, or -1
119 int first_line = new_line, last_line = new_line, nb_match = 1;
121 while(nb_match < console_height-1 && (first_line > 0 || last_line < nb_lines - 1)) {
125 while(first_line > 0 && !match(lines[first_line], regexp)) {
128 if(match(lines[first_line], regexp)) {
133 if(last_line < nb_lines - 1) {
135 while(last_line < nb_lines - 1 && !match(lines[last_line], regexp)) {
139 if(match(lines[last_line], regexp)) {
145 for(int l = first_line; l <= last_line; l++) {
146 if(match(lines[l], regexp)) {
149 while(lines[l][k] && k < buffer_size - 2 && k < console_width - 1) {
150 buffer[k] = lines[l][k];
155 while(k < console_width - 1) {
164 attron(COLOR_PAIR(2));
166 attroff(COLOR_PAIR(2));
171 last_printer_line = l;
177 *current_line = new_line;
181 if(noblink) { // Erase the rest of the window. That's slightly ugly.
183 while(k < console_width - 1) {
188 for(int l = nb_printed_lines; l < console_height; l++) {
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';
201 attroff(COLOR_PAIR(1));
203 refresh(); // After doing something on the display, we refresh it
206 int main(int argc, char **argv) {
207 char buffer[buffer_size];
208 char *lines[nb_lines_max];
211 char input_filename[buffer_size], output_filename[buffer_size];
212 strcpy(input_filename, "/dev/stdin");
213 strcpy(output_filename, "/tmp/selector.out");
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);
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);
229 else if(strcmp(argv[i], "-b") == 0) {
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]);
241 cerr << argv[0] << " [-o <output filename>] [-b] [-l <max number of lines]" << endl;
246 ifstream file(input_filename);
249 cerr << "Can not open \"" << input_filename << "\"" << endl;
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);
261 char regexp[buffer_size];
269 cerr << "No colors." << endl;
275 keypad(stdscr, TRUE);
278 // init_pair(1, COLOR_WHITE, COLOR_BLACK);
279 init_pair(1, COLOR_WHITE, COLOR_GREEN);
280 init_pair(2, COLOR_BLACK, COLOR_YELLOW);
286 update_screen(&line, 0, nb_lines, lines, regexp, noblink);
294 if(key >= ' ' && key <= 'z') {
295 regexp[regexp_point++] = key;
296 regexp[regexp_point] = '\0';
299 else if(key == KEY_BACKSPACE || key == KEY_DC || key == '
\b') {
300 if(regexp_point > 0) {
302 regexp[regexp_point] = '\0';
306 else if(key == KEY_UP || key == '
\10') {
310 else if(key == KEY_DOWN || key == '
\ e') {
314 update_screen(&line, motion, nb_lines, lines, regexp, noblink);
315 } while(key != '\n' && key != KEY_ENTER && key != '
\a');
321 ofstream out(output_filename);
323 cerr << "Can not open " << output_filename << " for writing." << endl;
326 if((key == KEY_ENTER || key == '\n') && line >= 0 && line < nb_lines) {
327 out << lines[line] << endl;
334 for(int l = 0; l < nb_lines; l++) {