Automatic commit
[selector.git] / selector.cc
index ab793b0..d809287 100644 (file)
 
 using namespace std;
 
-void build_display(char **choices, char *regexp) {
+const int buffer_size = 1024;
+const int nb_lines_max = 100000;
+
+void build_display(int line, int nb_lines, char **lines, char *regexp) {
+  char buffer[buffer_size];
+
+  int maxx = getmaxx(stdscr);
+  int maxy = getmaxy(stdscr);
+
   clear();         // Cleaning the window
   refresh();       // After doing something on the display, we refresh it
-  for(int y = 0; y < 10; y++) {
-    printw("y = %d\n", y);
+
+  use_default_colors();
+
+  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++;
+      }
+      buffer[k++] = '\n';
+      buffer[k++] = '\0';
+      printw(buffer);
+      nb_printed_lines++;
+    }
   }
-  printw("regexp = \"%s\"", regexp);
+
+  // 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!
 }
 
 int main(int argc, char **argv) {
   int dummy, xpos, ypos;
-  const int buffer_size = 1024;
-  const int nb_lines_max = 1000;
 
   char buffer[buffer_size];
   char *lines[nb_lines_max];
@@ -65,6 +94,7 @@ int main(int argc, char **argv) {
     file.getline(buffer, buffer_size);
     lines[nb_lines] = new char[strlen(buffer) + 1];
     strcpy(lines[nb_lines], buffer);
+    nb_lines++;
   }
 
   char regexp[buffer_size]="";
@@ -82,23 +112,29 @@ int main(int argc, char **argv) {
 
   noecho();        // I don't want echo when I press a key
   curs_set(0);     // I don't want to see the cursor
+  keypad(stdscr, TRUE);
 
   start_color();   // We will use colors
-  init_pair(1, COLOR_RED, COLOR_BLACK); // red on black for error messages
-
-  // attron(COLOR_PAIR(1));        // Let's print something in red on black
-  // printw("Hello world\n");      // That's how we print something!
-  // attroff(COLOR_PAIR(1));       // Let's get back to default colors!
+  init_pair(1, COLOR_WHITE, COLOR_BLACK); // red on black for error messages
 
   printw("Press a key to contine\n");
   int key;
 
+  build_display(0, nb_lines, lines, regexp);
+
   do {
-    build_display(0, regexp);
     key = getch();
-    regexp[regexp_point++] = key;
-    regexp[regexp_point] = '\0';
-  } while(key != 'q');
+    if(key >= ' ' && key <= 'z') {
+      regexp[regexp_point++] = key;
+      regexp[regexp_point] = '\0';
+    } 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