2 * selector is a simple shell command for selection of strings with a
3 * dynamic pattern-matching.
5 * Copyright (c) 2009 Francois Fleuret
6 * Written by Francois Fleuret <francois.fleuret@idiap.ch>
8 * This file is part of selector.
10 * selector is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 3 as
12 * published by the Free Software Foundation.
14 * selector is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with selector. If not, see <http://www.gnu.org/licenses/>.
24 // Here is how to use it as a super-history-search
25 // ./selector -v -f ${HISTFILE}
35 #include <sys/ioctl.h>
40 // Yeah, global variables!
42 int buffer_size = 1024;
43 int nb_lines_max = 1000;
44 char pattern_separator = ';';
45 int output_to_vt_buffer = 0;
47 //////////////////////////////////////////////////////////////////////
49 int match(char *string, int nb_patterns, char **patterns) {
50 for(int n = 0; n < nb_patterns; n++) {
51 if(strstr(string, patterns[n]) == 0) return 0;
56 //////////////////////////////////////////////////////////////////////
58 void check_opt(int argc, char **argv, int n_opt, int n, const char *help) {
59 if(n_opt + n >= argc) {
60 cerr << "Missing argument for " << argv[n_opt] << "."
62 << "Expecting " << help << "."
68 //////////////////////////////////////////////////////////////////////
70 int previous_visible(int current_line, int nb_lines, char **lines, int nb_patterns, char **patterns) {
71 int line = current_line - 1;
72 while(line >= 0 && !match(lines[line], nb_patterns, patterns)) line--;
76 int next_visible(int current_line, int nb_lines, char **lines, int nb_patterns, char **patterns) {
77 int line = current_line + 1;
78 while(line < nb_lines && !match(lines[line], nb_patterns, patterns)) line++;
86 void update_screen(int *current_line, int *temporary_line, int motion,
87 int nb_lines, char **lines,
91 char buffer[buffer_size];
93 // We split the pattern list into individual patterns
97 for(char *s = pattern_list; *s; s++) {
98 if(*s == pattern_separator) {
103 char splitted_patterns[strlen(pattern_list) + 1];
104 char *patterns[nb_patterns];
106 strcpy(splitted_patterns, pattern_list);
109 char *last_pattern_start = splitted_patterns;
110 for(char *s = splitted_patterns; n < nb_patterns; s++) {
111 if(*s == pattern_separator || *s == '\0') {
113 patterns[n++] = last_pattern_start;
114 last_pattern_start = s + 1;
118 // We now take care of printing the lines per se
120 int console_width = getmaxx(stdscr);
121 int console_height = getmaxy(stdscr);
123 int nb_printed_lines = 1;
125 // First, we find a visible line. In priority: The current, or the
126 // first visible after it, or the first visible before it.
129 if(match(lines[*current_line], nb_patterns, patterns)) {
130 new_line = *current_line;
132 new_line = next_visible(*current_line, nb_lines, lines, nb_patterns, patterns);
134 new_line = previous_visible(*current_line, nb_lines, lines, nb_patterns, patterns);
138 // If we found a visible line and we should move, let's move
140 if(new_line >= 0 && motion != 0) {
143 // We want to go down, let's find the first visible line below
144 for(int m = 0; l >= 0 && m < motion; m++) {
145 l = next_visible(l, nb_lines, lines, nb_patterns, patterns);
151 // We want to go up, let's find the first visible line above
152 for(int m = 0; l >= 0 && m < -motion; m++) {
153 l = previous_visible(l, nb_lines, lines, nb_patterns, patterns);
165 use_default_colors();
169 // Here new_line is either a line number matching the patterns, or -1
173 int first_line = new_line, last_line = new_line, nb_match = 1;
175 // We find the first and last line to show, so that the total of
176 // visible lines between them (them include) is console_height - 1
178 while(nb_match < console_height-1 && (first_line > 0 || last_line < nb_lines - 1)) {
182 while(first_line > 0 && !match(lines[first_line], nb_patterns, patterns)) {
185 if(match(lines[first_line], nb_patterns, patterns)) {
190 if(last_line < nb_lines - 1) {
192 while(last_line < nb_lines - 1 && !match(lines[last_line], nb_patterns, patterns)) {
196 if(match(lines[last_line], nb_patterns, patterns)) {
202 // Now we display them
204 for(int l = first_line; l <= last_line; l++) {
205 if(match(lines[l], nb_patterns, patterns)) {
208 while(lines[l][k] && k < buffer_size - 2 && k < console_width - 2) {
209 buffer[k] = lines[l][k];
213 // We fill the rest of the line with blanks if either we did
214 // not clear() or if this is the highlighted line
216 if(no_blink || l == new_line) {
217 while(k < console_width) {
225 // Highlight the highlighted line ...
228 attron(COLOR_PAIR(2));
229 addnstr(buffer, console_width);
230 attroff(COLOR_PAIR(2));
232 addnstr(buffer, console_width);
239 *temporary_line = new_line;
241 *current_line = new_line;
245 if(nb_printed_lines == 1) {
246 addnstr("[no selection]\n", console_width);
250 if(no_blink) { // Erase the rest of the window. That's slightly ugly.
252 while(k < console_width) {
257 for(int l = nb_printed_lines; l < console_height; l++) {
258 addnstr(buffer, console_width);
264 sprintf(buffer, "%d/%d pattern: %s",
265 nb_printed_lines - 1,
269 for(int k = strlen(buffer); k < console_width; k++) buffer[k] = ' ';
270 buffer[console_width] = '\0';
273 attron(COLOR_PAIR(1));
274 addnstr(buffer, console_width);
275 attroff(COLOR_PAIR(1));
282 //////////////////////////////////////////////////////////////////////
284 int main(int argc, char **argv) {
285 char buffer[buffer_size];
286 char *lines[nb_lines_max];
289 char input_filename[buffer_size], output_filename[buffer_size];
290 strcpy(input_filename, "");
291 strcpy(output_filename, "/tmp/selector.out");
295 if(strcmp(argv[i], "-o") == 0) {
296 check_opt(argc, argv, i, 1, "<output filename>");
297 strncpy(output_filename, argv[i+1], buffer_size);
301 else if(strcmp(argv[i], "-s") == 0) {
302 check_opt(argc, argv, i, 1, "<pattern separator>");
303 pattern_separator = argv[i+1][0];
307 else if(strcmp(argv[i], "-v") == 0) {
308 output_to_vt_buffer = 1;
312 else if(strcmp(argv[i], "-f") == 0) {
313 check_opt(argc, argv, i, 1, "<input filename>");
314 strncpy(input_filename, argv[i+1], buffer_size);
318 else if(strcmp(argv[i], "-b") == 0) {
323 else if(strcmp(argv[i], "-l") == 0) {
324 check_opt(argc, argv, i, 1, "<maximum number of lines>");
325 nb_lines_max = atoi(argv[i+1]);
334 << " [-o <output filename>]"
335 << " [-s <pattern separator>]"
336 << " [-l <max number of lines>]"
337 << " -f <input filename>"
339 if(strcmp(argv[i], "-h") == 0) {
347 if(!input_filename[0]) {
348 cerr << "You must specify a input file with -f." << endl;
352 ifstream file(input_filename);
355 cerr << "Can not open \"" << input_filename << "\"" << endl;
360 while(nb_lines < nb_lines_max && !file.eof()) {
361 file.getline(buffer, buffer_size);
362 lines[nb_lines] = new char[strlen(buffer) + 1];
363 strcpy(lines[nb_lines], buffer);
367 char patterns[buffer_size];
375 cerr << "No colors." << endl;
381 keypad(stdscr, TRUE);
384 // init_pair(1, COLOR_WHITE, COLOR_BLACK);
385 init_pair(1, COLOR_WHITE, COLOR_GREEN);
386 init_pair(2, COLOR_BLACK, COLOR_YELLOW);
390 int current_line = 0, temporary_line = 0;
392 update_screen(¤t_line, &temporary_line, 0, nb_lines, lines, patterns, no_blink);
400 if(key >= ' ' && key <= 'z') {
401 patterns[patterns_point++] = key;
402 patterns[patterns_point] = '\0';
405 else if(key == KEY_BACKSPACE || key == KEY_DC || key == '
\b') {
406 if(patterns_point > 0) {
408 patterns[patterns_point] = '\0';
412 else if(key == KEY_HOME) {
416 else if(key == KEY_END) {
417 current_line = nb_lines - 1;
420 else if(key == KEY_NPAGE) {
424 else if(key == KEY_PPAGE) {
428 else if(key == KEY_UP || key == '
\10') {
432 else if(key == KEY_DOWN || key == '
\ e') {
436 update_screen(¤t_line, &temporary_line, motion,
437 nb_lines, lines, patterns, no_blink);
438 } while(key != '\n' && key != KEY_ENTER && key != '
\a');
444 if(output_to_vt_buffer) {
445 if((key == KEY_ENTER || key == '\n') && temporary_line >= 0 && temporary_line < nb_lines) {
446 char *tty = ttyname (STDIN_FILENO);
447 int fd = open(tty, O_WRONLY);
449 struct termios oldtio, newtio;
452 // Save current port settings
453 tcgetattr(fd,&oldtio);
454 bzero(&newtio, sizeof(newtio));
455 // Set input mode (non-canonical, *no echo*,...)
456 tcflush(fd, TCIFLUSH);
457 tcsetattr(fd,TCSANOW, &newtio);
458 // Put the selected line in the tty input buffer
459 for(char *k = lines[temporary_line]; *k; k++) {
460 ioctl(fd, TIOCSTI, k);
462 // Restore the old settings
463 tcsetattr(fd,TCSANOW, &oldtio);
466 cerr << "Can not open " << tty << "." << endl;
471 ofstream out(output_filename);
473 cerr << "Can not open " << output_filename << " for writing." << endl;
476 if((key == KEY_ENTER || key == '\n') && temporary_line >= 0 && temporary_line < nb_lines) {
477 out << lines[temporary_line] << endl;
485 for(int l = 0; l < nb_lines; l++) {