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:
26 // ./selector -v -f ${HISTFILE}
28 // This software is highly Linux-specific, but I would be glad to get
29 // patches to make it work on other OS
39 #include <sys/ioctl.h>
46 const int buffer_size = 1024;
48 // Yeah, global variables!
50 int nb_lines_max = 10000;
51 char pattern_separator = ';';
52 int output_to_vt_buffer = 0;
55 int inverse_order = 0;
57 //////////////////////////////////////////////////////////////////////
59 // This looks severely Linux-only ...
61 void inject_into_tty_buffer(char *line) {
62 char *tty = ttyname(STDIN_FILENO);
63 int fd = open(tty, O_WRONLY);
65 struct termios oldtio, newtio;
68 // Save current port settings
69 tcgetattr(fd,&oldtio);
70 memset(&newtio, 0, sizeof(newtio));
71 // Set input mode (non-canonical, *no echo*,...)
72 tcflush(fd, TCIFLUSH);
73 tcsetattr(fd,TCSANOW, &newtio);
74 // Put the selected line in the tty input buffer
75 for(char *k = line; *k; k++) {
76 ioctl(fd, TIOCSTI, k);
78 // Restore the old settings
79 tcsetattr(fd,TCSANOW, &oldtio);
82 cerr << "Can not open " << tty << "." << endl;
87 //////////////////////////////////////////////////////////////////////
89 int match(char *string, int nb_patterns, char **patterns) {
90 for(int n = 0; n < nb_patterns; n++) {
91 if(strstr(string, patterns[n]) == 0) return 0;
96 //////////////////////////////////////////////////////////////////////
98 void check_opt(int argc, char **argv, int n_opt, int n, const char *help) {
99 if(n_opt + n >= argc) {
100 cerr << "Missing argument for " << argv[n_opt] << "."
102 << "Expecting " << help << "."
108 //////////////////////////////////////////////////////////////////////
110 int previous_visible(int current_line, int nb_lines, char **lines, int nb_patterns, char **patterns) {
111 int line = current_line - 1;
112 while(line >= 0 && !match(lines[line], nb_patterns, patterns)) line--;
116 int next_visible(int current_line, int nb_lines, char **lines, int nb_patterns, char **patterns) {
117 int line = current_line + 1;
118 while(line < nb_lines && !match(lines[line], nb_patterns, patterns)) line++;
126 void update_screen(int *current_line, int *temporary_line, int motion,
127 int nb_lines, char **lines,
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);
203 use_default_colors();
207 int nb_printed_lines = 1;
209 // Here new_line is either a line number matching the patterns, or -1
213 int first_line = new_line, last_line = new_line, nb_match = 1;
215 // We find the first and last line to show, so that the total of
216 // visible lines between them (them include) is console_height - 1
218 while(nb_match < console_height-1 && (first_line > 0 || last_line < nb_lines - 1)) {
222 while(first_line > 0 && !match(lines[first_line], nb_patterns, patterns)) {
225 if(match(lines[first_line], nb_patterns, patterns)) {
230 if(last_line < nb_lines - 1) {
232 while(last_line < nb_lines - 1 && !match(lines[last_line], nb_patterns, patterns)) {
236 if(match(lines[last_line], nb_patterns, patterns)) {
242 // Now we display them
244 for(int l = first_line; l <= last_line; l++) {
245 if(match(lines[l], nb_patterns, patterns)) {
248 while(lines[l][k] && k < buffer_size - 2 && k < console_width - 2) {
249 buffer[k] = lines[l][k];
253 // We fill the rest of the line with blanks if either we did
254 // not clear() or if this is the highlighted line
256 if(no_blink || l == new_line) {
257 while(k < console_width) {
265 // Highlight the highlighted line ...
269 attron(COLOR_PAIR(2));
270 addnstr(buffer, console_width);
271 attroff(COLOR_PAIR(2));
274 addnstr(buffer, console_width);
278 addnstr(buffer, console_width);
286 *current_line = new_line;
290 *temporary_line = new_line;
292 if(nb_printed_lines == 1) {
293 addnstr("[no selection]\n", console_width);
297 if(no_blink) { // Erase the rest of the window. That's slightly ugly.
299 while(k < console_width) {
304 for(int l = nb_printed_lines; l < console_height; l++) {
305 addnstr(buffer, console_width);
311 sprintf(buffer, "%d/%d pattern: %s",
312 nb_printed_lines - 1,
316 for(int k = strlen(buffer); k < console_width; k++) buffer[k] = ' ';
317 buffer[console_width] = '\0';
321 attron(COLOR_PAIR(1));
322 addnstr(buffer, console_width);
323 attroff(COLOR_PAIR(1));
326 addnstr(buffer, console_width);
335 //////////////////////////////////////////////////////////////////////
337 int main(int argc, char **argv) {
338 char buffer[buffer_size];
339 char *lines[nb_lines_max];
341 int color_fg_modeline, color_bg_modeline;
342 int color_fg_highlight, color_bg_highlight;
344 color_fg_modeline = COLOR_WHITE;
345 color_bg_modeline = COLOR_BLACK;
346 color_fg_highlight = COLOR_BLACK;
347 color_bg_highlight = COLOR_YELLOW;
349 setlocale(LC_ALL, "");
351 char input_filename[buffer_size], output_filename[buffer_size];
353 strcpy(input_filename, "");
354 strcpy(output_filename, "");
358 if(strcmp(argv[i], "-o") == 0) {
359 check_opt(argc, argv, i, 1, "<output filename>");
360 strncpy(output_filename, argv[i+1], buffer_size);
364 else if(strcmp(argv[i], "-s") == 0) {
365 check_opt(argc, argv, i, 1, "<pattern separator>");
366 pattern_separator = argv[i+1][0];
370 else if(strcmp(argv[i], "-v") == 0) {
371 output_to_vt_buffer = 1;
375 else if(strcmp(argv[i], "-m") == 0) {
380 else if(strcmp(argv[i], "-f") == 0) {
381 check_opt(argc, argv, i, 1, "<input filename>");
382 strncpy(input_filename, argv[i+1], buffer_size);
386 else if(strcmp(argv[i], "-b") == 0) {
391 else if(strcmp(argv[i], "-i") == 0) {
396 else if(strcmp(argv[i], "-z") == 0) {
401 else if(strcmp(argv[i], "-l") == 0) {
402 check_opt(argc, argv, i, 1, "<maximum number of lines>");
403 nb_lines_max = atoi(argv[i+1]);
407 else if(strcmp(argv[i], "-c") == 0) {
408 check_opt(argc, argv, i, 4, "<fg modeline> <bg modeline> <fg highlight> <bg highlight>");
409 color_fg_modeline = atoi(argv[i+1]);
410 color_bg_modeline = atoi(argv[i+2]);
411 color_fg_highlight = atoi(argv[i+3]);
412 color_bg_highlight = atoi(argv[i+4]);
417 cerr << "Selector version " << VERSION
419 << "Written by Francois Fleuret <francois@fleuret.org>"
427 << " [-c <fg modeline> <bg modeline> <fg highlight> <bg highlight>]"
428 << " [-o <output filename>]"
429 << " [-s <pattern separator>]"
430 << " [-l <max number of lines>]"
431 << " -f <input filename>"
433 if(strcmp(argv[i], "-h") == 0) {
441 if(!input_filename[0]) {
442 cerr << "You must specify a input file with -f." << endl;
446 ifstream file(input_filename);
449 cerr << "Can not open " << input_filename << endl;
454 while(nb_lines < nb_lines_max && !file.eof()) {
455 file.getline(buffer, buffer_size);
456 if(strcmp(buffer, "") != 0) {
458 if(zsh_history && *s == ':') {
459 while(*s && *s != ';') s++;
462 lines[nb_lines] = new char[strlen(s) + 1];
463 strcpy(lines[nb_lines], s);
469 for(int i = 0; i < nb_lines/2; i++) {
470 char *s = lines[nb_lines - 1 - i];
471 lines[nb_lines - 1 - i] = lines[i];
476 char patterns[buffer_size];
486 if(color_fg_modeline < 0 || color_fg_modeline >= COLORS ||
487 color_bg_modeline < 0 || color_bg_modeline >= COLORS ||
488 color_fg_highlight < 0 || color_bg_highlight >= COLORS ||
489 color_bg_highlight < 0 || color_bg_highlight >= COLORS) {
493 cerr << "Color numbers have to be between 0 and " << COLORS - 1 << "." << endl;
496 init_pair(1, color_fg_modeline, color_bg_modeline);
497 init_pair(2, color_fg_highlight, color_bg_highlight);
504 curs_set(0); // Hide the cursor
505 keypad(stdscr, TRUE); // So that the arrow keys work
508 int current_line = 0, temporary_line = 0;
510 update_screen(¤t_line, &temporary_line, 0, nb_lines, lines, patterns, no_blink);
518 if(key >= ' ' && key <= '~') {
519 patterns[patterns_point++] = key;
520 patterns[patterns_point] = '\0';
523 else if(key == KEY_BACKSPACE || key == KEY_DC || key == '
\b') {
524 if(patterns_point > 0) {
526 patterns[patterns_point] = '\0';
530 else if(key == KEY_HOME) {
534 else if(key == KEY_END) {
535 current_line = nb_lines - 1;
538 else if(key == KEY_NPAGE) {
542 else if(key == KEY_PPAGE) {
546 else if(key == KEY_UP || key == '
\10') {
550 else if(key == KEY_DOWN || key == '
\ e') {
554 update_screen(¤t_line, &temporary_line, motion,
555 nb_lines, lines, patterns, no_blink);
557 } while(key != '\n' && key != KEY_ENTER && key != '
\a');
563 if((key == KEY_ENTER || key == '\n')) {
564 if(output_to_vt_buffer) {
565 if(temporary_line >= 0 && temporary_line < nb_lines) {
566 inject_into_tty_buffer(lines[temporary_line]);
570 if(output_filename[0]) {
571 ofstream out(output_filename);
573 cerr << "Can not open " << output_filename << " for writing." << endl;
576 if(temporary_line >= 0 && temporary_line < nb_lines) {
577 out << lines[temporary_line] << endl;
586 for(int l = 0; l < nb_lines; l++) {