cli: introduce NmcColorPalette struct instead of plain array

We do have types in C. Use them.
This commit is contained in:
Thomas Haller
2020-10-08 12:33:16 +02:00
parent b7da3da7ac
commit 4e966a7f36
3 changed files with 85 additions and 49 deletions

View File

@@ -33,26 +33,48 @@
#define NMCLI_VERSION VERSION #define NMCLI_VERSION VERSION
#endif #endif
#define DEFAULT_PALETTE_INIT \ #define _NMC_COLOR_PALETTE_INIT() \
[NM_META_COLOR_CONNECTION_ACTIVATED] = "32", [NM_META_COLOR_CONNECTION_ACTIVATING] = "33", \ { \
[NM_META_COLOR_CONNECTION_DISCONNECTING] = "31", [NM_META_COLOR_CONNECTION_INVISIBLE] = "2", \ .ansi_seq = { \
[NM_META_COLOR_CONNECTION_EXTERNAL] = "32;2", [NM_META_COLOR_CONNECTIVITY_FULL] = "32", \ [NM_META_COLOR_CONNECTION_ACTIVATED] = "32", \
[NM_META_COLOR_CONNECTIVITY_LIMITED] = "33", [NM_META_COLOR_CONNECTIVITY_NONE] = "31", \ [NM_META_COLOR_CONNECTION_ACTIVATING] = "33", \
[NM_META_COLOR_CONNECTIVITY_PORTAL] = "33", [NM_META_COLOR_DEVICE_ACTIVATED] = "32", \ [NM_META_COLOR_CONNECTION_DISCONNECTING] = "31", \
[NM_META_COLOR_DEVICE_ACTIVATING] = "33", [NM_META_COLOR_DEVICE_DISCONNECTED] = "31", \ [NM_META_COLOR_CONNECTION_INVISIBLE] = "2", \
[NM_META_COLOR_DEVICE_FIRMWARE_MISSING] = "31", [NM_META_COLOR_DEVICE_PLUGIN_MISSING] = "31", \ [NM_META_COLOR_CONNECTION_EXTERNAL] = "32;2", \
[NM_META_COLOR_DEVICE_UNAVAILABLE] = "2", [NM_META_COLOR_DEVICE_DISABLED] = "31", \ [NM_META_COLOR_CONNECTIVITY_FULL] = "32", \
[NM_META_COLOR_DEVICE_EXTERNAL] = "32;2", [NM_META_COLOR_MANAGER_RUNNING] = "32", \ [NM_META_COLOR_CONNECTIVITY_LIMITED] = "33", \
[NM_META_COLOR_MANAGER_STARTING] = "33", [NM_META_COLOR_MANAGER_STOPPED] = "31", \ [NM_META_COLOR_CONNECTIVITY_NONE] = "31", \
[NM_META_COLOR_PERMISSION_AUTH] = "33", [NM_META_COLOR_PERMISSION_NO] = "31", \ [NM_META_COLOR_CONNECTIVITY_PORTAL] = "33", \
[NM_META_COLOR_PERMISSION_YES] = "32", [NM_META_COLOR_STATE_ASLEEP] = "31", \ [NM_META_COLOR_DEVICE_ACTIVATED] = "32", \
[NM_META_COLOR_STATE_CONNECTED_GLOBAL] = "32", [NM_META_COLOR_STATE_CONNECTED_LOCAL] = "32", \ [NM_META_COLOR_DEVICE_ACTIVATING] = "33", \
[NM_META_COLOR_STATE_CONNECTED_SITE] = "32", [NM_META_COLOR_STATE_CONNECTING] = "33", \ [NM_META_COLOR_DEVICE_DISCONNECTED] = "31", \
[NM_META_COLOR_STATE_DISCONNECTED] = "31", [NM_META_COLOR_STATE_DISCONNECTING] = "33", \ [NM_META_COLOR_DEVICE_FIRMWARE_MISSING] = "31", \
[NM_META_COLOR_WIFI_SIGNAL_EXCELLENT] = "32", [NM_META_COLOR_WIFI_SIGNAL_FAIR] = "35", \ [NM_META_COLOR_DEVICE_PLUGIN_MISSING] = "31", \
[NM_META_COLOR_WIFI_SIGNAL_GOOD] = "33", [NM_META_COLOR_WIFI_SIGNAL_POOR] = "36", \ [NM_META_COLOR_DEVICE_UNAVAILABLE] = "2", \
[NM_META_COLOR_WIFI_SIGNAL_UNKNOWN] = "2", [NM_META_COLOR_ENABLED] = "32", \ [NM_META_COLOR_DEVICE_DISABLED] = "31", \
[NM_META_COLOR_DISABLED] = "31", [NM_META_COLOR_DEVICE_EXTERNAL] = "32;2", \
[NM_META_COLOR_MANAGER_RUNNING] = "32", \
[NM_META_COLOR_MANAGER_STARTING] = "33", \
[NM_META_COLOR_MANAGER_STOPPED] = "31", \
[NM_META_COLOR_PERMISSION_AUTH] = "33", \
[NM_META_COLOR_PERMISSION_NO] = "31", \
[NM_META_COLOR_PERMISSION_YES] = "32", \
[NM_META_COLOR_STATE_ASLEEP] = "31", \
[NM_META_COLOR_STATE_CONNECTED_GLOBAL] = "32", \
[NM_META_COLOR_STATE_CONNECTED_LOCAL] = "32", \
[NM_META_COLOR_STATE_CONNECTED_SITE] = "32", \
[NM_META_COLOR_STATE_CONNECTING] = "33", \
[NM_META_COLOR_STATE_DISCONNECTED] = "31", \
[NM_META_COLOR_STATE_DISCONNECTING] = "33", \
[NM_META_COLOR_WIFI_SIGNAL_EXCELLENT] = "32", \
[NM_META_COLOR_WIFI_SIGNAL_FAIR] = "35", \
[NM_META_COLOR_WIFI_SIGNAL_GOOD] = "33", \
[NM_META_COLOR_WIFI_SIGNAL_POOR] = "36", \
[NM_META_COLOR_WIFI_SIGNAL_UNKNOWN] = "2", \
[NM_META_COLOR_ENABLED] = "32", \
[NM_META_COLOR_DISABLED] = "31", \
}, \
}
static NmCli nm_cli = { static NmCli nm_cli = {
.client = NULL, .client = NULL,
@@ -76,7 +98,7 @@ static NmCli nm_cli = {
.complete = FALSE, .complete = FALSE,
.nmc_config.show_secrets = FALSE, .nmc_config.show_secrets = FALSE,
.nmc_config.in_editor = FALSE, .nmc_config.in_editor = FALSE,
.nmc_config.palette = {DEFAULT_PALETTE_INIT}, .nmc_config.palette = _NMC_COLOR_PALETTE_INIT(),
.editor_status_line = FALSE, .editor_status_line = FALSE,
.editor_save_confirmation = TRUE, .editor_save_confirmation = TRUE,
}; };
@@ -565,12 +587,13 @@ static NM_UTILS_STRING_TABLE_LOOKUP_DEFINE(
{"wifi-signal-unknown", NM_META_COLOR_WIFI_SIGNAL_UNKNOWN}, ); {"wifi-signal-unknown", NM_META_COLOR_WIFI_SIGNAL_UNKNOWN}, );
static gboolean static gboolean
parse_color_scheme(char * palette_buffer, parse_color_scheme(char *palette_buffer, NmcColorPalette *out_palette, GError **error)
const char **palette /* _NM_META_COLOR_NUM elements */,
GError ** error)
{ {
char * p = palette_buffer; char *p = palette_buffer;
const char *tmp_palette[_NM_META_COLOR_NUM] = {DEFAULT_PALETTE_INIT};
nm_assert(out_palette);
*out_palette = (NmcColorPalette) _NMC_COLOR_PALETTE_INIT();
/* This reads through the raw color scheme file contents, identifying the /* This reads through the raw color scheme file contents, identifying the
* color names and sequences, putting in terminating NULs in place, so that * color names and sequences, putting in terminating NULs in place, so that
@@ -647,34 +670,45 @@ parse_color_scheme(char * palette_buffer,
continue; continue;
} }
tmp_palette[name_idx] = _resolve_color_alias(color) ?: color; out_palette->ansi_seq[name_idx] = _resolve_color_alias(color) ?: color;
} }
memcpy(palette, tmp_palette, sizeof(tmp_palette));
return TRUE; return TRUE;
} }
static void static void
set_colors(NmcColorOption color_option, set_colors(NmcColorOption color_option,
bool * out_use_colors, bool * out_use_colors,
char ** out_palette_buffer, char ** out_palette_buffer,
const char ** palette /* _NM_META_COLOR_NUM elements */) NmcColorPalette *out_palette)
{ {
gs_free char *palette_str = NULL; gs_free char *palette_str = NULL;
gboolean use_colors; gboolean use_colors;
gboolean palette_set = FALSE;
nm_assert(out_use_colors);
nm_assert(out_palette);
nm_assert(out_palette_buffer && !*out_palette_buffer);
use_colors = check_colors(color_option, &palette_str); use_colors = check_colors(color_option, &palette_str);
*out_use_colors = use_colors; *out_use_colors = use_colors;
if (use_colors && palette_str) {
GError *error = NULL;
if (!parse_color_scheme(palette_str, palette, &error)) { if (use_colors && palette_str) {
gs_free_error GError *error = NULL;
NmcColorPalette palette;
if (!parse_color_scheme(palette_str, &palette, &error))
g_debug("Error parsing color scheme: %s", error->message); g_debug("Error parsing color scheme: %s", error->message);
g_error_free(error); else {
} else
*out_palette_buffer = g_steal_pointer(&palette_str); *out_palette_buffer = g_steal_pointer(&palette_str);
*out_palette = palette;
palette_set = TRUE;
}
} }
if (!palette_set)
*out_palette = (NmcColorPalette) _NMC_COLOR_PALETTE_INIT();
} }
/*************************************************************************************/ /*************************************************************************************/
@@ -873,7 +907,7 @@ process_command_line(NmCli *nmc, int argc, char **argv_orig)
set_colors(colors, set_colors(colors,
&nmc->nmc_config_mutable.use_colors, &nmc->nmc_config_mutable.use_colors,
&nmc->palette_buffer, &nmc->palette_buffer,
nmc->nmc_config_mutable.palette); &nmc->nmc_config_mutable.palette);
/* Now run the requested command */ /* Now run the requested command */
nmc_do_cmd(nmc, nmcli_cmds, *argv, argc, argv); nmc_do_cmd(nmc, nmcli_cmds, *argv, argc, argv);

