mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-05-03 00:47:59 +02:00
cli: pass use_colors as separate option instead of global nmc
nmc contains all options and collects output data. It is very hard to understand what it does. Start splitting it up, and pass arguments along -- as needed.
This commit is contained in:
parent
a63c4d0824
commit
9359d5622e
7 changed files with 54 additions and 48 deletions
|
|
@ -6624,7 +6624,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->editor_prompt_color, NMC_TERM_FORMAT_NORMAL,
|
||||
prompt = nmc_colorize (nmc->nmc_config.use_colors, nmc->editor_prompt_color, NMC_TERM_FORMAT_NORMAL,
|
||||
"nmcli %s.%s> ",
|
||||
nm_setting_get_name (curr_setting), prop_name);
|
||||
|
||||
|
|
@ -6960,14 +6960,14 @@ typedef struct {
|
|||
} NmcEditorMenuContext;
|
||||
|
||||
static void
|
||||
menu_switch_to_level0 (NmCli *nmc,
|
||||
menu_switch_to_level0 (NmcColorOption color_option,
|
||||
NmcEditorMenuContext *menu_ctx,
|
||||
const char *prompt,
|
||||
NmcTermColor prompt_color)
|
||||
{
|
||||
menu_ctx->level = 0;
|
||||
g_free (menu_ctx->main_prompt);
|
||||
menu_ctx->main_prompt = nmc_colorize (nmc, prompt_color, NMC_TERM_FORMAT_NORMAL, "%s", prompt);
|
||||
menu_ctx->main_prompt = nmc_colorize (color_option, prompt_color, NMC_TERM_FORMAT_NORMAL, "%s", prompt);
|
||||
menu_ctx->curr_setting = NULL;
|
||||
g_strfreev (menu_ctx->valid_props);
|
||||
menu_ctx->valid_props = NULL;
|
||||
|
|
@ -6976,7 +6976,7 @@ menu_switch_to_level0 (NmCli *nmc,
|
|||
}
|
||||
|
||||
static void
|
||||
menu_switch_to_level1 (NmCli *nmc,
|
||||
menu_switch_to_level1 (NmcColorOption color_option,
|
||||
NmcEditorMenuContext *menu_ctx,
|
||||
NMSetting *setting,
|
||||
const char *setting_name,
|
||||
|
|
@ -6984,7 +6984,7 @@ menu_switch_to_level1 (NmCli *nmc,
|
|||
{
|
||||
menu_ctx->level = 1;
|
||||
g_free (menu_ctx->main_prompt);
|
||||
menu_ctx->main_prompt = nmc_colorize (nmc, prompt_color, NMC_TERM_FORMAT_NORMAL,
|
||||
menu_ctx->main_prompt = nmc_colorize (color_option, prompt_color, NMC_TERM_FORMAT_NORMAL,
|
||||
"nmcli %s> ", setting_name);
|
||||
menu_ctx->curr_setting = setting;
|
||||
g_strfreev (menu_ctx->valid_props);
|
||||
|
|
@ -7030,7 +7030,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t
|
|||
g_print (_("You may edit the following settings: %s\n"), valid_settings_str);
|
||||
|
||||
menu_ctx.level = 0;
|
||||
menu_ctx.main_prompt = nmc_colorize (nmc, nmc->editor_prompt_color, NMC_TERM_FORMAT_NORMAL,
|
||||
menu_ctx.main_prompt = nmc_colorize (nmc->nmc_config.use_colors, nmc->editor_prompt_color, NMC_TERM_FORMAT_NORMAL,
|
||||
BASE_PROMPT);
|
||||
menu_ctx.curr_setting = NULL;
|
||||
menu_ctx.valid_props = NULL;
|
||||
|
|
@ -7208,7 +7208,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, &menu_ctx, setting, setting_name, nmc->editor_prompt_color);
|
||||
menu_switch_to_level1 (nmc->nmc_config.use_colors, &menu_ctx, setting, setting_name, nmc->editor_prompt_color);
|
||||
|
||||
if (!cmd_arg_s) {
|
||||
g_print (_("You may edit the following properties: %s\n"), menu_ctx.valid_props_str);
|
||||
|
|
@ -7289,7 +7289,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, &menu_ctx, BASE_PROMPT, nmc->editor_prompt_color);
|
||||
menu_switch_to_level0 (nmc->nmc_config.use_colors, &menu_ctx, BASE_PROMPT, nmc->editor_prompt_color);
|
||||
nmc_tab_completion.setting = NULL; /* for TAB completion */
|
||||
}
|
||||
} else {
|
||||
|
|
@ -7315,7 +7315,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, &menu_ctx, BASE_PROMPT, nmc->editor_prompt_color);
|
||||
menu_switch_to_level0 (nmc->nmc_config.use_colors, &menu_ctx, BASE_PROMPT, nmc->editor_prompt_color);
|
||||
nmc_tab_completion.setting = NULL; /* for TAB completion */
|
||||
}
|
||||
} else
|
||||
|
|
@ -7666,7 +7666,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, &menu_ctx, BASE_PROMPT, nmc->editor_prompt_color);
|
||||
menu_switch_to_level0 (nmc->nmc_config.use_colors, &menu_ctx, BASE_PROMPT, nmc->editor_prompt_color);
|
||||
nmc_tab_completion.setting = NULL; /* for TAB completion */
|
||||
}
|
||||
break;
|
||||
|
|
@ -7712,10 +7712,10 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t
|
|||
nmc->editor_prompt_color = color;
|
||||
g_free (menu_ctx.main_prompt);
|
||||
if (menu_ctx.level == 0)
|
||||
menu_ctx.main_prompt = nmc_colorize (nmc, nmc->editor_prompt_color, NMC_TERM_FORMAT_NORMAL,
|
||||
menu_ctx.main_prompt = nmc_colorize (nmc->nmc_config.use_colors, nmc->editor_prompt_color, NMC_TERM_FORMAT_NORMAL,
|
||||
BASE_PROMPT);
|
||||
else
|
||||
menu_ctx.main_prompt = nmc_colorize (nmc, nmc->editor_prompt_color, NMC_TERM_FORMAT_NORMAL,
|
||||
menu_ctx.main_prompt = nmc_colorize (nmc->nmc_config.use_colors, nmc->editor_prompt_color, NMC_TERM_FORMAT_NORMAL,
|
||||
"nmcli %s> ",
|
||||
nm_setting_get_name (menu_ctx.curr_setting));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2383,7 +2383,7 @@ device_state (NMDevice *device, GParamSpec *pspec, NmCli *nmc)
|
|||
char *str;
|
||||
|
||||
nmc_device_state_to_color (state, &color, &color_fmt);
|
||||
str = nmc_colorize (nmc, color, color_fmt, "%s: %s\n",
|
||||
str = nmc_colorize (nmc->nmc_config.use_colors, color, color_fmt, "%s: %s\n",
|
||||
nm_device_get_iface (device),
|
||||
nmc_device_state_to_string (state));
|
||||
|
||||
|
|
|
|||
|
|
@ -1011,7 +1011,7 @@ networkmanager_running (NMClient *client, GParamSpec *param, NmCli *nmc)
|
|||
char *str;
|
||||
|
||||
running = nm_client_get_nm_running (client);
|
||||
str = nmc_colorize (nmc,
|
||||
str = nmc_colorize (nmc->nmc_config.use_colors,
|
||||
running ? NMC_TERM_COLOR_GREEN : NMC_TERM_COLOR_RED,
|
||||
NMC_TERM_FORMAT_NORMAL,
|
||||
running ? _("NetworkManager has started") : _("NetworkManager has stopped"));
|
||||
|
|
@ -1053,7 +1053,7 @@ client_connectivity (NMClient *client, GParamSpec *param, NmCli *nmc)
|
|||
char *str;
|
||||
|
||||
g_object_get (client, NM_CLIENT_CONNECTIVITY, &connectivity, NULL);
|
||||
str = nmc_colorize (nmc, connectivity_to_color (connectivity), NMC_TERM_FORMAT_NORMAL,
|
||||
str = nmc_colorize (nmc->nmc_config.use_colors, connectivity_to_color (connectivity), NMC_TERM_FORMAT_NORMAL,
|
||||
_("Connectivity is now '%s'\n"), nm_connectivity_to_string (connectivity));
|
||||
g_print ("%s", str);
|
||||
g_free (str);
|
||||
|
|
@ -1066,7 +1066,7 @@ client_state (NMClient *client, GParamSpec *param, NmCli *nmc)
|
|||
char *str;
|
||||
|
||||
g_object_get (client, NM_CLIENT_STATE, &state, NULL);
|
||||
str = nmc_colorize (nmc, state_to_color (state), NMC_TERM_FORMAT_NORMAL,
|
||||
str = nmc_colorize (nmc->nmc_config.use_colors, state_to_color (state), NMC_TERM_FORMAT_NORMAL,
|
||||
_("Networkmanager is now in the '%s' state\n"),
|
||||
nm_state_to_string (state));
|
||||
g_print ("%s", str);
|
||||
|
|
@ -1110,12 +1110,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_TERM_COLOR_RED, NMC_TERM_FORMAT_NORMAL, _("fw missing"));
|
||||
tmp = nmc_colorize (nmc->nmc_config.use_colors, NMC_TERM_COLOR_RED, NMC_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_TERM_COLOR_RED, NMC_TERM_FORMAT_NORMAL, _("plugin missing"));
|
||||
tmp = nmc_colorize (nmc->nmc_config.use_colors, NMC_TERM_COLOR_RED, NMC_TERM_FORMAT_NORMAL, _("plugin missing"));
|
||||
g_string_append_printf (outbuf, "%s, ", tmp);
|
||||
g_free (tmp);
|
||||
}
|
||||
|
|
@ -1236,7 +1236,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, color, NMC_TERM_FORMAT_NORMAL, _("%s VPN connection"),
|
||||
tmp = nmc_colorize (nmc->nmc_config.use_colors, color, NMC_TERM_FORMAT_NORMAL, _("%s VPN connection"),
|
||||
nm_active_connection_get_id (ac));
|
||||
g_print ("%s\n", tmp);
|
||||
g_free (tmp);
|
||||
|
|
@ -1254,7 +1254,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, color, color_fmt, "%s: %s%s%s",
|
||||
tmp = nmc_colorize (nmc->nmc_config.use_colors, color, color_fmt, "%s: %s%s%s",
|
||||
nm_device_get_iface (devices[i]),
|
||||
nmc_device_state_to_string (state),
|
||||
ac ? " to " : "",
|
||||
|
|
@ -1336,7 +1336,7 @@ do_monitor (NmCli *nmc, int argc, char **argv)
|
|||
if (!nm_client_get_nm_running (nmc->client)) {
|
||||
char *str;
|
||||
|
||||
str = nmc_colorize (nmc, NMC_TERM_COLOR_RED, NMC_TERM_FORMAT_NORMAL,
|
||||
str = nmc_colorize (nmc->nmc_config.use_colors, NMC_TERM_COLOR_RED, NMC_TERM_FORMAT_NORMAL,
|
||||
_("Networkmanager is not running (waiting for it)\n"));
|
||||
g_print ("%s", str);
|
||||
g_free (str);
|
||||
|
|
|
|||
|
|
@ -291,11 +291,11 @@ process_command_line (NmCli *nmc, int argc, char **argv)
|
|||
if (argc == 1 && nmc->complete)
|
||||
nmc_complete_strings (argv[0], "yes", "no", "auto", NULL);
|
||||
if (matches (argv[0], "auto"))
|
||||
nmc->use_colors = NMC_USE_COLOR_AUTO;
|
||||
nmc->nmc_config.use_colors = NMC_USE_COLOR_AUTO;
|
||||
else if (matches (argv[0], "yes"))
|
||||
nmc->use_colors = NMC_USE_COLOR_YES;
|
||||
nmc->nmc_config.use_colors = NMC_USE_COLOR_YES;
|
||||
else if (matches (argv[0], "no"))
|
||||
nmc->use_colors = NMC_USE_COLOR_NO;
|
||||
nmc->nmc_config.use_colors = NMC_USE_COLOR_NO;
|
||||
else {
|
||||
g_string_printf (nmc->return_text, _("Error: '%s' is not valid argument for '%s' option."), argv[0], opt);
|
||||
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
|
||||
|
|
@ -545,7 +545,7 @@ nmc_init (NmCli *nmc)
|
|||
nmc->ask = FALSE;
|
||||
nmc->complete = FALSE;
|
||||
nmc->show_secrets = FALSE;
|
||||
nmc->use_colors = NMC_USE_COLOR_AUTO;
|
||||
nmc->nmc_config.use_colors = NMC_USE_COLOR_AUTO;
|
||||
nmc->in_editor = FALSE;
|
||||
nmc->editor_status_line = FALSE;
|
||||
nmc->editor_save_confirmation = TRUE;
|
||||
|
|
|
|||
|
|
@ -138,6 +138,10 @@ typedef enum {
|
|||
NMC_USE_COLOR_NO,
|
||||
} NmcColorOption;
|
||||
|
||||
typedef struct _NmcConfig {
|
||||
NmcColorOption use_colors; /* Whether to use colors for output: option '--color' */
|
||||
} NmcConfig;
|
||||
|
||||
/* NmCli - main structure */
|
||||
typedef struct _NmCli {
|
||||
NMClient *client; /* Pointer to NMClient of libnm */
|
||||
|
|
@ -156,7 +160,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' */
|
||||
NmcConfig nmc_config;
|
||||
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 */
|
||||
|
|
|
|||
|
|
@ -313,24 +313,26 @@ nmc_term_format_sequence (NmcTermFormat format)
|
|||
}
|
||||
|
||||
static gboolean
|
||||
use_colors (NmCli *nmc)
|
||||
use_colors (NmcColorOption color_option)
|
||||
{
|
||||
if (nmc == NULL)
|
||||
return FALSE;
|
||||
if (color_option == NMC_USE_COLOR_AUTO) {
|
||||
static NmcColorOption cached = NMC_USE_COLOR_AUTO;
|
||||
|
||||
if (nmc->use_colors == NMC_USE_COLOR_AUTO) {
|
||||
if ( g_strcmp0 (g_getenv ("TERM"), "dumb") == 0
|
||||
|| !isatty (fileno (stdout)))
|
||||
nmc->use_colors = NMC_USE_COLOR_NO;
|
||||
else
|
||||
nmc->use_colors = NMC_USE_COLOR_YES;
|
||||
if (G_UNLIKELY (cached == NMC_USE_COLOR_AUTO)) {
|
||||
if ( g_strcmp0 (g_getenv ("TERM"), "dumb") == 0
|
||||
|| !isatty (fileno (stdout)))
|
||||
cached = NMC_USE_COLOR_NO;
|
||||
else
|
||||
cached = NMC_USE_COLOR_YES;
|
||||
}
|
||||
return cached == NMC_USE_COLOR_YES;
|
||||
}
|
||||
|
||||
return nmc->use_colors == NMC_USE_COLOR_YES;
|
||||
return color_option == NMC_USE_COLOR_YES;
|
||||
}
|
||||
|
||||
char *
|
||||
nmc_colorize (NmCli *nmc, NmcTermColor color, NmcTermFormat format, const char *fmt, ...)
|
||||
nmc_colorize (NmcColorOption color_option, NmcTermColor color, NmcTermFormat format, const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
char *str, *colored;
|
||||
|
|
@ -341,7 +343,7 @@ nmc_colorize (NmCli *nmc, NmcTermColor color, NmcTermFormat format, const char *
|
|||
str = g_strdup_vprintf (fmt, args);
|
||||
va_end (args);
|
||||
|
||||
if (!use_colors (nmc))
|
||||
if (!use_colors (color_option))
|
||||
return str;
|
||||
|
||||
ansi_color = nmc_term_color_sequence (color);
|
||||
|
|
@ -801,7 +803,7 @@ nmc_empty_output_fields (NmCli *nmc)
|
|||
}
|
||||
|
||||
static const char *
|
||||
colorize_string (NmCli *nmc,
|
||||
colorize_string (NmcColorOption color_option,
|
||||
NmcTermColor color,
|
||||
NmcTermFormat color_fmt,
|
||||
const char *str,
|
||||
|
|
@ -809,9 +811,9 @@ colorize_string (NmCli *nmc,
|
|||
{
|
||||
const char *out = str;
|
||||
|
||||
if ( use_colors (nmc)
|
||||
if ( use_colors (color_option)
|
||||
&& (color != NMC_TERM_COLOR_NORMAL || color_fmt != NMC_TERM_FORMAT_NORMAL)) {
|
||||
*out_to_free = nmc_colorize (nmc, color, color_fmt, "%s", str);
|
||||
*out_to_free = nmc_colorize (color_option, color, color_fmt, "%s", str);
|
||||
out = *out_to_free;
|
||||
}
|
||||
|
||||
|
|
@ -819,7 +821,7 @@ colorize_string (NmCli *nmc,
|
|||
}
|
||||
|
||||
static const char *
|
||||
get_value_to_print (NmCli *nmc,
|
||||
get_value_to_print (NmcColorOption color_option,
|
||||
NmcOutputField *field,
|
||||
gboolean field_name,
|
||||
const char *not_set_str,
|
||||
|
|
@ -843,7 +845,7 @@ get_value_to_print (NmCli *nmc,
|
|||
free_value = field->value && is_array && !field_name;
|
||||
|
||||
/* colorize the value */
|
||||
out = colorize_string (nmc, field->color, field->color_fmt, value, out_to_free);
|
||||
out = colorize_string (color_option, field->color, field->color_fmt, value, out_to_free);
|
||||
if (*out_to_free) {
|
||||
if (free_value)
|
||||
g_free (value);
|
||||
|
|
@ -936,7 +938,7 @@ print_required_fields (NmCli *nmc, const NmcOutputField field_values[])
|
|||
|
||||
for (p = (const char **) field_values[idx].value, j = 1; p && *p; p++, j++) {
|
||||
val = *p ? *p : not_set_str;
|
||||
print_val = colorize_string (nmc, field_values[idx].color, field_values[idx].color_fmt,
|
||||
print_val = colorize_string (nmc->nmc_config.use_colors, 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 : "",
|
||||
|
|
@ -956,7 +958,7 @@ print_required_fields (NmCli *nmc, const NmcOutputField field_values[])
|
|||
gs_free char *val_to_free = NULL;
|
||||
|
||||
val = val && *val ? val : not_set_str;
|
||||
print_val = colorize_string (nmc, field_values[idx].color, field_values[idx].color_fmt,
|
||||
print_val = colorize_string (nmc->nmc_config.use_colors, field_values[idx].color, field_values[idx].color_fmt,
|
||||
val, &val_to_free);
|
||||
tmp = g_strdup_printf ("%s%s%s:",
|
||||
section_prefix ? hdr_name : "",
|
||||
|
|
@ -984,7 +986,7 @@ print_required_fields (NmCli *nmc, const NmcOutputField field_values[])
|
|||
for (i = 0; i < fields.indices->len; i++) {
|
||||
int idx = g_array_index (fields.indices, int, i);
|
||||
gs_free char *val_to_free = NULL;
|
||||
const char *value = get_value_to_print (nmc, (NmcOutputField *) field_values+idx, field_names,
|
||||
const char *value = get_value_to_print (nmc->nmc_config.use_colors, (NmcOutputField *) field_values+idx, field_names,
|
||||
not_set_str, &val_to_free);
|
||||
|
||||
if (terse) {
|
||||
|
|
@ -1066,7 +1068,7 @@ print_data (NmCli *nmc)
|
|||
|
||||
row = g_ptr_array_index (nmc->output_data, j);
|
||||
field_names = row[0].flags & NMC_OF_FLAG_FIELD_NAMES;
|
||||
value = get_value_to_print (NULL, row+i, field_names, "--", &val_to_free);
|
||||
value = get_value_to_print (NMC_USE_COLOR_NO, row+i, field_names, "--", &val_to_free);
|
||||
len = nmc_string_screen_width (value, NULL);
|
||||
max_width = len > max_width ? len : max_width;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ void nmc_terminal_show_progress (const char *str);
|
|||
const char *nmc_term_color_sequence (NmcTermColor color);
|
||||
const char *nmc_term_format_sequence (NmcTermFormat format);
|
||||
NmcTermColor nmc_term_color_parse_string (const char *str, GError **error);
|
||||
char *nmc_colorize (NmCli *nmc, NmcTermColor color, NmcTermFormat format, const char * fmt, ...) _nm_printf (4, 5);
|
||||
char *nmc_colorize (NmcColorOption color_option, NmcTermColor color, NmcTermFormat 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);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue