2 * selector is a simple shell command for selection of strings with a
3 * dynamic pattern-matching.
5 * Copyright (c) 2008 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/>.
26 Here is the magical shell script for a smart bash-history. Note that
27 the line remains in /tmp/selector.out, which may be a scurity concern.
29 ./selector -f ~/.bash_history
30 OLD_SETTINGS=`stty -g`
32 writevt `tty` "`cat /tmp/selector.out`"
46 int buffer_size = 1024;
47 int nb_lines_max = 100000;
48 char pattern_separator = ';';
50 int match(char *string, int nb_patterns, char **patterns) {
51 for(int n = 0; n < nb_patterns; n++) {
52 if(strstr(string, patterns[n]) == 0) return 0;
57 void check_opt(int argc, char **argv, int n_opt, int n, const char *help) {
58 if(n_opt + n >= argc) {
59 cerr << "Missing argument for " << argv[n_opt] << ". Expecting " << help << "." << endl;
64 int previous_visible(int current_line, int nb_lines, char **lines, int nb_patterns, char **patterns) {
65 int line = current_line - 1;
66 while(line >= 0 && !match(lines[line], nb_patterns, patterns)) line--;
70 int next_visible(int current_line, int nb_lines, char **lines, int nb_patterns, char **patterns) {
71 int line = current_line + 1;
72 while(line < nb_lines && !match(lines[line], nb_patterns, patterns)) line++;
80 void update_screen(int *current_line, int *temporary_line, int motion,
81 int nb_lines, char **lines,
85 char buffer[buffer_size];
87 // We split the pattern list into individual patterns
91 for(char *s = pattern_list; *s; s++) {
92 if(*s == pattern_separator) {
97 char splitted_patterns[strlen(pattern_list) + 1];
98 char *patterns[nb_patterns];
100 strcpy(splitted_patterns, pattern_list);
103 char *last_pattern_start = splitted_patterns;
104 for(char *s = splitted_patterns; n < nb_patterns; s++) {
105 if(*s == pattern_separator || *s == '\0') {
107 patterns[n++] = last_pattern_start;
108 last_pattern_start = s + 1;
112 // We now take care of printing the lines per se
114 int console_width = getmaxx(stdscr);
115 int console_height = getmaxy(stdscr);
117 int nb_printed_lines = 1;
119 // First, we find a visible line. In priority: The current, or the
120 // first visible after it, or the first visible before it.
123 if(match(lines[*current_line], nb_patterns, patterns)) {
124 new_line = *current_line;
126 new_line = next_visible(*current_line, nb_lines, lines, nb_patterns, patterns);
128 new_line = previous_visible(*current_line, nb_lines, lines, nb_patterns, patterns);
132 // If we found a visible line and we should move, let's move
134 if(new_line >= 0 && motion != 0) {
139 // We want to go down, let's find the first visible line below
140 l = next_visible(new_line, nb_lines, lines, nb_patterns, patterns);
145 // We want to go up, let's find the first visible line above
146 l = previous_visible(new_line, nb_lines, lines, nb_patterns, patterns);
157 use_default_colors();
161 // Here new_line is either a line number matching the patterns, or -1
165 int first_line = new_line, last_line = new_line, nb_match = 1;
167 // We find the first and last line to show, so that the total of
168 // visible lines between them (them include) is console_height - 1
170 while(nb_match < console_height-1 && (first_line > 0 || last_line < nb_lines - 1)) {
174 while(first_line > 0 && !match(lines[first_line], nb_patterns, patterns)) {
177 if(match(lines[first_line], nb_patterns, patterns)) {
182 if(last_line < nb_lines - 1) {
184 while(last_line < nb_lines - 1 && !match(lines[last_line], nb_patterns, patterns)) {
188 if(match(lines[last_line], nb_patterns, patterns)) {
194 // Now we display them
196 for(int l = first_line; l <= last_line; l++) {
197 if(match(lines[l], nb_patterns, patterns)) {
200 while(lines[l][k] && k < buffer_size - 2 && k < console_width - 2) {
201 buffer[k] = lines[l][k];
205 if(noblink || l == new_line) {
206 while(k < console_width) {
215 attron(COLOR_PAIR(2));
216 addnstr(buffer, console_width);
217 attroff(COLOR_PAIR(2));
219 addnstr(buffer, console_width);
226 *temporary_line = new_line;
228 *current_line = new_line;
232 if(noblink) { // Erase the rest of the window. That's slightly ugly.
234 while(k < console_width) {
239 for(int l = nb_printed_lines; l < console_height; l++) {
240 addnstr(buffer, console_width);
246 sprintf(buffer, "%d/%d pattern: %s",
247 nb_printed_lines - 1,
251 for(int k = strlen(buffer); k < console_width; k++) buffer[k] = ' ';
252 buffer[console_width] = '\0';
255 attron(COLOR_PAIR(1));
256 addnstr(buffer, console_width);
257 attroff(COLOR_PAIR(1));
259 refresh(); // After doing something on the display, we refresh it
262 //////////////////////////////////////////////////////////////////////
264 int main(int argc, char **argv) {
265 char buffer[buffer_size];
266 char *lines[nb_lines_max];
269 char input_filename[buffer_size], output_filename[buffer_size];
270 strcpy(input_filename, "/dev/stdin");
271 strcpy(output_filename, "/tmp/selector.out");
275 if(strcmp(argv[i], "-o") == 0) {
276 check_opt(argc, argv, i, 1, "<output filename>");
277 strncpy(output_filename, argv[i+1], buffer_size);
281 else if(strcmp(argv[i], "-s") == 0) {
282 check_opt(argc, argv, i, 1, "<pattern separator>");
283 pattern_separator = argv[i+1][0];
287 else if(strcmp(argv[i], "-f") == 0) {
288 check_opt(argc, argv, i, 1, "<input filename>");
289 strncpy(input_filename, argv[i+1], buffer_size);
293 else if(strcmp(argv[i], "-b") == 0) {
298 else if(strcmp(argv[i], "-l") == 0) {
299 check_opt(argc, argv, i, 1, "<maximum number of lines>");
300 nb_lines_max = atoi(argv[i+1]);
305 cerr << argv[0] << " [-h] [-o <output filename>] [-b] [-l <max number of lines>] [-s <pattern separator>]" << endl;
306 if(strcmp(argv[i], "-h") == 0) {
314 ifstream file(input_filename);
317 cerr << "Can not open \"" << input_filename << "\"" << endl;
322 while(nb_lines < nb_lines_max && !file.eof()) {
323 file.getline(buffer, buffer_size);
324 lines[nb_lines] = new char[strlen(buffer) + 1];
325 strcpy(lines[nb_lines], buffer);
329 char patterns[buffer_size];
337 cerr << "No colors." << endl;
343 keypad(stdscr, TRUE);
346 // init_pair(1, COLOR_WHITE, COLOR_BLACK);
347 init_pair(1, COLOR_WHITE, COLOR_GREEN);
348 init_pair(2, COLOR_BLACK, COLOR_YELLOW);
352 int current_line = 0, temporary_line = 0;
354 update_screen(¤t_line, &temporary_line, 0, nb_lines, lines, patterns, noblink);
362 if(key >= ' ' && key <= 'z') {
363 patterns[patterns_point++] = key;
364 patterns[patterns_point] = '\0';
367 else if(key == KEY_BACKSPACE || key == KEY_DC || key == '
\b') {
368 if(patterns_point > 0) {
370 patterns[patterns_point] = '\0';
374 else if(key == KEY_UP || key == '
\10') {
378 else if(key == KEY_DOWN || key == '
\ e') {
382 update_screen(¤t_line, &temporary_line, motion,
383 nb_lines, lines, patterns, noblink);
384 } while(key != '\n' && key != KEY_ENTER && key != '
\a');
390 ofstream out(output_filename);
392 cerr << "Can not open " << output_filename << " for writing." << endl;
395 if((key == KEY_ENTER || key == '\n') && temporary_line >= 0 && temporary_line < nb_lines) {
396 out << lines[temporary_line] << endl;
403 for(int l = 0; l < nb_lines; l++) {