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 // Here is how 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 int match(char *string, int nb_patterns, char **patterns) {
57 for(int n = 0; n < nb_patterns; n++) {
58 if(strstr(string, patterns[n]) == 0) return 0;
63 //////////////////////////////////////////////////////////////////////
65 void check_opt(int argc, char **argv, int n_opt, int n, const char *help) {
66 if(n_opt + n >= argc) {
67 cerr << "Missing argument for " << argv[n_opt] << "."
69 << "Expecting " << help << "."
75 //////////////////////////////////////////////////////////////////////
77 int previous_visible(int current_line, int nb_lines, char **lines, int nb_patterns, char **patterns) {
78 int line = current_line - 1;
79 while(line >= 0 && !match(lines[line], nb_patterns, patterns)) line--;
83 int next_visible(int current_line, int nb_lines, char **lines, int nb_patterns, char **patterns) {
84 int line = current_line + 1;
85 while(line < nb_lines && !match(lines[line], nb_patterns, patterns)) line++;
93 void update_screen(int *current_line, int *temporary_line, int motion,
94 int nb_lines, char **lines,
98 char buffer[buffer_size];
100 // We split the pattern list into individual patterns
104 for(char *s = pattern_list; *s; s++) {
105 if(*s == pattern_separator) {
110 char splitted_patterns[strlen(pattern_list) + 1];
111 char *patterns[nb_patterns];
113 strcpy(splitted_patterns, pattern_list);
116 char *last_pattern_start = splitted_patterns;
117 for(char *s = splitted_patterns; n < nb_patterns; s++) {
118 if(*s == pattern_separator || *s == '\0') {
120 patterns[n++] = last_pattern_start;
121 last_pattern_start = s + 1;
125 // We now take care of printing the lines per se
127 int console_width = getmaxx(stdscr);
128 int console_height = getmaxy(stdscr);
130 int nb_printed_lines = 1;
132 // First, we find a visible line. In priority: The current, or the
133 // first visible after it, or the first visible before it.
136 if(match(lines[*current_line], nb_patterns, patterns)) {
137 new_line = *current_line;
139 new_line = next_visible(*current_line, nb_lines, lines, nb_patterns, patterns);
141 new_line = previous_visible(*current_line, nb_lines, lines, nb_patterns, patterns);
145 // If we found a visible line and we should move, let's move
147 if(new_line >= 0 && motion != 0) {
150 // We want to go down, let's find the first visible line below
151 for(int m = 0; l >= 0 && m < motion; m++) {
152 l = next_visible(l, nb_lines, lines, nb_patterns, patterns);
158 // We want to go up, let's find the first visible line above
159 for(int m = 0; l >= 0 && m < -motion; m++) {
160 l = previous_visible(l, nb_lines, lines, nb_patterns, patterns);
172 use_default_colors();
176 // Here new_line is either a line number matching the patterns, or -1
180 int first_line = new_line, last_line = new_line, nb_match = 1;
182 // We find the first and last line to show, so that the total of
183 // visible lines between them (them include) is console_height - 1
185 while(nb_match < console_height-1 && (first_line > 0 || last_line < nb_lines - 1)) {
189 while(first_line > 0 && !match(lines[first_line], nb_patterns, patterns)) {
192 if(match(lines[first_line], nb_patterns, patterns)) {
197 if(last_line < nb_lines - 1) {
199 while(last_line < nb_lines - 1 && !match(lines[last_line], nb_patterns, patterns)) {
203 if(match(lines[last_line], nb_patterns, patterns)) {
209 // Now we display them
211 for(int l = first_line; l <= last_line; l++) {
212 if(match(lines[l], nb_patterns, patterns)) {
215 while(lines[l][k] && k < buffer_size - 2 && k < console_width - 2) {
216 buffer[k] = lines[l][k];
220 // We fill the rest of the line with blanks if either we did
221 // not clear() or if this is the highlighted line
223 if(no_blink || l == new_line) {
224 while(k < console_width) {
232 // Highlight the highlighted line ...
236 attron(COLOR_PAIR(2));
237 addnstr(buffer, console_width);
238 attroff(COLOR_PAIR(2));
241 addnstr(buffer, console_width);
245 addnstr(buffer, console_width);
252 *temporary_line = new_line;
254 *current_line = new_line;
258 if(nb_printed_lines == 1) {
259 addnstr("[no selection]\n", console_width);
263 if(no_blink) { // Erase the rest of the window. That's slightly ugly.
265 while(k < console_width) {
270 for(int l = nb_printed_lines; l < console_height; l++) {
271 addnstr(buffer, console_width);
277 sprintf(buffer, "%d/%d pattern: %s",
278 nb_printed_lines - 1,
282 for(int k = strlen(buffer); k < console_width; k++) buffer[k] = ' ';
283 buffer[console_width] = '\0';
287 attron(COLOR_PAIR(1));
288 addnstr(buffer, console_width);
289 attroff(COLOR_PAIR(1));
292 addnstr(buffer, console_width);
301 //////////////////////////////////////////////////////////////////////
303 int main(int argc, char **argv) {
304 char buffer[buffer_size];
305 char *lines[nb_lines_max];
309 setlocale(LC_ALL, "");
311 char input_filename[buffer_size], output_filename[buffer_size];
312 strcpy(input_filename, "");
313 strcpy(output_filename, "/tmp/selector.out");
317 if(strcmp(argv[i], "-o") == 0) {
318 check_opt(argc, argv, i, 1, "<output filename>");
319 strncpy(output_filename, argv[i+1], buffer_size);
323 else if(strcmp(argv[i], "-s") == 0) {
324 check_opt(argc, argv, i, 1, "<pattern separator>");
325 pattern_separator = argv[i+1][0];
329 else if(strcmp(argv[i], "-v") == 0) {
330 output_to_vt_buffer = 1;
334 else if(strcmp(argv[i], "-m") == 0) {
339 else if(strcmp(argv[i], "-f") == 0) {
340 check_opt(argc, argv, i, 1, "<input filename>");
341 strncpy(input_filename, argv[i+1], buffer_size);
345 else if(strcmp(argv[i], "-b") == 0) {
350 else if(strcmp(argv[i], "-l") == 0) {
351 check_opt(argc, argv, i, 1, "<maximum number of lines>");
352 nb_lines_max = atoi(argv[i+1]);
356 else if(strcmp(argv[i], "-t") == 0) {
357 check_opt(argc, argv, i, 1, "<color theme number>");
358 color_theme = atoi(argv[i+1]);
363 cerr << "Selector version " << VERSION
365 << "Written by Francois Fleuret <francois@fleuret.org>"
372 << " [-t <color theme number>]"
373 << " [-o <output filename>]"
374 << " [-s <pattern separator>]"
375 << " [-l <max number of lines>]"
376 << " -f <input filename>"
378 if(strcmp(argv[i], "-h") == 0) {
386 if(!input_filename[0]) {
387 cerr << "You must specify a input file with -f." << endl;
391 ifstream file(input_filename);
394 cerr << "Can not open " << input_filename << endl;
399 while(nb_lines < nb_lines_max && !file.eof()) {
400 file.getline(buffer, buffer_size);
401 lines[nb_lines] = new char[strlen(buffer) + 1];
402 strcpy(lines[nb_lines], buffer);
406 char patterns[buffer_size];
416 switch(color_theme) {
419 init_pair(1, COLOR_WHITE, COLOR_GREEN);
420 init_pair(2, COLOR_BLACK, COLOR_YELLOW);
423 init_pair(1, COLOR_WHITE, COLOR_BLACK);
424 init_pair(2, COLOR_BLACK, COLOR_YELLOW);
427 init_pair(1, COLOR_BLACK, COLOR_GREEN);
428 init_pair(2, COLOR_BLACK, COLOR_YELLOW);
431 init_pair(1, COLOR_BLACK, COLOR_RED);
432 init_pair(2, COLOR_BLACK, COLOR_YELLOW);
435 init_pair(1, COLOR_WHITE, COLOR_BLACK);
436 init_pair(2, COLOR_BLACK, COLOR_BLUE);
439 init_pair(1, COLOR_BLACK, COLOR_MAGENTA);
440 init_pair(2, COLOR_BLACK, COLOR_CYAN);
449 curs_set(0); // Hide the cursor
450 keypad(stdscr, TRUE); // So that the arrow keys work
453 int current_line = 0, temporary_line = 0;
455 update_screen(¤t_line, &temporary_line, 0, nb_lines, lines, patterns, no_blink);
463 if(key >= ' ' && key <= '~') {
464 patterns[patterns_point++] = key;
465 patterns[patterns_point] = '\0';
468 else if(key == KEY_BACKSPACE || key == KEY_DC || key == '
\b') {
469 if(patterns_point > 0) {
471 patterns[patterns_point] = '\0';
475 else if(key == KEY_HOME) {
479 else if(key == KEY_END) {
480 current_line = nb_lines - 1;
483 else if(key == KEY_NPAGE) {
487 else if(key == KEY_PPAGE) {
491 else if(key == KEY_UP || key == '
\10') {
495 else if(key == KEY_DOWN || key == '
\ e') {
499 update_screen(¤t_line, &temporary_line, motion,
500 nb_lines, lines, patterns, no_blink);
502 } while(key != '\n' && key != KEY_ENTER && key != '
\a');
508 if((key == KEY_ENTER || key == '\n') && temporary_line >= 0 && temporary_line < nb_lines) {
509 if(output_to_vt_buffer) {
510 char *tty = ttyname(STDIN_FILENO);
511 int fd = open(tty, O_WRONLY);
513 struct termios oldtio, newtio;
516 // Save current port settings
517 tcgetattr(fd,&oldtio);
518 memset(&newtio, 0, sizeof(newtio));
519 // Set input mode (non-canonical, *no echo*,...)
520 tcflush(fd, TCIFLUSH);
521 tcsetattr(fd,TCSANOW, &newtio);
522 // Put the selected line in the tty input buffer
523 for(char *k = lines[temporary_line]; *k; k++) {
524 ioctl(fd, TIOCSTI, k);
526 // Restore the old settings
527 tcsetattr(fd,TCSANOW, &oldtio);
530 cerr << "Can not open " << tty << "." << endl;
534 ofstream out(output_filename);
536 cerr << "Can not open " << output_filename << " for writing." << endl;
539 out << lines[temporary_line] << endl;
545 for(int l = 0; l < nb_lines; l++) {