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
29 using namespace std;
30
31 const int buffer_size = 1024;
32 const int nb_lines_max = 100000;
33
34 void build_display(int key, int nb_lines, char **lines, char *regexp) {
35   char buffer[buffer_size];
36   clear();         // Cleaning the window
37   refresh();       // After doing something on the display, we refresh it
38   int maxx = getmaxx(stdscr);
39   int maxy = getmaxy(stdscr);
40   use_default_colors();
41   // printw("maxx %d maxy %d key %d\n", maxx, maxy, key);
42   printw("\n");
43   printw("\n");
44   int nb_printed_lines = 2;
45   for(int y = 0; nb_printed_lines < maxy && y < nb_lines; y++) {
46     if(strstr(lines[y], regexp)) {
47       int k = 0;
48       while(lines[y][k] && k < buffer_size - 2 && k < maxx) {
49         buffer[k] = lines[y][k];
50         k++;
51       }
52       buffer[k++] = '\n';
53       buffer[k++] = '\0';
54       printw(buffer);
55       nb_printed_lines++;
56     }
57   }
58   move(0, 0);
59   attron(COLOR_PAIR(1));        // Let's print something in red on black
60   printw("%d/%d pattern: %s\n", nb_printed_lines, nb_lines - 2, regexp);
61   attroff(COLOR_PAIR(1));       // Let's get back to default colors!
62 }
63
64 // I should find were this is defined ...
65
66 int main(int argc, char **argv) {
67   int dummy, xpos, ypos;
68
69   char buffer[buffer_size];
70   char *lines[nb_lines_max];
71
72   ifstream file(argv[1]);
73
74   if(argc != 2) {
75     cerr << argv[0] << " <file>" << endl;
76     return 1;
77   }
78
79   if(file.fail()) {
80     cerr << "Can not open \""
81          << argv[1]
82          << "\""
83          << endl;
84     return 1;
85   }
86
87   int nb_lines = 0;
88   while(nb_lines < nb_lines_max && !file.eof()) {
89     file.getline(buffer, buffer_size);
90     lines[nb_lines] = new char[strlen(buffer) + 1];
91     strcpy(lines[nb_lines], buffer);
92     nb_lines++;
93   }
94
95   char regexp[buffer_size]="";
96   int regexp_point;
97   regexp_point = 0;
98
99   initscr();        // Necessary to start a curses session
100
101   if (has_colors()) {
102     cout << "You can use color on this terminal" << endl;
103   } else {
104     cout << "No colors." << endl;
105     return 1;
106   }
107
108   noecho();        // I don't want echo when I press a key
109   curs_set(0);     // I don't want to see the cursor
110   keypad(stdscr, TRUE);
111
112   start_color();   // We will use colors
113   init_pair(1, COLOR_WHITE, COLOR_BLACK); // red on black for error messages
114
115   printw("Press a key to contine\n");
116   int key;
117
118   build_display(-1, nb_lines, lines, regexp);
119
120   do {
121     key = getch();
122     if(key >= ' ' && key <= 'z') {
123       regexp[regexp_point++] = key;
124       regexp[regexp_point] = '\0';
125     } else if(key == KEY_BACKSPACE || key == KEY_DC) {
126       if(regexp_point > 0) {
127         regexp_point--;
128         regexp[regexp_point] = '\0';
129       }
130     }
131     build_display(key, nb_lines, lines, regexp);
132   } while(key != '\n' && key != KEY_ENTER);
133
134   echo();         // We want to have echo
135   curs_set(1);    // We want to see the cursor again
136   endwin();       // Back to normal
137
138   for(int l = 0; l < nb_lines; l++) {
139     delete[] lines[l];
140   }
141
142   return 0;
143 }