Implement inverse sort order and fix regression

This commit is contained in:
pancake 2016-10-25 12:39:36 +02:00
parent d45999d10e
commit 0e5f39e4ae
3 changed files with 14 additions and 0 deletions

View File

@ -418,6 +418,7 @@ R_API void r_cons_reset() {
I.grep.nstrings = 0; // XXX
I.grep.line = -1;
I.grep.sort = -1;
I.grep.sort_invert = false;
I.grep.str = NULL;
memset (I.grep.tokens, 0, R_CONS_GREP_TOKENS);
I.grep.tokens_used = 0;

View File

@ -6,6 +6,7 @@
#define sdb_json_unindent r_cons_json_unindent
#include "../../shlr/sdb/src/json/indent.c"
/* TODO: remove globals */
static RList *sorted_lines = NULL;
static int sorted_column = -1;
@ -49,6 +50,8 @@ R_API void r_cons_grep(const char *str) {
}
cons = r_cons_singleton ();
memset (&(cons->grep), 0, sizeof (cons->grep));
sorted_column = 0;
cons->grep.sort = -1;
cons->grep.line = -1;
while (*str) {
switch (*str) {
@ -72,6 +75,12 @@ R_API void r_cons_grep(const char *str) {
break;
case '$':
str++;
if (*str == '!') {
cons->grep.sort_invert = true;
str++;
} else {
cons->grep.sort_invert = false;
}
cons->grep.sort = atoi (str);
while (IS_NUMBER (*str)) {
str++;
@ -398,6 +407,9 @@ R_API int r_cons_grepbuf(char *buf, int len) {
char *str;
sorted_column = cons->grep.sort;
r_list_sort (sorted_lines, cmp);
if (cons->grep.sort_invert) {
r_list_reverse (sorted_lines);
}
r_list_foreach (sorted_lines, iter, str) {
int len = strlen (str);
memcpy (ptr, str, len);

View File

@ -60,6 +60,7 @@ typedef struct r_cons_grep_t {
int range_line;
int line;
int sort;
bool sort_invert;
int f_line; //first line
int l_line; //last line
int tokens[R_CONS_GREP_TOKENS];