klist --json support
This commit is contained in:
101
lib/roken/rtbl.c
101
lib/roken/rtbl.c
@@ -34,6 +34,7 @@
|
||||
#include <config.h>
|
||||
|
||||
#include "roken.h"
|
||||
#include <ctype.h>
|
||||
#include "rtbl.h"
|
||||
|
||||
struct column_entry {
|
||||
@@ -188,7 +189,7 @@ column_compute_width (rtbl_t table, struct column_data *column)
|
||||
if(table->flags & RTBL_HEADER_STYLE_NONE)
|
||||
column->width = 0;
|
||||
else
|
||||
column->width = strlen (column->header);
|
||||
column->width = (int)strlen (column->header);
|
||||
for (i = 0; i < column->num_rows; i++)
|
||||
column->width = max (column->width, (int) strlen (column->rows[i].data));
|
||||
}
|
||||
@@ -362,6 +363,18 @@ rtbl_add_column_entryv (rtbl_t table, const char *column, const char *fmt, ...)
|
||||
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
|
||||
rtbl_format (rtbl_t table, FILE * f)
|
||||
{
|
||||
char *str = rtbl_format_str(table);
|
||||
if (str == NULL)
|
||||
return ENOMEM;
|
||||
fprintf(f, "%s", str);
|
||||
free(str);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static char *
|
||||
rtbl_format_pretty(rtbl_t table)
|
||||
{
|
||||
struct rk_strpool *p = NULL;
|
||||
size_t i, j;
|
||||
|
||||
for (i = 0; i < table->num_columns; i++)
|
||||
@@ -371,16 +384,18 @@ rtbl_format (rtbl_t table, FILE * f)
|
||||
struct column_data *c = table->columns[i];
|
||||
|
||||
if(table->column_separator != NULL && i > 0)
|
||||
fprintf (f, "%s", table->column_separator);
|
||||
fprintf (f, "%s", get_column_prefix (table, c));
|
||||
if(i == table->num_columns - 1 && c->suffix == NULL)
|
||||
p = rk_strpoolprintf(p, "%s", table->column_separator);
|
||||
p = rk_strpoolprintf(p, "%s", get_column_prefix (table, c));
|
||||
if (c == NULL) {
|
||||
/* do nothing if no column */
|
||||
} else if(i == table->num_columns - 1 && c->suffix == NULL)
|
||||
/* last column, so no need to pad with spaces */
|
||||
fprintf (f, "%-*s", 0, c->header);
|
||||
p = rk_strpoolprintf(p, "%-*s", 0, c->header);
|
||||
else
|
||||
fprintf (f, "%-*s", (int)c->width, c->header);
|
||||
fprintf (f, "%s", get_column_suffix (table, c));
|
||||
p = rk_strpoolprintf(p, "%-*s", (int)c->width, c->header);
|
||||
p = rk_strpoolprintf(p, "%s", get_column_suffix (table, c));
|
||||
}
|
||||
fprintf (f, "\n");
|
||||
p = rk_strpoolprintf(p, "\n");
|
||||
}
|
||||
|
||||
for (j = 0;; j++) {
|
||||
@@ -403,7 +418,7 @@ rtbl_format (rtbl_t table, FILE * f)
|
||||
struct column_data *c = table->columns[i];
|
||||
|
||||
if(table->column_separator != NULL && i > 0)
|
||||
fprintf (f, "%s", table->column_separator);
|
||||
p = rk_strpoolprintf(p, "%s", table->column_separator);
|
||||
|
||||
w = c->width;
|
||||
|
||||
@@ -414,16 +429,72 @@ rtbl_format (rtbl_t table, FILE * f)
|
||||
else
|
||||
w = -w;
|
||||
}
|
||||
fprintf (f, "%s", get_column_prefix (table, c));
|
||||
rk_strpoolprintf(p, "%s", get_column_prefix (table, c));
|
||||
if (c->num_rows <= j)
|
||||
fprintf (f, "%*s", w, "");
|
||||
p = rk_strpoolprintf(p, "%*s", w, "");
|
||||
else
|
||||
fprintf (f, "%*s", w, c->rows[j].data);
|
||||
fprintf (f, "%s", get_column_suffix (table, c));
|
||||
p = rk_strpoolprintf(p, "%*s", w, c->rows[j].data);
|
||||
p = rk_strpoolprintf(p, "%s", get_column_suffix (table, c));
|
||||
}
|
||||
fprintf (f, "\n");
|
||||
p = rk_strpoolprintf(p, "\n");
|
||||
}
|
||||
return 0;
|
||||
|
||||
return rk_strpoolcollect(p);
|
||||
}
|
||||
|
||||
static char *
|
||||
rtbl_format_json(rtbl_t table)
|
||||
{
|
||||
struct rk_strpool *p = NULL;
|
||||
size_t i, j;
|
||||
int comma;
|
||||
|
||||
p = rk_strpoolprintf(p, "[");
|
||||
for (j = 0;; j++) {
|
||||
int flag = 0;
|
||||
|
||||
/* are there any more rows left? */
|
||||
for (i = 0; flag == 0 && i < table->num_columns; ++i) {
|
||||
struct column_data *c = table->columns[i];
|
||||
|
||||
if (c->num_rows > j) {
|
||||
++flag;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (flag == 0)
|
||||
break;
|
||||
|
||||
p = rk_strpoolprintf(p, "%s{", j > 0 ? "," : "");
|
||||
|
||||
comma = 0;
|
||||
for (i = 0; i < table->num_columns; i++) {
|
||||
struct column_data *c = table->columns[i];
|
||||
|
||||
if (c->num_rows > j) {
|
||||
char *header = c->header;
|
||||
while (isspace((int)header[0])) /* trim off prefixed whitespace */
|
||||
header++;
|
||||
p = rk_strpoolprintf(p, "%s\"%s\" : \"%s\"",
|
||||
comma ? "," : "", header,
|
||||
c->rows[j].data);
|
||||
comma = 1;
|
||||
}
|
||||
}
|
||||
p = rk_strpoolprintf(p, "}");
|
||||
}
|
||||
p = rk_strpoolprintf(p, "]");
|
||||
|
||||
return rk_strpoolcollect(p);
|
||||
}
|
||||
|
||||
ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL
|
||||
rtbl_format_str (rtbl_t table)
|
||||
{
|
||||
if (table->flags & RTBL_JSON)
|
||||
return rtbl_format_json(table);
|
||||
|
||||
return rtbl_format_pretty(table);
|
||||
}
|
||||
|
||||
#ifdef TEST
|
||||
|
Reference in New Issue
Block a user