selector - A simple command line for dynamic pattern selection
.SH "SYNOPSIS"
.PP
-\fBselector\fP [\fBoptions\fP] -f <filename>
+\fBselector\fP [\fBoptions\fP] [<filename1> [<filename2> ...]]
.SH "DESCRIPTION"
.PP
\fBselector\fP is a command line dynamic string selection. As you type
edit the line and execute it as a standard command.
Keys corresponding to ASCII codes between ' ' and '~' add a character
-to the pattern string. The Delete key, Backspace key, ^D and ^H delete
-one character from the pattern string.
+to the pattern string. The Backspace key, ^H and ^? delete a character
+on the left of cursor, while Delete key and ^D delete a character on
+the right of the cursor.
The Up and Down cursor keys move the selected line accordingly, and
PageUp and PageDown move by ten lines. The Home and End key move to
start with the regexp mode activated
.IP "\fB-a\fP" 10
make the matching case sensitive
+.IP "\fB--\fP" 10
+state that all following arguments are filenames
.IP "\fB-t <title>\fP" 10
add a title in the modeline
.IP "\fB-c <fg modeline> <bg modeline> <fg highlight> <bg highlight>\fP" 10
//////////////////////////////////////////////////////////////////////
+void read_file(const char *input_filename,
+ int nb_lines_max, int *nb_lines, char **lines,
+ int hash_table_size, int *hash_table) {
+
+ char buffer[buffer_size], raw_line[buffer_size];;
+
+ ifstream file(input_filename);
+
+ if(file.fail()) {
+ cerr << "Can not open " << input_filename << endl;
+ exit(1);
+ }
+
+ while(*nb_lines < nb_lines_max && !file.eof()) {
+
+ file.getline(raw_line, buffer_size);
+
+ if(raw_line[0]) {
+
+ if(file.fail()) {
+ cerr << "Line too long:" << endl;
+ cerr << raw_line << endl;
+ exit(1);
+ }
+
+ char *s, *t;
+ const char *u;
+
+ s = buffer;
+ t = raw_line;
+ while(*t) {
+ u = unctrl(*t++);
+ while(*u) { *s++ = *u++; }
+ }
+ *s = '\0';
+
+ s = buffer;
+
+ if(zsh_history && *s == ':') {
+ while(*s && *s != ';') s++;
+ if(*s == ';') s++;
+ }
+
+ if(bash_history && (*s == ' ' || (*s >= '0' && *s <= '9'))) {
+ while(*s == ' ' || (*s >= '0' && *s <= '9')) s++;
+ }
+
+ int dup;
+
+ if(hash_table) {
+ dup = test_and_add(s, *nb_lines, lines, hash_table, hash_table_size);
+ } else {
+ dup = -1;
+ }
+
+ if(dup < 0) {
+ lines[*nb_lines] = new char[strlen(s) + 1];
+ strcpy(lines[*nb_lines], s);
+ } else {
+ // The string was already in there, so we do not allocate a
+ // new string but use the pointer to the first occurence of it
+ lines[*nb_lines] = lines[dup];
+ lines[dup] = 0;
+ }
+
+ (*nb_lines)++;
+ }
+ }
+}
+
+//////////////////////////////////////////////////////////////////////
+
int main(int argc, char **argv) {
if(!ttyname(STDIN_FILENO)) {
exit(1);
}
- char buffer[buffer_size], raw_line[buffer_size];;
int color_fg_modeline, color_bg_modeline;
int color_fg_highlight, color_bg_highlight;
int i = 1;
int error = 0, show_help = 0;
+ int rest_are_files = 0;
- while(!error && !show_help && i < argc) {
+ while(!error && !show_help && i < argc && argv[i][0] == '-' && !rest_are_files) {
if(strcmp(argv[i], "-o") == 0) {
check_opt(argc, argv, i, 1, "<output filename>");
i += 5;
}
+ else if(strcmp(argv[i], "--") == 0) {
+ rest_are_files = 1;
+ i++;
+ }
+
else if(strcmp(argv[i], "-h") == 0) {
show_help = 1;
i++;
}
else {
- cerr << "Unknown argument " << argv[i] << "." << endl;
+ cerr << "Unknown option " << argv[i] << "." << endl;
error = 1;
}
}
<< "Written by Francois Fleuret <francois@fleuret.org>."
<< endl
<< endl
- << "Usage: " << argv[0] << " [options] -f <file>" << endl
+ << "Usage: " << argv[0] << " [options] [<filename1> [<filename2> ...]]" << endl
<< endl
<< " -h show this help" << endl
<< " -v inject the selected line in the tty" << endl
<< " -e start in regexp mode" << endl
<< " -a case sensitive" << endl
<< " -m monochrome mode" << endl
+ << " -- rest of the arguments are filenames" << endl
<< " -t <title>" << endl
<< " add a title in the modeline" << endl
<< " -c <fg modeline> <bg modeline> <fg highlight> <bg highlight>" << endl
char **lines = new char *[nb_lines_max];
- if(!input_filename[0]) {
- cerr << "You must specify a input file with -f." << endl;
- exit(1);
- }
-
int nb_lines = 0;
-
- ifstream file(input_filename);
-
- if(file.fail()) {
- cerr << "Can not open " << input_filename << endl;
- return 1;
- }
-
int hash_table_size = nb_lines_max * 10;
int *hash_table = 0;
hash_table = new_hash_table(hash_table_size);
}
- while(nb_lines < nb_lines_max && !file.eof()) {
-
- file.getline(raw_line, buffer_size);
-
- if(raw_line[0]) {
-
- if(file.fail()) {
- cerr << "Line too long:" << endl;
- cerr << raw_line << endl;
- exit(1);
- }
-
- char *s, *t;
- const char *u;
-
- s = buffer;
- t = raw_line;
- while(*t) {
- u = unctrl(*t++);
- while(*u) { *s++ = *u++; }
- }
- *s = '\0';
-
- s = buffer;
-
- if(zsh_history && *s == ':') {
- while(*s && *s != ';') s++;
- if(*s == ';') s++;
- }
-
- if(bash_history && (*s == ' ' || (*s >= '0' && *s <= '9'))) {
- while(*s == ' ' || (*s >= '0' && *s <= '9')) s++;
- }
+ // if(i == argc && !input_filename[0]) {
+ // cerr << "You must provide a filename." << endl;
+ // exit(1);
+ // }
- int dup;
-
- if(hash_table) {
- dup = test_and_add(s, nb_lines, lines, hash_table, hash_table_size);
- } else {
- dup = -1;
- }
-
- if(dup < 0) {
- lines[nb_lines] = new char[strlen(s) + 1];
- strcpy(lines[nb_lines], s);
- } else {
- // The string was already in there, so we do not allocate a
- // new string but use the pointer to the first occurence of it
- lines[nb_lines] = lines[dup];
- lines[dup] = 0;
- }
+ if(input_filename[0]) {
+ read_file(input_filename,
+ nb_lines_max, &nb_lines, lines,
+ hash_table_size, hash_table);
+ }
- nb_lines++;
- }
+ while(i < argc) {
+ read_file(argv[i],
+ nb_lines_max, &nb_lines, lines,
+ hash_table_size, hash_table);
+ i++;
}
delete[] hash_table;