From d713debdfcb4f10de1e7dc4e1f7b70fd132fd0d0 Mon Sep 17 00:00:00 2001 From: pancake Date: Sun, 11 Jan 2015 02:28:59 +0100 Subject: [PATCH] Fix left and top border issues with canvas text (nicer graphs) --- libr/cons/canvas.c | 18 +++++++++++++----- libr/core/graph.c | 25 ++++++++++++++++++++++--- libr/util/str.c | 9 +++++---- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/libr/cons/canvas.c b/libr/cons/canvas.c index 2bed204563..58b76c966d 100644 --- a/libr/cons/canvas.c +++ b/libr/cons/canvas.c @@ -55,7 +55,7 @@ R_API int r_cons_canvas_gotoxy(RConsCanvas *c, int x, int y) { ret = R_FALSE; } if (x <0) { - c->x = 0; + //c->x = 0; ret = R_FALSE; } if (y <0) { @@ -99,7 +99,7 @@ static char *prefixline(RConsCanvas *c, int *left) { } R_API void r_cons_canvas_write(RConsCanvas *c, const char *_s) { - int left, slen, i; + int left, slen, i, linenum = 0; char *p, *s, *str; char *line, *n; @@ -110,8 +110,9 @@ R_API void r_cons_canvas_write(RConsCanvas *c, const char *_s) { line = getrow (s, &n); p = prefixline (c, &left); slen = R_MIN (left-1, strlen (line)); - if (slen<1) + if (slen<1) { break; + } if (!G (c->x-c->sx+slen, c->y-c->sy)) { // TODO : chop slen slen = (c->w - (c->x-c->sx)); @@ -119,13 +120,20 @@ R_API void r_cons_canvas_write(RConsCanvas *c, const char *_s) { break; continue; } +// top border skipping lines +//if ((c->y-1-c->sy)<-40) { +// continue; +//} +int delta = 0; if (!G (c->x-c->sx-slen, c->y-c->sy)) continue; - memcpy (p, line, slen); + memcpy (p, line+delta, slen-delta); if (!n) break; s = n; - if (!G (c->x-c->sx, c->y+1-c->sy)) + if (!G (c->x-c->sx, c->y+1-c->sy)) { break; + } + linenum ++; } free (str); } diff --git a/libr/core/graph.c b/libr/core/graph.c index cee919d6f1..b997744f22 100644 --- a/libr/core/graph.c +++ b/libr/core/graph.c @@ -55,6 +55,8 @@ static Edge edges[] = { static void Node_print(RConsCanvas *can, Node *n, int cur) { char title[128]; + int delta_x = 0; + int delta_y = 0; if (small_nodes) { if (!G (n->x+2, n->y-1)) @@ -89,6 +91,16 @@ static void Node_print(RConsCanvas *can, Node *n, int cur) { return; } #endif + { + int x = n->x + can->sx; + int y = n->y + can->sy; + if (x<-2) { + delta_x = -x-2; + } + if (y<-1) { + delta_y = -y-2; + } + } if (cur) { //F (n->x,n->y, n->w, n->h, '.'); snprintf (title, sizeof (title)-1, @@ -102,9 +114,16 @@ static void Node_print(RConsCanvas *can, Node *n, int cur) { G (n->x+2, n->y+2); //if ( // TODO: temporary crop depending on out of screen offsets - //n->text = r_str_crop (n->text, 1,1,4,4); - W (n->text); - //} + { + char *text = r_str_crop (n->text, + delta_x, delta_y, 9999, 9999); + if (text) { + W (text); + free (text); + } else { + W (n->text); + } + } if (G (n->x+1, n->y+1)) W (title); B (n->x, n->y, n->w, n->h); diff --git a/libr/util/str.c b/libr/util/str.c index 25fc8cf1d8..23e961f59e 100644 --- a/libr/util/str.c +++ b/libr/util/str.c @@ -1474,10 +1474,11 @@ R_API char *r_str_crop(const char *str, int x, int y, int w, int h) { *r++ = *str; ch++; cw = 0; - } else - if (ch>=y && ch=x && cw=y && ch=x && cw