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 // Yeah, global variables!
48 int buffer_size = 1024;
49 int nb_lines_max = 10000;
50 char pattern_separator = ';';
51 int output_to_vt_buffer = 0;
54 //////////////////////////////////////////////////////////////////////
56 // This looks severely Linux-only ...
58 void inject_into_tty_buffer(char *line) {
59 char *tty = ttyname(STDIN_FILENO);
60 int fd = open(tty, O_WRONLY);
62 struct termios oldtio, newtio;
65 // Save current port settings
66 tcgetattr(fd,&oldtio);
67 memset(&newtio, 0, sizeof(newtio));
68 // Set input mode (non-canonical, *no echo*,...)
69 tcflush(fd, TCIFLUSH);
70 tcsetattr(fd,TCSANOW, &newtio);
71 // Put the selected line in the tty input buffer
72 for(char *k = line; *k; k++) {
73 ioctl(fd, TIOCSTI, k);
75 // Restore the old settings
76 tcsetattr(fd,TCSANOW, &oldtio);
79 cerr << "Can not open " << tty << "." << endl;
84 //////////////////////////////////////////////////////////////////////
86 int match(char *string, int nb_patterns, char **patterns) {
87 for(int n = 0; n < nb_patterns; n++) {
88 if(strstr(string, patterns[n]) == 0) return 0;
93 //////////////////////////////////////////////////////////////////////
95 void check_opt(int argc, char **argv, int n_opt, int n, const char *help) {
96 if(n_opt + n >= argc) {
97 cerr << "Missing argument for " << argv[n_opt] << "."
99 << "Expecting " << help << "."
105 //////////////////////////////////////////////////////////////////////
107 int previous_visible(int current_line, int nb_lines, char **lines, int nb_patterns, char **patterns) {
108 int line = current_line - 1;
109 while(line >= 0 && !match(lines[line], nb_patterns, patterns)) line--;
113 int next_visible(int current_line, int nb_lines, char **lines, int nb_patterns, char **patterns) {
114 int line = current_line + 1;
115 while(line < nb_lines && !match(lines[line], nb_patterns, patterns)) line++;
123 void update_screen(int *current_line, int *temporary_line, int motion,
124 int nb_lines, char **lines,
128 char buffer[buffer_size];
130 // We split the pattern list into individual patterns
134 for(char *s = pattern_list; *s; s++) {
135 if(*s == pattern_separator) {
140 char splitted_patterns[strlen(pattern_list) + 1];
141 char *patterns[nb_patterns];
143 strcpy(splitted_patterns, pattern_list);
146 char *last_pattern_start = splitted_patterns;
147 for(char *s = splitted_patterns; n < nb_patterns; s++) {
148 if(*s == pattern_separator || *s == '\0') {
150 patterns[n++] = last_pattern_start;
151 last_pattern_start = s + 1;
155 // We now take care of printing the lines per se
157 int console_width = getmaxx(stdscr);
158 int console_height = getmaxy(stdscr);
160 // First, we find a visible line. In priority: The current, or the
161 // first visible after it, or the first visible before it.
164 if(match(lines[*current_line], nb_patterns, patterns)) {
165 new_line = *current_line;
167 new_line = next_visible(*current_line, nb_lines, lines, nb_patterns, patterns);
169 new_line = previous_visible(*current_line, nb_lines, lines, nb_patterns, patterns);
173 // If we found a visible line and we should move, let's move
175 if(new_line >= 0 && motion != 0) {
178 // We want to go down, let's find the first visible line below
179 for(int m = 0; l >= 0 && m < motion; m++) {
180 l = next_visible(l, nb_lines, lines, nb_patterns, patterns);
186 // We want to go up, let's find the first visible line above
187 for(int m = 0; l >= 0 && m < -motion; m++) {
188 l = previous_visible(l, nb_lines, lines, nb_patterns, patterns);
200 use_default_colors();
204 int nb_printed_lines = 1;
206 // Here new_line is either a line number matching the patterns, or -1
210 int first_line = new_line, last_line = new_line, nb_match = 1;
212 // We find the first and last line to show, so that the total of
213 // visible lines between them (them include) is console_height - 1
215 while(nb_match < console_height-1 && (first_line > 0 || last_line < nb_lines - 1)) {
219 while(first_line > 0 && !match(lines[first_line], nb_patterns, patterns)) {
222 if(match(lines[first_line], nb_patterns, patterns)) {
227 if(last_line < nb_lines - 1) {
229 while(last_line < nb_lines - 1 && !match(lines[last_line], nb_patterns, patterns)) {
233 if(match(lines[last_line], nb_patterns, patterns)) {
239 // Now we display them
241 for(int l = first_line; l <= last_line; l++) {
242 if(match(lines[l], nb_patterns, patterns)) {
245 while(lines[l][k] && k < buffer_size - 2 && k < console_width - 2) {
246 buffer[k] = lines[l][k];
250 // We fill the rest of the line with blanks if either we did
251 // not clear() or if this is the highlighted line
253 if(no_blink || l == new_line) {
254 while(k < console_width) {
262 // Highlight the highlighted line ...
266 attron(COLOR_PAIR(2));
267 addnstr(buffer, console_width);
268 attroff(COLOR_PAIR(2));
271 addnstr(buffer, console_width);
275 addnstr(buffer, console_width);
283 *current_line = new_line;
287 *temporary_line = new_line;
289 if(nb_printed_lines == 1) {
290 addnstr("[no selection]\n", console_width);
294 if(no_blink) { // Erase the rest of the window. That's slightly ugly.
296 while(k < console_width) {
301 for(int l = nb_printed_lines; l < console_height; l++) {
302 addnstr(buffer, console_width);
308 sprintf(buffer, "%d/%d pattern: %s",
309 nb_printed_lines - 1,
313 for(int k = strlen(buffer); k < console_width; k++) buffer[k] = ' ';
314 buffer[console_width] = '\0';
318 attron(COLOR_PAIR(1));
319 addnstr(buffer, console_width);
320 attroff(COLOR_PAIR(1));
323 addnstr(buffer, console_width);
332 //////////////////////////////////////////////////////////////////////
334 int main(int argc, char **argv) {
335 char buffer[buffer_size];
336 char *lines[nb_lines_max];
338 int color_fg_modeline, color_bg_modeline;
339 int color_fg_highlight, color_bg_highlight;
341 color_fg_modeline = COLOR_WHITE;
342 color_bg_modeline = COLOR_BLACK;
343 color_fg_highlight = COLOR_BLACK;
344 color_bg_highlight = COLOR_YELLOW;
346 setlocale(LC_ALL, "");
348 char input_filename[buffer_size], output_filename[buffer_size];
349 strcpy(input_filename, "");
350 strcpy(output_filename, "/tmp/selector.out");
354 if(strcmp(argv[i], "-o") == 0) {
355 check_opt(argc, argv, i, 1, "<output filename>");
356 strncpy(output_filename, argv[i+1], buffer_size);
360 else if(strcmp(argv[i], "-s") == 0) {
361 check_opt(argc, argv, i, 1, "<pattern separator>");
362 pattern_separator = argv[i+1][0];
366 else if(strcmp(argv[i], "-v") == 0) {
367 output_to_vt_buffer = 1;
371 else if(strcmp(argv[i], "-m") == 0) {
376 else if(strcmp(argv[i], "-f") == 0) {
377 check_opt(argc, argv, i, 1, "<input filename>");
378 strncpy(input_filename, argv[i+1], buffer_size);
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>"
411 << " [-c <fg modeline> <bg modeline> <fg highlight> <bg highlight>]"
412 << " [-o <output filename>]"
413 << " [-s <pattern separator>]"
414 << " [-l <max number of lines>]"
415 << " -f <input filename>"
417 if(strcmp(argv[i], "-h") == 0) {
425 if(!input_filename[0]) {
426 cerr << "You must specify a input file with -f." << endl;
430 ifstream file(input_filename);
433 cerr << "Can not open " << input_filename << endl;
438 while(nb_lines < nb_lines_max && !file.eof()) {
439 file.getline(buffer, buffer_size);
440 lines[nb_lines] = new char[strlen(buffer) + 1];
441 strcpy(lines[nb_lines], buffer);
445 char patterns[buffer_size];
455 if(color_fg_modeline < 0 || color_fg_modeline >= COLORS ||
456 color_bg_modeline < 0 || color_bg_modeline >= COLORS ||
457 color_fg_highlight < 0 || color_bg_highlight >= COLORS ||
458 color_bg_highlight < 0 || color_bg_highlight >= COLORS) {
462 cerr << "Color numbers have to be between 0 and " << COLORS - 1 << "." << endl;
465 init_pair(1, color_fg_modeline, color_bg_modeline);
466 init_pair(2, color_fg_highlight, color_bg_highlight);
473 curs_set(0); // Hide the cursor
474 keypad(stdscr, TRUE); // So that the arrow keys work
477 int current_line = 0, temporary_line = 0;
479 update_screen(¤t_line, &temporary_line, 0, nb_lines, lines, patterns, no_blink);
487 if(key >= ' ' && key <= '~') {
488 patterns[patterns_point++] = key;
489 patterns[patterns_point] = '\0';
492 else if(key == KEY_BACKSPACE || key == KEY_DC || key == '
\b') {
493 if(patterns_point > 0) {
495 patterns[patterns_point] = '\0';
499 else if(key == KEY_HOME) {
503 else if(key == KEY_END) {
504 current_line = nb_lines - 1;
507 else if(key == KEY_NPAGE) {
511 else if(key == KEY_PPAGE) {
515 else if(key == KEY_UP || key == '
\10') {
519 else if(key == KEY_DOWN || key == '
\ e') {
523 update_screen(¤t_line, &temporary_line, motion,
524 nb_lines, lines, patterns, no_blink);
526 } while(key != '\n' && key != KEY_ENTER && key != '
\a');
532 if((key == KEY_ENTER || key == '\n')) {
533 if(output_to_vt_buffer) {
534 if(temporary_line >= 0 && temporary_line < nb_lines) {
535 inject_into_tty_buffer(lines[temporary_line]);
538 ofstream out(output_filename);
540 cerr << "Can not open " << output_filename << " for writing." << endl;
543 if(temporary_line >= 0 && temporary_line < nb_lines) {
544 out << lines[temporary_line] << endl;
553 for(int l = 0; l < nb_lines; l++) {