diff options
author | Jiří Klimeš <jklimes@redhat.com> | 2015-02-16 12:18:42 +0100 |
---|---|---|
committer | Jiří Klimeš <jklimes@redhat.com> | 2015-02-23 09:24:21 +0100 |
commit | 758e488f13140e55581c756ebab5ed7edb716f65 (patch) | |
tree | 5b0f316f14b35cb670cf67594954a7ab4d929010 /clients/cli | |
parent | bc265d4372e2c615e431a020fa270ddd4419a5c6 (diff) | |
download | NetworkManager-758e488f13140e55581c756ebab5ed7edb716f65.tar.gz |
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
Diffstat (limited to 'clients/cli')
-rw-r--r-- | clients/cli/nmcli.c | 22 | ||||
-rw-r--r-- | clients/cli/nmcli.h | 9 | ||||
-rw-r--r-- | clients/cli/utils.c | 4 |
3 files changed, 32 insertions, 3 deletions
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] <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; 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 --- */ |