Added cbreak and intrflush into ncurses initialization.
[selector.git] / selector.cc
index 8aeabca..739f6f8 100644 (file)
  */
 
 // To use it as a super-history-search for bash:
-// selector -q -b -i -d -v -w -l 10000 <(history)
+// selector -q -b -i -d -v -w -l ${HISTSIZE} <(history)
 
 #include <fstream>
 #include <iostream>
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
 #include <string.h>
 #include <ncurses.h>
 #include <fcntl.h>
@@ -126,7 +127,7 @@ void error_feedback() {
 //////////////////////////////////////////////////////////////////////
 // A quick and dirty hash table
 
-// The table itself stores index of the strings in a char
+// The table itself stores indexes of the strings taken in a char
 // **table. When a string is added, if it was already in the table,
 // the new index replaces the previous one.
 
@@ -144,7 +145,8 @@ int *new_hash_table(int hash_table_size) {
 // the previous index it had.
 
 int test_and_add(char *new_string, int new_index,
-                 char **strings, int *hash_table, int hash_table_size) {
+                 char **strings,
+                 int *hash_table, int hash_table_size) {
 
   unsigned int code = 0;
 
@@ -330,16 +332,17 @@ int next_visible(int current_line, int nb_lines, char **lines, matcher_t *matche
 //////////////////////////////////////////////////////////////////////
 
 // The value passed to this routine in current_focus_line is the index
-// of the line to highlighted if it matches the matcher. The line
-// highlighted is the first the one matching the matcher in that
-// order: The current_focus_line itself, the first with a greated
-// index, the first with a lesser index.
+// of the line we should have highlited if there was no motion and if
+// it matched the matcher. So, the line actually highlighted is the
+// first one matching the matcher in that order: (1)
+// current_focus_line after motion, (2) the first with a greater
+// index, (3) the first with a lesser index.
 
 // The index of the line actually shown highlighted is written in
 // displayed_focus_line (it can be -1)
 
 // If there is a motion and a line is actually shown highlighted, its
-// value is written in current_focus_line
+// value is written in current_focus_line.
 
 void update_screen(int *current_focus_line, int *displayed_focus_line,
                    int motion,
@@ -355,12 +358,12 @@ void update_screen(int *current_focus_line, int *displayed_focus_line,
   int console_width = getmaxx(stdscr);
   int console_height = getmaxy(stdscr);
 
-  // First, we find a visible line. In priority: The current, or the
-  // first visible after it, or the first visible before it.
+  // First, we find a visible line.
 
   int nb_printed_lines = 0;
 
   use_default_colors();
+
   addstr("\n");
 
   if(matcher.regexp_error) {
@@ -887,8 +890,10 @@ int main(int argc, char **argv) {
   // Here we start to display with curse
 
   initscr();
-
+  cbreak();
   noecho();
+  // nonl();
+  intrflush(stdscr, FALSE);
 
   // So that the arrow keys work
   keypad(stdscr, TRUE);
@@ -1034,10 +1039,20 @@ int main(int argc, char **argv) {
 
   if((key == KEY_ENTER || key == '\n')) {
 
-    if(output_to_vt_buffer) {
-      if(displayed_focus_line >= 0 && displayed_focus_line < nb_lines) {
-        inject_into_tty_buffer(lines[displayed_focus_line]);
+    char *t;
+
+    if(displayed_focus_line >= 0 && displayed_focus_line < nb_lines) {
+      t = lines[displayed_focus_line];
+      if(label_separator) {
+        while(*t && *t != label_separator) t++;
+        if(*t) t++;
       }
+    } else {
+      t = 0;
+    }
+
+    if(output_to_vt_buffer && t) {
+      inject_into_tty_buffer(t);
     }
 
     if(output_filename[0]) {
@@ -1046,14 +1061,15 @@ int main(int argc, char **argv) {
         cerr << "Can not open " << output_filename << " for writing." << endl;
         exit(1);
       } else {
-        if(displayed_focus_line >= 0 && displayed_focus_line < nb_lines) {
-          out << lines[displayed_focus_line] << endl;
+        if(t) {
+          out << t << endl;
         } else {
           out << endl;
         }
       }
       out.flush();
     }
+
   } else {
     cout << "Aborted." << endl;
   }