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