3 * selector is a simple shell command for selection of strings with a
4 * 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:
27 // alias h='./selector -i -b -v -f <(history)'
29 // This software is highly Linux-specific, but I would be glad to get
30 // patches to make it work on other OS
40 #include <sys/ioctl.h>
47 const int buffer_size = 1024;
49 // Yeah, global variables!
51 int nb_lines_max = 1000;
52 char pattern_separator = ';';
53 int output_to_vt_buffer = 0;
55 int zsh_history = 0, bash_history = 0;
56 int inverse_order = 0;
57 int remove_duplicates = 0;
59 //////////////////////////////////////////////////////////////////////
61 // int test_and_set_hash(char *s, char **hashed, int hash_size) {
62 // uint32_t code = 0, mask;
65 // for(int k = 0; s[k]; k++) {
66 // code += ((uint32_t) (s[k])) << (8 * k%4);
69 // code = code%hash_table_size;
76 //////////////////////////////////////////////////////////////////////
78 // This looks severely Linux-only ...
80 void inject_into_tty_buffer(char *line) {
81 struct termios oldtio, newtio;
82 tcgetattr(STDIN_FILENO,&oldtio);
83 memset(&newtio, 0, sizeof(newtio));
84 // Set input mode (non-canonical, *no echo*,...)
85 tcsetattr(STDIN_FILENO,TCSANOW, &newtio);
86 // Put the selected line in the tty input buffer
87 for(char *k = line; *k; k++) {
88 ioctl(STDIN_FILENO, TIOCSTI, k);
90 // Restore the old settings
91 tcsetattr(STDIN_FILENO,TCSANOW, &oldtio);
94 //////////////////////////////////////////////////////////////////////
96 int match(char *string, int nb_patterns, char **patterns) {
97 for(int n = 0; n < nb_patterns; n++) {
98 if(strstr(string, patterns[n]) == 0) return 0;
103 //////////////////////////////////////////////////////////////////////
105 void check_opt(int argc, char **argv, int n_opt, int n, const char *help) {
106 if(n_opt + n >= argc) {
107 cerr << "Missing argument for " << argv[n_opt] << "."
109 << "Expecting " << help << "."
115 //////////////////////////////////////////////////////////////////////
117 int previous_visible(int current_line, int nb_lines, char **lines, int nb_patterns, char **patterns) {
118 int line = current_line - 1;
119 while(line >= 0 && !match(lines[line], nb_patterns, patterns)) line--;
123 int next_visible(int current_line, int nb_lines, char **lines, int nb_patterns, char **patterns) {
124 int line = current_line + 1;
125 while(line < nb_lines && !match(lines[line], nb_patterns, patterns)) line++;
133 void update_screen(int *current_line, int *temporary_line, int motion,
134 int nb_lines, char **lines,
135 char *pattern_list) {
137 char buffer[buffer_size];
139 // We split the pattern list into individual patterns
143 for(char *s = pattern_list; *s; s++) {
144 if(*s == pattern_separator) {
149 char splitted_patterns[strlen(pattern_list) + 1];
150 char *patterns[nb_patterns];
152 strcpy(splitted_patterns, pattern_list);
155 char *last_pattern_start = splitted_patterns;
156 for(char *s = splitted_patterns; n < nb_patterns; s++) {
157 if(*s == pattern_separator || *s == '\0') {
159 patterns[n++] = last_pattern_start;
160 last_pattern_start = s + 1;
164 // We now take care of printing the lines per se
166 int console_width = getmaxx(stdscr);
167 int console_height = getmaxy(stdscr);
169 // First, we find a visible line. In priority: The current, or the
170 // first visible after it, or the first visible before it.
173 if(match(lines[*current_line], nb_patterns, patterns)) {
174 new_line = *current_line;
176 new_line = next_visible(*current_line, nb_lines, lines, nb_patterns, patterns);
178 new_line = previous_visible(*current_line, nb_lines, lines, nb_patterns, patterns);
182 // If we found a visible line and we should move, let's move
184 if(new_line >= 0 && motion != 0) {
187 // We want to go down, let's find the first visible line below
188 for(int m = 0; l >= 0 && m < motion; m++) {
189 l = next_visible(l, nb_lines, lines, nb_patterns, patterns);
195 // We want to go up, let's find the first visible line above
196 for(int m = 0; l >= 0 && m < -motion; m++) {
197 l = previous_visible(l, nb_lines, lines, nb_patterns, patterns);
207 use_default_colors();
211 int nb_printed_lines = 0;
213 // Here new_line is either a line number matching the patterns, or -1
217 int first_line = new_line, last_line = new_line, nb_match = 1;
219 // We find the first and last line to show, so that the total of
220 // visible lines between them (them include) is console_height - 1
222 while(nb_match < console_height-1 && (first_line > 0 || last_line < nb_lines - 1)) {
226 while(first_line > 0 && !match(lines[first_line], nb_patterns, patterns)) {
229 if(match(lines[first_line], nb_patterns, patterns)) {
234 if(last_line < nb_lines - 1) {
236 while(last_line < nb_lines - 1 && !match(lines[last_line], nb_patterns, patterns)) {
240 if(match(lines[last_line], nb_patterns, patterns)) {
246 // Now we display them
248 for(int l = first_line; l <= last_line; l++) {
249 if(match(lines[l], nb_patterns, patterns)) {
252 while(lines[l][k] && k < buffer_size - 2 && k < console_width - 2) {
253 buffer[k] = lines[l][k];
257 // We fill the rest of the line with blanks if either we did
258 // not clear() or if this is the highlighted line
261 while(k < console_width) {
269 // Highlight the highlighted line ...
273 attron(COLOR_PAIR(2));
274 addnstr(buffer, console_width);
275 attroff(COLOR_PAIR(2));
278 addnstr(buffer, console_width);
282 addnstr(buffer, console_width);
290 *current_line = new_line;
294 *temporary_line = new_line;
296 if(nb_printed_lines == 0) {
297 addnstr("[no selection]\n", console_width);
302 sprintf(buffer, "%d/%d pattern: %s",
307 for(int k = strlen(buffer); k < console_width; k++) buffer[k] = ' ';
308 buffer[console_width] = '\0';
312 attron(COLOR_PAIR(1));
313 addnstr(buffer, console_width);
314 attroff(COLOR_PAIR(1));
317 addnstr(buffer, console_width);
326 //////////////////////////////////////////////////////////////////////
328 int main(int argc, char **argv) {
329 char buffer[buffer_size];
330 int color_fg_modeline, color_bg_modeline;
331 int color_fg_highlight, color_bg_highlight;
333 color_fg_modeline = COLOR_WHITE;
334 color_bg_modeline = COLOR_BLACK;
335 color_fg_highlight = COLOR_BLACK;
336 color_bg_highlight = COLOR_YELLOW;
338 setlocale(LC_ALL, "");
340 char input_filename[buffer_size], output_filename[buffer_size];
342 strcpy(input_filename, "");
343 strcpy(output_filename, "");
348 if(strcmp(argv[i], "-o") == 0) {
349 check_opt(argc, argv, i, 1, "<output filename>");
350 strncpy(output_filename, argv[i+1], buffer_size);
354 else if(strcmp(argv[i], "-s") == 0) {
355 check_opt(argc, argv, i, 1, "<pattern separator>");
356 pattern_separator = argv[i+1][0];
360 else if(strcmp(argv[i], "-v") == 0) {
361 output_to_vt_buffer = 1;
365 else if(strcmp(argv[i], "-m") == 0) {
370 else if(strcmp(argv[i], "-f") == 0) {
371 check_opt(argc, argv, i, 1, "<input filename>");
372 strncpy(input_filename, argv[i+1], buffer_size);
376 else if(strcmp(argv[i], "-i") == 0) {
381 else if(strcmp(argv[i], "-z") == 0) {
386 else if(strcmp(argv[i], "-b") == 0) {
391 else if(strcmp(argv[i], "-r") == 0) {
392 remove_duplicates = 1;
396 else if(strcmp(argv[i], "-l") == 0) {
397 check_opt(argc, argv, i, 1, "<maximum number of lines>");
398 nb_lines_max = atoi(argv[i+1]);
402 else if(strcmp(argv[i], "-c") == 0) {
403 check_opt(argc, argv, i, 4, "<fg modeline> <bg modeline> <fg highlight> <bg highlight>");
404 color_fg_modeline = atoi(argv[i+1]);
405 color_bg_modeline = atoi(argv[i+2]);
406 color_fg_highlight = atoi(argv[i+3]);
407 color_bg_highlight = atoi(argv[i+4]);
412 cerr << "Selector version " << VERSION
414 << "Written by Francois Fleuret <francois@fleuret.org>"
423 << " [-c <fg modeline> <bg modeline> <fg highlight> <bg highlight>]"
424 << " [-o <output filename>]"
425 << " [-s <pattern separator>]"
426 << " [-l <max number of lines>]"
427 << " -f <input filename>"
429 if(strcmp(argv[i], "-h") == 0) {
437 char **lines = new char *[nb_lines_max];
439 if(!input_filename[0]) {
440 cerr << "You must specify a input file with -f." << endl;
446 ifstream file(input_filename);
449 cerr << "Can not open " << input_filename << endl;
453 while(nb_lines < nb_lines_max && !file.eof()) {
454 file.getline(buffer, buffer_size);
455 if(strcmp(buffer, "") != 0) {
457 if(zsh_history && *s == ':') {
458 while(*s && *s != ';') s++;
462 if(bash_history && (*s == ' ' || (*s >= '0' && *s <= '9'))) {
463 while(*s == ' ' || (*s >= '0' && *s <= '9')) s++;
466 if(!remove_duplicates || nb_lines == 0 || strcmp(lines[nb_lines - 1], s)) {
467 lines[nb_lines] = new char[strlen(s) + 1];
468 strcpy(lines[nb_lines], s);
475 for(int i = 0; i < nb_lines/2; i++) {
476 char *s = lines[nb_lines - 1 - i];
477 lines[nb_lines - 1 - i] = lines[i];
482 char patterns[buffer_size];
492 if(color_fg_modeline < 0 || color_fg_modeline >= COLORS ||
493 color_bg_modeline < 0 || color_bg_modeline >= COLORS ||
494 color_fg_highlight < 0 || color_bg_highlight >= COLORS ||
495 color_bg_highlight < 0 || color_bg_highlight >= COLORS) {
499 cerr << "Color numbers have to be between 0 and " << COLORS - 1 << "." << endl;
502 init_pair(1, color_fg_modeline, color_bg_modeline);
503 init_pair(2, color_fg_highlight, color_bg_highlight);
510 curs_set(0); // Hide the cursor
511 keypad(stdscr, TRUE); // So that the arrow keys work
514 int current_line = 0, temporary_line = 0;
516 update_screen(¤t_line, &temporary_line, 0, nb_lines, lines, patterns);
524 if(key >= ' ' && key <= '~') {
525 patterns[patterns_point++] = key;
526 patterns[patterns_point] = '\0';
529 else if(key == KEY_BACKSPACE || key == '
\b' || key == '
\7f' ||
530 key == KEY_DC || key == '
\ 4') {
531 if(patterns_point > 0) {
533 patterns[patterns_point] = '\0';
537 else if(key == KEY_HOME) {
541 else if(key == KEY_END) {
542 current_line = nb_lines - 1;
545 else if(key == KEY_NPAGE) {
549 else if(key == KEY_PPAGE) {
553 else if(key == KEY_UP || key == '
\10') {
557 else if(key == KEY_DOWN || key == '
\ e') {
561 update_screen(¤t_line, &temporary_line, motion,
562 nb_lines, lines, patterns);
564 } while(key != '\n' && key != KEY_ENTER && key != '
\a');
570 if((key == KEY_ENTER || key == '\n')) {
572 if(output_to_vt_buffer) {
573 if(temporary_line >= 0 && temporary_line < nb_lines) {
574 inject_into_tty_buffer(lines[temporary_line]);
578 if(output_filename[0]) {
579 ofstream out(output_filename);
581 cerr << "Can not open " << output_filename << " for writing." << endl;
584 if(temporary_line >= 0 && temporary_line < nb_lines) {
585 out << lines[temporary_line] << endl;
595 for(int l = 0; l < nb_lines; l++) {