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 void build_display(char **choices, char *regexp) {
32   clear();         // Cleaning the window
33   refresh();       // After doing something on the display, we refresh it
34   for(int y = 0; y < 10; y++) {
35     printw("y = %d\n", y);
36   }
37   printw("regexp = \"%s\"", regexp);
38 }
39
40 int main(int argc, char **argv) {
41   int dummy, xpos, ypos;
42   const int buffer_size = 1024;
43   const int nb_lines_max = 1000;
44
45   char buffer[buffer_size];
46   char *lines[nb_lines_max];
47
48   ifstream file(argv[1]);
49
50   if(argc != 2) {
51     cerr << argv[0] << " <file>" << endl;
52     return 1;
53   }
54
55   if(file.fail()) {
56     cerr << "Can not open \""
57          << argv[1]
58          << "\""
59          << endl;
60     return 1;
61   }
62
63   int nb_lines = 0;
64   while(nb_lines < nb_lines_max && !file.eof()) {
65     file.getline(buffer, buffer_size);
66     lines[nb_lines] = new char[strlen(buffer) + 1];
67     strcpy(lines[nb_lines], buffer);
68   }
69
70   char regexp[buffer_size]="";
71   int regexp_point;
72   regexp_point = 0;
73
74   initscr();        // Necessary to start a curses session
75
76   if (has_colors()) {
77     cout << "You can use color on this terminal" << endl;
78   } else {
79     cout << "No colors." << endl;
80     return 1;
81   }
82
83   noecho();        // I don't want echo when I press a key
84   curs_set(0);     // I don't want to see the cursor
85
86   start_color();   // We will use colors
87   init_pair(1, COLOR_RED, COLOR_BLACK); // red on black for error messages
88
89   // attron(COLOR_PAIR(1));        // Let's print something in red on black
90   // printw("Hello world\n");      // That's how we print something!
91   // attroff(COLOR_PAIR(1));       // Let's get back to default colors!
92
93   printw("Press a key to contine\n");
94   int key;
95
96   do {
97     build_display(0, regexp);
98     key = getch();
99     regexp[regexp_point++] = key;
100     regexp[regexp_point] = '\0';
101   } while(key != 'q');
102
103   echo();         // We want to have echo
104   curs_set(1);    // We want to see the cursor again
105   endwin();       // Back to normal
106
107   for(int l = 0; l < nb_lines; l++) {
108     delete[] lines[l];
109   }
110
111   return 0;
112 }