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>
44 // Yeah, global variables!
46 int buffer_size = 1024;
47 int nb_lines_max = 1000;
48 char pattern_separator = ';';
49 int output_to_vt_buffer = 0;
51 //////////////////////////////////////////////////////////////////////
53 int match(char *string, int nb_patterns, char **patterns) {
54 for(int n = 0; n < nb_patterns; n++) {
55 if(strstr(string, patterns[n]) == 0) return 0;
60 //////////////////////////////////////////////////////////////////////
62 void check_opt(int argc, char **argv, int n_opt, int n, const char *help) {
63 if(n_opt + n >= argc) {
64 cerr << "Missing argument for " << argv[n_opt] << "."
66 << "Expecting " << help << "."
72 //////////////////////////////////////////////////////////////////////
74 int previous_visible(int current_line, int nb_lines, char **lines, int nb_patterns, char **patterns) {
75 int line = current_line - 1;
76 while(line >= 0 && !match(lines[line], nb_patterns, patterns)) line--;
80 int next_visible(int current_line, int nb_lines, char **lines, int nb_patterns, char **patterns) {
81 int line = current_line + 1;
82 while(line < nb_lines && !match(lines[line], nb_patterns, patterns)) line++;
90 void update_screen(int *current_line, int *temporary_line, int motion,
91 int nb_lines, char **lines,
95 char buffer[buffer_size];
97 // We split the pattern list into individual patterns
101 for(char *s = pattern_list; *s; s++) {
102 if(*s == pattern_separator) {
107 char splitted_patterns[strlen(pattern_list) + 1];
108 char *patterns[nb_patterns];
110 strcpy(splitted_patterns, pattern_list);
113 char *last_pattern_start = splitted_patterns;
114 for(char *s = splitted_patterns; n < nb_patterns; s++) {
115 if(*s == pattern_separator || *s == '\0') {
117 patterns[n++] = last_pattern_start;
118 last_pattern_start = s + 1;
122 // We now take care of printing the lines per se
124 int console_width = getmaxx(stdscr);
125 int console_height = getmaxy(stdscr);
127 int nb_printed_lines = 1;
129 // First, we find a visible line. In priority: The current, or the
130 // first visible after it, or the first visible before it.
133 if(match(lines[*current_line], nb_patterns, patterns)) {
134 new_line = *current_line;
136 new_line = next_visible(*current_line, nb_lines, lines, nb_patterns, patterns);
138 new_line = previous_visible(*current_line, nb_lines, lines, nb_patterns, patterns);
142 // If we found a visible line and we should move, let's move
144 if(new_line >= 0 && motion != 0) {
147 // We want to go down, let's find the first visible line below
148 for(int m = 0; l >= 0 && m < motion; m++) {
149 l = next_visible(l, nb_lines, lines, nb_patterns, patterns);
155 // We want to go up, let's find the first visible line above
156 for(int m = 0; l >= 0 && m < -motion; m++) {
157 l = previous_visible(l, nb_lines, lines, nb_patterns, patterns);
169 use_default_colors();
173 // Here new_line is either a line number matching the patterns, or -1
177 int first_line = new_line, last_line = new_line, nb_match = 1;
179 // We find the first and last line to show, so that the total of
180 // visible lines between them (them include) is console_height - 1
182 while(nb_match < console_height-1 && (first_line > 0 || last_line < nb_lines - 1)) {
186 while(first_line > 0 && !match(lines[first_line], nb_patterns, patterns)) {
189 if(match(lines[first_line], nb_patterns, patterns)) {
194 if(last_line < nb_lines - 1) {
196 while(last_line < nb_lines - 1 && !match(lines[last_line], nb_patterns, patterns)) {
200 if(match(lines[last_line], nb_patterns, patterns)) {
206 // Now we display them
208 for(int l = first_line; l <= last_line; l++) {
209 if(match(lines[l], nb_patterns, patterns)) {
212 while(lines[l][k] && k < buffer_size - 2 && k < console_width - 2) {
213 buffer[k] = lines[l][k];
217 // We fill the rest of the line with blanks if either we did
218 // not clear() or if this is the highlighted line
220 if(no_blink || l == new_line) {
221 while(k < console_width) {
229 // Highlight the highlighted line ...
232 attron(COLOR_PAIR(2));
233 addnstr(buffer, console_width);
234 attroff(COLOR_PAIR(2));
236 addnstr(buffer, console_width);
243 *temporary_line = new_line;
245 *current_line = new_line;
249 if(nb_printed_lines == 1) {
250 addnstr("[no selection]\n", console_width);
254 if(no_blink) { // Erase the rest of the window. That's slightly ugly.
256 while(k < console_width) {
261 for(int l = nb_printed_lines; l < console_height; l++) {
262 addnstr(buffer, console_width);
268 sprintf(buffer, "%d/%d pattern: %s",
269 nb_printed_lines - 1,
273 for(int k = strlen(buffer); k < console_width; k++) buffer[k] = ' ';
274 buffer[console_width] = '\0';
277 attron(COLOR_PAIR(1));
278 addnstr(buffer, console_width);
279 attroff(COLOR_PAIR(1));
286 //////////////////////////////////////////////////////////////////////
288 int main(int argc, char **argv) {
289 char buffer[buffer_size];
290 char *lines[nb_lines_max];
293 char input_filename[buffer_size], output_filename[buffer_size];
294 strcpy(input_filename, "");
295 strcpy(output_filename, "/tmp/selector.out");
299 if(strcmp(argv[i], "-o") == 0) {
300 check_opt(argc, argv, i, 1, "<output filename>");
301 strncpy(output_filename, argv[i+1], buffer_size);
305 else if(strcmp(argv[i], "-s") == 0) {
306 check_opt(argc, argv, i, 1, "<pattern separator>");
307 pattern_separator = argv[i+1][0];
311 else if(strcmp(argv[i], "-v") == 0) {
312 output_to_vt_buffer = 1;
316 else if(strcmp(argv[i], "-f") == 0) {
317 check_opt(argc, argv, i, 1, "<input filename>");
318 strncpy(input_filename, argv[i+1], buffer_size);
322 else if(strcmp(argv[i], "-b") == 0) {
327 else if(strcmp(argv[i], "-l") == 0) {
328 check_opt(argc, argv, i, 1, "<maximum number of lines>");
329 nb_lines_max = atoi(argv[i+1]);
338 << " [-o <output filename>]"
339 << " [-s <pattern separator>]"
340 << " [-l <max number of lines>]"
341 << " -f <input filename>"
343 if(strcmp(argv[i], "-h") == 0) {
351 if(!input_filename[0]) {
352 cerr << "You must specify a input file with -f." << endl;
356 ifstream file(input_filename);
359 cerr << "Can not open \"" << input_filename << "\"" << endl;
364 while(nb_lines < nb_lines_max && !file.eof()) {
365 file.getline(buffer, buffer_size);
366 lines[nb_lines] = new char[strlen(buffer) + 1];
367 strcpy(lines[nb_lines], buffer);
371 char patterns[buffer_size];
379 cerr << "No colors." << endl;
385 keypad(stdscr, TRUE);
388 // init_pair(1, COLOR_WHITE, COLOR_BLACK);
389 init_pair(1, COLOR_WHITE, COLOR_GREEN);
390 init_pair(2, COLOR_BLACK, COLOR_YELLOW);
394 int current_line = 0, temporary_line = 0;
396 update_screen(¤t_line, &temporary_line, 0, nb_lines, lines, patterns, no_blink);
404 if(key >= ' ' && key <= 'z') {
405 patterns[patterns_point++] = key;
406 patterns[patterns_point] = '\0';
409 else if(key == KEY_BACKSPACE || key == KEY_DC || key == '
\b') {
410 if(patterns_point > 0) {
412 patterns[patterns_point] = '\0';
416 else if(key == KEY_HOME) {
420 else if(key == KEY_END) {
421 current_line = nb_lines - 1;
424 else if(key == KEY_NPAGE) {
428 else if(key == KEY_PPAGE) {
432 else if(key == KEY_UP || key == '
\10') {
436 else if(key == KEY_DOWN || key == '
\ e') {
440 update_screen(¤t_line, &temporary_line, motion,
441 nb_lines, lines, patterns, no_blink);
442 } while(key != '\n' && key != KEY_ENTER && key != '
\a');
448 if((key == KEY_ENTER || key == '\n') && temporary_line >= 0 && temporary_line < nb_lines) {
449 if(output_to_vt_buffer) {
450 char *tty = ttyname (STDIN_FILENO);
451 int fd = open(tty, O_WRONLY);
453 struct termios oldtio, newtio;
456 // Save current port settings
457 tcgetattr(fd,&oldtio);
458 bzero(&newtio, sizeof(newtio));
459 // Set input mode (non-canonical, *no echo*,...)
460 tcflush(fd, TCIFLUSH);
461 tcsetattr(fd,TCSANOW, &newtio);
462 // Put the selected line in the tty input buffer
463 for(char *k = lines[temporary_line]; *k; k++) {
464 ioctl(fd, TIOCSTI, k);
466 // Restore the old settings
467 tcsetattr(fd,TCSANOW, &oldtio);
470 cerr << "Can not open " << tty << "." << endl;
474 ofstream out(output_filename);
476 cerr << "Can not open " << output_filename << " for writing." << endl;
479 out << lines[temporary_line] << endl;
485 for(int l = 0; l < nb_lines; l++) {