From 12b60e7c251a5631a2d2e84c8441f10d8e53ff03 Mon Sep 17 00:00:00 2001 From: Peter Rajnoha Date: Tue, 22 Aug 2023 10:23:20 +0200 Subject: [PATCH] libdm: report: support reporting field IDs in headings Add new DM_REPORT_OUTPUT_FIELD_IDS_IN_HEADINGS report output flag. If enabled, column IDs are reported instead of column names in report headings. The 'column IDs' are IDs as found in 'const struct dm_report_field_type *fields' array that is passed during report initialization (that is, a call to dm_report_init/dm_report_init_with_selection). In this case, the 'id' dm_report_field_type member is used instead of the 'heading' member. --- WHATS_NEW_DM | 1 + device_mapper/all.h | 1 + device_mapper/libdm-report.c | 17 +++++++++++++++-- libdm/libdevmapper.h | 1 + libdm/libdm-report.c | 17 +++++++++++++++-- 5 files changed, 33 insertions(+), 4 deletions(-) diff --git a/WHATS_NEW_DM b/WHATS_NEW_DM index 2d7559426..78275ebf1 100644 --- a/WHATS_NEW_DM +++ b/WHATS_NEW_DM @@ -1,5 +1,6 @@ Version 1.02.197 - =================================== + Add DM_REPORT_OUTPUT_FIELD_IDS_IN_HEADINGS to provide alternative headings. Version 1.02.196 - 02nd August 2023 =================================== diff --git a/device_mapper/all.h b/device_mapper/all.h index d5b061d4e..4851d6b4a 100644 --- a/device_mapper/all.h +++ b/device_mapper/all.h @@ -1875,6 +1875,7 @@ const void *dm_report_value_cache_get(struct dm_report *rh, const char *name); #define DM_REPORT_OUTPUT_FIELD_UNQUOTED 0x00000010 #define DM_REPORT_OUTPUT_COLUMNS_AS_ROWS 0x00000020 #define DM_REPORT_OUTPUT_MULTIPLE_TIMES 0x00000040 +#define DM_REPORT_OUTPUT_FIELD_IDS_IN_HEADINGS 0x00000080 struct dm_report *dm_report_init(uint32_t *report_types, const struct dm_report_object_type *types, diff --git a/device_mapper/libdm-report.c b/device_mapper/libdm-report.c index 2d4c8883b..c3bab4906 100644 --- a/device_mapper/libdm-report.c +++ b/device_mapper/libdm-report.c @@ -4263,7 +4263,9 @@ static int _report_headings(struct dm_report *rh) fields = fp->implicit ? _implicit_report_fields : rh->fields; - heading = fields[fp->field_num].heading; + heading = rh->flags & DM_REPORT_OUTPUT_FIELD_IDS_IN_HEADINGS ? + fields[fp->field_num].id : fields[fp->field_num].heading; + if (rh->flags & DM_REPORT_OUTPUT_ALIGNED) { if (dm_snprintf(buf, buf_size, "%-*.*s", fp->width, fp->width, heading) < 0) { @@ -4315,6 +4317,7 @@ static void _recalculate_fields(struct dm_report *rh) struct row *row; struct dm_report_field *field; int len; + size_t id_len; dm_list_iterate_items(row, &rh->rows) { dm_list_iterate_items(field, &row->fields) { @@ -4329,6 +4332,12 @@ static void _recalculate_fields(struct dm_report *rh) field->props->width = len; } + + if (rh->flags & DM_REPORT_OUTPUT_FIELD_IDS_IN_HEADINGS) { + id_len = strlen(rh->fields[field->props->field_num].id); + if (field->props->width < id_len) + field->props->width = id_len; + } } } @@ -4740,6 +4749,7 @@ static int _output_as_rows(struct dm_report *rh) struct field_properties *fp; struct dm_report_field *field; struct row *row; + const char *heading; dm_list_iterate_items(fp, &rh->field_props) { if (fp->flags & FLD_HIDDEN) { @@ -4758,7 +4768,10 @@ static int _output_as_rows(struct dm_report *rh) } if ((rh->flags & DM_REPORT_OUTPUT_HEADINGS)) { - if (!dm_pool_grow_object(rh->mem, fields[fp->field_num].heading, 0)) { + heading = rh->flags & DM_REPORT_OUTPUT_FIELD_IDS_IN_HEADINGS ? + fields[fp->field_num].id : fields[fp->field_num].heading; + + if (!dm_pool_grow_object(rh->mem, heading, 0)) { log_error("dm_report: Failed to extend row for field name"); goto bad; } diff --git a/libdm/libdevmapper.h b/libdm/libdevmapper.h index 8d56cea5b..53f1c601f 100644 --- a/libdm/libdevmapper.h +++ b/libdm/libdevmapper.h @@ -3058,6 +3058,7 @@ const void *dm_report_value_cache_get(struct dm_report *rh, const char *name); #define DM_REPORT_OUTPUT_FIELD_UNQUOTED 0x00000010 #define DM_REPORT_OUTPUT_COLUMNS_AS_ROWS 0x00000020 #define DM_REPORT_OUTPUT_MULTIPLE_TIMES 0x00000040 +#define DM_REPORT_OUTPUT_FIELD_IDS_IN_HEADINGS 0x00000080 struct dm_report *dm_report_init(uint32_t *report_types, const struct dm_report_object_type *types, diff --git a/libdm/libdm-report.c b/libdm/libdm-report.c index 8eaf5896e..5f5f0a773 100644 --- a/libdm/libdm-report.c +++ b/libdm/libdm-report.c @@ -4262,7 +4262,9 @@ static int _report_headings(struct dm_report *rh) fields = fp->implicit ? _implicit_report_fields : rh->fields; - heading = fields[fp->field_num].heading; + heading = rh->flags & DM_REPORT_OUTPUT_FIELD_IDS_IN_HEADINGS ? + fields[fp->field_num].id : fields[fp->field_num].heading; + if (rh->flags & DM_REPORT_OUTPUT_ALIGNED) { if (dm_snprintf(buf, buf_size, "%-*.*s", fp->width, fp->width, heading) < 0) { @@ -4314,6 +4316,7 @@ static void _recalculate_fields(struct dm_report *rh) struct row *row; struct dm_report_field *field; int len; + size_t id_len; dm_list_iterate_items(row, &rh->rows) { dm_list_iterate_items(field, &row->fields) { @@ -4328,6 +4331,12 @@ static void _recalculate_fields(struct dm_report *rh) field->props->width = len; } + + if (rh->flags & DM_REPORT_OUTPUT_FIELD_IDS_IN_HEADINGS) { + id_len = strlen(rh->fields[field->props->field_num].id); + if (field->props->width < id_len) + field->props->width = id_len; + } } } @@ -4739,6 +4748,7 @@ static int _output_as_rows(struct dm_report *rh) struct field_properties *fp; struct dm_report_field *field; struct row *row; + const char *heading; dm_list_iterate_items(fp, &rh->field_props) { if (fp->flags & FLD_HIDDEN) { @@ -4757,7 +4767,10 @@ static int _output_as_rows(struct dm_report *rh) } if ((rh->flags & DM_REPORT_OUTPUT_HEADINGS)) { - if (!dm_pool_grow_object(rh->mem, fields[fp->field_num].heading, 0)) { + heading = rh->flags & DM_REPORT_OUTPUT_FIELD_IDS_IN_HEADINGS ? + fields[fp->field_num].id : fields[fp->field_num].heading; + + if (!dm_pool_grow_object(rh->mem, heading, 0)) { log_error("dm_report: Failed to extend row for field name"); goto bad; }