nmcli: make filtering color sequences a utility function

The code will be used on other places too.
This commit is contained in:
Jiří Klimeš 2015-02-13 13:25:16 +01:00
parent 40e98f5d68
commit 3e8fc26d25
3 changed files with 45 additions and 19 deletions

View file

@ -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");

View file

@ -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.

View file

@ -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);