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