From: Francois Fleuret Date: Fri, 13 Mar 2009 10:19:11 +0000 (+0100) Subject: Added an option to invert the orders of lines. X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=commitdiff_plain;h=1aa2ab0e57b73a863712019a4c8153d08e088ce1;p=selector.git Added an option to invert the orders of lines. --- diff --git a/selector.1 b/selector.1 index dce6be2..e3452d5 100644 --- a/selector.1 +++ b/selector.1 @@ -34,6 +34,8 @@ display help and exits inject the selected line into the tty input buffer .IP "\fB-m\fP" 10 force the monochrome mode +.IP "\fB-i\fP" 10 +inverse the order of lines so that the recent lines are at the top .IP "\fB-z\fP" 10 remove the time prefix from zsh history .IP "\fB-c \fP" 10 diff --git a/selector.cc b/selector.cc index 2990c72..f89f6b8 100644 --- a/selector.cc +++ b/selector.cc @@ -51,6 +51,7 @@ char pattern_separator = ';'; int output_to_vt_buffer = 0; int with_colors = 1; int zsh_history = 0; +int inverse_order = 0; ////////////////////////////////////////////////////////////////////// @@ -385,6 +386,11 @@ int main(int argc, char **argv) { i++; } + else if(strcmp(argv[i], "-i") == 0) { + inverse_order = 1; + i++; + } + else if(strcmp(argv[i], "-z") == 0) { zsh_history = 1; i++; @@ -415,6 +421,7 @@ int main(int argc, char **argv) { << " [-v]" << " [-m]" << " [-z]" + << " [-i]" << " [-c ]" << " [-o ]" << " [-s ]" @@ -444,14 +451,24 @@ int main(int argc, char **argv) { int nb_lines = 0; while(nb_lines < nb_lines_max && !file.eof()) { file.getline(buffer, buffer_size); - char *s = buffer; - if(zsh_history && *s == ':') { - while(*s && *s != ';') s++; - if(*s == ';') s++; - } - lines[nb_lines] = new char[strlen(s) + 1]; - strcpy(lines[nb_lines], s); - nb_lines++; + if(strcmp(buffer, "") != 0) { + char *s = buffer; + if(zsh_history && *s == ':') { + while(*s && *s != ';') s++; + if(*s == ';') s++; + } + lines[nb_lines] = new char[strlen(s) + 1]; + strcpy(lines[nb_lines], s); + nb_lines++; + } + } + + if(inverse_order) { + for(int i = 0; i < nb_lines/2; i++) { + char *s = lines[nb_lines - 1 - i]; + lines[nb_lines - 1 - i] = lines[i]; + lines[i] = s; + } } char patterns[buffer_size];