Implement r_cons_less() activated with ~.. suffix

This commit is contained in:
pancake 2014-02-11 02:32:37 +01:00
parent 1390d9f2d5
commit 4eb57a0080
5 changed files with 104 additions and 4 deletions

View File

@ -1,7 +1,7 @@
include ../config.mk
NAME=r_cons
OBJS=cons.o pipe.o output.o grep.o canvas.o
OBJS=cons.o pipe.o output.o grep.o canvas.o less.o
OBJS+=line.o hud.o rgb.o input.o pal.o editor.o
DEPS=r_util

View File

@ -301,7 +301,7 @@ R_API const char *r_cons_get_buffer() {
R_API void r_cons_filter() {
/* grep*/
if (I.grep.nstrings>0||I.grep.tokenfrom!=0||I.grep.tokento!=ST32_MAX||I.grep.line!=-1)
if (I.grep.nstrings>0||I.grep.tokenfrom!=0||I.grep.tokento!=ST32_MAX||I.grep.line!=-1 || I.grep.less)
r_cons_grepbuf (I.buffer, I.buffer_len);
/* html */
/* TODO */

View File

@ -32,15 +32,22 @@ R_API void r_cons_grep(const char *str) {
cons->grep.neg = 0;
cons->grep.amp = 0;
cons->grep.end = 0;
cons->grep.less = 0;
cons->grep.line = -1;
cons->grep.begin = 0;
cons->grep.counter = 0;
cons->grep.nstrings = 0;
cons->grep.tokenfrom = 0;
cons->grep.tokento = ST32_MAX;
cons->grep.line = -1;
cons->grep.counter = cons->grep.neg = 0;
while (*str) {
switch (*str) {
case '.':
if (str[1]=='.') {
cons->grep.less = 1;
return;
}
break;
case '&': str++; cons->grep.amp = 1; break;
case '^': str++; cons->grep.begin = 1; break;
case '!': str++; cons->grep.neg = 1; break;
@ -122,6 +129,17 @@ R_API int r_cons_grepbuf(char *buf, int len) {
char *tline, *tbuf, *p, *out, *in = buf;
int ret, buffer_len = 0, l = 0, tl = 0;
if (cons->grep.less) {
cons->grep.less = 0;
r_cons_less (buf);
buf[0] = 0;
cons->buffer_len = 0;
if (cons->buffer)
cons->buffer[0] = 0;
free (cons->buffer);
cons->buffer = NULL;
return 0;
}
if (!cons->buffer) {
cons->buffer_len = len+20;
cons->buffer = malloc (cons->buffer_len);

79
libr/cons/less.c Normal file
View File

@ -0,0 +1,79 @@
/* radare2 - LGPL - Copyright 2014 - pancake */
#include <r_cons.h>
static void printpage (const char *line, int *index, int from, int to) {
int i;
r_cons_clear00 ();
for (i=from; i<to; i++) {
// TODO: chop column width, clear lines
r_cons_printf ("%s\n", line+index[i]);
}
r_cons_flush ();
}
static int *splitlines (char *s, int *lines_count) {
int lines_size = 128;
int *lines = malloc (lines_size*sizeof(int));
int i, row = 0;
int sidx = 0;
lines[row++] = 0;
for (i=0; s[i]; i++) {
if (row>=lines_size) {
lines_size += 128;
lines = realloc (lines, lines_size*sizeof(int));
}
if (s[i]=='\n') {
s[i] = 0;
lines[row++] = i+1;
}
sidx++;
}
*lines_count = row;
return lines;
}
R_API void r_cons_less_str(const char *str) {
int lines_count;
int w, h, ch, to, ui = 1, from = 0;
char *p = strdup (str);
int *lines = splitlines (p, &lines_count);
r_cons_set_raw (R_TRUE);
r_cons_show_cursor (R_FALSE);
r_cons_reset ();
while (ui) {
w = r_cons_get_size (&h);
to = R_MIN (lines_count, from+h);
if (from+3>lines_count)
from = lines_count-3;
printpage (p, lines, from, to);
ch = r_cons_readchar ();
ch = r_cons_arrow_to_hjkl (ch);
switch (ch) {
case ' ': from += h; break;
case 'g': from = 0; break;
case 'G': from = lines_count-1-h; break;
case 'q': ui = 0; break;
case 'j': from++; break;
case 'J': from+=h; break;
case 'k': if (from>0) from--;
case 'K': from = (from>=h)? from-=h: 0;
break;
}
}
free (lines);
r_cons_set_raw (R_FALSE);
r_cons_show_cursor (R_TRUE);
}
R_API void r_cons_less() {
r_cons_less_str (r_cons_singleton ()->buffer);
}
#if 0
main (int argc, char **argv) {
char *s = r_file_slurp (argv[1], NULL);
r_cons_new ();
r_cons_less (s);
}
#endif

View File

@ -46,6 +46,7 @@ typedef struct r_cons_grep_t {
int nstrings;
char *str;
int counter;
int less;
int line;
int tokenfrom;
int tokento;
@ -289,6 +290,8 @@ R_API void r_cons_memcat(const char *str, int len);
R_API void r_cons_newline();
R_API void r_cons_filter();
R_API void r_cons_flush();
R_API void r_cons_less_str(const char *str);
R_API void r_cons_less();
R_API void r_cons_memset(char ch, int len);
R_API void r_cons_visual_flush();
R_API void r_cons_visual_write (char *buffer);