cli: enhance printing to align tabular output properly and not to waste space

Until now we have used a static width defined for each column for tabular
output. Even if this worked in most cases, it was not optimal, because by
using too wide columns we wasted space, and in case of a too narrow column the
alignment broke. So, we need to know the longest string in a column to be able
to align columns in the tabular output. Thus, the printing has to be postponed
till we have all data available, and can find the widest column. This value is
then used for aligning while printing the data.

Arrays of NmcOutputField (rows) are inserted into output_data array. When all
data have been added, print_data() can be used to print the whole output_data
array with proper alignment.

A single row can be printed using print_required_fields().

Also, output flags are redone to better match the new output_data array.
The flags are needed for every row (in tabular output); they are stored in
the first field (NmcOutputField) for the whole row.

Addapted set_val_str() and set_val_arr() to set value type (char * x char **).
Added set_val_strc(), set_val_arrc() for const values that should not be freed.

output_data takes ownership of the data added to it and takes care of freeing
the memory.

See e.g.
https://bugzilla.gnome.org/show_bug.cgi?id=699503
This commit is contained in:
Jiří Klimeš
2013-05-22 08:36:09 +02:00
parent f2e5f38f6c
commit e6870789b5
4 changed files with 184 additions and 62 deletions

View File

@@ -66,31 +66,25 @@ typedef enum {
/* === Output fields === */
/* Flags for NmcOutputField */
#define NMC_OF_FLAG_ARRAY 0x00000001 /* 'value' is an NULL-terminated array rather then a single string */
#define NMC_OF_FLAG_FIELD_NAMES 0x00000001 /* Print field names instead of values */
#define NMC_OF_FLAG_SECTION_PREFIX 0x00000002 /* Use the first value as section prefix for the other field names - just in multiline */
#define NMC_OF_FLAG_MAIN_HEADER_ADD 0x00000004 /* Print main header in addition to values/field names */
#define NMC_OF_FLAG_MAIN_HEADER_ONLY 0x00000008 /* Print main header only */
typedef struct {
const char *name; /* Field's name */
const char *name_l10n; /* Field's name for translation */
int width; /* Width in screen columns */
void *value; /* Value of current field - char* or char** */
guint32 flags; /* Flags */
const char *name; /* Field's name */
const char *name_l10n; /* Field's name for translation */
int width; /* Width in screen columns */
void *value; /* Value of current field - char* or char** (NULL-terminated array) */
gboolean value_is_array; /* Whether value is char ** instead of char* */
gboolean free_value; /* Whether to free the value */
guint32 flags; /* Flags - whether and how to print values/field names/headers */
} NmcOutputField;
/* Flags for NmcPrintFields */
#define NMC_PF_FLAG_MULTILINE 0x00000001 /* Multiline output instead of tabular */
#define NMC_PF_FLAG_TERSE 0x00000002 /* Terse output mode */
#define NMC_PF_FLAG_PRETTY 0x00000004 /* Pretty output mode */
#define NMC_PF_FLAG_MAIN_HEADER_ADD 0x00000008 /* Print main header in addition to values/field names */
#define NMC_PF_FLAG_MAIN_HEADER_ONLY 0x00000010 /* Print main header only */
#define NMC_PF_FLAG_FIELD_NAMES 0x00000020 /* Print field names instead of values */
#define NMC_PF_FLAG_ESCAPE 0x00000040 /* Escape column separator and '\' */
#define NMC_PF_FLAG_SECTION_PREFIX 0x00000080 /* Use the first value as section prefix for the other field names - just in multiline */
typedef struct {
GArray *indices; /* Array of field indices to the array of allowed fields */
char *header_name; /* Name of the output */
int indent; /* Indent by this number of spaces */
guint32 flags; /* Various flags for controlling output: see NMC_PF_FLAG_* values */
} NmcPrintFields;
/* NmCli - main structure */
@@ -114,7 +108,7 @@ typedef struct _NmCli {
gboolean mode_specified; /* Whether tabular/multiline mode was specified via '--mode' option */
gboolean escape_values; /* Whether to escape ':' and '\' in terse tabular mode */
char *required_fields; /* Required fields in output: '--fields' option */
NmcOutputField *allowed_fields; /* Array of allowed fields for particular commands */
GPtrArray *output_data; /* GPtrArray of arrays of NmcOutputField structs - accumulates data for output */
NmcPrintFields print_fields; /* Structure with field indices to print */
gboolean nocheck_ver; /* Don't check nmcli and NM versions: option '--nocheck' */
gboolean ask; /* Ask for missing parameters: option '--ask' */