From 57eef7c412e17b8b32a7666843e1c1e547dc6d45 Mon Sep 17 00:00:00 2001 From: Bjoern Walk Date: Wed, 1 May 2024 07:34:51 +0200 Subject: [PATCH] hyptop: Keep track of string-typed columns MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For structured output (i.e. JSON), some type information of the columns is required, at least the ability to distinguish values that are strings and possibly need to be quoted. Unfortunately at the time column values are formatted this information is lost. The column types are specified implicitly during the unit conversion of the raw value. For example, online time is stored as a u64 value but is converted into a "d:h:m" string. Introduce a private flag per column that signifies if that column is to be formatted as a string. This flag will be set in the appropriate unit conversion function. Reviewed-by: Mete Durlu Signed-off-by: Bjoern Walk Signed-off-by: Jan Höppner --- hyptop/table.h | 6 ++++++ hyptop/table_col_unit.c | 6 +++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/hyptop/table.h b/hyptop/table.h index 34e42940..87d9f791 100644 --- a/hyptop/table.h +++ b/hyptop/table.h @@ -106,6 +106,7 @@ struct table_col_priv { char head_char[2]; char head_last[TABLE_HEADING_SIZE]; int rsort; + int needs_quotes; }; /* @@ -135,6 +136,11 @@ static inline int table_col_enabled(struct table_col *col) return col->p->enabled; } +static inline int table_col_needs_quotes(struct table_col *col) +{ + return col->p->needs_quotes; +} + /* * Table Column Constructor Macros */ diff --git a/hyptop/table_col_unit.c b/hyptop/table_col_unit.c index 630ad5d1..0f460ab2 100644 --- a/hyptop/table_col_unit.c +++ b/hyptop/table_col_unit.c @@ -63,7 +63,7 @@ static int l_unit_raw(struct table_col *col, struct table_entry *e) */ static int l_str(struct table_col *col, struct table_entry *e) { - (void) col; + col->p->needs_quotes = 1; return strlen(e->str); } @@ -226,6 +226,8 @@ static int l_unit_hm_u64(char *str, u64 v1, int negative) static int l_unit_hm(struct table_col *col, struct table_entry *e) { + col->p->needs_quotes = 1; + if (!e->set) return snprintf(e->str, sizeof(e->str), L_COL_NOT_SET_STR); @@ -272,6 +274,8 @@ static int l_unit_dhm_u64(char *str, u64 v1, int negative) static int l_unit_dhm(struct table_col *col, struct table_entry *e) { + col->p->needs_quotes = 1; + if (!e->set) return snprintf(e->str, sizeof(e->str), L_COL_NOT_SET_STR);