Deal properly with no selection, just output nothing.
authorFrancois Fleuret <francois@fleuret.org>
Fri, 13 Mar 2009 07:10:06 +0000 (08:10 +0100)
committerFrancois Fleuret <francois@fleuret.org>
Fri, 13 Mar 2009 07:10:06 +0000 (08:10 +0100)
selector.cc

index 0f71d64..56b5b2a 100644 (file)
@@ -279,12 +279,13 @@ void update_screen(int *current_line, int *temporary_line, int motion,
       }
     }
 
-    *temporary_line = new_line;
     if(motion != 0) {
       *current_line = new_line;
     }
   }
 
+  *temporary_line = new_line;
+
   if(nb_printed_lines == 1) {
     addnstr("[no selection]\n", console_width);
     nb_printed_lines++;
@@ -527,16 +528,22 @@ int main(int argc, char **argv) {
   curs_set(1);
   endwin();
 
-  if((key == KEY_ENTER || key == '\n') && temporary_line >= 0 && temporary_line < nb_lines) {
+  if((key == KEY_ENTER || key == '\n')) {
     if(output_to_vt_buffer) {
-      inject_into_tty_buffer(lines[temporary_line]);
+      if(temporary_line >= 0 && temporary_line < nb_lines) {
+        inject_into_tty_buffer(lines[temporary_line]);
+      }
     } else {
       ofstream out(output_filename);
       if(out.fail()) {
         cerr << "Can not open " << output_filename << " for writing." << endl;
         exit(1);
       } else {
-        out << lines[temporary_line] << endl;
+        if(temporary_line >= 0 && temporary_line < nb_lines) {
+          out << lines[temporary_line] << endl;
+        } else {
+          out << endl;
+        }
       }
       out.flush();
     }