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 = 10000;
52 char pattern_separator = ';';
53 int output_to_vt_buffer = 0;
55 int zsh_history = 0, bash_history = 0;
56 int inverse_order = 0;
58 //////////////////////////////////////////////////////////////////////
60 // This looks severely Linux-only ...
62 void inject_into_tty_buffer(char *line) {
63 char *tty = ttyname(STDIN_FILENO);
64 int fd = open(tty, O_RDWR);
66 struct termios oldtio, newtio;
69 // Save current port settings
70 tcgetattr(fd,&oldtio);
71 memset(&newtio, 0, sizeof(newtio));
72 // Set input mode (non-canonical, *no echo*,...)
73 tcflush(fd, TCIFLUSH);
74 tcsetattr(fd,TCSANOW, &newtio);
75 // Put the selected line in the tty input buffer
76 for(char *k = line; *k; k++) {
77 ioctl(fd, TIOCSTI, k);
79 // Restore the old settings
80 tcsetattr(fd,TCSANOW, &oldtio);
83 cerr << "Can not open " << tty << "." << endl;
88 //////////////////////////////////////////////////////////////////////
90 int match(char *string, int nb_patterns, char **patterns) {
91 for(int n = 0; n < nb_patterns; n++) {
92 if(strstr(string, patterns[n]) == 0) return 0;
97 //////////////////////////////////////////////////////////////////////
99 void check_opt(int argc, char **argv, int n_opt, int n, const char *help) {
100 if(n_opt + n >= argc) {
101 cerr << "Missing argument for " << argv[n_opt] << "."
103 << "Expecting " << help << "."
109 //////////////////////////////////////////////////////////////////////
111 int previous_visible(int current_line, int nb_lines, char **lines, int nb_patterns, char **patterns) {
112 int line = current_line - 1;
113 while(line >= 0 && !match(lines[line], nb_patterns, patterns)) line--;
117 int next_visible(int current_line, int nb_lines, char **lines, int nb_patterns, char **patterns) {
118 int line = current_line + 1;
119 while(line < nb_lines && !match(lines[line], nb_patterns, patterns)) line++;
127 void update_screen(int *current_line, int *temporary_line, int motion,
128 int nb_lines, char **lines,
129 char *pattern_list) {
131 char buffer[buffer_size];
133 // We split the pattern list into individual patterns
137 for(char *s = pattern_list; *s; s++) {
138 if(*s == pattern_separator) {
143 char splitted_patterns[strlen(pattern_list) + 1];
144 char *patterns[nb_patterns];
146 strcpy(splitted_patterns, pattern_list);
149 char *last_pattern_start = splitted_patterns;
150 for(char *s = splitted_patterns; n < nb_patterns; s++) {
151 if(*s == pattern_separator || *s == '\0') {
153 patterns[n++] = last_pattern_start;
154 last_pattern_start = s + 1;
158 // We now take care of printing the lines per se
160 int console_width = getmaxx(stdscr);
161 int console_height = getmaxy(stdscr);
163 // First, we find a visible line. In priority: The current, or the
164 // first visible after it, or the first visible before it.
167 if(match(lines[*current_line], nb_patterns, patterns)) {
168 new_line = *current_line;
170 new_line = next_visible(*current_line, nb_lines, lines, nb_patterns, patterns);
172 new_line = previous_visible(*current_line, nb_lines, lines, nb_patterns, patterns);
176 // If we found a visible line and we should move, let's move
178 if(new_line >= 0 && motion != 0) {
181 // We want to go down, let's find the first visible line below
182 for(int m = 0; l >= 0 && m < motion; m++) {
183 l = next_visible(l, nb_lines, lines, nb_patterns, patterns);
189 // We want to go up, let's find the first visible line above
190 for(int m = 0; l >= 0 && m < -motion; m++) {
191 l = previous_visible(l, nb_lines, lines, nb_patterns, patterns);
201 use_default_colors();
205 int nb_printed_lines = 1;
207 // Here new_line is either a line number matching the patterns, or -1
211 int first_line = new_line, last_line = new_line, nb_match = 1;
213 // We find the first and last line to show, so that the total of
214 // visible lines between them (them include) is console_height - 1
216 while(nb_match < console_height-1 && (first_line > 0 || last_line < nb_lines - 1)) {
220 while(first_line > 0 && !match(lines[first_line], nb_patterns, patterns)) {
223 if(match(lines[first_line], nb_patterns, patterns)) {
228 if(last_line < nb_lines - 1) {
230 while(last_line < nb_lines - 1 && !match(lines[last_line], nb_patterns, patterns)) {
234 if(match(lines[last_line], nb_patterns, patterns)) {
240 // Now we display them
242 for(int l = first_line; l <= last_line; l++) {
243 if(match(lines[l], nb_patterns, patterns)) {
246 while(lines[l][k] && k < buffer_size - 2 && k < console_width - 2) {
247 buffer[k] = lines[l][k];
251 // We fill the rest of the line with blanks if either we did
252 // not clear() or if this is the highlighted line
255 while(k < console_width) {
263 // Highlight the highlighted line ...
267 attron(COLOR_PAIR(2));
268 addnstr(buffer, console_width);
269 attroff(COLOR_PAIR(2));
272 addnstr(buffer, console_width);
276 addnstr(buffer, console_width);
284 *current_line = new_line;
288 *temporary_line = new_line;
290 if(nb_printed_lines == 1) {
291 addnstr("[no selection]\n", console_width);
297 sprintf(buffer, "%d/%d pattern: %s",
298 nb_printed_lines - 1,
302 for(int k = strlen(buffer); k < console_width; k++) buffer[k] = ' ';
303 buffer[console_width] = '\0';
307 attron(COLOR_PAIR(1));
308 addnstr(buffer, console_width);
309 attroff(COLOR_PAIR(1));
312 addnstr(buffer, console_width);
321 //////////////////////////////////////////////////////////////////////
323 int main(int argc, char **argv) {
324 char buffer[buffer_size];
325 char *lines[nb_lines_max];
326 int color_fg_modeline, color_bg_modeline;
327 int color_fg_highlight, color_bg_highlight;
329 color_fg_modeline = COLOR_WHITE;
330 color_bg_modeline = COLOR_BLACK;
331 color_fg_highlight = COLOR_BLACK;
332 color_bg_highlight = COLOR_YELLOW;
334 setlocale(LC_ALL, "");
336 char input_filename[buffer_size], output_filename[buffer_size];
338 strcpy(input_filename, "");
339 strcpy(output_filename, "");
344 if(strcmp(argv[i], "-o") == 0) {
345 check_opt(argc, argv, i, 1, "<output filename>");
346 strncpy(output_filename, argv[i+1], buffer_size);
350 else if(strcmp(argv[i], "-s") == 0) {
351 check_opt(argc, argv, i, 1, "<pattern separator>");
352 pattern_separator = argv[i+1][0];
356 else if(strcmp(argv[i], "-v") == 0) {
357 output_to_vt_buffer = 1;
361 else if(strcmp(argv[i], "-m") == 0) {
366 else if(strcmp(argv[i], "-f") == 0) {
367 check_opt(argc, argv, i, 1, "<input filename>");
368 strncpy(input_filename, argv[i+1], buffer_size);
372 else if(strcmp(argv[i], "-i") == 0) {
377 else if(strcmp(argv[i], "-z") == 0) {
382 else if(strcmp(argv[i], "-b") == 0) {
387 else if(strcmp(argv[i], "-l") == 0) {
388 check_opt(argc, argv, i, 1, "<maximum number of lines>");
389 nb_lines_max = atoi(argv[i+1]);
393 else if(strcmp(argv[i], "-c") == 0) {
394 check_opt(argc, argv, i, 4, "<fg modeline> <bg modeline> <fg highlight> <bg highlight>");
395 color_fg_modeline = atoi(argv[i+1]);
396 color_bg_modeline = atoi(argv[i+2]);
397 color_fg_highlight = atoi(argv[i+3]);
398 color_bg_highlight = atoi(argv[i+4]);
403 cerr << "Selector version " << VERSION
405 << "Written by Francois Fleuret <francois@fleuret.org>"
413 << " [-c <fg modeline> <bg modeline> <fg highlight> <bg highlight>]"
414 << " [-o <output filename>]"
415 << " [-s <pattern separator>]"
416 << " [-l <max number of lines>]"
417 << " -f <input filename>"
419 if(strcmp(argv[i], "-h") == 0) {
427 if(!input_filename[0]) {
428 cerr << "You must specify a input file with -f." << endl;
434 ifstream file(input_filename);
437 cerr << "Can not open " << input_filename << endl;
441 while(nb_lines < nb_lines_max && !file.eof()) {
442 file.getline(buffer, buffer_size);
443 if(strcmp(buffer, "") != 0) {
445 if(zsh_history && *s == ':') {
446 while(*s && *s != ';') s++;
450 if(bash_history && (*s == ' ' || (*s >= '0' && *s <= '9'))) {
451 while(*s == ' ' || (*s >= '0' && *s <= '9')) s++;
454 lines[nb_lines] = new char[strlen(s) + 1];
455 strcpy(lines[nb_lines], s);
461 for(int i = 0; i < nb_lines/2; i++) {
462 char *s = lines[nb_lines - 1 - i];
463 lines[nb_lines - 1 - i] = lines[i];
468 char patterns[buffer_size];
478 if(color_fg_modeline < 0 || color_fg_modeline >= COLORS ||
479 color_bg_modeline < 0 || color_bg_modeline >= COLORS ||
480 color_fg_highlight < 0 || color_bg_highlight >= COLORS ||
481 color_bg_highlight < 0 || color_bg_highlight >= COLORS) {
485 cerr << "Color numbers have to be between 0 and " << COLORS - 1 << "." << endl;
488 init_pair(1, color_fg_modeline, color_bg_modeline);
489 init_pair(2, color_fg_highlight, color_bg_highlight);
496 curs_set(0); // Hide the cursor
497 keypad(stdscr, TRUE); // So that the arrow keys work
500 int current_line = 0, temporary_line = 0;
502 update_screen(¤t_line, &temporary_line, 0, nb_lines, lines, patterns);
510 if(key >= ' ' && key <= '~') {
511 patterns[patterns_point++] = key;
512 patterns[patterns_point] = '\0';
515 else if(key == KEY_BACKSPACE || key == '
\b' ||
516 key == KEY_DC || key == '
\ 4') {
517 if(patterns_point > 0) {
519 patterns[patterns_point] = '\0';
523 else if(key == KEY_HOME) {
527 else if(key == KEY_END) {
528 current_line = nb_lines - 1;
531 else if(key == KEY_NPAGE) {
535 else if(key == KEY_PPAGE) {
539 else if(key == KEY_UP || key == '
\10') {
543 else if(key == KEY_DOWN || key == '
\ e') {
547 update_screen(¤t_line, &temporary_line, motion,
548 nb_lines, lines, patterns);
550 } while(key != '\n' && key != KEY_ENTER && key != '
\a');
556 if((key == KEY_ENTER || key == '\n')) {
558 if(output_to_vt_buffer) {
559 if(temporary_line >= 0 && temporary_line < nb_lines) {
560 inject_into_tty_buffer(lines[temporary_line]);
564 if(output_filename[0]) {
565 ofstream out(output_filename);
567 cerr << "Can not open " << output_filename << " for writing." << endl;
570 if(temporary_line >= 0 && temporary_line < nb_lines) {
571 out << lines[temporary_line] << endl;
581 for(int l = 0; l < nb_lines; l++) {