nmcli: add global '--colors' option for controlling color output

nmcli -c auto  ->  colors will only be used when stdout is a terminal
nmcli -c yes   ->  colors will be enabled unconditionally
nmcli -c no    ->  colors will be disabled unconditionally
This commit is contained in:
Jiří Klimeš 2015-02-16 12:18:42 +01:00
parent bc265d4372
commit 758e488f13
4 changed files with 41 additions and 5 deletions

View file

@ -16,7 +16,7 @@
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Copyright 2010 - 2014 Red Hat, Inc.
* Copyright 2010 - 2015 Red Hat, Inc.
*/
/* Generated configuration file */
@ -89,6 +89,7 @@ usage (const char *prog_name)
" -t[erse] terse output\n"
" -p[retty] pretty output\n"
" -m[ode] tabular|multiline output mode\n"
" -c[olors] auto|yes|no whether to use colors in output\n"
" -f[ields] <field1,field2,...>|all|common specify fields to output\n"
" -e[scape] yes|no escape columns separators in values\n"
" -n[ocheck] don't check nmcli and NetworkManager versions\n"
@ -210,6 +211,24 @@ parse_command_line (NmCli *nmc, int argc, char **argv)
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
return nmc->return_value;
}
} else if (matches (opt, "-colors") == 0) {
next_arg (&argc, &argv);
if (argc <= 1) {
g_string_printf (nmc->return_text, _("Error: missing argument for '%s' option."), opt);
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
return nmc->return_value;
}
if (matches (argv[1], "auto") == 0)
nmc->use_colors = NMC_USE_COLOR_AUTO;
else if (matches (argv[1], "yes") == 0)
nmc->use_colors = NMC_USE_COLOR_YES;
else if (matches (argv[1], "no") == 0)
nmc->use_colors = NMC_USE_COLOR_NO;
else {
g_string_printf (nmc->return_text, _("Error: '%s' is not valid argument for '%s' option."), argv[1], opt);
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
return nmc->return_value;
}
} else if (matches (opt, "-escape") == 0) {
next_arg (&argc, &argv);
if (argc <= 1) {
@ -526,6 +545,7 @@ nmc_init (NmCli *nmc)
memset (&nmc->print_fields, '\0', sizeof (NmcPrintFields));
nmc->nocheck_ver = FALSE;
nmc->ask = FALSE;
nmc->use_colors = NMC_USE_COLOR_AUTO;
nmc->in_editor = FALSE;
nmc->editor_status_line = FALSE;
nmc->editor_save_confirmation = TRUE;

View file

@ -14,7 +14,7 @@
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Copyright 2010 - 2014 Red Hat, Inc.
* Copyright 2010 - 2015 Red Hat, Inc.
*/
#ifndef NMC_NMCLI_H
@ -122,6 +122,12 @@ typedef struct {
int indent; /* Indent by this number of spaces */
} NmcPrintFields;
typedef enum {
NMC_USE_COLOR_AUTO,
NMC_USE_COLOR_YES,
NMC_USE_COLOR_NO,
} NmcColorOption;
/* NmCli - main structure */
typedef struct _NmCli {
NMClient *client; /* Pointer to NMClient of libnm */
@ -143,6 +149,7 @@ typedef struct _NmCli {
NMCPrintOutput print_output; /* Output mode */
gboolean multiline_output; /* Multiline output instead of default tabular */
gboolean mode_specified; /* Whether tabular/multiline mode was specified via '--mode' option */
NmcColorOption use_colors; /* Whether to use colors for output: option '--color' */
gboolean escape_values; /* Whether to escape ':' and '\' in terse tabular mode */
char *required_fields; /* Required fields in output: '--fields' option */
GPtrArray *output_data; /* GPtrArray of arrays of NmcOutputField structs - accumulates data for output */

View file

@ -1058,7 +1058,9 @@ print_required_fields (NmCli *nmc, const NmcOutputField field_values[])
return;
/* Only show colors if the output is a terminal */
colorize = isatty (fileno (stdout));
colorize = nmc->use_colors == NMC_USE_COLOR_YES ? TRUE :
nmc->use_colors == NMC_USE_COLOR_NO ? FALSE :
isatty (fileno (stdout));
if (multiline) {
/* --- Multiline mode --- */

View file

@ -19,9 +19,9 @@
.\" with this manual; if not, write to the Free Software Foundation, Inc.,
.\" 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
.\"
.\" Copyright 2010 - 2014 Red Hat, Inc.
.\" Copyright 2010 - 2015 Red Hat, Inc.
.\"
.TH NMCLI "1" "3 December 2014"
.TH NMCLI "1" "19 February 2015"
.SH NAME
nmcli \- command\(hyline tool for controlling NetworkManager
@ -45,6 +45,8 @@ nmcli \- command\(hyline tool for controlling NetworkManager
.br
\fB\-m\fR[\fImode\fR] tabular | multiline
.br
\fB\-c\fR[\fIcolors\fR] auto | yes | no
.br
\fB\-f\fR[\fIields\fR] <field1,field2,...> | all | common
.br
\fB\-e\fR[\fIscape\fR] yes | no
@ -106,6 +108,11 @@ Columns define particular properties of the entry.
\fImultiline\fP \(en Each entry comprises multiple lines, each property on its own
line. The values are prefixed with the property name.
.TP
.B \-c, \-\-colors auto|yes|no
This option controls color output (using terminal escape sequences). \fIyes\fP
enables colors, \fIno\fP disables them, \fIauto\fP only produces colors when
standard output is directed to a terminal. The default value is \fIauto\fP.
.TP
.B \-f, \-\-fields <field1,field2,...> | all | common
This option is used to specify what fields (column names) should be printed.
Valid field names differ for specific commands. List available fields by