summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJiří Klimeš <jklimes@redhat.com>2015-06-10 17:41:53 +0200
committerJiří Klimeš <jklimes@redhat.com>2015-06-11 10:00:54 +0200
commit3641ddb00a19e7b0e094e8d9881221fdb87ce9c0 (patch)
tree013edb0844060063f63199caf8b7605953a9b99b
parent5eba53cd53a25ebf104db2cfba1eddff0aa9edd6 (diff)
downloadNetworkManager-3641ddb00a19e7b0e094e8d9881221fdb87ce9c0.tar.gz
cli: take color name arguments for "nmcli prompt-color" in editor (bgo #744936)
Adjust nmcli prompt-color description and make it more friendly for translators. nmcli> nmcli prompt-color green https://bugzilla.gnome.org/show_bug.cgi?id=744936
-rw-r--r--clients/cli/connections.c32
-rw-r--r--clients/cli/utils.c23
-rw-r--r--clients/cli/utils.h1
3 files changed, 41 insertions, 15 deletions
diff --git a/clients/cli/connections.c b/clients/cli/connections.c
index 28c7cd6006..280cb6f0af 100644
--- a/clients/cli/connections.c
+++ b/clients/cli/connections.c
@@ -6861,10 +6861,15 @@ editor_main_help (const char *command)
case NMC_EDITOR_MAIN_CMD_NMCLI:
g_print (_("nmcli [<conf-option> <value>] :: nmcli configuration\n\n"
"Configures nmcli. The following options are available:\n"
- "status-line yes | no [default: no]\n"
- "save-confirmation yes | no [default: yes]\n"
- "show-secrets yes | no [default: no]\n"
- "prompt-color <0-8> [default: 0]\n"
+ "status-line yes | no [default: no]\n"
+ "save-confirmation yes | no [default: yes]\n"
+ "show-secrets yes | no [default: no]\n"
+ "prompt-color <color> | <0-8> [default: 0]\n"
+ "%s" /* color table description */
+ "\n"
+ "Examples: nmcli> nmcli status-line yes\n"
+ " nmcli> nmcli save-confirmation no\n"
+ " nmcli> nmcli prompt-color 3\n"),
" 0 = normal\n"
" 1 = \33[30mblack\33[0m\n"
" 2 = \33[31mred\33[0m\n"
@@ -6873,11 +6878,7 @@ editor_main_help (const char *command)
" 5 = \33[34mblue\33[0m\n"
" 6 = \33[35mmagenta\33[0m\n"
" 7 = \33[36mcyan\33[0m\n"
- " 8 = \33[37mwhite\33[0m\n"
- "\n"
- "Examples: nmcli> nmcli status-line yes\n"
- " nmcli> nmcli save-confirmation no\n"
- " nmcli> nmcli prompt-color 3\n"));
+ " 8 = \33[37mwhite\33[0m\n");
break;
case NMC_EDITOR_MAIN_CMD_QUIT:
g_print (_("quit :: exit nmcli\n\n"
@@ -8330,12 +8331,13 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t
} else
nmc->editor_show_secrets = bb;
} else if (cmd_arg_p && matches (cmd_arg_p, "prompt-color") == 0) {
- unsigned long color;
- if (!nmc_string_to_uint (cmd_arg_v ? g_strstrip (cmd_arg_v) : "X",
- TRUE, 0, 8, &color))
- g_print (_("Error: bad color number: '%s'; use <0-8>\n"),
- cmd_arg_v ? cmd_arg_v : "");
- else {
+ GError *tmp_err = NULL;
+ NmcTermColor color;
+ color = nmc_term_color_parse_string (cmd_arg_v ? g_strstrip (cmd_arg_v) : " ", &tmp_err);
+ if (tmp_err) {
+ g_print (_("Error: bad color: %s\n"), tmp_err->message);
+ g_clear_error (&tmp_err);
+ } else {
nmc->editor_prompt_color = color;
g_free (menu_ctx.main_prompt);
if (menu_ctx.level == 0)
diff --git a/clients/cli/utils.c b/clients/cli/utils.c
index 575ece7c0c..690ec9bbc2 100644
--- a/clients/cli/utils.c
+++ b/clients/cli/utils.c
@@ -307,6 +307,29 @@ nmc_term_color_sequence (NmcTermColor color)
}
}
+/* Parses @str for color as string or number */
+NmcTermColor
+nmc_term_color_parse_string (const char *str, GError **error)
+{
+ unsigned long color_int;
+ static const char *colors[] = { "normal", "black", "red", "green", "yellow",
+ "blue", "magenta", "cyan", "white", NULL };
+
+ if (nmc_string_to_uint (str, TRUE, 0, 8, &color_int)) {
+ return (NmcTermColor) color_int;
+ } else {
+ const char *color, **p;
+ int i;
+
+ color = nmc_string_is_valid (str, colors, error);
+ for (p = colors, i = 0; *p != NULL; p++, i++) {
+ if (*p == color)
+ return (NmcTermColor) i;
+ }
+ return -1;
+ }
+}
+
const char *
nmc_term_format_sequence (NmcTermFormat format)
{
diff --git a/clients/cli/utils.h b/clients/cli/utils.h
index 54f76b071a..02550f7733 100644
--- a/clients/cli/utils.h
+++ b/clients/cli/utils.h
@@ -75,6 +75,7 @@ void nmc_terminal_erase_line (void);
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 (NmcTermColor color, NmcTermFormat format, const char * fmt, ...);
void nmc_filter_out_colors_inplace (char *str);
char *nmc_filter_out_colors (const char *str);