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 int refresh_screen(int *screen_line, int *line, int nb_lines, char **lines, char *regexp, int noblink) {
35   char buffer[buffer_size];
36
37   int maxx = getmaxx(stdscr);
38   int maxy = min(buffer_size-2, getmaxy(stdscr));
39
40   if(!noblink) {
41     clear();
42   }
43
44   use_default_colors();
45
46   printw("\n");
47
48   int nb_printed_lines = 1, last_printer_line = -1;
49   int y = 0;
50   int current_line = -1;
51
52   while(nb_printed_lines < maxy && y < nb_lines) {
53     if(strstr(lines[y], regexp) &&
54        (last_printer_line < 0 || strcmp(lines[y], lines[last_printer_line]))) {
55       int k = 0;
56
57       while(lines[y][k] && k < buffer_size - 2 && k < maxx - 1) {
58         buffer[k] = lines[y][k];
59         k++;
60       }
61
62       if(noblink) {
63         while(k < maxx - 1) {
64           buffer[k++] = ' ';
65         }
66       }
67       buffer[k++] = '\n';
68       buffer[k++] = '\0';
69
70       if(nb_printed_lines == *line + 1) {
71         attron(COLOR_PAIR(2));
72         printw(buffer);
73         attroff(COLOR_PAIR(2));
74         current_line = y;
75       } else {
76         printw(buffer);
77       }
78
79       last_printer_line = y;
80       nb_printed_lines++;
81     }
82     y++;
83   }
84
85   if(noblink) { // Erase the rest of the window. That's slightly ugly.
86     int k = 0;
87     while(k < maxx - 1) {
88       buffer[k++] = ' ';
89     }
90     buffer[k++] = '\n';
91     buffer[k++] = '\0';
92     for(int l = nb_printed_lines; l < maxy; l++) {
93       printw(buffer);
94     }
95   }
96
97   // Draw the modeline
98
99   move(0, 0);
100   attron(COLOR_PAIR(1));
101   sprintf(buffer, "%d/%d pattern: %s", nb_printed_lines - 1, nb_lines, regexp);
102   for(int k = strlen(buffer); k < maxx - 1; k++) buffer[k] = ' ';
103   buffer[maxx-1] = '\0';
104   printw(buffer);
105   attroff(COLOR_PAIR(1));
106
107   refresh();       // After doing something on the display, we refresh it
108
109   return current_line;
110 }
111
112 int main(int argc, char **argv) {
113   char buffer[buffer_size];
114   char *lines[nb_lines_max];
115   int noblink = 1;
116
117   char *file_name;
118   char stdin_name[] = "/dev/stdin";
119
120   if(argc == 2 && strcmp(argv[1], "-")) {
121     file_name = argv[1];
122   } else {
123     file_name = stdin_name;
124   }
125
126   ifstream file(file_name);
127
128   if(file.fail()) {
129     cerr << "Can not open \"" << file_name << "\"" << endl;
130     return 1;
131   }
132
133   int nb_lines = 0;
134   while(nb_lines < nb_lines_max && !file.eof()) {
135     file.getline(buffer, buffer_size);
136     lines[nb_lines] = new char[strlen(buffer) + 1];
137     strcpy(lines[nb_lines], buffer);
138     nb_lines++;
139   }
140
141   char regexp[buffer_size]="";
142   int regexp_point;
143   regexp_point = 0;
144
145   initscr();
146
147   if(!has_colors()) {
148     cerr << "No colors." << endl;
149     return 1;
150   }
151
152   noecho();
153   curs_set(0);
154   keypad(stdscr, TRUE);
155
156   start_color();
157   init_pair(1, COLOR_WHITE, COLOR_BLACK);
158   init_pair(2, COLOR_BLACK, COLOR_YELLOW);
159
160   int key;
161
162   int line = 0, screen_line = 0;
163
164   refresh_screen(&screen_line, &line, nb_lines, lines, regexp, noblink);
165
166   int current_line = -1;
167
168   do {
169
170     key = getch();
171
172     if(key >= ' ' && key <= 'z') {
173       regexp[regexp_point++] = key;
174       regexp[regexp_point] = '\0';
175     }
176
177     else if(key == KEY_BACKSPACE || key == KEY_DC) {
178       if(regexp_point > 0) {
179         regexp_point--;
180         regexp[regexp_point] = '\0';
181       }
182     }
183
184     else if(key == KEY_UP || key == '\10') {
185       if(line > 0) {
186         line--;
187       }
188     }
189
190     else if(key == KEY_DOWN || key == '\ e') {
191       line++;
192     }
193
194     current_line = refresh_screen(&screen_line, &line, nb_lines, lines, regexp, noblink);
195   } while(key != '\n' && key != KEY_ENTER && key != '\a');
196
197   echo();
198   curs_set(1);
199   endwin();
200
201   ofstream out("/tmp/selector.out");
202   out << lines[current_line] << endl;
203   out.flush();
204
205   for(int l = 0; l < nb_lines; l++) {
206     delete[] lines[l];
207   }
208
209   return 0;
210 }