cli: use a palette to implement coloring

This basically replaces the (NMMetaTermColor, NMMetaTermFormat) combo
with NMMetaColor that describes the colored element semantically as
opposed to storing the raw attributes.

A (currently static) paletted is used to translate the semantic color
code to the actual ANSI controle sequence. This matches what
terminal-colors.d(5) schemes use, making it convenient to implement
customizable palettes.
This commit is contained in:
Lubomir Rintel
2018-03-29 12:31:33 +02:00
parent 9dfe825840
commit 31aa2cfe29
11 changed files with 207 additions and 272 deletions

View File

@@ -16,7 +16,7 @@
* with this program; if not, write to the Free Software Foundation, Inc., * with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* Copyright 2012 - 2017 Red Hat, Inc. * Copyright 2012 - 2018 Red Hat, Inc.
*/ */
#include "nm-default.h" #include "nm-default.h"
@@ -119,7 +119,7 @@ _metagen_ip4_config_get_fcn (const NMMetaEnvironment *environment,
nm_assert (info->info_type < _NMC_GENERIC_INFO_TYPE_IP4_CONFIG_NUM); nm_assert (info->info_type < _NMC_GENERIC_INFO_TYPE_IP4_CONFIG_NUM);
NMC_HANDLE_TERMFORMAT (NM_META_TERM_COLOR_NORMAL); NMC_HANDLE_COLOR (NM_META_COLOR_NONE);
NM_SET_OUT (out_is_default, TRUE); NM_SET_OUT (out_is_default, TRUE);
switch (info->info_type) { switch (info->info_type) {
@@ -202,7 +202,7 @@ _metagen_ip6_config_get_fcn (const NMMetaEnvironment *environment,
nm_assert (info->info_type < _NMC_GENERIC_INFO_TYPE_IP6_CONFIG_NUM); nm_assert (info->info_type < _NMC_GENERIC_INFO_TYPE_IP6_CONFIG_NUM);
NMC_HANDLE_TERMFORMAT (NM_META_TERM_COLOR_NORMAL); NMC_HANDLE_COLOR (NM_META_COLOR_NONE);
NM_SET_OUT (out_is_default, TRUE); NM_SET_OUT (out_is_default, TRUE);
switch (info->info_type) { switch (info->info_type) {

View File

@@ -811,17 +811,17 @@ found:
return found; return found;
} }
void NMMetaColor
nmc_active_connection_state_to_color (NMActiveConnectionState state, NMMetaTermColor *color) nmc_active_connection_state_to_color (NMActiveConnectionState state)
{ {
*color = NM_META_TERM_COLOR_NORMAL;
if (state == NM_ACTIVE_CONNECTION_STATE_ACTIVATING) if (state == NM_ACTIVE_CONNECTION_STATE_ACTIVATING)
*color = NM_META_TERM_COLOR_YELLOW; return NM_META_COLOR_CONNECTION_ACTIVATING;
else if (state == NM_ACTIVE_CONNECTION_STATE_ACTIVATED) else if (state == NM_ACTIVE_CONNECTION_STATE_ACTIVATED)
*color = NM_META_TERM_COLOR_GREEN; return NM_META_COLOR_CONNECTION_ACTIVATED;
else if (state > NM_ACTIVE_CONNECTION_STATE_ACTIVATED) else if (state > NM_ACTIVE_CONNECTION_STATE_ACTIVATED)
*color = NM_META_TERM_COLOR_RED; return NM_META_COLOR_CONNECTION_DISCONNECTING;
else
return NM_META_COLOR_CONNECTION_UNKNOWN;
} }
/* Essentially a version of nm_setting_connection_get_connection_type() that /* Essentially a version of nm_setting_connection_get_connection_type() that
@@ -864,7 +864,7 @@ fill_output_connection (NMConnection *connection, NMClient *client, NMCPrintOutp
const char *ac_state = NULL; const char *ac_state = NULL;
NMActiveConnectionState ac_state_int = NM_ACTIVE_CONNECTION_STATE_UNKNOWN; NMActiveConnectionState ac_state_int = NM_ACTIVE_CONNECTION_STATE_UNKNOWN;
char *ac_dev = NULL; char *ac_dev = NULL;
NMMetaTermColor color; NMMetaColor color;
s_con = nm_connection_get_setting_connection (connection); s_con = nm_connection_get_setting_connection (connection);
g_assert (s_con); g_assert (s_con);
@@ -893,7 +893,7 @@ fill_output_connection (NMConnection *connection, NMClient *client, NMCPrintOutp
arr = nmc_dup_fields_array ((const NMMetaAbstractInfo *const*) nmc_fields_con_show, 0); arr = nmc_dup_fields_array ((const NMMetaAbstractInfo *const*) nmc_fields_con_show, 0);
/* Show active connections in color */ /* Show active connections in color */
nmc_active_connection_state_to_color (ac_state_int, &color); color = nmc_active_connection_state_to_color (ac_state_int);
set_val_color_all (arr, color); set_val_color_all (arr, color);
set_val_strc (arr, 0, nm_setting_connection_get_id (s_con)); set_val_strc (arr, 0, nm_setting_connection_get_id (s_con));
@@ -944,7 +944,7 @@ fill_output_connection_for_invisible (NMActiveConnection *ac, NMCPrintOutput pri
set_val_strc (arr, 12, ac_path); set_val_strc (arr, 12, ac_path);
set_val_strc (arr, 13, NULL); set_val_strc (arr, 13, NULL);
set_val_color_fmt_all (arr, NM_META_TERM_FORMAT_DIM); set_val_color_all (arr, NM_META_COLOR_CONNECTION_INVISIBLE);
g_ptr_array_add (output_data, arr); g_ptr_array_add (output_data, arr);
} }
@@ -6470,8 +6470,7 @@ property_edit_submenu (NmCli *nmc,
/* Set global variable for use in TAB completion */ /* Set global variable for use in TAB completion */
nmc_tab_completion.property = prop_name; nmc_tab_completion.property = prop_name;
prompt = nmc_colorize (&nmc->nmc_config, NM_META_TERM_COLOR_NORMAL, NM_META_TERM_FORMAT_NORMAL, prompt = nmc_colorize (&nmc->nmc_config, NM_META_COLOR_PROMPT, "nmcli %s.%s> ",
"nmcli %s.%s> ",
nm_setting_get_name (curr_setting), prop_name); nm_setting_get_name (curr_setting), prop_name);
while (cmd_property_loop) { while (cmd_property_loop) {
@@ -6803,12 +6802,11 @@ typedef struct {
static void static void
menu_switch_to_level0 (const NmcConfig *nmc_config, menu_switch_to_level0 (const NmcConfig *nmc_config,
NmcEditorMenuContext *menu_ctx, NmcEditorMenuContext *menu_ctx,
const char *prompt, const char *prompt)
NMMetaTermColor prompt_color)
{ {
menu_ctx->level = 0; menu_ctx->level = 0;
g_free (menu_ctx->main_prompt); g_free (menu_ctx->main_prompt);
menu_ctx->main_prompt = nmc_colorize (nmc_config, prompt_color, NM_META_TERM_FORMAT_NORMAL, "%s", prompt); menu_ctx->main_prompt = nmc_colorize (nmc_config, NM_META_COLOR_PROMPT, "%s", prompt);
menu_ctx->curr_setting = NULL; menu_ctx->curr_setting = NULL;
g_strfreev (menu_ctx->valid_props); g_strfreev (menu_ctx->valid_props);
menu_ctx->valid_props = NULL; menu_ctx->valid_props = NULL;
@@ -6820,13 +6818,11 @@ static void
menu_switch_to_level1 (const NmcConfig *nmc_config, menu_switch_to_level1 (const NmcConfig *nmc_config,
NmcEditorMenuContext *menu_ctx, NmcEditorMenuContext *menu_ctx,
NMSetting *setting, NMSetting *setting,
const char *setting_name, const char *setting_name)
NMMetaTermColor prompt_color)
{ {
menu_ctx->level = 1; menu_ctx->level = 1;
g_free (menu_ctx->main_prompt); g_free (menu_ctx->main_prompt);
menu_ctx->main_prompt = nmc_colorize (nmc_config, prompt_color, NM_META_TERM_FORMAT_NORMAL, menu_ctx->main_prompt = nmc_colorize (nmc_config, NM_META_COLOR_PROMPT, "nmcli %s> ", setting_name);
"nmcli %s> ", setting_name);
menu_ctx->curr_setting = setting; menu_ctx->curr_setting = setting;
g_strfreev (menu_ctx->valid_props); g_strfreev (menu_ctx->valid_props);
menu_ctx->valid_props = nmc_setting_get_valid_properties (menu_ctx->curr_setting); menu_ctx->valid_props = nmc_setting_get_valid_properties (menu_ctx->curr_setting);
@@ -6864,8 +6860,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t
valid_settings_str = get_valid_options_string (valid_settings_main, valid_settings_slave); valid_settings_str = get_valid_options_string (valid_settings_main, valid_settings_slave);
g_print (_("You may edit the following settings: %s\n"), valid_settings_str); g_print (_("You may edit the following settings: %s\n"), valid_settings_str);
menu_ctx.main_prompt = nmc_colorize (&nmc->nmc_config, NM_META_TERM_COLOR_NORMAL, NM_META_TERM_FORMAT_NORMAL, menu_ctx.main_prompt = nmc_colorize (&nmc->nmc_config, NM_META_COLOR_PROMPT, BASE_PROMPT);
BASE_PROMPT);
/* Get remote connection */ /* Get remote connection */
con_tmp = nm_client_get_connection_by_uuid (nmc->client, con_tmp = nm_client_get_connection_by_uuid (nmc->client,
@@ -7041,7 +7036,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t
nmc_tab_completion.setting = setting; nmc_tab_completion.setting = setting;
/* Switch to level 1 */ /* Switch to level 1 */
menu_switch_to_level1 (&nmc->nmc_config, &menu_ctx, setting, setting_name, NM_META_TERM_COLOR_NORMAL); menu_switch_to_level1 (&nmc->nmc_config, &menu_ctx, setting, setting_name);
if (!cmd_arg_s) { if (!cmd_arg_s) {
g_print (_("You may edit the following properties: %s\n"), menu_ctx.valid_props_str); g_print (_("You may edit the following properties: %s\n"), menu_ctx.valid_props_str);
@@ -7123,7 +7118,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t
connection_remove_setting (connection, ss); connection_remove_setting (connection, ss);
if (ss == menu_ctx.curr_setting) { if (ss == menu_ctx.curr_setting) {
/* If we removed the setting we are in, go up */ /* If we removed the setting we are in, go up */
menu_switch_to_level0 (&nmc->nmc_config, &menu_ctx, BASE_PROMPT, NM_META_TERM_COLOR_NORMAL); menu_switch_to_level0 (&nmc->nmc_config, &menu_ctx, BASE_PROMPT);
nmc_tab_completion.setting = NULL; /* for TAB completion */ nmc_tab_completion.setting = NULL; /* for TAB completion */
} }
} else { } else {
@@ -7151,7 +7146,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t
/* coverity[copy_paste_error] - suppress Coverity COPY_PASTE_ERROR defect */ /* coverity[copy_paste_error] - suppress Coverity COPY_PASTE_ERROR defect */
if (ss == menu_ctx.curr_setting) { if (ss == menu_ctx.curr_setting) {
/* If we removed the setting we are in, go up */ /* If we removed the setting we are in, go up */
menu_switch_to_level0 (&nmc->nmc_config, &menu_ctx, BASE_PROMPT, NM_META_TERM_COLOR_NORMAL); menu_switch_to_level0 (&nmc->nmc_config, &menu_ctx, BASE_PROMPT);
nmc_tab_completion.setting = NULL; /* for TAB completion */ nmc_tab_completion.setting = NULL; /* for TAB completion */
} }
} else } else
@@ -7507,7 +7502,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t
case NMC_EDITOR_MAIN_CMD_BACK: case NMC_EDITOR_MAIN_CMD_BACK:
/* Go back (up) an the menu */ /* Go back (up) an the menu */
if (menu_ctx.level == 1) { if (menu_ctx.level == 1) {
menu_switch_to_level0 (&nmc->nmc_config, &menu_ctx, BASE_PROMPT, NM_META_TERM_COLOR_NORMAL); menu_switch_to_level0 (&nmc->nmc_config, &menu_ctx, BASE_PROMPT);
nmc_tab_completion.setting = NULL; /* for TAB completion */ nmc_tab_completion.setting = NULL; /* for TAB completion */
} }
break; break;

View File

@@ -14,7 +14,7 @@
* with this program; if not, write to the Free Software Foundation, Inc., * with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* (C) Copyright 2010 - 2014 Red Hat, Inc. * (C) Copyright 2010 - 2018 Red Hat, Inc.
*/ */
#ifndef NMC_CONNECTIONS_H #ifndef NMC_CONNECTIONS_H
@@ -33,7 +33,7 @@ nmc_read_connection_properties (NmCli *nmc,
char ***argv, char ***argv,
GError **error); GError **error);
void nmc_active_connection_state_to_color (NMActiveConnectionState state, NMMetaTermColor *color); NMMetaColor nmc_active_connection_state_to_color (NMActiveConnectionState state);
extern const NmcMetaGenericInfo *const nmc_fields_con_show[]; extern const NmcMetaGenericInfo *const nmc_fields_con_show[];
extern const NmcMetaGenericInfo *const nmc_fields_con_active_details_general[]; extern const NmcMetaGenericInfo *const nmc_fields_con_active_details_general[];

View File

@@ -707,22 +707,19 @@ sort_access_points (const GPtrArray *aps)
return sorted; return sorted;
} }
static void static NMMetaColor
wifi_signal_to_color (guint8 strength, NMMetaTermColor *color, NMMetaTermFormat *color_fmt) wifi_signal_to_color (guint8 strength)
{ {
*color = NM_META_TERM_COLOR_NORMAL;
*color_fmt = NM_META_TERM_FORMAT_NORMAL;
if (strength > 80) if (strength > 80)
*color = NM_META_TERM_COLOR_GREEN; return NM_META_COLOR_WIFI_SIGNAL_EXCELLENT;
else if (strength > 55) else if (strength > 55)
*color = NM_META_TERM_COLOR_YELLOW; return NM_META_COLOR_WIFI_SIGNAL_GOOD;
else if (strength > 30) else if (strength > 30)
*color = NM_META_TERM_COLOR_MAGENTA; return NM_META_COLOR_WIFI_SIGNAL_FAIR;
else if (strength > 5) else if (strength > 5)
*color = NM_META_TERM_COLOR_CYAN; return NM_META_COLOR_WIFI_SIGNAL_POOR;
else else
*color_fmt = NM_META_TERM_FORMAT_DIM; return NM_META_COLOR_WIFI_SIGNAL_UNKNOWN;
} }
static char * static char *
@@ -795,8 +792,7 @@ fill_output_access_point (gpointer data, gpointer user_data)
GString *security_str; GString *security_str;
char *ap_name; char *ap_name;
const char *sig_bars; const char *sig_bars;
NMMetaTermColor color; NMMetaColor color;
NMMetaTermFormat color_fmt;
if (info->active_bssid) { if (info->active_bssid) {
const char *current_bssid = nm_access_point_get_bssid (ap); const char *current_bssid = nm_access_point_get_bssid (ap);
@@ -882,11 +878,10 @@ fill_output_access_point (gpointer data, gpointer user_data)
set_val_strc (arr, 16, nm_object_get_path (NM_OBJECT (ap))); set_val_strc (arr, 16, nm_object_get_path (NM_OBJECT (ap)));
/* Set colors */ /* Set colors */
wifi_signal_to_color (strength, &color, &color_fmt); color = wifi_signal_to_color (strength);
set_val_color_all (arr, color); set_val_color_all (arr, color);
set_val_color_fmt_all (arr, color_fmt);
if (active) if (active)
arr[15].color = NM_META_TERM_COLOR_GREEN; arr[15].color = NM_META_COLOR_CONNECTION_ACTIVATED;
g_ptr_array_add (info->output_data, arr); g_ptr_array_add (info->output_data, arr);
@@ -1463,20 +1458,19 @@ show_device_info (NMDevice *device, NmCli *nmc)
return TRUE; return TRUE;
} }
void NMMetaColor
nmc_device_state_to_color (NMDeviceState state, NMMetaTermColor *color, NMMetaTermFormat *color_fmt) nmc_device_state_to_color (NMDeviceState state)
{ {
*color = NM_META_TERM_COLOR_NORMAL;
*color_fmt = NM_META_TERM_FORMAT_NORMAL;
if (state <= NM_DEVICE_STATE_UNAVAILABLE) if (state <= NM_DEVICE_STATE_UNAVAILABLE)
*color_fmt= NM_META_TERM_FORMAT_DIM; return NM_META_COLOR_DEVICE_UNAVAILABLE;
else if (state == NM_DEVICE_STATE_DISCONNECTED) else if (state == NM_DEVICE_STATE_DISCONNECTED)
*color = NM_META_TERM_COLOR_RED; return NM_META_COLOR_DEVICE_DISCONNECTED;
else if (state >= NM_DEVICE_STATE_PREPARE && state <= NM_DEVICE_STATE_SECONDARIES) else if (state >= NM_DEVICE_STATE_PREPARE && state <= NM_DEVICE_STATE_SECONDARIES)
*color = NM_META_TERM_COLOR_YELLOW; return NM_META_COLOR_DEVICE_ACTIVATING;
else if (state == NM_DEVICE_STATE_ACTIVATED) else if (state == NM_DEVICE_STATE_ACTIVATED)
*color = NM_META_TERM_COLOR_GREEN; return NM_META_COLOR_DEVICE_ACTIVATED;
g_return_val_if_reached (NM_META_COLOR_DEVICE_UNKNOWN);
} }
static void static void
@@ -1484,8 +1478,7 @@ fill_output_device_status (NMDevice *device, GPtrArray *output_data)
{ {
NMActiveConnection *ac; NMActiveConnection *ac;
NMDeviceState state; NMDeviceState state;
NMMetaTermColor color; NMMetaColor color;
NMMetaTermFormat color_fmt;
NmcOutputField *arr = nmc_dup_fields_array ((const NMMetaAbstractInfo *const*) nmc_fields_dev_status, NmcOutputField *arr = nmc_dup_fields_array ((const NMMetaAbstractInfo *const*) nmc_fields_dev_status,
0); 0);
@@ -1493,9 +1486,8 @@ fill_output_device_status (NMDevice *device, GPtrArray *output_data)
ac = nm_device_get_active_connection (device); ac = nm_device_get_active_connection (device);
/* Show devices in color */ /* Show devices in color */
nmc_device_state_to_color (state, &color, &color_fmt); color = nmc_device_state_to_color (state);
set_val_color_all (arr, color); set_val_color_all (arr, color);
set_val_color_fmt_all (arr, color_fmt);
set_val_strc (arr, 0, nm_device_get_iface (device)); set_val_strc (arr, 0, nm_device_get_iface (device));
set_val_strc (arr, 1, nm_device_get_type_description (device)); set_val_strc (arr, 1, nm_device_get_type_description (device));
@@ -2406,12 +2398,11 @@ static void
device_state (NMDevice *device, GParamSpec *pspec, NmCli *nmc) device_state (NMDevice *device, GParamSpec *pspec, NmCli *nmc)
{ {
NMDeviceState state = nm_device_get_state (device); NMDeviceState state = nm_device_get_state (device);
NMMetaTermColor color; NMMetaColor color;
NMMetaTermFormat color_fmt;
char *str; char *str;
nmc_device_state_to_color (state, &color, &color_fmt); color = nmc_device_state_to_color (state);
str = nmc_colorize (&nmc->nmc_config, color, color_fmt, "%s: %s\n", str = nmc_colorize (&nmc->nmc_config, color, "%s: %s\n",
nm_device_get_iface (device), nm_device_get_iface (device),
nmc_device_state_to_string (state)); nmc_device_state_to_string (state));

View File

@@ -14,7 +14,7 @@
* with this program; if not, write to the Free Software Foundation, Inc., * with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* (C) Copyright 2010 Red Hat, Inc. * (C) Copyright 2010 - 2018 Red Hat, Inc.
*/ */
#ifndef NMC_DEVICES_H #ifndef NMC_DEVICES_H
@@ -32,7 +32,7 @@ void monitor_devices (NmCli *nmc);
NMDevice ** nmc_get_devices_sorted (NMClient *client); NMDevice ** nmc_get_devices_sorted (NMClient *client);
void nmc_device_state_to_color (NMDeviceState state, NMMetaTermColor *color, NMMetaTermFormat *color_fmt); NMMetaColor nmc_device_state_to_color (NMDeviceState state);
extern const NmcMetaGenericInfo *const nmc_fields_dev_status[]; extern const NmcMetaGenericInfo *const nmc_fields_dev_status[];
extern const NmcMetaGenericInfo *const nmc_fields_dev_show_general[]; extern const NmcMetaGenericInfo *const nmc_fields_dev_show_general[];

View File

@@ -55,23 +55,26 @@ nm_state_to_string (NMState state)
return _(nm_state_to_string_no_l10n (state)); return _(nm_state_to_string_no_l10n (state));
} }
static NMMetaTermColor static NMMetaColor
state_to_color (NMState state) state_to_color (NMState state)
{ {
switch (state) { switch (state) {
case NM_STATE_CONNECTING: case NM_STATE_CONNECTING:
return NM_META_TERM_COLOR_YELLOW; return NM_META_COLOR_STATE_CONNECTING;
case NM_STATE_CONNECTED_LOCAL: case NM_STATE_CONNECTED_LOCAL:
return NM_META_COLOR_STATE_CONNECTED_LOCAL;
case NM_STATE_CONNECTED_SITE: case NM_STATE_CONNECTED_SITE:
return NM_META_COLOR_STATE_CONNECTED_SITE;
case NM_STATE_CONNECTED_GLOBAL: case NM_STATE_CONNECTED_GLOBAL:
return NM_META_TERM_COLOR_GREEN; return NM_META_COLOR_STATE_CONNECTED_GLOBAL;
case NM_STATE_DISCONNECTING: case NM_STATE_DISCONNECTING:
return NM_META_TERM_COLOR_YELLOW; return NM_META_COLOR_STATE_DISCONNECTING;
case NM_STATE_ASLEEP: case NM_STATE_ASLEEP:
return NM_META_COLOR_STATE_ASLEEP;
case NM_STATE_DISCONNECTED: case NM_STATE_DISCONNECTED:
return NM_META_TERM_COLOR_RED; return NM_META_COLOR_STATE_DISCONNECTED;
default: default:
return NM_META_TERM_COLOR_NORMAL; return NM_META_COLOR_STATE_UNKNOWN;
} }
} }
@@ -90,19 +93,20 @@ nm_connectivity_to_string (NMConnectivityState connectivity)
return _(nm_connectivity_to_string_no_l10n (connectivity)); return _(nm_connectivity_to_string_no_l10n (connectivity));
} }
static NMMetaTermColor static NMMetaColor
connectivity_to_color (NMConnectivityState connectivity) connectivity_to_color (NMConnectivityState connectivity)
{ {
switch (connectivity) { switch (connectivity) {
case NM_CONNECTIVITY_NONE: case NM_CONNECTIVITY_NONE:
return NM_META_TERM_COLOR_RED; return NM_META_COLOR_CONNECTIVITY_NONE;
case NM_CONNECTIVITY_PORTAL: case NM_CONNECTIVITY_PORTAL:
return NM_META_COLOR_CONNECTIVITY_PORTAL;
case NM_CONNECTIVITY_LIMITED: case NM_CONNECTIVITY_LIMITED:
return NM_META_TERM_COLOR_YELLOW; return NM_META_COLOR_CONNECTIVITY_LIMITED;
case NM_CONNECTIVITY_FULL: case NM_CONNECTIVITY_FULL:
return NM_META_TERM_COLOR_GREEN; return NM_META_COLOR_CONNECTIVITY_FULL;
default: default:
return NM_META_TERM_COLOR_NORMAL; return NM_META_COLOR_CONNECTIVITY_UNKNOWN;
} }
} }
@@ -155,11 +159,11 @@ NM_UTILS_LOOKUP_STR_DEFINE_STATIC (permission_result_to_string_no_l10n, NMClient
NM_UTILS_LOOKUP_ITEM_IGNORE (NM_CLIENT_PERMISSION_RESULT_UNKNOWN), NM_UTILS_LOOKUP_ITEM_IGNORE (NM_CLIENT_PERMISSION_RESULT_UNKNOWN),
); );
_NM_UTILS_LOOKUP_DEFINE (static, permission_result_to_color, NMClientPermissionResult, NMMetaTermColor, _NM_UTILS_LOOKUP_DEFINE (static, permission_result_to_color, NMClientPermissionResult, NMMetaColor,
NM_UTILS_LOOKUP_DEFAULT (NM_META_TERM_COLOR_NORMAL), NM_UTILS_LOOKUP_DEFAULT (NM_META_COLOR_PERMISSION_UNKNOWN),
NM_UTILS_LOOKUP_ITEM (NM_CLIENT_PERMISSION_RESULT_YES, NM_META_TERM_COLOR_GREEN), NM_UTILS_LOOKUP_ITEM (NM_CLIENT_PERMISSION_RESULT_YES, NM_META_COLOR_PERMISSION_YES),
NM_UTILS_LOOKUP_ITEM (NM_CLIENT_PERMISSION_RESULT_NO, NM_META_TERM_COLOR_RED), NM_UTILS_LOOKUP_ITEM (NM_CLIENT_PERMISSION_RESULT_NO, NM_META_COLOR_PERMISSION_NO),
NM_UTILS_LOOKUP_ITEM (NM_CLIENT_PERMISSION_RESULT_AUTH, NM_META_TERM_COLOR_YELLOW), NM_UTILS_LOOKUP_ITEM (NM_CLIENT_PERMISSION_RESULT_AUTH, NM_META_COLOR_PERMISSION_AUTH),
NM_UTILS_LOOKUP_ITEM_IGNORE (NM_CLIENT_PERMISSION_RESULT_UNKNOWN), NM_UTILS_LOOKUP_ITEM_IGNORE (NM_CLIENT_PERMISSION_RESULT_UNKNOWN),
); );
@@ -186,26 +190,26 @@ _metagen_general_status_get_fcn (const NMMetaEnvironment *environment,
switch (info->info_type) { switch (info->info_type) {
case NMC_GENERIC_INFO_TYPE_GENERAL_STATUS_RUNNING: case NMC_GENERIC_INFO_TYPE_GENERAL_STATUS_RUNNING:
NMC_HANDLE_TERMFORMAT (NM_META_TERM_COLOR_NORMAL); NMC_HANDLE_COLOR (NM_META_COLOR_NONE);
value = N_("running"); value = N_("running");
goto translate_and_out; goto translate_and_out;
case NMC_GENERIC_INFO_TYPE_GENERAL_STATUS_VERSION: case NMC_GENERIC_INFO_TYPE_GENERAL_STATUS_VERSION:
NMC_HANDLE_TERMFORMAT (NM_META_TERM_COLOR_NORMAL); NMC_HANDLE_COLOR (NM_META_COLOR_NONE);
value = nm_client_get_version (nmc->client); value = nm_client_get_version (nmc->client);
goto clone_and_out; goto clone_and_out;
case NMC_GENERIC_INFO_TYPE_GENERAL_STATUS_STATE: case NMC_GENERIC_INFO_TYPE_GENERAL_STATUS_STATE:
state = nm_client_get_state (nmc->client); state = nm_client_get_state (nmc->client);
NMC_HANDLE_TERMFORMAT (state_to_color (state)); NMC_HANDLE_COLOR (state_to_color (state));
value = nm_state_to_string_no_l10n (state); value = nm_state_to_string_no_l10n (state);
goto translate_and_out; goto translate_and_out;
case NMC_GENERIC_INFO_TYPE_GENERAL_STATUS_STARTUP: case NMC_GENERIC_INFO_TYPE_GENERAL_STATUS_STARTUP:
v_bool = nm_client_get_startup (nmc->client); v_bool = nm_client_get_startup (nmc->client);
NMC_HANDLE_TERMFORMAT (v_bool ? NM_META_TERM_COLOR_YELLOW : NM_META_TERM_COLOR_GREEN); NMC_HANDLE_COLOR (v_bool ? NM_META_COLOR_MANAGER_STARTING : NM_META_COLOR_MANAGER_RUNNING);
value = v_bool ? N_("starting") : N_("started"); value = v_bool ? N_("starting") : N_("started");
goto translate_and_out; goto translate_and_out;
case NMC_GENERIC_INFO_TYPE_GENERAL_STATUS_CONNECTIVITY: case NMC_GENERIC_INFO_TYPE_GENERAL_STATUS_CONNECTIVITY:
connectivity = nm_client_get_connectivity (nmc->client); connectivity = nm_client_get_connectivity (nmc->client);
NMC_HANDLE_TERMFORMAT (connectivity_to_color (connectivity)); NMC_HANDLE_COLOR (connectivity_to_color (connectivity));
value = nm_connectivity_to_string_no_l10n (connectivity); value = nm_connectivity_to_string_no_l10n (connectivity);
goto translate_and_out; goto translate_and_out;
case NMC_GENERIC_INFO_TYPE_GENERAL_STATUS_NETWORKING: case NMC_GENERIC_INFO_TYPE_GENERAL_STATUS_NETWORKING:
@@ -234,7 +238,7 @@ _metagen_general_status_get_fcn (const NMMetaEnvironment *environment,
g_return_val_if_reached (NULL); g_return_val_if_reached (NULL);
enabled_out: enabled_out:
NMC_HANDLE_TERMFORMAT (v_bool ? NM_META_TERM_COLOR_GREEN : NM_META_TERM_COLOR_RED); NMC_HANDLE_COLOR (v_bool ? NM_META_COLOR_ENABLED : NM_META_COLOR_DISABLED);
value = v_bool ? N_("enabled") : N_("disabled"); value = v_bool ? N_("enabled") : N_("disabled");
goto translate_and_out; goto translate_and_out;
@@ -293,11 +297,11 @@ _metagen_general_permissions_get_fcn (const NMMetaEnvironment *environment,
switch (info->info_type) { switch (info->info_type) {
case NMC_GENERIC_INFO_TYPE_GENERAL_PERMISSIONS_PERMISSION: case NMC_GENERIC_INFO_TYPE_GENERAL_PERMISSIONS_PERMISSION:
NMC_HANDLE_TERMFORMAT (NM_META_TERM_COLOR_NORMAL); NMC_HANDLE_COLOR (NM_META_COLOR_NONE);
return permission_to_string (perm); return permission_to_string (perm);
case NMC_GENERIC_INFO_TYPE_GENERAL_PERMISSIONS_VALUE: case NMC_GENERIC_INFO_TYPE_GENERAL_PERMISSIONS_VALUE:
perm_result = nm_client_get_permission_result (nmc->client, perm); perm_result = nm_client_get_permission_result (nmc->client, perm);
NMC_HANDLE_TERMFORMAT (permission_result_to_color (perm_result)); NMC_HANDLE_COLOR (permission_result_to_color (perm_result));
s = permission_result_to_string_no_l10n (perm_result); s = permission_result_to_string_no_l10n (perm_result);
if (get_type == NM_META_ACCESSOR_GET_TYPE_PRETTY) if (get_type == NM_META_ACCESSOR_GET_TYPE_PRETTY)
return _(s); return _(s);
@@ -340,7 +344,7 @@ _metagen_general_logging_get_fcn (const NMMetaEnvironment *environment,
nm_assert (info->info_type < _NMC_GENERIC_INFO_TYPE_GENERAL_LOGGING_NUM); nm_assert (info->info_type < _NMC_GENERIC_INFO_TYPE_GENERAL_LOGGING_NUM);
NMC_HANDLE_TERMFORMAT (NM_META_TERM_COLOR_NORMAL); NMC_HANDLE_COLOR (NM_META_COLOR_NONE);
if (!d->initialized) { if (!d->initialized) {
d->initialized = TRUE; d->initialized = TRUE;
@@ -1087,8 +1091,7 @@ networkmanager_running (NMClient *client, GParamSpec *param, NmCli *nmc)
running = nm_client_get_nm_running (client); running = nm_client_get_nm_running (client);
str = nmc_colorize (&nmc->nmc_config, str = nmc_colorize (&nmc->nmc_config,
running ? NM_META_TERM_COLOR_GREEN : NM_META_TERM_COLOR_RED, running ? NM_META_COLOR_MANAGER_RUNNING : NM_META_COLOR_MANAGER_STOPPED,
NM_META_TERM_FORMAT_NORMAL,
running ? _("NetworkManager has started") : _("NetworkManager has stopped")); running ? _("NetworkManager has started") : _("NetworkManager has stopped"));
g_print ("%s\n", str); g_print ("%s\n", str);
g_free (str); g_free (str);
@@ -1128,7 +1131,7 @@ client_connectivity (NMClient *client, GParamSpec *param, NmCli *nmc)
char *str; char *str;
g_object_get (client, NM_CLIENT_CONNECTIVITY, &connectivity, NULL); g_object_get (client, NM_CLIENT_CONNECTIVITY, &connectivity, NULL);
str = nmc_colorize (&nmc->nmc_config, connectivity_to_color (connectivity), NM_META_TERM_FORMAT_NORMAL, str = nmc_colorize (&nmc->nmc_config, connectivity_to_color (connectivity),
_("Connectivity is now '%s'\n"), nm_connectivity_to_string (connectivity)); _("Connectivity is now '%s'\n"), nm_connectivity_to_string (connectivity));
g_print ("%s", str); g_print ("%s", str);
g_free (str); g_free (str);
@@ -1141,7 +1144,7 @@ client_state (NMClient *client, GParamSpec *param, NmCli *nmc)
char *str; char *str;
g_object_get (client, NM_CLIENT_STATE, &state, NULL); g_object_get (client, NM_CLIENT_STATE, &state, NULL);
str = nmc_colorize (&nmc->nmc_config, state_to_color (state), NM_META_TERM_FORMAT_NORMAL, str = nmc_colorize (&nmc->nmc_config, state_to_color (state),
_("Networkmanager is now in the '%s' state\n"), _("Networkmanager is now in the '%s' state\n"),
nm_state_to_string (state)); nm_state_to_string (state));
g_print ("%s", str); g_print ("%s", str);
@@ -1184,12 +1187,12 @@ device_overview (NmCli *nmc, NMDevice *device)
if (!nm_device_get_autoconnect (device)) if (!nm_device_get_autoconnect (device))
g_string_append_printf (outbuf, "%s, ", _("autoconnect")); g_string_append_printf (outbuf, "%s, ", _("autoconnect"));
if (nm_device_get_firmware_missing (device)) { if (nm_device_get_firmware_missing (device)) {
tmp = nmc_colorize (&nmc->nmc_config, NM_META_TERM_COLOR_RED, NM_META_TERM_FORMAT_NORMAL, _("fw missing")); tmp = nmc_colorize (&nmc->nmc_config, NM_META_COLOR_DEVICE_FIRMWARE_MISSING, _("fw missing"));
g_string_append_printf (outbuf, "%s, ", tmp); g_string_append_printf (outbuf, "%s, ", tmp);
g_free (tmp); g_free (tmp);
} }
if (nm_device_get_nm_plugin_missing (device)) { if (nm_device_get_nm_plugin_missing (device)) {
tmp = nmc_colorize (&nmc->nmc_config, NM_META_TERM_COLOR_RED, NM_META_TERM_FORMAT_NORMAL, _("plugin missing")); tmp = nmc_colorize (&nmc->nmc_config, NM_META_COLOR_DEVICE_PLUGIN_MISSING, _("plugin missing"));
g_string_append_printf (outbuf, "%s, ", tmp); g_string_append_printf (outbuf, "%s, ", tmp);
g_free (tmp); g_free (tmp);
} }
@@ -1290,7 +1293,7 @@ do_overview (NmCli *nmc, int argc, char **argv)
NMDevice **devices; NMDevice **devices;
const GPtrArray *p; const GPtrArray *p;
NMActiveConnection *ac; NMActiveConnection *ac;
NMMetaTermColor color; NMMetaColor color;
NMDnsEntry *dns; NMDnsEntry *dns;
char *tmp; char *tmp;
int i; int i;
@@ -1314,8 +1317,8 @@ do_overview (NmCli *nmc, int argc, char **argv)
continue; continue;
state = nm_active_connection_get_state (ac); state = nm_active_connection_get_state (ac);
nmc_active_connection_state_to_color (state, &color); color = nmc_active_connection_state_to_color (state);
tmp = nmc_colorize (&nmc->nmc_config, color, NM_META_TERM_FORMAT_NORMAL, _("%s VPN connection"), tmp = nmc_colorize (&nmc->nmc_config, color, _("%s VPN connection"),
nm_active_connection_get_id (ac)); nm_active_connection_get_id (ac));
g_print ("%s\n", tmp); g_print ("%s\n", tmp);
g_free (tmp); g_free (tmp);
@@ -1326,14 +1329,13 @@ do_overview (NmCli *nmc, int argc, char **argv)
devices = nmc_get_devices_sorted (nmc->client); devices = nmc_get_devices_sorted (nmc->client);
for (i = 0; devices[i]; i++) { for (i = 0; devices[i]; i++) {
NMMetaTermFormat color_fmt;
NMDeviceState state; NMDeviceState state;
ac = nm_device_get_active_connection (devices[i]); ac = nm_device_get_active_connection (devices[i]);
state = nm_device_get_state (devices[i]); state = nm_device_get_state (devices[i]);
nmc_device_state_to_color (state, &color, &color_fmt); color = nmc_device_state_to_color (state);
tmp = nmc_colorize (&nmc->nmc_config, color, color_fmt, "%s: %s%s%s", tmp = nmc_colorize (&nmc->nmc_config, color, "%s: %s%s%s",
nm_device_get_iface (devices[i]), nm_device_get_iface (devices[i]),
nmc_device_state_to_string (state), nmc_device_state_to_string (state),
ac ? " to " : "", ac ? " to " : "",
@@ -1416,7 +1418,7 @@ do_monitor (NmCli *nmc, int argc, char **argv)
if (!nm_client_get_nm_running (nmc->client)) { if (!nm_client_get_nm_running (nmc->client)) {
char *str; char *str;
str = nmc_colorize (&nmc->nmc_config, NM_META_TERM_COLOR_RED, NM_META_TERM_FORMAT_NORMAL, str = nmc_colorize (&nmc->nmc_config, NM_META_COLOR_MANAGER_STOPPED,
_("Networkmanager is not running (waiting for it)\n")); _("Networkmanager is not running (waiting for it)\n"));
g_print ("%s", str); g_print ("%s", str);
g_free (str); g_free (str);

View File

@@ -73,6 +73,42 @@ 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 = {
[NM_META_COLOR_CONNECTION_ACTIVATED] = "32",
[NM_META_COLOR_CONNECTION_ACTIVATING] = "33",
[NM_META_COLOR_CONNECTION_DISCONNECTING] = "31",
[NM_META_COLOR_CONNECTION_INVISIBLE] = "2",
[NM_META_COLOR_CONNECTIVITY_FULL] = "32",
[NM_META_COLOR_CONNECTIVITY_LIMITED] = "33",
[NM_META_COLOR_CONNECTIVITY_NONE] = "31",
[NM_META_COLOR_CONNECTIVITY_PORTAL] = "33",
[NM_META_COLOR_DEVICE_ACTIVATED] = "32",
[NM_META_COLOR_DEVICE_ACTIVATING] = "33",
[NM_META_COLOR_DEVICE_DISCONNECTED] = "31",
[NM_META_COLOR_DEVICE_FIRMWARE_MISSING] = "31",
[NM_META_COLOR_DEVICE_PLUGIN_MISSING] = "31",
[NM_META_COLOR_DEVICE_UNAVAILABLE] = "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",
},
.editor_status_line = FALSE, .editor_status_line = FALSE,
.editor_save_confirmation = TRUE, .editor_save_confirmation = TRUE,
}; };

View File

@@ -93,8 +93,7 @@ struct _NmcOutputField {
gboolean value_is_array; /* Whether value is char** instead of char* */ gboolean value_is_array; /* Whether value is char** instead of char* */
gboolean free_value; /* Whether to free the value */ gboolean free_value; /* Whether to free the value */
NmcOfFlags flags; /* Flags - whether and how to print values/field names/headers */ NmcOfFlags flags; /* Flags - whether and how to print values/field names/headers */
NMMetaTermColor color; /* Use this color to print value */ NMMetaColor color; /* Use this color to print value */
NMMetaTermFormat color_fmt; /* Use this terminal format to print value */
}; };
typedef struct _NmcConfig { typedef struct _NmcConfig {
@@ -105,6 +104,7 @@ typedef struct _NmcConfig {
bool in_editor; /* Whether running the editor - nmcli con edit' */ bool in_editor; /* Whether running the editor - nmcli con edit' */
bool show_secrets; /* Whether to display secrets (both input and output): option '--show-secrets' */ bool 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 */
} NmcConfig; } NmcConfig;
typedef struct _NmcOutputData { typedef struct _NmcOutputData {

View File

@@ -86,11 +86,11 @@ _meta_type_nmc_generic_info_get_fcn (const NMMetaAbstractInfo *abstract_info,
if (!NM_IN_SET (get_type, if (!NM_IN_SET (get_type,
NM_META_ACCESSOR_GET_TYPE_PARSABLE, NM_META_ACCESSOR_GET_TYPE_PARSABLE,
NM_META_ACCESSOR_GET_TYPE_PRETTY, NM_META_ACCESSOR_GET_TYPE_PRETTY,
NM_META_ACCESSOR_GET_TYPE_TERMFORMAT)) NM_META_ACCESSOR_GET_TYPE_COLOR))
g_return_val_if_reached (NULL); g_return_val_if_reached (NULL);
/* omitting the out_to_free value is only allowed for TERMFORMAT. */ /* omitting the out_to_free value is only allowed for COLOR. */
nm_assert (out_to_free || NM_IN_SET (get_type, NM_META_ACCESSOR_GET_TYPE_TERMFORMAT)); nm_assert (out_to_free || NM_IN_SET (get_type, NM_META_ACCESSOR_GET_TYPE_COLOR));
if (info->get_fcn) { if (info->get_fcn) {
return info->get_fcn (environment, environment_user_data, return info->get_fcn (environment, environment_user_data,
@@ -103,7 +103,7 @@ _meta_type_nmc_generic_info_get_fcn (const NMMetaAbstractInfo *abstract_info,
} }
if (info->nested) { if (info->nested) {
NMC_HANDLE_TERMFORMAT (NM_META_TERM_COLOR_NORMAL); NMC_HANDLE_COLOR (NM_META_COLOR_NONE);
return info->name; return info->name;
} }
@@ -121,17 +121,14 @@ const NMMetaType nmc_meta_type_generic_info = {
static const char * static const char *
colorize_string (const NmcConfig *nmc_config, colorize_string (const NmcConfig *nmc_config,
NMMetaTermColor color, NMMetaColor color,
NMMetaTermFormat color_fmt,
const char *str, const char *str,
char **out_to_free) char **out_to_free)
{ {
const char *out = str; const char *out = str;
if ( nmc_config if (nmc_config && nmc_config->use_colors) {
&& nmc_config->use_colors *out_to_free = nmc_colorize (nmc_config, color, "%s", str);
&& (color != NM_META_TERM_COLOR_NORMAL || color_fmt != NM_META_TERM_FORMAT_NORMAL)) {
*out_to_free = nmc_colorize (nmc_config, color, color_fmt, "%s", str);
out = *out_to_free; out = *out_to_free;
} }
@@ -378,89 +375,24 @@ nmc_terminal_show_progress (const char *str)
idx = 0; idx = 0;
} }
const char *
nmc_term_color_sequence (NMMetaTermColor color)
{
switch (color) {
case NM_META_TERM_COLOR_BLACK:
return "\33[30m";
break;
case NM_META_TERM_COLOR_RED:
return "\33[31m";
break;
case NM_META_TERM_COLOR_GREEN:
return "\33[32m";
break;
case NM_META_TERM_COLOR_YELLOW:
return "\33[33m";
break;
case NM_META_TERM_COLOR_BLUE:
return "\33[34m";
break;
case NM_META_TERM_COLOR_MAGENTA:
return "\33[35m";
break;
case NM_META_TERM_COLOR_CYAN:
return "\33[36m";
break;
case NM_META_TERM_COLOR_WHITE:
return "\33[37m";
break;
default:
return "";
break;
}
}
const char *
nmc_term_format_sequence (NMMetaTermFormat format)
{
switch (format) {
case NM_META_TERM_FORMAT_BOLD:
return "\33[1m";
break;
case NM_META_TERM_FORMAT_DIM:
return "\33[2m";
break;
case NM_META_TERM_FORMAT_UNDERLINE:
return "\33[4m";
break;
case NM_META_TERM_FORMAT_BLINK:
return "\33[5m";
break;
case NM_META_TERM_FORMAT_REVERSE:
return "\33[7m";
break;
case NM_META_TERM_FORMAT_HIDDEN:
return "\33[8m";
break;
default:
return "";
break;
}
}
char * char *
nmc_colorize (const NmcConfig *nmc_config, NMMetaTermColor color, NMMetaTermFormat format, const char *fmt, ...) nmc_colorize (const NmcConfig *nmc_config, NMMetaColor color, const char *fmt, ...)
{ {
va_list args; va_list args;
char *str, *colored; char *str, *colored;
const char *ansi_color, *color_end, *ansi_fmt, *format_end; const char *ansi_seq = NULL;
static const char *end_seq = "\33[0m";
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];
if (ansi_seq == NULL)
return str; return str;
ansi_color = nmc_term_color_sequence (color); colored = g_strdup_printf ("\33[%sm%s\33[0m", ansi_seq, str);
ansi_fmt = nmc_term_format_sequence (format);
color_end = *ansi_color ? end_seq : "";
format_end = *ansi_fmt ? end_seq : "";
colored = g_strdup_printf ("%s%s%s%s%s", ansi_fmt, ansi_color, str, color_end, format_end);
g_free (str); g_free (str);
return colored; return colored;
} }
@@ -677,7 +609,7 @@ set_val_arrc (NmcOutputField fields_array[], guint32 idx, const char **value)
} }
void void
set_val_color_all (NmcOutputField fields_array[], NMMetaTermColor color) set_val_color_all (NmcOutputField fields_array[], NMMetaColor color)
{ {
int i; int i;
@@ -686,16 +618,6 @@ set_val_color_all (NmcOutputField fields_array[], NMMetaTermColor color)
} }
} }
void
set_val_color_fmt_all (NmcOutputField fields_array[], NMMetaTermFormat format)
{
int i;
for (i = 0; fields_array[i].info; i++) {
fields_array[i].color_fmt = format;
}
}
/* /*
* Free 'value' members in array of NmcOutputField * Free 'value' members in array of NmcOutputField
*/ */
@@ -974,8 +896,7 @@ typedef enum {
typedef struct { typedef struct {
guint row_idx; guint row_idx;
const PrintDataHeaderCell *header_cell; const PrintDataHeaderCell *header_cell;
NMMetaTermColor term_color; NMMetaColor color;
NMMetaTermFormat term_format;
union { union {
const char *plain; const char *plain;
const char *const*strv; const char *const*strv;
@@ -1138,17 +1059,15 @@ _print_fill (const NmcConfig *nmc_config,
cell->text_to_free = !!to_free; cell->text_to_free = !!to_free;
} }
nm_meta_termformat_unpack (nm_meta_abstract_info_get (info, cell->color = GPOINTER_TO_INT (nm_meta_abstract_info_get (info,
nmc_meta_environment, nmc_meta_environment,
nmc_meta_environment_arg, nmc_meta_environment_arg,
target, target,
NM_META_ACCESSOR_GET_TYPE_TERMFORMAT, NM_META_ACCESSOR_GET_TYPE_COLOR,
NM_META_ACCESSOR_GET_FLAGS_NONE, NM_META_ACCESSOR_GET_FLAGS_NONE,
&color_out_flags, &color_out_flags,
NULL, NULL,
NULL), NULL));
&cell->term_color,
&cell->term_format);
if (cell->text_format == PRINT_DATA_CELL_FORMAT_TYPE_PLAIN) { if (cell->text_format == PRINT_DATA_CELL_FORMAT_TYPE_PLAIN) {
if (pretty && (!cell->text.plain|| !cell->text.plain[0])) { if (pretty && (!cell->text.plain|| !cell->text.plain[0])) {
@@ -1333,9 +1252,7 @@ _print_do (const NmcConfig *nmc_config,
gs_free char *text_to_free = NULL; gs_free char *text_to_free = NULL;
const char *text; const char *text;
text = colorize_string (nmc_config, text = colorize_string (nmc_config, cell->color, lines[i_lines], &text_to_free);
cell->term_color, cell->term_format,
lines[i_lines], &text_to_free);
if (multiline) { if (multiline) {
gs_free char *prefix = NULL; gs_free char *prefix = NULL;
@@ -1561,7 +1478,7 @@ get_value_to_print (const NmcConfig *nmc_config,
} }
/* colorize the value */ /* colorize the value */
out = colorize_string (nmc_config, field->color, field->color_fmt, value, out_to_free); out = colorize_string (nmc_config, field->color, value, out_to_free);
if (out && out == free_value) { if (out && out == free_value) {
nm_assert (!*out_to_free); nm_assert (!*out_to_free);
@@ -1655,7 +1572,7 @@ print_required_fields (const NmcConfig *nmc_config,
gs_free char *tmp = NULL; gs_free char *tmp = NULL;
val = *p ?: not_set_str; val = *p ?: not_set_str;
print_val = colorize_string (nmc_config, field_values[idx].color, field_values[idx].color_fmt, print_val = colorize_string (nmc_config, field_values[idx].color,
val, &val_to_free); val, &val_to_free);
tmp = g_strdup_printf ("%s%s%s[%d]:", tmp = g_strdup_printf ("%s%s%s[%d]:",
section_prefix ? (const char*) field_values[0].value : "", section_prefix ? (const char*) field_values[0].value : "",
@@ -1676,7 +1593,7 @@ print_required_fields (const NmcConfig *nmc_config,
/* value is a string */ /* value is a string */
val = val && *val ? val : not_set_str; val = val && *val ? val : not_set_str;
print_val = colorize_string (nmc_config, field_values[idx].color, field_values[idx].color_fmt, print_val = colorize_string (nmc_config, field_values[idx].color,
val, &val_to_free); val, &val_to_free);
tmp = g_strdup_printf ("%s%s%s:", tmp = g_strdup_printf ("%s%s%s:",
section_prefix ? hdr_name : "", section_prefix ? hdr_name : "",

View File

@@ -41,9 +41,7 @@ char *ssid_to_hex (const char *str, gsize len);
void nmc_terminal_erase_line (void); void nmc_terminal_erase_line (void);
void nmc_terminal_show_progress (const char *str); void nmc_terminal_show_progress (const char *str);
void nmc_terminal_spawn_pager (const NmcConfig *nmc_config); void nmc_terminal_spawn_pager (const NmcConfig *nmc_config);
const char *nmc_term_color_sequence (NMMetaTermColor color); char *nmc_colorize (const NmcConfig *nmc_config, NMMetaColor color, const char * fmt, ...) _nm_printf (3, 4);
const char *nmc_term_format_sequence (NMMetaTermFormat format);
char *nmc_colorize (const NmcConfig *nmc_config, NMMetaTermColor color, NMMetaTermFormat format, const char * fmt, ...) _nm_printf (4, 5);
void nmc_filter_out_colors_inplace (char *str); void nmc_filter_out_colors_inplace (char *str);
char *nmc_filter_out_colors (const char *str); char *nmc_filter_out_colors (const char *str);
char *nmc_get_user_input (const char *ask_str); char *nmc_get_user_input (const char *ask_str);
@@ -56,8 +54,7 @@ void set_val_str (NmcOutputField fields_array[], guint32 index, char *value);
void set_val_strc (NmcOutputField fields_array[], guint32 index, const char *value); void set_val_strc (NmcOutputField fields_array[], guint32 index, const char *value);
void set_val_arr (NmcOutputField fields_array[], guint32 index, char **value); void set_val_arr (NmcOutputField fields_array[], guint32 index, char **value);
void set_val_arrc (NmcOutputField fields_array[], guint32 index, const char **value); void set_val_arrc (NmcOutputField fields_array[], guint32 index, const char **value);
void set_val_color_all (NmcOutputField fields_array[], NMMetaTermColor color); void set_val_color_all (NmcOutputField fields_array[], NMMetaColor color);
void set_val_color_fmt_all (NmcOutputField fields_array[], NMMetaTermFormat format);
void nmc_free_output_field_values (NmcOutputField fields_array[]); void nmc_free_output_field_values (NmcOutputField fields_array[]);
GArray *parse_output_fields (const char *fields_str, GArray *parse_output_fields (const char *fields_str,
@@ -126,10 +123,10 @@ typedef enum {
} NmcGenericInfoType; } NmcGenericInfoType;
#define NMC_HANDLE_TERMFORMAT(color) \ #define NMC_HANDLE_COLOR(color) \
G_STMT_START { \ G_STMT_START { \
if (get_type == NM_META_ACCESSOR_GET_TYPE_TERMFORMAT) \ if (get_type == NM_META_ACCESSOR_GET_TYPE_COLOR) \
return nm_meta_termformat_pack ((color), NM_META_TERM_FORMAT_NORMAL); \ return GINT_TO_POINTER (color); \
} G_STMT_END } G_STMT_END
struct _NmcMetaGenericInfo { struct _NmcMetaGenericInfo {

View File

@@ -14,7 +14,7 @@
* with this program; if not, write to the Free Software Foundation, Inc., * with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* Copyright 2010 - 2017 Red Hat, Inc. * Copyright 2010 - 2018 Red Hat, Inc.
*/ */
#ifndef __NM_META_SETTING_DESC_H__ #ifndef __NM_META_SETTING_DESC_H__
@@ -83,31 +83,54 @@ struct _NMDevice;
#define NM_META_TEXT_PROMPT_PROXY_METHOD_CHOICES "(" NM_META_TEXT_WORD_NONE "/" NM_META_TEXT_WORD_AUTO ") [" NM_META_TEXT_WORD_NONE "]" #define NM_META_TEXT_PROMPT_PROXY_METHOD_CHOICES "(" NM_META_TEXT_WORD_NONE "/" NM_META_TEXT_WORD_AUTO ") [" NM_META_TEXT_WORD_NONE "]"
typedef enum { typedef enum {
NM_META_TERM_COLOR_NORMAL = 0, NM_META_COLOR_NONE = 0,
NM_META_TERM_COLOR_BLACK = 1, NM_META_COLOR_CONNECTION_ACTIVATED,
NM_META_TERM_COLOR_RED = 2, NM_META_COLOR_CONNECTION_ACTIVATING,
NM_META_TERM_COLOR_GREEN = 3, NM_META_COLOR_CONNECTION_DISCONNECTING,
NM_META_TERM_COLOR_YELLOW = 4, NM_META_COLOR_CONNECTION_INVISIBLE,
NM_META_TERM_COLOR_BLUE = 5, NM_META_COLOR_CONNECTION_UNKNOWN,
NM_META_TERM_COLOR_MAGENTA = 6, NM_META_COLOR_CONNECTIVITY_FULL,
NM_META_TERM_COLOR_CYAN = 7, NM_META_COLOR_CONNECTIVITY_LIMITED,
NM_META_TERM_COLOR_WHITE = 8, NM_META_COLOR_CONNECTIVITY_NONE,
} NMMetaTermColor; NM_META_COLOR_CONNECTIVITY_PORTAL,
NM_META_COLOR_CONNECTIVITY_UNKNOWN,
typedef enum { NM_META_COLOR_DEVICE_ACTIVATED,
NM_META_TERM_FORMAT_NORMAL = 0, NM_META_COLOR_DEVICE_ACTIVATING,
NM_META_TERM_FORMAT_BOLD = 1, NM_META_COLOR_DEVICE_DISCONNECTED,
NM_META_TERM_FORMAT_DIM = 2, NM_META_COLOR_DEVICE_FIRMWARE_MISSING,
NM_META_TERM_FORMAT_UNDERLINE = 3, NM_META_COLOR_DEVICE_PLUGIN_MISSING,
NM_META_TERM_FORMAT_BLINK = 4, NM_META_COLOR_DEVICE_UNAVAILABLE,
NM_META_TERM_FORMAT_REVERSE = 5, NM_META_COLOR_DEVICE_UNKNOWN,
NM_META_TERM_FORMAT_HIDDEN = 6, NM_META_COLOR_MANAGER_RUNNING,
} NMMetaTermFormat; NM_META_COLOR_MANAGER_STARTING,
NM_META_COLOR_MANAGER_STOPPED,
NM_META_COLOR_PERMISSION_AUTH,
NM_META_COLOR_PERMISSION_NO,
NM_META_COLOR_PERMISSION_UNKNOWN,
NM_META_COLOR_PERMISSION_YES,
NM_META_COLOR_PROMPT,
NM_META_COLOR_STATE_ASLEEP,
NM_META_COLOR_STATE_CONNECTED_GLOBAL,
NM_META_COLOR_STATE_CONNECTED_LOCAL,
NM_META_COLOR_STATE_CONNECTED_SITE,
NM_META_COLOR_STATE_CONNECTING,
NM_META_COLOR_STATE_DISCONNECTED,
NM_META_COLOR_STATE_DISCONNECTING,
NM_META_COLOR_STATE_UNKNOWN,
NM_META_COLOR_WIFI_SIGNAL_EXCELLENT,
NM_META_COLOR_WIFI_SIGNAL_FAIR,
NM_META_COLOR_WIFI_SIGNAL_GOOD,
NM_META_COLOR_WIFI_SIGNAL_POOR,
NM_META_COLOR_WIFI_SIGNAL_UNKNOWN,
NM_META_COLOR_DISABLED,
NM_META_COLOR_ENABLED,
_NM_META_COLOR_NUM
} NMMetaColor;
typedef enum { typedef enum {
NM_META_ACCESSOR_GET_TYPE_PRETTY, NM_META_ACCESSOR_GET_TYPE_PRETTY,
NM_META_ACCESSOR_GET_TYPE_PARSABLE, NM_META_ACCESSOR_GET_TYPE_PARSABLE,
NM_META_ACCESSOR_GET_TYPE_TERMFORMAT, NM_META_ACCESSOR_GET_TYPE_COLOR,
} NMMetaAccessorGetType; } NMMetaAccessorGetType;
typedef enum { typedef enum {
@@ -115,32 +138,6 @@ typedef enum {
NM_META_ACCESSOR_SETTING_INIT_TYPE_CLI, NM_META_ACCESSOR_SETTING_INIT_TYPE_CLI,
} NMMetaAccessorSettingInitType; } NMMetaAccessorSettingInitType;
static inline void
nm_meta_termformat_unpack (gconstpointer value, NMMetaTermColor *out_color, NMMetaTermFormat *out_format)
{
/* get_fcn() with NM_META_ACCESSOR_GET_TYPE_TERMFORMAT returns a pointer
* that encodes NMMetaTermColor and NMMetaTermFormat. Unpack it. */
if (!value) {
/* by default, objects that don't support NM_META_ACCESSOR_GET_TYPE_TERMFORMAT
* return NULL. This allows for an explicit fallback value here... */
NM_SET_OUT (out_color, NM_META_TERM_COLOR_NORMAL);
NM_SET_OUT (out_format, NM_META_TERM_FORMAT_NORMAL);
} else {
NM_SET_OUT (out_color, GPOINTER_TO_UINT (value) & 0xFF);
NM_SET_OUT (out_format, (GPOINTER_TO_UINT (value) & 0xFF00) >> 8);
}
}
static inline gconstpointer
nm_meta_termformat_pack (NMMetaTermColor color, NMMetaTermFormat format)
{
/* get_fcn() with NM_META_ACCESSOR_GET_TYPE_TERMFORMAT returns a pointer
* that encodes NMMetaTermColor and NMMetaTermFormat. Pack it. */
return GUINT_TO_POINTER (((guint) 0x10000) | (((guint) color) & 0xFFu) | ((((guint) format) & 0xFFu) << 8));
}
#define NM_META_TERMFORMAT_DEFAULT() (nm_meta_termformat_pack (NM_META_TERM_COLOR_NORMAL, NM_META_TERM_FORMAT_NORMAL))
typedef enum { typedef enum {
NM_META_ACCESSOR_GET_FLAGS_NONE = 0, NM_META_ACCESSOR_GET_FLAGS_NONE = 0,
NM_META_ACCESSOR_GET_FLAGS_ACCEPT_STRV = (1LL << 0), NM_META_ACCESSOR_GET_FLAGS_ACCEPT_STRV = (1LL << 0),