Automatic commit
[selector.git] / selector.cc
index 4b41d07..cc25520 100644 (file)
 #include <iostream>
 #include <fstream>
 #include <string.h>
+#include <stdlib.h>
 
 using namespace std;
 
-ostream *log;
-
-const int buffer_size = 1024;
-const int nb_lines_max = 100000;
+int buffer_size = 1024;
+int nb_lines_max = 100000;
 
 int match(char *string, char *regexp) {
   return strstr(string, regexp) != 0;
 }
 
+void check_opt(int argc, char **argv, int n_opt, int n, const char *help) {
+  if(n_opt + n >= argc) {
+    cerr << "Missing argument for " << argv[n_opt] << ". Expecting " << help << "." << endl;
+    exit(1);
+  }
+}
+
+int previous_visible(int current_line, int nb_lines, char **lines, char *regexp) {
+  int line = current_line - 1;
+  while(line >= 0 && !match(lines[line], regexp)) line--;
+  return line;
+}
+
+int next_visible(int current_line, int nb_lines, char **lines, char *regexp) {
+  int line = current_line + 1;
+  while(line < nb_lines && !match(lines[line], regexp)) line++;
+
+  if(line < nb_lines)
+    return line;
+  else
+    return -1;
+}
+
 void update_screen(int *current_line, int motion,
                    int nb_lines, char **lines,
                    char *regexp, int noblink) {
@@ -46,52 +68,57 @@ void update_screen(int *current_line, int motion,
   int console_width = getmaxx(stdscr);
   int console_height = getmaxy(stdscr);
 
-  if(!noblink) {
-    clear();
-  }
-
-  use_default_colors();
-
-  printw("\n");
-
   int nb_printed_lines = 1, last_printer_line = -1;
 
+  // First, we find a visible line. In priority: The current, or the
+  // first visible after it, or the first visible before it.
+
   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--;
-      }
-    }
+  if(match(lines[*current_line], regexp)) {
+    new_line = *current_line;
   } else {
-
-    new_line = *current_line - 1;
-    while(new_line >= 0 && ! match(lines[new_line], regexp)) {
-      new_line--;
+    new_line = next_visible(*current_line, nb_lines, lines, regexp);
+    if(new_line < 0) {
+      new_line = previous_visible(*current_line, nb_lines, lines, regexp);
     }
+  }
 
-    if(new_line < 0) {
-      new_line = *current_line;
-      while(new_line < nb_lines && !match(lines[new_line], regexp)) {
-        new_line++;
+  // If we found a visible line and we should move, let's move
+
+  if(new_line >= 0 && motion != 0) {
+    int l = new_line;
+    l += motion;
+
+    if(motion > 0) {
+      // We want to go down, let's find the first visible line below
+      l = next_visible(new_line, nb_lines, lines, regexp);
+      if(l >= 0) {
+        new_line = l;
       }
-      if(new_line == nb_lines) {
-        new_line = -1;
+    } else {
+      // We want to go up, let's find the first visible line above
+      l = previous_visible(new_line, nb_lines, lines, regexp);
+      if(l >= 0) {
+        new_line = l;
       }
     }
   }
 
+  if(!noblink) {
+    clear();
+  }
+
+  use_default_colors();
+
+  addstr("\n");
+
   // 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)) {
+    while(nb_match < console_height-1 && (first_line > 0 || last_line < nb_lines - 1)) {
 
       if(first_line > 0) {
         first_line--;
@@ -129,15 +156,16 @@ void update_screen(int *current_line, int motion,
             buffer[k++] = ' ';
           }
         }
+
         buffer[k++] = '\n';
         buffer[k++] = '\0';
 
         if(l == new_line) {
           attron(COLOR_PAIR(2));
-          printw(buffer);
+          addstr(buffer);
           attroff(COLOR_PAIR(2));
         } else {
-          printw(buffer);
+          addstr(buffer);
         }
 
         last_printer_line = l;
@@ -145,7 +173,9 @@ void update_screen(int *current_line, int motion,
       }
     }
 
-    *current_line = new_line;
+    if(motion != 0) {
+      *current_line = new_line;
+    }
   }
 
   if(noblink) { // Erase the rest of the window. That's slightly ugly.
@@ -156,7 +186,7 @@ void update_screen(int *current_line, int motion,
     buffer[k++] = '\n';
     buffer[k++] = '\0';
     for(int l = nb_printed_lines; l < console_height; l++) {
-      printw(buffer);
+      addstr(buffer);
     }
   }
 
@@ -167,7 +197,7 @@ void update_screen(int *current_line, int motion,
   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);
+  addstr(buffer);
   attroff(COLOR_PAIR(1));
 
   refresh();       // After doing something on the display, we refresh it
@@ -176,23 +206,47 @@ void update_screen(int *current_line, int motion,
 int main(int argc, char **argv) {
   char buffer[buffer_size];
   char *lines[nb_lines_max];
-  int noblink = 0;
+  int noblink = 1;
+
+  char input_filename[buffer_size], output_filename[buffer_size];
+  strcpy(input_filename, "/dev/stdin");
+  strcpy(output_filename, "/tmp/selector.out");
+
+  int i = 1;
+  while(i < argc) {
+    if(strcmp(argv[i], "-o") == 0) {
+      check_opt(argc, argv, i, 1, "<output filename>");
+      strncpy(output_filename, argv[i+1], buffer_size);
+      i += 2;
+    }
 
-  log = new fstream("/tmp/selector.log");
+    else if(strcmp(argv[i], "-f") == 0) {
+      check_opt(argc, argv, i, 1, "<input filename>");
+      strncpy(input_filename, argv[i+1], buffer_size);
+      i += 2;
+    }
 
-  char *file_name;
-  char stdin_name[] = "/dev/stdin";
+    else if(strcmp(argv[i], "-b") == 0) {
+      noblink = 1;
+      i++;
+    }
 
-  if(argc == 2 && strcmp(argv[1], "-")) {
-    file_name = argv[1];
-  } else {
-    file_name = stdin_name;
+    else if(strcmp(argv[i], "-l") == 0) {
+      check_opt(argc, argv, i, 1, "<maximum number of lines>");
+      nb_lines_max = atoi(argv[i+1]);
+      i += 2;
+    }
+
+    else {
+      cerr << argv[0] << " [-o <output filename>] [-b] [-l <max number of lines]" << endl;
+      exit(1);
+    }
   }
 
-  ifstream file(file_name);
+  ifstream file(input_filename);
 
   if(file.fail()) {
-    cerr << "Can not open \"" << file_name << "\"" << endl;
+    cerr << "Can not open \"" << input_filename << "\"" << endl;
     return 1;
   }
 
@@ -204,7 +258,8 @@ int main(int argc, char **argv) {
     nb_lines++;
   }
 
-  char regexp[buffer_size]="";
+  char regexp[buffer_size];
+  regexp[0] = '\0';
   int regexp_point;
   regexp_point = 0;
 
@@ -220,16 +275,15 @@ int main(int argc, char **argv) {
   keypad(stdscr, TRUE);
 
   start_color();
-  init_pair(1, COLOR_WHITE, COLOR_BLACK);
+  // init_pair(1, COLOR_WHITE, COLOR_BLACK);
+  init_pair(1, COLOR_WHITE, COLOR_GREEN);
   init_pair(2, COLOR_BLACK, COLOR_YELLOW);
 
   int key;
 
   int line = 0;
 
-  update_screen(&line, 0,
-                nb_lines, lines,
-                regexp, noblink);
+  update_screen(&line, 0, nb_lines, lines, regexp, noblink);
 
   do {
 
@@ -242,7 +296,7 @@ int main(int argc, char **argv) {
       regexp[regexp_point] = '\0';
     }
 
-    else if(key == KEY_BACKSPACE || key == KEY_DC) {
+    else if(key == KEY_BACKSPACE || key == KEY_DC || key == '\b') {
       if(regexp_point > 0) {
         regexp_point--;
         regexp[regexp_point] = '\0';
@@ -264,20 +318,22 @@ int main(int argc, char **argv) {
   curs_set(1);
   endwin();
 
-  ofstream out("/tmp/selector.out");
-  if((key == KEY_ENTER || key == '\n') && line >= 0 && line < nb_lines) {
-    out << lines[line] << endl;
+  ofstream out(output_filename);
+  if(out.fail()) {
+    cerr << "Can not open " << output_filename << " for writing." << endl;
+    exit(1);
   } else {
-    out << endl;
+    if((key == KEY_ENTER || key == '\n') && line >= 0 && line < nb_lines) {
+      out << lines[line] << endl;
+    } else {
+      out << endl;
+    }
+    out.flush();
   }
-  out.flush();
 
   for(int l = 0; l < nb_lines; l++) {
     delete[] lines[l];
   }
 
-  log->flush();
-  delete log;
-
-  return 0;
+  exit(0);
 }