From 9c5cfc48f7eeaa73c4737189e7eb4052ae0e9a33 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Wed, 23 Jan 2019 02:50:00 +0100 Subject: [PATCH] table: simplify rendering of tables --- common/lib/table.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/common/lib/table.c b/common/lib/table.c index a9342216c..08c3f04fc 100644 --- a/common/lib/table.c +++ b/common/lib/table.c @@ -65,9 +65,9 @@ void table_header(struct table *t) if (t->width != log_get_width()) table_resize(t, log_get_width()); - char *line1 = strf("\b\b" BOX_UD); - char *line2 = strf("\b\b" BOX_UD); - char *line3 = strf("\b"); + char *line1 = NULL; + char *line2 = NULL; + char *line3 = NULL; for (int i = 0; i < t->ncols; i++) { int w, u; @@ -80,22 +80,21 @@ void table_header(struct table *t) u = t->cols[i]._width + strlen(unit) - strlenp(unit); if (t->cols[i].align == TABLE_ALIGN_LEFT) { - strcatf(&line1, " %-*.*s\e[0m " BOX_UD, w, w, col); - strcatf(&line2, " %-*.*s\e[0m " BOX_UD, u, u, unit); + strcatf(&line1, " %-*.*s\e[0m", w, w, col); + strcatf(&line2, " %-*.*s\e[0m", u, u, unit); } else { - strcatf(&line1, " %*.*s\e[0m " BOX_UD, w, w, col); - strcatf(&line2, " %*.*s\e[0m " BOX_UD, u, u, unit); + strcatf(&line1, " %*.*s\e[0m", w, w, col); + strcatf(&line2, " %*.*s\e[0m", u, u, unit); } for (int j = 0; j < t->cols[i]._width + 2; j++) { strcatf(&line3, "%s", BOX_LR); } - if (i == t->ncols - 1) { - strcatf(&line3, "%s", BOX_UDL); - } - else { + if (i != t->ncols - 1) { + strcatf(&line1, " %s", BOX_UD); + strcatf(&line2, " %s", BOX_UD); strcatf(&line3, "%s", BOX_UDLR); } @@ -121,7 +120,7 @@ void table_row(struct table *t, ...) va_list args; va_start(args, t); - char *line = strf("\b\b" BOX_UD); + char *line = NULL; for (int i = 0; i < t->ncols; ++i) { char *col = vstrf(t->cols[i].format, args); @@ -131,9 +130,12 @@ void table_row(struct table *t, ...) int w = t->cols[i]._width + r - l; if (t->cols[i].align == TABLE_ALIGN_LEFT) - strcatf(&line, " %-*.*s\e[0m " BOX_UD, w, w, col); + strcatf(&line, " %-*.*s\e[0m ", w, w, col); else - strcatf(&line, " %*.*s\e[0m " BOX_UD, w, w, col); + strcatf(&line, " %*.*s\e[0m ", w, w, col); + + if (i != t->ncols - 1) + strcatf(&line, BOX_UD); free(col); }