Commit a91da03b authored by Jiří Klimeš's avatar Jiří Klimeš

cli: 'dev list' adjustments

Modifications are mainly for multiline mode to ease parsing - each
field name is prefixed with a section name.
'dev list' now also supports printing particular sections specifed via
'--fields' option.
parent 31970798
......@@ -154,7 +154,7 @@ show_connection (NMConnection *data, gpointer user_data)
nmc->allowed_fields[6].value = nm_setting_connection_get_autoconnect (s_con) ? _("yes") : _("no");
nmc->allowed_fields[7].value = nm_setting_connection_get_read_only (s_con) ? _("yes") : _("no");
nmc->print_fields.flags &= ~NMC_PF_FLAG_MAIN_HEADER & ~NMC_PF_FLAG_FIELD_NAMES; /* Clear header flags */
nmc->print_fields.flags &= ~NMC_PF_FLAG_MAIN_HEADER_ADD & ~NMC_PF_FLAG_MAIN_HEADER_ONLY & ~NMC_PF_FLAG_FIELD_NAMES; /* Clear header flags */
print_fields (nmc->print_fields, nmc->allowed_fields);
g_free (timestamp_str);
......@@ -227,12 +227,12 @@ do_connections_list (NmCli *nmc, int argc, char **argv)
if (argc == 0) {
valid_param_specified = TRUE;
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_MAIN_HEADER | NMC_PF_FLAG_FIELD_NAMES;
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_MAIN_HEADER_ADD | NMC_PF_FLAG_FIELD_NAMES;
nmc->print_fields.header_name = _("System connections");
print_fields (nmc->print_fields, nmc->allowed_fields);
g_slist_foreach (nmc->system_connections, (GFunc) show_connection, nmc);
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_MAIN_HEADER | NMC_PF_FLAG_FIELD_NAMES;
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_MAIN_HEADER_ADD | NMC_PF_FLAG_FIELD_NAMES;
nmc->print_fields.header_name = _("User connections");
print_fields (nmc->print_fields, nmc->allowed_fields);
g_slist_foreach (nmc->user_connections, (GFunc) show_connection, nmc);
......@@ -264,7 +264,7 @@ do_connections_list (NmCli *nmc, int argc, char **argv)
else if (strcmp (*argv, "system") == 0) {
valid_param_specified = TRUE;
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_MAIN_HEADER | NMC_PF_FLAG_FIELD_NAMES;
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_MAIN_HEADER_ADD | NMC_PF_FLAG_FIELD_NAMES;
nmc->print_fields.header_name = _("System connections");
print_fields (nmc->print_fields, nmc->allowed_fields);
g_slist_foreach (nmc->system_connections, (GFunc) show_connection, nmc);
......@@ -273,7 +273,7 @@ do_connections_list (NmCli *nmc, int argc, char **argv)
else if (strcmp (*argv, "user") == 0) {
valid_param_specified = TRUE;
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_MAIN_HEADER | NMC_PF_FLAG_FIELD_NAMES;
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_MAIN_HEADER_ADD | NMC_PF_FLAG_FIELD_NAMES;
nmc->print_fields.header_name = _("User connections");
print_fields (nmc->print_fields, nmc->allowed_fields);
g_slist_foreach (nmc->user_connections, (GFunc) show_connection, nmc);
......@@ -353,7 +353,7 @@ show_active_connection (gpointer data, gpointer user_data)
info->nmc->allowed_fields[6].value = nm_active_connection_get_specific_object (active);
info->nmc->allowed_fields[7].value = NM_IS_VPN_CONNECTION (active) ? _("yes") : _("no");
info->nmc->print_fields.flags &= ~NMC_PF_FLAG_MAIN_HEADER & ~NMC_PF_FLAG_FIELD_NAMES; /* Clear header flags */
info->nmc->print_fields.flags &= ~NMC_PF_FLAG_MAIN_HEADER_ADD & ~NMC_PF_FLAG_MAIN_HEADER_ONLY & ~NMC_PF_FLAG_FIELD_NAMES; /* Clear header flags */
print_fields (info->nmc->print_fields, info->nmc->allowed_fields);
break;
}
......@@ -403,7 +403,7 @@ do_connections_status (NmCli *nmc, int argc, char **argv)
goto error;
}
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_MAIN_HEADER | NMC_PF_FLAG_FIELD_NAMES;
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_MAIN_HEADER_ADD | NMC_PF_FLAG_FIELD_NAMES;
nmc->print_fields.header_name = _("Active connections");
print_fields (nmc->print_fields, nmc->allowed_fields);
......
This diff is collapsed.
......@@ -130,7 +130,7 @@ show_nm_status (NmCli *nmc)
return nmc->return_value;
}
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_MAIN_HEADER | NMC_PF_FLAG_FIELD_NAMES;
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_MAIN_HEADER_ADD | NMC_PF_FLAG_FIELD_NAMES;
nmc->print_fields.header_name = _("NetworkManager status");
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print header */
......@@ -152,7 +152,7 @@ show_nm_status (NmCli *nmc)
nmc->allowed_fields[4].value = wwan_hw_enabled_str;
nmc->allowed_fields[5].value = wwan_enabled_str;
nmc->print_fields.flags &= ~NMC_PF_FLAG_MAIN_HEADER & ~NMC_PF_FLAG_FIELD_NAMES; /* Clear header flags */
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag;
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print values */
return NMC_RESULT_SUCCESS;
......@@ -205,11 +205,11 @@ do_network_manager (NmCli *nmc, int argc, char **argv)
}
nmc->allowed_fields = nmc_fields_nm_status;
nmc->print_fields.indices = parse_output_fields (NMC_FIELDS_NM_WIFI, nmc->allowed_fields, NULL);
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_MAIN_HEADER | NMC_PF_FLAG_FIELD_NAMES;
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_MAIN_HEADER_ADD | NMC_PF_FLAG_FIELD_NAMES;
nmc->print_fields.header_name = _("WiFi enabled");
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print header */
nmc->allowed_fields[3].value = nm_client_wireless_get_enabled (nmc->client) ? _("enabled") : _("disabled");
nmc->print_fields.flags &= ~NMC_PF_FLAG_MAIN_HEADER & ~NMC_PF_FLAG_FIELD_NAMES; /* Clear header flags */
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag;
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print header */
} else {
if (!strcmp (*argv, "on"))
......@@ -237,11 +237,11 @@ do_network_manager (NmCli *nmc, int argc, char **argv)
}
nmc->allowed_fields = nmc_fields_nm_status;
nmc->print_fields.indices = parse_output_fields (NMC_FIELDS_NM_WWAN, nmc->allowed_fields, NULL);
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_MAIN_HEADER | NMC_PF_FLAG_FIELD_NAMES;
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_MAIN_HEADER_ADD | NMC_PF_FLAG_FIELD_NAMES;
nmc->print_fields.header_name = _("WWAN enabled");
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print header */
nmc->allowed_fields[5].value = nm_client_wwan_get_enabled (nmc->client) ? _("enabled") : _("disabled");
nmc->print_fields.flags &= ~NMC_PF_FLAG_MAIN_HEADER & ~NMC_PF_FLAG_FIELD_NAMES; /* Clear header flags */
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag;
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print header */
} else {
if (!strcmp (*argv, "on"))
......
......@@ -66,12 +66,14 @@ typedef struct {
} NmcOutputField;
/* Flags for NmcPrintFields */
#define NMC_PF_FLAG_MULTILINE 0x00000001 /* Multiline output instead of tabular*/
#define NMC_PF_FLAG_TERSE 0x00000002 /* Terse outpud mode */
#define NMC_PF_FLAG_PRETTY 0x00000004 /* Pretty output mode */
#define NMC_PF_FLAG_MAIN_HEADER 0x00000008 /* Print main header */
#define NMC_PF_FLAG_FIELD_NAMES 0x00000010 /* Print field names instead of values */
#define NMC_PF_FLAG_ESCAPE 0x00000020 /* Escape column separator and '\' */
#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 */
......
......@@ -129,14 +129,17 @@ print_fields (const NmcPrintFields fields, const NmcOutputField field_values[])
gboolean multiline = fields.flags & NMC_PF_FLAG_MULTILINE;
gboolean terse = fields.flags & NMC_PF_FLAG_TERSE;
gboolean pretty = fields.flags & NMC_PF_FLAG_PRETTY;
gboolean main_header = fields.flags & NMC_PF_FLAG_MAIN_HEADER;
gboolean main_header_add = fields.flags & NMC_PF_FLAG_MAIN_HEADER_ADD;
gboolean main_header_only = fields.flags & NMC_PF_FLAG_MAIN_HEADER_ONLY;
gboolean field_names = fields.flags & NMC_PF_FLAG_FIELD_NAMES;
gboolean escape = fields.flags & NMC_PF_FLAG_ESCAPE;
gboolean section_prefix = fields.flags & NMC_PF_FLAG_SECTION_PREFIX;
gboolean main_header = main_header_add || main_header_only;
/* No headers are printed in terse mode:
* - neither main header nor field (column) names
*/
if ((main_header || field_names) && terse)
if ((main_header_only || field_names) && terse)
return;
if (multiline) {
......@@ -157,12 +160,16 @@ print_fields (const NmcPrintFields fields, const NmcOutputField field_values[])
}
/* Print values */
if (!field_names) {
if (!main_header_only && !field_names) {
for (i = 0; i < fields.indices->len; i++) {
char *tmp;
idx = g_array_index (fields.indices, int, i);
tmp = g_strdup_printf ("%s:", _(field_values[idx].name_l10n));
printf ("%-*s%s\n", terse ? 0 : 20, tmp, field_values[idx].value ? field_values[idx].value : not_set_str);
if (section_prefix && idx == 0) /* The first field is section prefix */
continue;
tmp = g_strdup_printf ("%s%s%s:", section_prefix ? field_values[0].value : "",
section_prefix ? "." : "",
_(field_values[idx].name_l10n));
printf ("%-*s%s\n", terse ? 0 : 32, tmp, field_values[idx].value ? field_values[idx].value : not_set_str);
g_free (tmp);
}
if (pretty) {
......@@ -222,7 +229,7 @@ print_fields (const NmcPrintFields fields, const NmcOutputField field_values[])
}
/* Print actual values */
if (str->len > 0) {
if (!main_header_only && str->len > 0) {
g_string_truncate (str, str->len-1); /* Chop off last column separator */
if (fields.indent > 0) {
indent_str = g_strnfill (fields.indent, ' ');
......@@ -233,7 +240,7 @@ print_fields (const NmcPrintFields fields, const NmcOutputField field_values[])
}
/* Print horizontal separator */
if (field_names && pretty) {
if (!main_header_only && field_names && pretty) {
if (str->len > 0) {
line = g_strnfill (table_width, '-');
printf ("%s\n", line);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment