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 char input_filename[buffer_size], output_filename[buffer_size];
310 strcpy(input_filename, "");
311 strcpy(output_filename, "/tmp/selector.out");
315 if(strcmp(argv[i], "-o") == 0) {
316 check_opt(argc, argv, i, 1, "<output filename>");
317 strncpy(output_filename, argv[i+1], buffer_size);
321 else if(strcmp(argv[i], "-s") == 0) {
322 check_opt(argc, argv, i, 1, "<pattern separator>");
323 pattern_separator = argv[i+1][0];
327 else if(strcmp(argv[i], "-v") == 0) {
328 output_to_vt_buffer = 1;
332 else if(strcmp(argv[i], "-m") == 0) {
337 else if(strcmp(argv[i], "-f") == 0) {
338 check_opt(argc, argv, i, 1, "<input filename>");
339 strncpy(input_filename, argv[i+1], buffer_size);
343 else if(strcmp(argv[i], "-b") == 0) {
348 else if(strcmp(argv[i], "-l") == 0) {
349 check_opt(argc, argv, i, 1, "<maximum number of lines>");
350 nb_lines_max = atoi(argv[i+1]);
354 else if(strcmp(argv[i], "-t") == 0) {
355 check_opt(argc, argv, i, 1, "<color theme number>");
356 color_theme = atoi(argv[i+1]);
361 cerr << "Selector version " << VERSION
363 << "Written by Francois Fleuret <francois@fleuret.org>"
370 << " [-t <color theme number>]"
371 << " [-o <output filename>]"
372 << " [-s <pattern separator>]"
373 << " [-l <max number of lines>]"
374 << " -f <input filename>"
376 if(strcmp(argv[i], "-h") == 0) {
384 if(!input_filename[0]) {
385 cerr << "You must specify a input file with -f." << endl;
389 ifstream file(input_filename);
392 cerr << "Can not open " << input_filename << endl;
397 while(nb_lines < nb_lines_max && !file.eof()) {
398 file.getline(buffer, buffer_size);
399 lines[nb_lines] = new char[strlen(buffer) + 1];
400 strcpy(lines[nb_lines], buffer);
404 char patterns[buffer_size];
414 switch(color_theme) {
417 init_pair(1, COLOR_WHITE, COLOR_GREEN);
418 init_pair(2, COLOR_BLACK, COLOR_YELLOW);
421 init_pair(1, COLOR_WHITE, COLOR_BLACK);
422 init_pair(2, COLOR_BLACK, COLOR_YELLOW);
425 init_pair(1, COLOR_BLACK, COLOR_GREEN);
426 init_pair(2, COLOR_BLACK, COLOR_YELLOW);
429 init_pair(1, COLOR_BLACK, COLOR_RED);
430 init_pair(2, COLOR_BLACK, COLOR_YELLOW);
433 init_pair(1, COLOR_WHITE, COLOR_BLACK);
434 init_pair(2, COLOR_BLACK, COLOR_BLUE);
437 init_pair(1, COLOR_BLACK, COLOR_MAGENTA);
438 init_pair(2, COLOR_BLACK, COLOR_CYAN);
447 curs_set(0); // Hide the cursor
448 keypad(stdscr, TRUE); // So that the arrow keys work
451 int current_line = 0, temporary_line = 0;
453 update_screen(¤t_line, &temporary_line, 0, nb_lines, lines, patterns, no_blink);
461 if(key >= ' ' && key <= '~') {
462 patterns[patterns_point++] = key;
463 patterns[patterns_point] = '\0';
466 else if(key == KEY_BACKSPACE || key == KEY_DC || key == '
\b') {
467 if(patterns_point > 0) {
469 patterns[patterns_point] = '\0';
473 else if(key == KEY_HOME) {
477 else if(key == KEY_END) {
478 current_line = nb_lines - 1;
481 else if(key == KEY_NPAGE) {
485 else if(key == KEY_PPAGE) {
489 else if(key == KEY_UP || key == '
\10') {
493 else if(key == KEY_DOWN || key == '
\ e') {
497 update_screen(¤t_line, &temporary_line, motion,
498 nb_lines, lines, patterns, no_blink);
500 } while(key != '\n' && key != KEY_ENTER && key != '
\a');
506 if((key == KEY_ENTER || key == '\n') && temporary_line >= 0 && temporary_line < nb_lines) {
507 if(output_to_vt_buffer) {
508 char *tty = ttyname(STDIN_FILENO);
509 int fd = open(tty, O_WRONLY);
511 struct termios oldtio, newtio;
514 // Save current port settings
515 tcgetattr(fd,&oldtio);
516 bzero(&newtio, sizeof(newtio));
517 // Set input mode (non-canonical, *no echo*,...)
518 tcflush(fd, TCIFLUSH);
519 tcsetattr(fd,TCSANOW, &newtio);
520 // Put the selected line in the tty input buffer
521 for(char *k = lines[temporary_line]; *k; k++) {
522 ioctl(fd, TIOCSTI, k);
524 // Restore the old settings
525 tcsetattr(fd,TCSANOW, &oldtio);
528 cerr << "Can not open " << tty << "." << endl;
532 ofstream out(output_filename);
534 cerr << "Can not open " << output_filename << " for writing." << endl;
537 out << lines[temporary_line] << endl;
543 for(int l = 0; l < nb_lines; l++) {