diff --git a/clients/cli/nmcli.c b/clients/cli/nmcli.c index 8d9f7fef89..63c4b777a7 100644 --- a/clients/cli/nmcli.c +++ b/clients/cli/nmcli.c @@ -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] |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; diff --git a/clients/cli/nmcli.h b/clients/cli/nmcli.h index ee1a86f96e..06662fefee 100644 --- a/clients/cli/nmcli.h +++ b/clients/cli/nmcli.h @@ -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 */ diff --git a/clients/cli/utils.c b/clients/cli/utils.c index 3036dfe48f..ab591d9563 100644 --- a/clients/cli/utils.c +++ b/clients/cli/utils.c @@ -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 --- */ diff --git a/man/nmcli.1.in b/man/nmcli.1.in index 8ca2a3b2aa..0609795997 100644 --- a/man/nmcli.1.in +++ b/man/nmcli.1.in @@ -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] | 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 | 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