3 * selector is a simple command line utility for selection of strings
4 * with a dynamic pattern-matching.
6 * Copyright (c) 2009 Francois Fleuret
7 * Written by Francois Fleuret <francois@fleuret.org>
9 * This file is part of selector.
11 * selector is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 3 as
13 * published by the Free Software Foundation.
15 * selector is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with selector. If not, see <http://www.gnu.org/licenses/>.
25 // To use it as a super-history-search for bash:
26 // selector -q -b -i -d -v -w -l ${HISTSIZE} <(history)
36 #include <sys/ioctl.h>
44 const int buffer_size = 4096;
46 // Yeah, global variables!
48 int nb_lines_max = 1000;
49 char pattern_separator = ';';
50 char label_separator = '\0';
51 int output_to_vt_buffer = 0;
52 int add_control_qs = 0;
56 int inverse_order = 0;
57 int remove_duplicates = 0;
59 int case_sensitive = 0;
63 int attr_modeline, attr_focus_line, attr_error;
65 //////////////////////////////////////////////////////////////////////
67 void inject_into_tty_buffer(char *string) {
68 struct termios oldtio, newtio;
69 tcgetattr(STDIN_FILENO, &oldtio);
70 memset(&newtio, 0, sizeof(newtio));
71 // Set input mode (non-canonical, *no echo*,...)
72 tcsetattr(STDIN_FILENO, TCSANOW, &newtio);
73 const char control_q = '\021';
74 // Put the selected string in the tty input buffer
75 for(const char *k = string; *k; k++) {
76 if(add_control_qs && !(*k >= ' ' && *k <= '~')) {
77 // Add ^Q to quote control characters
78 ioctl(STDIN_FILENO, TIOCSTI, &control_q);
80 ioctl(STDIN_FILENO, TIOCSTI, k);
82 // Restore the old settings
83 tcsetattr(STDIN_FILENO, TCSANOW, &oldtio);
86 //////////////////////////////////////////////////////////////////////
88 void check_opt(int argc, char **argv, int n_opt, int n, const char *help) {
89 if(n_opt + n >= argc) {
90 cerr << "Missing argument for " << argv[n_opt] << "."
92 << "Expecting " << help << "."
98 int string_to_positive_integer(char *string) {
103 for(char *s = string; *s; s++) {
104 if(*s >= '0' && *s <= '9') {
105 result = result * 10 + int(*s - '0');
111 cerr << "Value `" << string << "' is not a positive integer." << endl;
118 void error_feedback() {
126 //////////////////////////////////////////////////////////////////////
127 // A quick and dirty hash table
129 // The table itself stores indexes of the strings taken in a char
130 // **table. When a string is added, if it was already in the table,
131 // the new index replaces the previous one.
133 int *new_hash_table(int hash_table_size) {
135 result = new int[hash_table_size];
136 for(int k = 0; k < hash_table_size; k++) {
142 // Adds new_string in the table, associated to new_index. If this
143 // string was not already in the table, returns -1. Otherwise, returns
144 // the previous index it had.
146 int test_and_add(char *new_string, int new_index,
148 int *hash_table, int hash_table_size) {
150 unsigned int code = 0;
152 // This is my recipe. I checked, it seems to work (as long as
153 // hash_table_size is not a multiple of 387433 that should be okay)
155 for(int k = 0; new_string[k]; k++) {
156 code = code * 387433 + (unsigned int) (new_string[k]);
159 code = code % hash_table_size;
161 while(hash_table[code] >= 0) {
162 // There is a string with that code
163 if(strcmp(new_string, strings[hash_table[code]]) == 0) {
164 // It is the same string, we keep a copy of the stored index
165 int result = hash_table[code];
167 hash_table[code] = new_index;
168 // And return the previous one
171 // This collision was not the same string, let's move to the next
173 code = (code + 1) % hash_table_size;
176 // This string was not already in there, store the index in the
177 // table and return -1
178 hash_table[code] = new_index;
182 //////////////////////////////////////////////////////////////////////
183 // A matcher matches either with a collection of substrings, or with a
191 char *splitted_patterns, **patterns;
194 int match(char *string, matcher_t *matcher) {
195 if(matcher->nb_patterns >= 0) {
196 if(matcher->case_sensitive) {
197 for(int n = 0; n < matcher->nb_patterns; n++) {
198 if(strstr(string, matcher->patterns[n]) == 0) return 0;
201 for(int n = 0; n < matcher->nb_patterns; n++) {
202 if(strcasestr(string, matcher->patterns[n]) == 0) return 0;
207 return regexec(&matcher->preg, string, 0, 0, 0) == 0;
211 void free_matcher(matcher_t *matcher) {
212 if(matcher->nb_patterns < 0) {
213 if(!matcher->regexp_error) regfree(&matcher->preg);
215 delete[] matcher->splitted_patterns;
216 delete[] matcher->patterns;
220 void initialize_matcher(int use_regexp, int case_sensitive,
221 matcher_t *matcher, const char *pattern) {
224 matcher->nb_patterns = -1;
225 matcher->regexp_error = regcomp(&matcher->preg, pattern, case_sensitive ? 0 : REG_ICASE);
227 matcher->regexp_error = 0;
228 matcher->nb_patterns = 1;
229 matcher->case_sensitive = case_sensitive;
231 for(const char *s = pattern; *s; s++) {
232 if(*s == pattern_separator) {
233 matcher->nb_patterns++;
237 matcher->splitted_patterns = new char[strlen(pattern) + 1];
238 matcher->patterns = new char*[matcher->nb_patterns];
240 strcpy(matcher->splitted_patterns, pattern);
243 char *last_pattern_start = matcher->splitted_patterns;
244 for(char *s = matcher->splitted_patterns; n < matcher->nb_patterns; s++) {
245 if(*s == pattern_separator || *s == '\0') {
247 matcher->patterns[n++] = last_pattern_start;
248 last_pattern_start = s + 1;
254 //////////////////////////////////////////////////////////////////////
257 void delete_char(char *buffer, int *position) {
258 if(buffer[*position]) {
260 while(c < buffer_size && buffer[c]) {
261 buffer[c] = buffer[c+1];
264 } else error_feedback();
267 void backspace_char(char *buffer, int *position) {
269 if(buffer[*position]) {
270 int c = *position - 1;
272 buffer[c] = buffer[c+1];
276 buffer[*position - 1] = '\0';
280 } else error_feedback();
283 void insert_char(char *buffer, int *position, char character) {
284 if(strlen(buffer) < buffer_size - 1) {
286 char t = buffer[c], u;
295 buffer[(*position)++] = character;
296 } else error_feedback();
299 void kill_before_cursor(char *buffer, int *position) {
301 while(buffer[*position + s]) {
302 buffer[s] = buffer[*position + s];
309 void kill_after_cursor(char *buffer, int *position) {
310 buffer[*position] = '\0';
313 //////////////////////////////////////////////////////////////////////
315 int previous_visible(int current_line, int nb_lines, char **lines, matcher_t *matcher) {
316 int line = current_line - 1;
317 while(line >= 0 && !match(lines[line], matcher)) line--;
321 int next_visible(int current_line, int nb_lines, char **lines, matcher_t *matcher) {
322 int line = current_line + 1;
323 while(line < nb_lines && !match(lines[line], matcher)) line++;
331 //////////////////////////////////////////////////////////////////////
333 // The value passed to this routine in current_focus_line is the index
334 // of the line to highlight if it matches the matcher. The line
335 // highlighted is the first one matching the matcher in that order:
336 // (1) current_focus_line after motion, (2) the first with a greater
337 // index, (3) the first with a lesser index.
339 // The index of the line actually shown highlighted is written in
340 // displayed_focus_line (it can be -1)
342 // If there is a motion and a line is actually shown highlighted, its
343 // value is written in current_focus_line
345 void update_screen(int *current_focus_line, int *displayed_focus_line,
347 int nb_lines, char **lines,
351 char buffer[buffer_size];
354 initialize_matcher(use_regexp, case_sensitive, &matcher, pattern);
356 int console_width = getmaxx(stdscr);
357 int console_height = getmaxy(stdscr);
359 // First, we find a visible line. In priority: The current, or the
360 // first visible after it, or the first visible before it.
362 int nb_printed_lines = 0;
364 use_default_colors();
368 if(matcher.regexp_error) {
370 addnstr("Regexp syntax error", console_width);
372 } else if(nb_lines > 0) {
374 if(match(lines[*current_focus_line], &matcher)) {
375 new_focus_line = *current_focus_line;
377 new_focus_line = next_visible(*current_focus_line, nb_lines, lines, &matcher);
378 if(new_focus_line < 0) {
379 new_focus_line = previous_visible(*current_focus_line, nb_lines, lines, &matcher);
383 // If we found a visible line and we should move, let's move
385 if(new_focus_line >= 0 && motion != 0) {
386 int l = new_focus_line;
388 // We want to go down, let's find the first visible line below
389 for(int m = 0; l >= 0 && m < motion; m++) {
390 l = next_visible(l, nb_lines, lines, &matcher);
396 // We want to go up, let's find the first visible line above
397 for(int m = 0; l >= 0 && m < -motion; m++) {
398 l = previous_visible(l, nb_lines, lines, &matcher);
406 // Here new_focus_line is either a line number matching the pattern, or -1
408 if(new_focus_line >= 0) {
410 int first_line = new_focus_line, last_line = new_focus_line, nb_match = 1;
412 // We find the first and last line to show, so that the total of
413 // visible lines between them (them included) is console_height-1
415 while(nb_match < console_height-1 && (first_line > 0 || last_line < nb_lines - 1)) {
419 while(first_line > 0 && !match(lines[first_line], &matcher)) {
422 if(match(lines[first_line], &matcher)) {
427 if(nb_match < console_height - 1 && last_line < nb_lines - 1) {
429 while(last_line < nb_lines - 1 && !match(lines[last_line], &matcher)) {
433 if(match(lines[last_line], &matcher)) {
439 // Now we display them
441 for(int l = first_line; l <= last_line; l++) {
442 if(match(lines[l], &matcher)) {
445 while(lines[l][k] && k < buffer_size - 2 && k < console_width - 2) {
446 buffer[k] = lines[l][k];
450 // We fill the rest of the line with blanks if this is the
453 if(l == new_focus_line) {
454 while(k < console_width) {
464 // Highlight the highlighted line ...
466 if(l == new_focus_line) {
467 attron(attr_focus_line);
468 addnstr(buffer, console_width);
469 attroff(attr_focus_line);
471 addnstr(buffer, console_width);
478 // If we are on a focused line and we moved, this become the new
482 *current_focus_line = new_focus_line;
486 *displayed_focus_line = new_focus_line;
488 if(nb_printed_lines == 0) {
490 addnstr("No selection", console_width);
495 addnstr("Empty choice", console_width);
505 attron(attr_modeline);
507 for(int k = 0; k < console_width; k++) buffer[k] = ' ';
508 buffer[console_width] = '\0';
509 addnstr(buffer, console_width);
513 // There must be a more elegant way of moving the cursor at a
514 // location met during display
521 cursor_x += strlen(title) + 1;
524 sprintf(buffer, "%d/%d ", nb_printed_lines, nb_lines);
526 cursor_x += strlen(buffer);
528 addnstr(pattern, cursor_position);
529 cursor_x += cursor_position;
531 if(pattern[cursor_position]) {
532 addstr(pattern + cursor_position);
537 if(use_regexp || case_sensitive) {
554 attroff(attr_modeline);
559 free_matcher(&matcher);
562 //////////////////////////////////////////////////////////////////////
564 void read_file(const char *input_filename,
565 int nb_lines_max, int *nb_lines, char **lines,
566 int hash_table_size, int *hash_table) {
568 char raw_line[buffer_size];;
570 ifstream file(input_filename);
573 cerr << "Can not open " << input_filename << endl;
577 while(*nb_lines < nb_lines_max && !file.eof()) {
579 file.getline(raw_line, buffer_size);
584 cerr << "Line too long:" << endl;
585 cerr << raw_line << endl;
593 // Remove the zsh history prefix
595 if(zsh_history && *t == ':') {
596 while(*t && *t != ';') t++;
600 // Remove the bash history prefix
603 while(*t == ' ') t++;
604 while(*t >= '0' && *t <= '9') t++;
605 while(*t == ' ') t++;
608 // Check for duplicates with the hash table and insert the line
609 // in the list if necessary
614 dup = test_and_add(t, *nb_lines, lines, hash_table, hash_table_size);
620 lines[*nb_lines] = new char[strlen(t) + 1];
621 strcpy(lines[*nb_lines], t);
623 // The string was already in there, so we do not allocate a
624 // new string but use the pointer to the first occurence of it
625 lines[*nb_lines] = lines[dup];
634 //////////////////////////////////////////////////////////////////////
636 int main(int argc, char **argv) {
638 if(!ttyname(STDIN_FILENO)) {
639 cerr << "The standard input is not a tty." << endl;
643 int color_fg_modeline, color_bg_modeline;
644 int color_fg_highlight, color_bg_highlight;
646 color_fg_modeline = COLOR_WHITE;
647 color_bg_modeline = COLOR_BLACK;
648 color_fg_highlight = COLOR_BLACK;
649 color_bg_highlight = COLOR_YELLOW;
651 setlocale(LC_ALL, "");
653 char input_filename[buffer_size], output_filename[buffer_size];
655 strcpy(input_filename, "");
656 strcpy(output_filename, "");
659 int error = 0, show_help = 0;
660 int rest_are_files = 0;
662 while(!error && !show_help && i < argc && argv[i][0] == '-' && !rest_are_files) {
664 if(strcmp(argv[i], "-o") == 0) {
665 check_opt(argc, argv, i, 1, "<output filename>");
666 strncpy(output_filename, argv[i+1], buffer_size);
670 else if(strcmp(argv[i], "-s") == 0) {
671 check_opt(argc, argv, i, 1, "<pattern separator>");
672 pattern_separator = argv[i+1][0];
676 else if(strcmp(argv[i], "-x") == 0) {
677 check_opt(argc, argv, i, 1, "<label separator>");
678 label_separator = argv[i+1][0];
682 else if(strcmp(argv[i], "-v") == 0) {
683 output_to_vt_buffer = 1;
687 else if(strcmp(argv[i], "-w") == 0) {
692 else if(strcmp(argv[i], "-m") == 0) {
697 else if(strcmp(argv[i], "-q") == 0) {
702 else if(strcmp(argv[i], "-f") == 0) {
703 check_opt(argc, argv, i, 1, "<input filename>");
704 strncpy(input_filename, argv[i+1], buffer_size);
708 else if(strcmp(argv[i], "-i") == 0) {
713 else if(strcmp(argv[i], "-b") == 0) {
718 else if(strcmp(argv[i], "-z") == 0) {
723 else if(strcmp(argv[i], "-d") == 0) {
724 remove_duplicates = 1;
728 else if(strcmp(argv[i], "-e") == 0) {
733 else if(strcmp(argv[i], "-a") == 0) {
738 else if(strcmp(argv[i], "-t") == 0) {
739 check_opt(argc, argv, i, 1, "<title>");
741 title = new char[strlen(argv[i+1]) + 1];
742 strcpy(title, argv[i+1]);
746 else if(strcmp(argv[i], "-l") == 0) {
747 check_opt(argc, argv, i, 1, "<maximum number of lines>");
748 nb_lines_max = string_to_positive_integer(argv[i+1]);
752 else if(strcmp(argv[i], "-c") == 0) {
753 check_opt(argc, argv, i, 4, "<fg modeline> <bg modeline> <fg highlight> <bg highlight>");
754 color_fg_modeline = string_to_positive_integer(argv[i + 1]);
755 color_bg_modeline = string_to_positive_integer(argv[i + 2]);
756 color_fg_highlight = string_to_positive_integer(argv[i + 3]);
757 color_bg_highlight = string_to_positive_integer(argv[i + 4]);
761 else if(strcmp(argv[i], "--") == 0) {
766 else if(strcmp(argv[i], "-h") == 0) {
772 cerr << "Unknown option " << argv[i] << "." << endl;
777 if(show_help || error) {
778 cerr << "Selector version " << VERSION << "-R" << REVISION_NUMBER
780 << "Written by Francois Fleuret <francois@fleuret.org>."
783 << "Usage: " << argv[0] << " [options] [<filename1> [<filename2> ...]]" << endl
785 << " -h show this help" << endl
786 << " -v inject the selected line in the tty" << endl
787 << " -w quote control characters with ^Qs when using -v" << endl
788 << " -d remove duplicated lines" << endl
789 << " -b remove the bash history line prefix" << endl
790 << " -z remove the zsh history line prefix" << endl
791 << " -i invert the order of lines" << endl
792 << " -e start in regexp mode" << endl
793 << " -a case sensitive" << endl
794 << " -m monochrome mode" << endl
795 << " -q make a flash instead of a beep on an edition error" << endl
796 << " -- all following arguments are filenames" << endl
797 << " -t <title>" << endl
798 << " add a title in the modeline" << endl
799 << " -c <fg modeline> <bg modeline> <fg highlight> <bg highlight>" << endl
800 << " set the display colors" << endl
801 << " -o <output filename>" << endl
802 << " set a file to write the selected line to" << endl
803 << " -s <pattern separator>" << endl
804 << " set the symbol to separate substrings in the pattern" << endl
805 << " -x <label separator>" << endl
806 << " set the symbol to terminate the label" << endl
807 << " -l <max number of lines>" << endl
808 << " set the maximum number of lines to take into account" << endl
814 char **lines = new char *[nb_lines_max];
817 int hash_table_size = nb_lines_max * 10;
820 if(remove_duplicates) {
821 hash_table = new_hash_table(hash_table_size);
824 if(input_filename[0]) {
825 read_file(input_filename,
826 nb_lines_max, &nb_lines, lines,
827 hash_table_size, hash_table);
832 nb_lines_max, &nb_lines, lines,
833 hash_table_size, hash_table);
839 // Now remove the null strings
842 for(int k = 0; k < nb_lines; k++) {
844 lines[n++] = lines[k];
851 for(int i = 0; i < nb_lines / 2; i++) {
852 char *s = lines[nb_lines - 1 - i];
853 lines[nb_lines - 1 - i] = lines[i];
858 // Build the labels from the strings, take only the part before the
859 // label_separator and transform control characters to printable
862 char **labels = new char *[nb_lines];
863 for(int l = 0; l < nb_lines; l++) {
868 while(*t && *t != label_separator) {
872 labels[l] = new char[e + 1];
875 while(*t && *t != label_separator) {
877 while(*u) { *s++ = *u++; }
882 char pattern[buffer_size];
888 //////////////////////////////////////////////////////////////////////
889 // Here we start to display with curse
895 // So that the arrow keys work
896 keypad(stdscr, TRUE);
898 attr_error = A_STANDOUT;
899 attr_modeline = A_REVERSE;
900 attr_focus_line = A_STANDOUT;
902 if(with_colors && has_colors()) {
906 if(color_fg_modeline < 0 || color_fg_modeline >= COLORS ||
907 color_bg_modeline < 0 || color_bg_modeline >= COLORS ||
908 color_fg_highlight < 0 || color_bg_highlight >= COLORS ||
909 color_bg_highlight < 0 || color_bg_highlight >= COLORS) {
912 cerr << "Color numbers have to be between 0 and " << COLORS - 1 << "." << endl;
916 init_pair(1, color_fg_modeline, color_bg_modeline);
917 attr_modeline = COLOR_PAIR(1);
919 init_pair(2, color_fg_highlight, color_bg_highlight);
920 attr_focus_line = COLOR_PAIR(2);
922 init_pair(3, COLOR_WHITE, COLOR_RED);
923 attr_error = COLOR_PAIR(3);
928 int current_focus_line = 0, displayed_focus_line = 0;
930 update_screen(¤t_focus_line, &displayed_focus_line,
932 nb_lines, labels, cursor_position, pattern);
940 if(key >= ' ' && key <= '~') { // Insert character
941 insert_char(pattern, &cursor_position, key);
944 else if(key == KEY_BACKSPACE ||
945 key == '\010' || // ^H
946 key == '\177') { // ^?
947 backspace_char(pattern, &cursor_position);
950 else if(key == KEY_DC ||
951 key == '\004') { // ^D
952 delete_char(pattern, &cursor_position);
955 else if(key == KEY_HOME) {
956 current_focus_line = 0;
959 else if(key == KEY_END) {
960 current_focus_line = nb_lines - 1;
963 else if(key == KEY_NPAGE) {
967 else if(key == KEY_PPAGE) {
971 else if(key == KEY_DOWN ||
972 key == '\016') { // ^N
976 else if(key == KEY_UP ||
977 key == '\020') { // ^P
981 else if(key == KEY_LEFT ||
982 key == '\002') { // ^B
983 if(cursor_position > 0) cursor_position--;
984 else error_feedback();
987 else if(key == KEY_RIGHT ||
988 key == '\006') { // ^F
989 if(pattern[cursor_position]) cursor_position++;
990 else error_feedback();
993 else if(key == '\001') { // ^A
997 else if(key == '\005') { // ^E
998 cursor_position = strlen(pattern);
1001 else if(key == '\022') { // ^R
1002 use_regexp = !use_regexp;
1005 else if(key == '\011') { // ^I
1006 case_sensitive = !case_sensitive;
1009 else if(key == '\025') { // ^U
1010 kill_before_cursor(pattern, &cursor_position);
1013 else if(key == '\013') { // ^K
1014 kill_after_cursor(pattern, &cursor_position);
1017 else if(key == '\014') { // ^L
1018 // I suspect that we may sometime mess up the display
1022 update_screen(¤t_focus_line, &displayed_focus_line,
1024 nb_lines, labels, cursor_position, pattern);
1026 } while(key != '\007' && // ^G
1027 key != '\033' && // ^[ (escape)
1034 //////////////////////////////////////////////////////////////////////
1035 // Here we come back to standard display
1037 if((key == KEY_ENTER || key == '\n')) {
1039 if(output_to_vt_buffer) {
1040 if(displayed_focus_line >= 0 && displayed_focus_line < nb_lines) {
1041 inject_into_tty_buffer(lines[displayed_focus_line]);
1045 if(output_filename[0]) {
1046 ofstream out(output_filename);
1048 cerr << "Can not open " << output_filename << " for writing." << endl;
1051 if(displayed_focus_line >= 0 && displayed_focus_line < nb_lines) {
1052 out << lines[displayed_focus_line] << endl;
1061 cout << "Aborted." << endl;
1064 for(int l = 0; l < nb_lines; l++) {