using namespace std;
+ostream *log;
+
const int buffer_size = 1024;
const int nb_lines_max = 100000;
-void build_display(int line, int nb_lines, char **lines, char *regexp) {
+int match(char *string, char *regexp) {
+ return strstr(string, regexp) != 0;
+}
+
+void update_screen(int *current_line, int motion,
+ int nb_lines, char **lines,
+ char *regexp, int noblink) {
+
char buffer[buffer_size];
- clear(); // Cleaning the window
- refresh(); // After doing something on the display, we refresh it
- int maxx = getmaxx(stdscr);
- int maxy = getmaxy(stdscr);
+
+ int console_width = getmaxx(stdscr);
+ int console_height = getmaxy(stdscr);
+
+ if(!noblink) {
+ clear();
+ }
+
use_default_colors();
- // printw("maxx %d maxy %d key %d\n", maxx, maxy, key);
- printw("\n");
+
printw("\n");
- int nb_printed_lines = 2;
- for(int y = 0; nb_printed_lines < maxy && y < nb_lines; y++) {
- if(strstr(lines[y], regexp)) {
- int k = 0;
- while(lines[y][k] && k < buffer_size - 2 && k < maxx) {
- buffer[k] = lines[y][k];
- k++;
+
+ int nb_printed_lines = 1, last_printer_line = -1;
+
+ int new_line;
+ if(motion >= 0) {
+ new_line = *current_line + motion;
+ while(new_line < nb_lines && !match(lines[new_line], regexp)) {
+ new_line++;
+ }
+ if(new_line == nb_lines) {
+ new_line = *current_line;
+ while(new_line >= 0 && ! match(lines[new_line], regexp)) {
+ new_line--;
+ }
+ }
+ } else {
+
+ new_line = *current_line - 1;
+ while(new_line >= 0 && ! match(lines[new_line], regexp)) {
+ new_line--;
+ }
+
+ if(new_line < 0) {
+ new_line = *current_line;
+ while(new_line < nb_lines && !match(lines[new_line], regexp)) {
+ new_line++;
}
- buffer[k++] = '\n';
- buffer[k++] = '\0';
+ if(new_line == nb_lines) {
+ new_line = -1;
+ }
+ }
+ }
+
+ // Here new_line is either a line number matching the regexp, or -1
+
+ if(new_line >= 0) {
+ int first_line = new_line, last_line = new_line, nb_match = 1;
+
+ while(nb_match < console_height && (first_line > 0 || last_line < nb_lines - 1)) {
+
+ if(first_line > 0) {
+ first_line--;
+ while(first_line > 0 && !match(lines[first_line], regexp)) {
+ first_line--;
+ }
+ if(match(lines[first_line], regexp)) {
+ nb_match++;
+ }
+ }
+
+ if(last_line < nb_lines - 1) {
+ last_line++;
+ while(last_line < nb_lines - 1 && !match(lines[last_line], regexp)) {
+ last_line++;
+ }
+
+ if(match(lines[last_line], regexp)) {
+ nb_match++;
+ }
+ }
+ }
+
+ for(int l = first_line; l <= last_line; l++) {
+ if(match(lines[l], regexp)) {
+ int k = 0;
+
+ while(lines[l][k] && k < buffer_size - 2 && k < console_width - 1) {
+ buffer[k] = lines[l][k];
+ k++;
+ }
+
+ if(noblink) {
+ while(k < console_width - 1) {
+ buffer[k++] = ' ';
+ }
+ }
+ buffer[k++] = '\n';
+ buffer[k++] = '\0';
+
+ if(l == new_line) {
+ attron(COLOR_PAIR(2));
+ printw(buffer);
+ attroff(COLOR_PAIR(2));
+ } else {
+ printw(buffer);
+ }
+
+ last_printer_line = l;
+ nb_printed_lines++;
+ }
+ }
+
+ *current_line = new_line;
+ }
+
+ if(noblink) { // Erase the rest of the window. That's slightly ugly.
+ int k = 0;
+ while(k < console_width - 1) {
+ buffer[k++] = ' ';
+ }
+ buffer[k++] = '\n';
+ buffer[k++] = '\0';
+ for(int l = nb_printed_lines; l < console_height; l++) {
printw(buffer);
- nb_printed_lines++;
}
}
+
+ // Draw the modeline
+
move(0, 0);
- attron(COLOR_PAIR(1)); // Let's print something in red on black
- printw("%d/%d pattern: %s\n", nb_printed_lines, nb_lines - 2, regexp);
- attroff(COLOR_PAIR(1)); // Let's get back to default colors!
-}
+ attron(COLOR_PAIR(1));
+ sprintf(buffer, "%d/%d pattern: %s", nb_printed_lines - 1, nb_lines, regexp);
+ for(int k = strlen(buffer); k < console_width - 1; k++) buffer[k] = ' ';
+ buffer[console_width-1] = '\0';
+ printw(buffer);
+ attroff(COLOR_PAIR(1));
-// I should find were this is defined ...
+ refresh(); // After doing something on the display, we refresh it
+}
int main(int argc, char **argv) {
- int dummy, xpos, ypos;
-
char buffer[buffer_size];
char *lines[nb_lines_max];
+ int noblink = 0;
- ifstream file(argv[1]);
+ log = new fstream("/tmp/selector.log");
- if(argc != 2) {
- cerr << argv[0] << " <file>" << endl;
- return 1;
+ char *file_name;
+ char stdin_name[] = "/dev/stdin";
+
+ if(argc == 2 && strcmp(argv[1], "-")) {
+ file_name = argv[1];
+ } else {
+ file_name = stdin_name;
}
+ ifstream file(file_name);
+
if(file.fail()) {
- cerr << "Can not open \""
- << argv[1]
- << "\""
- << endl;
+ cerr << "Can not open \"" << file_name << "\"" << endl;
return 1;
}
int regexp_point;
regexp_point = 0;
- initscr(); // Necessary to start a curses session
+ initscr();
- if (has_colors()) {
- cout << "You can use color on this terminal" << endl;
- } else {
- cout << "No colors." << endl;
+ if(!has_colors()) {
+ cerr << "No colors." << endl;
return 1;
}
- noecho(); // I don't want echo when I press a key
- curs_set(0); // I don't want to see the cursor
+ noecho();
+ curs_set(0);
keypad(stdscr, TRUE);
- start_color(); // We will use colors
- init_pair(1, COLOR_WHITE, COLOR_BLACK); // red on black for error messages
+ start_color();
+ init_pair(1, COLOR_WHITE, COLOR_BLACK);
+ init_pair(2, COLOR_BLACK, COLOR_YELLOW);
- printw("Press a key to contine\n");
int key;
- build_display(0, nb_lines, lines, regexp);
+ int line = 0;
+
+ update_screen(&line, 0,
+ nb_lines, lines,
+ regexp, noblink);
do {
+
key = getch();
+
+ int motion = 0;
+
if(key >= ' ' && key <= 'z') {
regexp[regexp_point++] = key;
regexp[regexp_point] = '\0';
- } else if(key == KEY_BACKSPACE || key == KEY_DC) {
+ }
+
+ else if(key == KEY_BACKSPACE || key == KEY_DC) {
if(regexp_point > 0) {
regexp_point--;
regexp[regexp_point] = '\0';
}
}
- build_display(0, nb_lines, lines, regexp);
- } while(key != '\n' && key != KEY_ENTER);
- echo(); // We want to have echo
- curs_set(1); // We want to see the cursor again
- endwin(); // Back to normal
+ else if(key == KEY_UP || key == '\10') {
+ motion = -1;
+ }
+
+ else if(key == KEY_DOWN || key == '\ e') {
+ motion = 1;
+ }
+
+ update_screen(&line, motion, nb_lines, lines, regexp, noblink);
+ } while(key != '\n' && key != KEY_ENTER && key != '\a');
+
+ echo();
+ curs_set(1);
+ endwin();
+
+ // ofstream out("/tmp/selector.out");
+ // if(key == KEY_ENTER || key == '\n') {
+ // out << lines[highlighted_line] << endl;
+ // } else {
+ // out << endl;
+ // }
+ // out.flush();
for(int l = 0; l < nb_lines; l++) {
delete[] lines[l];
}
+ log->flush();
+ delete log;
+
return 0;
}