View File

@@ -73,6 +73,10 @@ typedef enum {
NMC_OF_FLAG_MAIN_HEADER_ONLY = 0x00000008, /* Print main header only */ NMC_OF_FLAG_MAIN_HEADER_ONLY = 0x00000008, /* Print main header only */
} NmcOfFlags; } NmcOfFlags;
typedef struct {
const char *ansi_seq[_NM_META_COLOR_NUM];
} NmcColorPalette;
extern const NMMetaType nmc_meta_type_generic_info; extern const NMMetaType nmc_meta_type_generic_info;
typedef struct _NmcOutputField NmcOutputField; typedef struct _NmcOutputField NmcOutputField;
@@ -96,8 +100,8 @@ typedef struct _NmcConfig {
bool in_editor; /* Whether running the editor - nmcli con edit' */ bool in_editor; /* Whether running the editor - nmcli con edit' */
bool bool
show_secrets; /* Whether to display secrets (both input and output): option '--show-secrets' */ show_secrets; /* Whether to display secrets (both input and output): option '--show-secrets' */
bool overview; /* Overview mode (hide default values) */ bool overview; /* Overview mode (hide default values) */
const char *palette[_NM_META_COLOR_NUM]; /* Color palette */ NmcColorPalette palette;
} NmcConfig; } NmcConfig;
typedef struct { typedef struct {

View File

@@ -374,23 +374,21 @@ nmc_terminal_show_progress(const char *str)
char * char *
nmc_colorize(const NmcConfig *nmc_config, NMMetaColor color, const char *fmt, ...) nmc_colorize(const NmcConfig *nmc_config, NMMetaColor color, const char *fmt, ...)
{ {
va_list args; va_list args;
char * str, *colored; gs_free char *str = NULL;
const char *ansi_seq = NULL; const char * ansi_seq = NULL;
va_start(args, fmt); va_start(args, fmt);
str = g_strdup_vprintf(fmt, args); str = g_strdup_vprintf(fmt, args);
va_end(args); va_end(args);
if (nmc_config->use_colors) if (nmc_config->use_colors)
ansi_seq = nmc_config->palette[color]; ansi_seq = nmc_config->palette.ansi_seq[color];
if (ansi_seq == NULL) if (!ansi_seq)
return str; return g_steal_pointer(&str);
colored = g_strdup_printf("\33[%sm%s\33[0m", ansi_seq, str); return g_strdup_printf("\33[%sm%s\33[0m", ansi_seq, str);
g_free(str);
return colored;
} }
/* /*