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 = 1000;
50 char pattern_separator = ';';
51 int output_to_vt_buffer = 0;
53 //////////////////////////////////////////////////////////////////////
55 int match(char *string, int nb_patterns, char **patterns) {
56 for(int n = 0; n < nb_patterns; n++) {
57 if(strstr(string, patterns[n]) == 0) return 0;
62 //////////////////////////////////////////////////////////////////////
64 void check_opt(int argc, char **argv, int n_opt, int n, const char *help) {
65 if(n_opt + n >= argc) {
66 cerr << "Missing argument for " << argv[n_opt] << "."
68 << "Expecting " << help << "."
74 //////////////////////////////////////////////////////////////////////
76 int previous_visible(int current_line, int nb_lines, char **lines, int nb_patterns, char **patterns) {
77 int line = current_line - 1;
78 while(line >= 0 && !match(lines[line], nb_patterns, patterns)) line--;
82 int next_visible(int current_line, int nb_lines, char **lines, int nb_patterns, char **patterns) {
83 int line = current_line + 1;
84 while(line < nb_lines && !match(lines[line], nb_patterns, patterns)) line++;
92 void update_screen(int *current_line, int *temporary_line, int motion,
93 int nb_lines, char **lines,
97 char buffer[buffer_size];
99 // We split the pattern list into individual patterns
103 for(char *s = pattern_list; *s; s++) {
104 if(*s == pattern_separator) {
109 char splitted_patterns[strlen(pattern_list) + 1];
110 char *patterns[nb_patterns];
112 strcpy(splitted_patterns, pattern_list);
115 char *last_pattern_start = splitted_patterns;
116 for(char *s = splitted_patterns; n < nb_patterns; s++) {
117 if(*s == pattern_separator || *s == '\0') {
119 patterns[n++] = last_pattern_start;
120 last_pattern_start = s + 1;
124 // We now take care of printing the lines per se
126 int console_width = getmaxx(stdscr);
127 int console_height = getmaxy(stdscr);
129 int nb_printed_lines = 1;
131 // First, we find a visible line. In priority: The current, or the
132 // first visible after it, or the first visible before it.
135 if(match(lines[*current_line], nb_patterns, patterns)) {
136 new_line = *current_line;
138 new_line = next_visible(*current_line, nb_lines, lines, nb_patterns, patterns);
140 new_line = previous_visible(*current_line, nb_lines, lines, nb_patterns, patterns);
144 // If we found a visible line and we should move, let's move
146 if(new_line >= 0 && motion != 0) {
149 // We want to go down, let's find the first visible line below
150 for(int m = 0; l >= 0 && m < motion; m++) {
151 l = next_visible(l, nb_lines, lines, nb_patterns, patterns);
157 // We want to go up, let's find the first visible line above
158 for(int m = 0; l >= 0 && m < -motion; m++) {
159 l = previous_visible(l, nb_lines, lines, nb_patterns, patterns);
171 use_default_colors();
175 // Here new_line is either a line number matching the patterns, or -1
179 int first_line = new_line, last_line = new_line, nb_match = 1;
181 // We find the first and last line to show, so that the total of
182 // visible lines between them (them include) is console_height - 1
184 while(nb_match < console_height-1 && (first_line > 0 || last_line < nb_lines - 1)) {
188 while(first_line > 0 && !match(lines[first_line], nb_patterns, patterns)) {
191 if(match(lines[first_line], nb_patterns, patterns)) {
196 if(last_line < nb_lines - 1) {
198 while(last_line < nb_lines - 1 && !match(lines[last_line], nb_patterns, patterns)) {
202 if(match(lines[last_line], nb_patterns, patterns)) {
208 // Now we display them
210 for(int l = first_line; l <= last_line; l++) {
211 if(match(lines[l], nb_patterns, patterns)) {
214 while(lines[l][k] && k < buffer_size - 2 && k < console_width - 2) {
215 buffer[k] = lines[l][k];
219 // We fill the rest of the line with blanks if either we did
220 // not clear() or if this is the highlighted line
222 if(no_blink || l == new_line) {
223 while(k < console_width) {
231 // Highlight the highlighted line ...
234 attron(COLOR_PAIR(2));
235 addnstr(buffer, console_width);
236 attroff(COLOR_PAIR(2));
238 addnstr(buffer, console_width);
245 *temporary_line = new_line;
247 *current_line = new_line;
251 if(nb_printed_lines == 1) {
252 addnstr("[no selection]\n", console_width);
256 if(no_blink) { // Erase the rest of the window. That's slightly ugly.
258 while(k < console_width) {
263 for(int l = nb_printed_lines; l < console_height; l++) {
264 addnstr(buffer, console_width);
270 sprintf(buffer, "%d/%d pattern: %s",
271 nb_printed_lines - 1,
275 for(int k = strlen(buffer); k < console_width; k++) buffer[k] = ' ';
276 buffer[console_width] = '\0';
279 attron(COLOR_PAIR(1));
280 addnstr(buffer, console_width);
281 attroff(COLOR_PAIR(1));
288 //////////////////////////////////////////////////////////////////////
290 int main(int argc, char **argv) {
291 char buffer[buffer_size];
292 char *lines[nb_lines_max];
295 char input_filename[buffer_size], output_filename[buffer_size];
296 strcpy(input_filename, "");
297 strcpy(output_filename, "/tmp/selector.out");
301 if(strcmp(argv[i], "-o") == 0) {
302 check_opt(argc, argv, i, 1, "<output filename>");
303 strncpy(output_filename, argv[i+1], buffer_size);
307 else if(strcmp(argv[i], "-s") == 0) {
308 check_opt(argc, argv, i, 1, "<pattern separator>");
309 pattern_separator = argv[i+1][0];
313 else if(strcmp(argv[i], "-v") == 0) {
314 output_to_vt_buffer = 1;
318 else if(strcmp(argv[i], "-f") == 0) {
319 check_opt(argc, argv, i, 1, "<input filename>");
320 strncpy(input_filename, argv[i+1], buffer_size);
324 else if(strcmp(argv[i], "-b") == 0) {
329 else if(strcmp(argv[i], "-l") == 0) {
330 check_opt(argc, argv, i, 1, "<maximum number of lines>");
331 nb_lines_max = atoi(argv[i+1]);
336 cerr << "Selector version " << VERSION
338 << "Written by Francois Fleuret <francois@fleuret.org>"
344 << " [-o <output filename>]"
345 << " [-s <pattern separator>]"
346 << " [-l <max number of lines>]"
347 << " -f <input filename>"
349 if(strcmp(argv[i], "-h") == 0) {
357 if(!input_filename[0]) {
358 cerr << "You must specify a input file with -f." << endl;
362 ifstream file(input_filename);
365 cerr << "Can not open " << input_filename << endl;
370 while(nb_lines < nb_lines_max && !file.eof()) {
371 file.getline(buffer, buffer_size);
372 lines[nb_lines] = new char[strlen(buffer) + 1];
373 strcpy(lines[nb_lines], buffer);
377 char patterns[buffer_size];
385 cerr << "No colors." << endl;
391 keypad(stdscr, TRUE);
394 // init_pair(1, COLOR_WHITE, COLOR_BLACK);
395 init_pair(1, COLOR_WHITE, COLOR_GREEN);
396 init_pair(2, COLOR_BLACK, COLOR_YELLOW);
400 int current_line = 0, temporary_line = 0;
402 update_screen(¤t_line, &temporary_line, 0, nb_lines, lines, patterns, no_blink);
410 if(key >= ' ' && key <= 'z') {
411 patterns[patterns_point++] = key;
412 patterns[patterns_point] = '\0';
415 else if(key == KEY_BACKSPACE || key == KEY_DC || key == '
\b') {
416 if(patterns_point > 0) {
418 patterns[patterns_point] = '\0';
422 else if(key == KEY_HOME) {
426 else if(key == KEY_END) {
427 current_line = nb_lines - 1;
430 else if(key == KEY_NPAGE) {
434 else if(key == KEY_PPAGE) {
438 else if(key == KEY_UP || key == '
\10') {
442 else if(key == KEY_DOWN || key == '
\ e') {
446 update_screen(¤t_line, &temporary_line, motion,
447 nb_lines, lines, patterns, no_blink);
448 } while(key != '\n' && key != KEY_ENTER && key != '
\a');
454 if((key == KEY_ENTER || key == '\n') && temporary_line >= 0 && temporary_line < nb_lines) {
455 if(output_to_vt_buffer) {
456 char *tty = ttyname (STDIN_FILENO);
457 int fd = open(tty, O_WRONLY);
459 struct termios oldtio, newtio;
462 // Save current port settings
463 tcgetattr(fd,&oldtio);
464 bzero(&newtio, sizeof(newtio));
465 // Set input mode (non-canonical, *no echo*,...)
466 tcflush(fd, TCIFLUSH);
467 tcsetattr(fd,TCSANOW, &newtio);
468 // Put the selected line in the tty input buffer
469 for(char *k = lines[temporary_line]; *k; k++) {
470 ioctl(fd, TIOCSTI, k);
472 // Restore the old settings
473 tcsetattr(fd,TCSANOW, &oldtio);
476 cerr << "Can not open " << tty << "." << endl;
480 ofstream out(output_filename);
482 cerr << "Can not open " << output_filename << " for writing." << endl;
485 out << lines[temporary_line] << endl;
491 for(int l = 0; l < nb_lines; l++) {