2 ///////////////////////////////////////////////////////////////////////////
5 // This program is free software: you can redistribute it and/or modify //
6 // it under the terms of the version 3 of the GNU General Public License //
7 // as published by the Free Software Foundation. //
9 // This program is distributed in the hope that it will be useful, but //
10 // WITHOUT ANY WARRANTY; without even the implied warranty of //
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
12 // General Public License for more details. //
14 // You should have received a copy of the GNU General Public License //
15 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
17 // Written by and Copyright (C) Francois Fleuret //
18 // Contact <francois.fleuret@idiap.ch> for comments & bug reports //
21 ///////////////////////////////////////////////////////////////////////////
25 Here is the magical shell script for a smart bash-history. Note that
26 the line remains in /tmp/selector.out, which may be a scurity concern.
28 ./selector -f ~/.bash_history
29 OLD_SETTINGS=`stty -g`
31 writevt `tty` "`cat /tmp/selector.out`"
45 int buffer_size = 1024;
46 int nb_lines_max = 100000;
47 char pattern_separator = ';';
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 void check_opt(int argc, char **argv, int n_opt, int n, const char *help) {
57 if(n_opt + n >= argc) {
58 cerr << "Missing argument for " << argv[n_opt] << ". Expecting " << help << "." << endl;
63 int previous_visible(int current_line, int nb_lines, char **lines, int nb_patterns, char **patterns) {
64 int line = current_line - 1;
65 while(line >= 0 && !match(lines[line], nb_patterns, patterns)) line--;
69 int next_visible(int current_line, int nb_lines, char **lines, int nb_patterns, char **patterns) {
70 int line = current_line + 1;
71 while(line < nb_lines && !match(lines[line], nb_patterns, patterns)) line++;
79 void update_screen(int *current_line, int *temporary_line, int motion,
80 int nb_lines, char **lines,
84 char buffer[buffer_size];
86 // We split the pattern list into individual patterns
90 for(char *s = pattern_list; *s; s++) {
91 if(*s == pattern_separator) {
96 char splitted_patterns[strlen(pattern_list) + 1];
97 char *patterns[nb_patterns];
99 strcpy(splitted_patterns, pattern_list);
102 char *last_pattern_start = splitted_patterns;
103 for(char *s = splitted_patterns; n < nb_patterns; s++) {
104 if(*s == pattern_separator || *s == '\0') {
106 patterns[n++] = last_pattern_start;
107 last_pattern_start = s + 1;
111 // We now take care of printing the lines per se
113 int console_width = getmaxx(stdscr);
114 int console_height = getmaxy(stdscr);
116 int nb_printed_lines = 1;
118 // First, we find a visible line. In priority: The current, or the
119 // first visible after it, or the first visible before it.
122 if(match(lines[*current_line], nb_patterns, patterns)) {
123 new_line = *current_line;
125 new_line = next_visible(*current_line, nb_lines, lines, nb_patterns, patterns);
127 new_line = previous_visible(*current_line, nb_lines, lines, nb_patterns, patterns);
131 // If we found a visible line and we should move, let's move
133 if(new_line >= 0 && motion != 0) {
138 // We want to go down, let's find the first visible line below
139 l = next_visible(new_line, nb_lines, lines, nb_patterns, patterns);
144 // We want to go up, let's find the first visible line above
145 l = previous_visible(new_line, nb_lines, lines, nb_patterns, patterns);
156 use_default_colors();
160 // Here new_line is either a line number matching the patterns, or -1
164 int first_line = new_line, last_line = new_line, nb_match = 1;
166 // We find the first and last line to show, so that the total of
167 // visible lines between them (them include) is console_height - 1
169 while(nb_match < console_height-1 && (first_line > 0 || last_line < nb_lines - 1)) {
173 while(first_line > 0 && !match(lines[first_line], nb_patterns, patterns)) {
176 if(match(lines[first_line], nb_patterns, patterns)) {
181 if(last_line < nb_lines - 1) {
183 while(last_line < nb_lines - 1 && !match(lines[last_line], nb_patterns, patterns)) {
187 if(match(lines[last_line], nb_patterns, patterns)) {
193 // Now we display them
195 for(int l = first_line; l <= last_line; l++) {
196 if(match(lines[l], nb_patterns, patterns)) {
199 while(lines[l][k] && k < buffer_size - 2 && k < console_width - 2) {
200 buffer[k] = lines[l][k];
204 if(noblink || l == new_line) {
205 while(k < console_width) {
214 attron(COLOR_PAIR(2));
215 addnstr(buffer, console_width);
216 attroff(COLOR_PAIR(2));
218 addnstr(buffer, console_width);
225 *temporary_line = new_line;
227 *current_line = new_line;
231 if(noblink) { // Erase the rest of the window. That's slightly ugly.
233 while(k < console_width) {
238 for(int l = nb_printed_lines; l < console_height; l++) {
239 addnstr(buffer, console_width);
245 sprintf(buffer, "%d/%d pattern: %s",
246 nb_printed_lines - 1,
250 for(int k = strlen(buffer); k < console_width; k++) buffer[k] = ' ';
251 buffer[console_width] = '\0';
254 attron(COLOR_PAIR(1));
255 addnstr(buffer, console_width);
256 attroff(COLOR_PAIR(1));
258 refresh(); // After doing something on the display, we refresh it
261 //////////////////////////////////////////////////////////////////////
263 int main(int argc, char **argv) {
264 char buffer[buffer_size];
265 char *lines[nb_lines_max];
268 char input_filename[buffer_size], output_filename[buffer_size];
269 strcpy(input_filename, "/dev/stdin");
270 strcpy(output_filename, "/tmp/selector.out");
274 if(strcmp(argv[i], "-o") == 0) {
275 check_opt(argc, argv, i, 1, "<output filename>");
276 strncpy(output_filename, argv[i+1], buffer_size);
280 else if(strcmp(argv[i], "-s") == 0) {
281 check_opt(argc, argv, i, 1, "<pattern separator>");
282 pattern_separator = argv[i+1][0];
286 else if(strcmp(argv[i], "-f") == 0) {
287 check_opt(argc, argv, i, 1, "<input filename>");
288 strncpy(input_filename, argv[i+1], buffer_size);
292 else if(strcmp(argv[i], "-b") == 0) {
297 else if(strcmp(argv[i], "-l") == 0) {
298 check_opt(argc, argv, i, 1, "<maximum number of lines>");
299 nb_lines_max = atoi(argv[i+1]);
304 cerr << argv[0] << " [-h] [-o <output filename>] [-b] [-l <max number of lines>] [-s <pattern separator>]" << endl;
305 if(strcmp(argv[i], "-h") == 0) {
313 ifstream file(input_filename);
316 cerr << "Can not open \"" << input_filename << "\"" << endl;
321 while(nb_lines < nb_lines_max && !file.eof()) {
322 file.getline(buffer, buffer_size);
323 lines[nb_lines] = new char[strlen(buffer) + 1];
324 strcpy(lines[nb_lines], buffer);
328 char patterns[buffer_size];
336 cerr << "No colors." << endl;
342 keypad(stdscr, TRUE);
345 // init_pair(1, COLOR_WHITE, COLOR_BLACK);
346 init_pair(1, COLOR_WHITE, COLOR_GREEN);
347 init_pair(2, COLOR_BLACK, COLOR_YELLOW);
351 int current_line = 0, temporary_line = 0;
353 update_screen(¤t_line, &temporary_line, 0, nb_lines, lines, patterns, noblink);
361 if(key >= ' ' && key <= 'z') {
362 patterns[patterns_point++] = key;
363 patterns[patterns_point] = '\0';
366 else if(key == KEY_BACKSPACE || key == KEY_DC || key == '
\b') {
367 if(patterns_point > 0) {
369 patterns[patterns_point] = '\0';
373 else if(key == KEY_UP || key == '
\10') {
377 else if(key == KEY_DOWN || key == '
\ e') {
381 update_screen(¤t_line, &temporary_line, motion,
382 nb_lines, lines, patterns, noblink);
383 } while(key != '\n' && key != KEY_ENTER && key != '
\a');
389 ofstream out(output_filename);
391 cerr << "Can not open " << output_filename << " for writing." << endl;
394 if((key == KEY_ENTER || key == '\n') && temporary_line >= 0 && temporary_line < nb_lines) {
395 out << lines[temporary_line] << endl;
402 for(int l = 0; l < nb_lines; l++) {