summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubomir Rintel <lkundrak@v3.sk>2018-03-29 12:25:14 +0200
committerLubomir Rintel <lkundrak@v3.sk>2018-05-10 14:36:58 +0200
commit9dfe8258409c1a3903f8f82518983f11ccf42a2f (patch)
tree866fbced47f917e2811e0ef972577805777bdb6e
parent56a5b2738925fab6300f15246923b6f071732238 (diff)
downloadNetworkManager-9dfe8258409c1a3903f8f82518983f11ccf42a2f.tar.gz
cli: rework enabling and disabling colors
This actually makes very little difference at the moment, but will make things more confortable later on, when the logic of enabling/disabling coloring will involve terminal-colors.d(5). Instead of deciding whether to use colors lazily with use_colors(), it's done very early on nmcli initialization and a boolean use_colors field is stored in the NmcConfig instance instead of the raw tristate option of NmcColorOption type (which is now confined to nmcli.c). Wherever the NmcColorOption was used previously, the whole NmcConfig instance is passed around. That might seem pointless (since only the use_colors boolean is actually used at the moment), but will be utilized to pass around the actual color palette in future.
-rw-r--r--clients/cli/connections.c20
-rw-r--r--clients/cli/devices.c4
-rw-r--r--clients/cli/general.c18
-rw-r--r--clients/cli/nmcli.c36
-rw-r--r--clients/cli/nmcli.h8
-rw-r--r--clients/cli/utils.c46
-rw-r--r--clients/cli/utils.h3
7 files changed, 69 insertions, 66 deletions
diff --git a/clients/cli/connections.c b/clients/cli/connections.c
index 405da31533..823dd49a7c 100644
--- a/clients/cli/connections.c
+++ b/clients/cli/connections.c
@@ -6470,7 +6470,7 @@ property_edit_submenu (NmCli *nmc,
/* Set global variable for use in TAB completion */
nmc_tab_completion.property = prop_name;
- prompt = nmc_colorize (nmc->nmc_config.use_colors, NM_META_TERM_COLOR_NORMAL, NM_META_TERM_FORMAT_NORMAL,
+ prompt = nmc_colorize (&nmc->nmc_config, NM_META_TERM_COLOR_NORMAL, NM_META_TERM_FORMAT_NORMAL,
"nmcli %s.%s> ",
nm_setting_get_name (curr_setting), prop_name);
@@ -6801,14 +6801,14 @@ typedef struct {
} NmcEditorMenuContext;
static void
-menu_switch_to_level0 (NmcColorOption color_option,
+menu_switch_to_level0 (const NmcConfig *nmc_config,
NmcEditorMenuContext *menu_ctx,
const char *prompt,
NMMetaTermColor prompt_color)
{
menu_ctx->level = 0;
g_free (menu_ctx->main_prompt);
- menu_ctx->main_prompt = nmc_colorize (color_option, prompt_color, NM_META_TERM_FORMAT_NORMAL, "%s", prompt);
+ menu_ctx->main_prompt = nmc_colorize (nmc_config, prompt_color, NM_META_TERM_FORMAT_NORMAL, "%s", prompt);
menu_ctx->curr_setting = NULL;
g_strfreev (menu_ctx->valid_props);
menu_ctx->valid_props = NULL;
@@ -6817,7 +6817,7 @@ menu_switch_to_level0 (NmcColorOption color_option,
}
static void
-menu_switch_to_level1 (NmcColorOption color_option,
+menu_switch_to_level1 (const NmcConfig *nmc_config,
NmcEditorMenuContext *menu_ctx,
NMSetting *setting,
const char *setting_name,
@@ -6825,7 +6825,7 @@ menu_switch_to_level1 (NmcColorOption color_option,
{
menu_ctx->level = 1;
g_free (menu_ctx->main_prompt);
- menu_ctx->main_prompt = nmc_colorize (color_option, prompt_color, NM_META_TERM_FORMAT_NORMAL,
+ menu_ctx->main_prompt = nmc_colorize (nmc_config, prompt_color, NM_META_TERM_FORMAT_NORMAL,
"nmcli %s> ", setting_name);
menu_ctx->curr_setting = setting;
g_strfreev (menu_ctx->valid_props);
@@ -6864,7 +6864,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t
valid_settings_str = get_valid_options_string (valid_settings_main, valid_settings_slave);
g_print (_("You may edit the following settings: %s\n"), valid_settings_str);
- menu_ctx.main_prompt = nmc_colorize (nmc->nmc_config.use_colors, NM_META_TERM_COLOR_NORMAL, NM_META_TERM_FORMAT_NORMAL,
+ menu_ctx.main_prompt = nmc_colorize (&nmc->nmc_config, NM_META_TERM_COLOR_NORMAL, NM_META_TERM_FORMAT_NORMAL,
BASE_PROMPT);
/* Get remote connection */
@@ -7041,7 +7041,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t
nmc_tab_completion.setting = setting;
/* Switch to level 1 */
- menu_switch_to_level1 (nmc->nmc_config.use_colors, &menu_ctx, setting, setting_name, NM_META_TERM_COLOR_NORMAL);
+ menu_switch_to_level1 (&nmc->nmc_config, &menu_ctx, setting, setting_name, NM_META_TERM_COLOR_NORMAL);
if (!cmd_arg_s) {
g_print (_("You may edit the following properties: %s\n"), menu_ctx.valid_props_str);
@@ -7123,7 +7123,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t
connection_remove_setting (connection, ss);
if (ss == menu_ctx.curr_setting) {
/* If we removed the setting we are in, go up */
- menu_switch_to_level0 (nmc->nmc_config.use_colors, &menu_ctx, BASE_PROMPT, NM_META_TERM_COLOR_NORMAL);
+ menu_switch_to_level0 (&nmc->nmc_config, &menu_ctx, BASE_PROMPT, NM_META_TERM_COLOR_NORMAL);
nmc_tab_completion.setting = NULL; /* for TAB completion */
}
} else {
@@ -7151,7 +7151,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t
/* coverity[copy_paste_error] - suppress Coverity COPY_PASTE_ERROR defect */
if (ss == menu_ctx.curr_setting) {
/* If we removed the setting we are in, go up */
- menu_switch_to_level0 (nmc->nmc_config.use_colors, &menu_ctx, BASE_PROMPT, NM_META_TERM_COLOR_NORMAL);
+ menu_switch_to_level0 (&nmc->nmc_config, &menu_ctx, BASE_PROMPT, NM_META_TERM_COLOR_NORMAL);
nmc_tab_completion.setting = NULL; /* for TAB completion */
}
} else
@@ -7507,7 +7507,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t
case NMC_EDITOR_MAIN_CMD_BACK:
/* Go back (up) an the menu */
if (menu_ctx.level == 1) {
- menu_switch_to_level0 (nmc->nmc_config.use_colors, &menu_ctx, BASE_PROMPT, NM_META_TERM_COLOR_NORMAL);
+ menu_switch_to_level0 (&nmc->nmc_config, &menu_ctx, BASE_PROMPT, NM_META_TERM_COLOR_NORMAL);
nmc_tab_completion.setting = NULL; /* for TAB completion */
}
break;
diff --git a/clients/cli/devices.c b/clients/cli/devices.c
index 5ad546a28a..607339dde0 100644
--- a/clients/cli/devices.c
+++ b/clients/cli/devices.c
@@ -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 - 2018 Red Hat, Inc.
*/
#include "nm-default.h"
@@ -2411,7 +2411,7 @@ device_state (NMDevice *device, GParamSpec *pspec, NmCli *nmc)
char *str;
nmc_device_state_to_color (state, &color, &color_fmt);
- str = nmc_colorize (nmc->nmc_config.use_colors, color, color_fmt, "%s: %s\n",
+ str = nmc_colorize (&nmc->nmc_config, color, color_fmt, "%s: %s\n",
nm_device_get_iface (device),
nmc_device_state_to_string (state));
diff --git a/clients/cli/general.c b/clients/cli/general.c
index 17c0d79476..437ac9d165 100644
--- a/clients/cli/general.c
+++ b/clients/cli/general.c
@@ -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 - 2017 Red Hat, Inc.
+ * Copyright 2010 - 2018 Red Hat, Inc.
*/
#include "nm-default.h"
@@ -1086,7 +1086,7 @@ networkmanager_running (NMClient *client, GParamSpec *param, NmCli *nmc)
char *str;
running = nm_client_get_nm_running (client);
- str = nmc_colorize (nmc->nmc_config.use_colors,
+ str = nmc_colorize (&nmc->nmc_config,
running ? NM_META_TERM_COLOR_GREEN : NM_META_TERM_COLOR_RED,
NM_META_TERM_FORMAT_NORMAL,
running ? _("NetworkManager has started") : _("NetworkManager has stopped"));
@@ -1128,7 +1128,7 @@ client_connectivity (NMClient *client, GParamSpec *param, NmCli *nmc)
char *str;
g_object_get (client, NM_CLIENT_CONNECTIVITY, &connectivity, NULL);
- str = nmc_colorize (nmc->nmc_config.use_colors, connectivity_to_color (connectivity), NM_META_TERM_FORMAT_NORMAL,
+ str = nmc_colorize (&nmc->nmc_config, connectivity_to_color (connectivity), NM_META_TERM_FORMAT_NORMAL,
_("Connectivity is now '%s'\n"), nm_connectivity_to_string (connectivity));
g_print ("%s", str);
g_free (str);
@@ -1141,7 +1141,7 @@ client_state (NMClient *client, GParamSpec *param, NmCli *nmc)
char *str;
g_object_get (client, NM_CLIENT_STATE, &state, NULL);
- str = nmc_colorize (nmc->nmc_config.use_colors, state_to_color (state), NM_META_TERM_FORMAT_NORMAL,
+ str = nmc_colorize (&nmc->nmc_config, state_to_color (state), NM_META_TERM_FORMAT_NORMAL,
_("Networkmanager is now in the '%s' state\n"),
nm_state_to_string (state));
g_print ("%s", str);
@@ -1184,12 +1184,12 @@ device_overview (NmCli *nmc, NMDevice *device)
if (!nm_device_get_autoconnect (device))
g_string_append_printf (outbuf, "%s, ", _("autoconnect"));
if (nm_device_get_firmware_missing (device)) {
- tmp = nmc_colorize (nmc->nmc_config.use_colors, NM_META_TERM_COLOR_RED, NM_META_TERM_FORMAT_NORMAL, _("fw missing"));
+ tmp = nmc_colorize (&nmc->nmc_config, NM_META_TERM_COLOR_RED, NM_META_TERM_FORMAT_NORMAL, _("fw missing"));
g_string_append_printf (outbuf, "%s, ", tmp);
g_free (tmp);
}
if (nm_device_get_nm_plugin_missing (device)) {
- tmp = nmc_colorize (nmc->nmc_config.use_colors, NM_META_TERM_COLOR_RED, NM_META_TERM_FORMAT_NORMAL, _("plugin missing"));
+ tmp = nmc_colorize (&nmc->nmc_config, NM_META_TERM_COLOR_RED, NM_META_TERM_FORMAT_NORMAL, _("plugin missing"));
g_string_append_printf (outbuf, "%s, ", tmp);
g_free (tmp);
}
@@ -1315,7 +1315,7 @@ do_overview (NmCli *nmc, int argc, char **argv)
state = nm_active_connection_get_state (ac);
nmc_active_connection_state_to_color (state, &color);
- tmp = nmc_colorize (nmc->nmc_config.use_colors, color, NM_META_TERM_FORMAT_NORMAL, _("%s VPN connection"),
+ tmp = nmc_colorize (&nmc->nmc_config, color, NM_META_TERM_FORMAT_NORMAL, _("%s VPN connection"),
nm_active_connection_get_id (ac));
g_print ("%s\n", tmp);
g_free (tmp);
@@ -1333,7 +1333,7 @@ do_overview (NmCli *nmc, int argc, char **argv)
state = nm_device_get_state (devices[i]);
nmc_device_state_to_color (state, &color, &color_fmt);
- tmp = nmc_colorize (nmc->nmc_config.use_colors, color, color_fmt, "%s: %s%s%s",
+ tmp = nmc_colorize (&nmc->nmc_config, color, color_fmt, "%s: %s%s%s",
nm_device_get_iface (devices[i]),
nmc_device_state_to_string (state),
ac ? " to " : "",
@@ -1416,7 +1416,7 @@ do_monitor (NmCli *nmc, int argc, char **argv)
if (!nm_client_get_nm_running (nmc->client)) {
char *str;
- str = nmc_colorize (nmc->nmc_config.use_colors, NM_META_TERM_COLOR_RED, NM_META_TERM_FORMAT_NORMAL,
+ str = nmc_colorize (&nmc->nmc_config, NM_META_TERM_COLOR_RED, NM_META_TERM_FORMAT_NORMAL,
_("Networkmanager is not running (waiting for it)\n"));
g_print ("%s", str);
g_free (str);
diff --git a/clients/cli/nmcli.c b/clients/cli/nmcli.c
index 1fa9cc6cb5..fe9e1c12fb 100644
--- a/clients/cli/nmcli.c
+++ b/clients/cli/nmcli.c
@@ -72,7 +72,6 @@ NmCli nm_cli = {
.ask = FALSE,
.complete = FALSE,
.nmc_config.show_secrets = FALSE,
- .nmc_config.use_colors = NMC_USE_COLOR_AUTO,
.nmc_config.in_editor = FALSE,
.editor_status_line = FALSE,
.editor_save_confirmation = TRUE,
@@ -294,9 +293,36 @@ matches_arg (NmCli *nmc, int *argc, char ***argv, const char *pattern, char **ar
return TRUE;
}
+typedef enum {
+ NMC_USE_COLOR_AUTO,
+ NMC_USE_COLOR_YES,
+ NMC_USE_COLOR_NO,
+} NmcColorOption;
+
+static void
+set_colors (NmCli *nmc, NmcColorOption *color_option)
+{
+ if (*color_option == NMC_USE_COLOR_AUTO) {
+ if ( g_strcmp0 (g_getenv ("TERM"), "dumb") == 0
+ || !isatty (STDOUT_FILENO))
+ *color_option = NMC_USE_COLOR_NO;
+ }
+
+ switch (*color_option) {
+ case NMC_USE_COLOR_YES:
+ case NMC_USE_COLOR_AUTO:
+ nmc->nmc_config_mutable.use_colors = TRUE;
+ break;
+ case NMC_USE_COLOR_NO:
+ nmc->nmc_config_mutable.use_colors = FALSE;
+ break;
+ }
+}
+
static gboolean
process_command_line (NmCli *nmc, int argc, char **argv)
{
+ NmcColorOption colors = NMC_USE_COLOR_AUTO;
char *base;
base = strrchr (argv[0], '/');
@@ -376,11 +402,11 @@ process_command_line (NmCli *nmc, int argc, char **argv)
if (argc == 1 && nmc->complete)
complete_option_with_value (argv[0], value, "yes", "no", "auto", NULL);
if (matches (value, "auto"))
- nmc->nmc_config_mutable.use_colors = NMC_USE_COLOR_AUTO;
+ colors = NMC_USE_COLOR_AUTO;
else if (matches (value, "yes"))
- nmc->nmc_config_mutable.use_colors = NMC_USE_COLOR_YES;
+ colors = NMC_USE_COLOR_YES;
else if (matches (value, "no"))
- nmc->nmc_config_mutable.use_colors = NMC_USE_COLOR_NO;
+ colors = NMC_USE_COLOR_NO;
else {
g_string_printf (nmc->return_text, _("Error: '%s' is not valid argument for '%s' option."), value, argv[0]);
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
@@ -446,6 +472,8 @@ process_command_line (NmCli *nmc, int argc, char **argv)
if (nmc->required_fields)
nmc->nmc_config_mutable.overview = FALSE;
+ set_colors (nmc, &colors);
+
/* Now run the requested command */
nmc_do_cmd (nmc, nmcli_cmds, *argv, argc, argv);
diff --git a/clients/cli/nmcli.h b/clients/cli/nmcli.h
index 768479855a..66a1122f83 100644
--- a/clients/cli/nmcli.h
+++ b/clients/cli/nmcli.h
@@ -97,15 +97,9 @@ struct _NmcOutputField {
NMMetaTermFormat color_fmt; /* Use this terminal format to print value */
};
-typedef enum {
- NMC_USE_COLOR_AUTO,
- NMC_USE_COLOR_YES,
- NMC_USE_COLOR_NO,
-} NmcColorOption;
-
typedef struct _NmcConfig {
NMCPrintOutput print_output; /* Output mode */
- NmcColorOption use_colors; /* Whether to use colors for output: option '--color' */
+ gboolean use_colors; /* Whether to use colors for output: option '--color' */
bool multiline_output; /* Multiline output instead of default tabular */
bool escape_values; /* Whether to escape ':' and '\' in terse tabular mode */
bool in_editor; /* Whether running the editor - nmcli con edit' */
diff --git a/clients/cli/utils.c b/clients/cli/utils.c
index 79fb81dd02..f1cd222da1 100644
--- a/clients/cli/utils.c
+++ b/clients/cli/utils.c
@@ -119,27 +119,8 @@ const NMMetaType nmc_meta_type_generic_info = {
/*****************************************************************************/
-static gboolean
-use_colors (NmcColorOption color_option)
-{
- if (color_option == NMC_USE_COLOR_AUTO) {
- static NmcColorOption cached = NMC_USE_COLOR_AUTO;
-
- if (G_UNLIKELY (cached == NMC_USE_COLOR_AUTO)) {
- if ( g_strcmp0 (g_getenv ("TERM"), "dumb") == 0
- || !isatty (STDOUT_FILENO))
- cached = NMC_USE_COLOR_NO;
- else
- cached = NMC_USE_COLOR_YES;
- }
- return cached == NMC_USE_COLOR_YES;
- }
-
- return color_option == NMC_USE_COLOR_YES;
-}
-
static const char *
-colorize_string (NmcColorOption color_option,
+colorize_string (const NmcConfig *nmc_config,
NMMetaTermColor color,
NMMetaTermFormat color_fmt,
const char *str,
@@ -147,9 +128,10 @@ colorize_string (NmcColorOption color_option,
{
const char *out = str;
- if ( use_colors (color_option)
+ if ( nmc_config
+ && nmc_config->use_colors
&& (color != NM_META_TERM_COLOR_NORMAL || color_fmt != NM_META_TERM_FORMAT_NORMAL)) {
- *out_to_free = nmc_colorize (color_option, color, color_fmt, "%s", str);
+ *out_to_free = nmc_colorize (nmc_config, color, color_fmt, "%s", str);
out = *out_to_free;
}
@@ -459,7 +441,7 @@ nmc_term_format_sequence (NMMetaTermFormat format)
}
char *
-nmc_colorize (NmcColorOption color_option, NMMetaTermColor color, NMMetaTermFormat format, const char *fmt, ...)
+nmc_colorize (const NmcConfig *nmc_config, NMMetaTermColor color, NMMetaTermFormat format, const char *fmt, ...)
{
va_list args;
char *str, *colored;
@@ -470,7 +452,7 @@ nmc_colorize (NmcColorOption color_option, NMMetaTermColor color, NMMetaTermForm
str = g_strdup_vprintf (fmt, args);
va_end (args);
- if (!use_colors (color_option))
+ if (!nmc_config->use_colors)
return str;
ansi_color = nmc_term_color_sequence (color);
@@ -1351,7 +1333,7 @@ _print_do (const NmcConfig *nmc_config,
gs_free char *text_to_free = NULL;
const char *text;
- text = colorize_string (nmc_config->use_colors,
+ text = colorize_string (nmc_config,
cell->term_color, cell->term_format,
lines[i_lines], &text_to_free);
if (multiline) {
@@ -1482,7 +1464,7 @@ nmc_terminal_spawn_pager (const NmcConfig *nmc_config)
if ( nm_cli.nmc_config.in_editor
|| nm_cli.pager_pid > 0
|| nmc_config->print_output == NMC_PRINT_TERSE
- || !use_colors (nmc_config->use_colors)
+ || !nmc_config->use_colors
|| g_strcmp0 (pager, "") == 0
|| getauxval (AT_SECURE))
return;
@@ -1553,7 +1535,7 @@ nmc_terminal_spawn_pager (const NmcConfig *nmc_config)
/*****************************************************************************/
static const char *
-get_value_to_print (NmcColorOption color_option,
+get_value_to_print (const NmcConfig *nmc_config,
const NmcOutputField *field,
gboolean field_name,
const char *not_set_str,
@@ -1579,7 +1561,7 @@ get_value_to_print (NmcColorOption color_option,
}
/* colorize the value */
- out = colorize_string (color_option, field->color, field->color_fmt, value, out_to_free);
+ out = colorize_string (nmc_config, field->color, field->color_fmt, value, out_to_free);
if (out && out == free_value) {
nm_assert (!*out_to_free);
@@ -1673,7 +1655,7 @@ print_required_fields (const NmcConfig *nmc_config,
gs_free char *tmp = NULL;
val = *p ?: not_set_str;
- print_val = colorize_string (nmc_config->use_colors, field_values[idx].color, field_values[idx].color_fmt,
+ print_val = colorize_string (nmc_config, field_values[idx].color, field_values[idx].color_fmt,
val, &val_to_free);
tmp = g_strdup_printf ("%s%s%s[%d]:",
section_prefix ? (const char*) field_values[0].value : "",
@@ -1694,7 +1676,7 @@ print_required_fields (const NmcConfig *nmc_config,
/* value is a string */
val = val && *val ? val : not_set_str;
- print_val = colorize_string (nmc_config->use_colors, field_values[idx].color, field_values[idx].color_fmt,
+ print_val = colorize_string (nmc_config, field_values[idx].color, field_values[idx].color_fmt,
val, &val_to_free);
tmp = g_strdup_printf ("%s%s%s:",
section_prefix ? hdr_name : "",
@@ -1725,7 +1707,7 @@ print_required_fields (const NmcConfig *nmc_config,
idx = g_array_index (indices, int, i);
- value = get_value_to_print (nmc_config->use_colors, (NmcOutputField *) field_values+idx, field_names,
+ value = get_value_to_print (nmc_config, (NmcOutputField *) field_values+idx, field_names,
not_set_str, &val_to_free);
if (terse) {
@@ -1798,7 +1780,7 @@ print_data_prepare_width (GPtrArray *output_data)
row = g_ptr_array_index (output_data, j);
field_names = row[0].flags & NMC_OF_FLAG_FIELD_NAMES;
- value = get_value_to_print (NMC_USE_COLOR_NO, row+i, field_names, "--", &val_to_free);
+ value = get_value_to_print (NULL, row+i, field_names, "--", &val_to_free);
len = nmc_string_screen_width (value, NULL);
max_width = len > max_width ? len : max_width;
}
diff --git a/clients/cli/utils.h b/clients/cli/utils.h
index 09180bf912..e73973baec 100644
--- a/clients/cli/utils.h
+++ b/clients/cli/utils.h
@@ -41,10 +41,9 @@ char *ssid_to_hex (const char *str, gsize len);
void nmc_terminal_erase_line (void);
void nmc_terminal_show_progress (const char *str);
void nmc_terminal_spawn_pager (const NmcConfig *nmc_config);
-gboolean nmc_term_use_colors (NmcColorOption color_option);
const char *nmc_term_color_sequence (NMMetaTermColor color);
const char *nmc_term_format_sequence (NMMetaTermFormat format);
-char *nmc_colorize (NmcColorOption color_option, NMMetaTermColor color, NMMetaTermFormat format, const char * fmt, ...) _nm_printf (4, 5);
+char *nmc_colorize (const NmcConfig *nmc_config, NMMetaTermColor color, NMMetaTermFormat format, const char * fmt, ...) _nm_printf (4, 5);
void nmc_filter_out_colors_inplace (char *str);
char *nmc_filter_out_colors (const char *str);
char *nmc_get_user_input (const char *ask_str);