summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJiří Klimeš <jklimes@redhat.com>2015-02-13 13:25:16 +0100
committerJiří Klimeš <jklimes@redhat.com>2015-02-23 09:24:21 +0100
commit3e8fc26d25a6412295f2fbef5344c612fe7b6279 (patch)
treed4bdf011dc3775f061efa8a6b687e1312d459ba4
parent40e98f5d685bc41da32f65e6ee3b5ad1b46cca3f (diff)
downloadNetworkManager-3e8fc26d25a6412295f2fbef5344c612fe7b6279.tar.gz
nmcli: make filtering color sequences a utility function
The code will be used on other places too.
-rw-r--r--clients/cli/connections.c19
-rw-r--r--clients/cli/utils.c41
-rw-r--r--clients/cli/utils.h4
3 files changed, 45 insertions, 19 deletions
diff --git a/clients/cli/connections.c b/clients/cli/connections.c
index bbe28d6f79..5d5ffc00c0 100644
--- a/clients/cli/connections.c
+++ b/clients/cli/connections.c
@@ -6329,11 +6329,8 @@ nmcli_editor_tab_completion (const char *text, int start, int end)
{
char **match_array = NULL;
const char *line = rl_line_buffer;
- const char *prompt = rl_prompt;
rl_compentry_func_t *generator_func = NULL;
- gboolean copy_char;
- const char *p1;
- char *p2, *prompt_tmp;
+ char *prompt_tmp;
char *word = NULL;
size_t n1;
int num;
@@ -6351,19 +6348,7 @@ nmcli_editor_tab_completion (const char *text, int start, int end)
rl_complete_with_tilde_expansion = 1;
/* Filter out possible ANSI color escape sequences */
- p1 = prompt;
- p2 = prompt_tmp = g_strdup (prompt);
- copy_char = TRUE;
- while (*p1) {
- if (*p1 == '\33')
- copy_char = FALSE;
- if (copy_char)
- *p2++ = *p1;
- if (!copy_char && *p1 == 'm')
- copy_char = TRUE;
- p1++;
- }
- *p2 = '\0';
+ prompt_tmp = nmc_filter_out_colors ((const char *) rl_prompt);
/* Find the first non-space character */
n1 = strspn (line, " \t");
diff --git a/clients/cli/utils.c b/clients/cli/utils.c
index 60cf9f93f5..981743c780 100644
--- a/clients/cli/utils.c
+++ b/clients/cli/utils.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.
*
- * (C) Copyright 2010 - 2014 Red Hat, Inc.
+ * Copyright 2010 - 2015 Red Hat, Inc.
*/
/* Generated configuration file */
@@ -329,6 +329,45 @@ nmc_colorize (NmcTermColor color, const char *fmt, ...)
return colored;
}
+/* Filter out possible ANSI color escape sequences */
+/* It directly modifies the passed string @str. */
+void
+nmc_filter_out_colors_inplace (char *str)
+{
+ const char *p1;
+ char *p2;
+ gboolean copy_char = TRUE;
+
+ if (!str)
+ return;
+
+ p1 = p2 = str;
+ while (*p1) {
+ if (*p1 == '\33' && *(p1+1) == '[')
+ copy_char = FALSE;
+ if (copy_char)
+ *p2++ = *p1;
+ if (!copy_char && *p1 == 'm')
+ copy_char = TRUE;
+ p1++;
+ }
+ *p2 = '\0';
+}
+
+/* Filter out possible ANSI color escape sequences */
+char *
+nmc_filter_out_colors (const char *str)
+{
+ char *filtered;
+
+ if (!str)
+ return NULL;
+
+ filtered = g_strdup (str);
+ nmc_filter_out_colors_inplace (filtered);
+ return filtered;
+}
+
/*
* Convert string to signed integer.
* If required, the resulting number is checked to be in the <min,max> range.
diff --git a/clients/cli/utils.h b/clients/cli/utils.h
index fc43b0ed92..6177f9987f 100644
--- a/clients/cli/utils.h
+++ b/clients/cli/utils.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_UTILS_H
@@ -68,6 +68,8 @@ void nmc_terminal_erase_line (void);
void nmc_terminal_show_progress (const char *str);
const char *nmc_term_color_sequence (NmcTermColor color);
char *nmc_colorize (NmcTermColor color, const char * fmt, ...);
+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);
int nmc_string_to_arg_array (const char *line, const char *delim, char ***argv, int *argc);
const char *nmc_string_is_valid (const char *input, const char **allowed, GError **error);