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