Commit 31aa2cfe authored by Lubomir Rintel's avatar Lubomir Rintel 🥕

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.
parent 9dfe8258
......@@ -16,7 +16,7 @@
* with this program; if not, write to the Free Software Foundation, Inc.,
* 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"
......@@ -119,7 +119,7 @@ _metagen_ip4_config_get_fcn (const NMMetaEnvironment *environment,
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);
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);
NMC_HANDLE_TERMFORMAT (NM_META_TERM_COLOR_NORMAL);
NMC_HANDLE_COLOR (NM_META_COLOR_NONE);
NM_SET_OUT (out_is_default, TRUE);
switch (info->info_type) {
......
......@@ -811,17 +811,17 @@ found:
return found;
}
void
nmc_active_connection_state_to_color (NMActiveConnectionState state, NMMetaTermColor *color)
NMMetaColor
nmc_active_connection_state_to_color (NMActiveConnectionState state)
{
*color = NM_META_TERM_COLOR_NORMAL;
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)
*color = NM_META_TERM_COLOR_GREEN;
return NM_META_COLOR_CONNECTION_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
......@@ -864,7 +864,7 @@ fill_output_connection (NMConnection *connection, NMClient *client, NMCPrintOutp
const char *ac_state = NULL;
NMActiveConnectionState ac_state_int = NM_ACTIVE_CONNECTION_STATE_UNKNOWN;
char *ac_dev = NULL;
NMMetaTermColor color;
NMMetaColor color;
s_con = nm_connection_get_setting_connection (connection);
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);
/* 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_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, 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);
}
......@@ -6470,8 +6470,7 @@ property_edit_submenu (NmCli *nmc,
/* Set global variable for use in TAB completion */
nmc_tab_completion.property = prop_name;
prompt = nmc_colorize (&nmc->nmc_config, NM_META_TERM_COLOR_NORMAL, NM_META_TERM_FORMAT_NORMAL,
"nmcli %s.%s> ",
prompt = nmc_colorize (&nmc->nmc_config, NM_META_COLOR_PROMPT, "nmcli %s.%s> ",
nm_setting_get_name (curr_setting), prop_name);
while (cmd_property_loop) {
......@@ -6803,12 +6802,11 @@ typedef struct {
static void
menu_switch_to_level0 (const NmcConfig *nmc_config,
NmcEditorMenuContext *menu_ctx,
const char *prompt,
NMMetaTermColor prompt_color)
const char *prompt)
{
menu_ctx->level = 0;
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;
g_strfreev (menu_ctx->valid_props);
menu_ctx->valid_props = NULL;
......@@ -6820,13 +6818,11 @@ static void
menu_switch_to_level1 (const NmcConfig *nmc_config,
NmcEditorMenuContext *menu_ctx,
NMSetting *setting,
const char *setting_name,
NMMetaTermColor prompt_color)
const char *setting_name)
{
menu_ctx->level = 1;
g_free (menu_ctx->main_prompt);
menu_ctx->main_prompt = nmc_colorize (nmc_config, prompt_color, NM_META_TERM_FORMAT_NORMAL,
"nmcli %s> ", setting_name);
menu_ctx->main_prompt = nmc_colorize (nmc_config, NM_META_COLOR_PROMPT, "nmcli %s> ", setting_name);
menu_ctx->curr_setting = setting;
g_strfreev (menu_ctx->valid_props);
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);
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,
BASE_PROMPT);
menu_ctx.main_prompt = nmc_colorize (&nmc->nmc_config, NM_META_COLOR_PROMPT, BASE_PROMPT);
/* Get remote connection */
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;
/* 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) {
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);
if (ss == menu_ctx.curr_setting) {
/* 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 */
}
} 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 */
if (ss == menu_ctx.curr_setting) {
/* 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 */
}
} else
......@@ -7507,7 +7502,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t
case NMC_EDITOR_MAIN_CMD_BACK:
/* Go back (up) an the menu */
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 */
}
break;
......
......@@ -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.
* (C) Copyright 2010 - 2018 Red Hat, Inc.
*/
#ifndef NMC_CONNECTIONS_H
......@@ -33,7 +33,7 @@ nmc_read_connection_properties (NmCli *nmc,
char ***argv,
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_active_details_general[];
......
......@@ -707,22 +707,19 @@ sort_access_points (const GPtrArray *aps)
return sorted;
}
static void
wifi_signal_to_color (guint8 strength, NMMetaTermColor *color, NMMetaTermFormat *color_fmt)
static NMMetaColor
wifi_signal_to_color (guint8 strength)
{
*color = NM_META_TERM_COLOR_NORMAL;
*color_fmt = NM_META_TERM_FORMAT_NORMAL;
if (strength > 80)
*color = NM_META_TERM_COLOR_GREEN;
return NM_META_COLOR_WIFI_SIGNAL_EXCELLENT;
else if (strength > 55)
*color = NM_META_TERM_COLOR_YELLOW;
return NM_META_COLOR_WIFI_SIGNAL_GOOD;
else if (strength > 30)
*color = NM_META_TERM_COLOR_MAGENTA;
return NM_META_COLOR_WIFI_SIGNAL_FAIR;
else if (strength > 5)
*color = NM_META_TERM_COLOR_CYAN;
return NM_META_COLOR_WIFI_SIGNAL_POOR;
else
*color_fmt = NM_META_TERM_FORMAT_DIM;
return NM_META_COLOR_WIFI_SIGNAL_UNKNOWN;
}
static char *
......@@ -795,8 +792,7 @@ fill_output_access_point (gpointer data, gpointer user_data)
GString *security_str;
char *ap_name;
const char *sig_bars;
NMMetaTermColor color;
NMMetaTermFormat color_fmt;
NMMetaColor color;
if (info->active_bssid) {
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 colors */
wifi_signal_to_color (strength, &color, &color_fmt);
color = wifi_signal_to_color (strength);
set_val_color_all (arr, color);
set_val_color_fmt_all (arr, color_fmt);
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);
......@@ -1463,20 +1458,19 @@ show_device_info (NMDevice *device, NmCli *nmc)
return TRUE;
}
void
nmc_device_state_to_color (NMDeviceState state, NMMetaTermColor *color, NMMetaTermFormat *color_fmt)
NMMetaColor
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)
*color_fmt= NM_META_TERM_FORMAT_DIM;
return NM_META_COLOR_DEVICE_UNAVAILABLE;
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)
*color = NM_META_TERM_COLOR_YELLOW;
return NM_META_COLOR_DEVICE_ACTIVATING;
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
......@@ -1484,8 +1478,7 @@ fill_output_device_status (NMDevice *device, GPtrArray *output_data)
{
NMActiveConnection *ac;
NMDeviceState state;
NMMetaTermColor color;
NMMetaTermFormat color_fmt;
NMMetaColor color;
NmcOutputField *arr = nmc_dup_fields_array ((const NMMetaAbstractInfo *const*) nmc_fields_dev_status,
0);
......@@ -1493,9 +1486,8 @@ fill_output_device_status (NMDevice *device, GPtrArray *output_data)
ac = nm_device_get_active_connection (device);
/* 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_fmt_all (arr, color_fmt);
set_val_strc (arr, 0, nm_device_get_iface (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)
{
NMDeviceState state = nm_device_get_state (device);
NMMetaTermColor color;
NMMetaTermFormat color_fmt;
NMMetaColor color;
char *str;
nmc_device_state_to_color (state, &color, &color_fmt);
str = nmc_colorize (&nmc->nmc_config, color, color_fmt, "%s: %s\n",
color = nmc_device_state_to_color (state);
str = nmc_colorize (&nmc->nmc_config, color, "%s: %s\n",
nm_device_get_iface (device),
nmc_device_state_to_string (state));
......
......@@ -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 Red Hat, Inc.
* (C) Copyright 2010 - 2018 Red Hat, Inc.
*/
#ifndef NMC_DEVICES_H
......@@ -32,7 +32,7 @@ void monitor_devices (NmCli *nmc);
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_show_general[];
......
......@@ -55,23 +55,26 @@ nm_state_to_string (NMState state)
return _(nm_state_to_string_no_l10n (state));
}
static NMMetaTermColor
static NMMetaColor
state_to_color (NMState state)
{
switch (state) {
case NM_STATE_CONNECTING:
return NM_META_TERM_COLOR_YELLOW;
return NM_META_COLOR_STATE_CONNECTING;
case NM_STATE_CONNECTED_LOCAL:
return NM_META_COLOR_STATE_CONNECTED_LOCAL;
case NM_STATE_CONNECTED_SITE:
return NM_META_COLOR_STATE_CONNECTED_SITE;
case NM_STATE_CONNECTED_GLOBAL:
return NM_META_TERM_COLOR_GREEN;
return NM_META_COLOR_STATE_CONNECTED_GLOBAL;
case NM_STATE_DISCONNECTING:
return NM_META_TERM_COLOR_YELLOW;
return NM_META_COLOR_STATE_DISCONNECTING;
case NM_STATE_ASLEEP:
return NM_META_COLOR_STATE_ASLEEP;
case NM_STATE_DISCONNECTED:
return NM_META_TERM_COLOR_RED;
return NM_META_COLOR_STATE_DISCONNECTED;
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));
}
static NMMetaTermColor
static NMMetaColor
connectivity_to_color (NMConnectivityState connectivity)
{
switch (connectivity) {
case NM_CONNECTIVITY_NONE:
return NM_META_TERM_COLOR_RED;
return NM_META_COLOR_CONNECTIVITY_NONE;
case NM_CONNECTIVITY_PORTAL:
return NM_META_COLOR_CONNECTIVITY_PORTAL;
case NM_CONNECTIVITY_LIMITED:
return NM_META_TERM_COLOR_YELLOW;
return NM_META_COLOR_CONNECTIVITY_LIMITED;
case NM_CONNECTIVITY_FULL:
return NM_META_TERM_COLOR_GREEN;
return NM_META_COLOR_CONNECTIVITY_FULL;
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_DEFINE (static, permission_result_to_color, NMClientPermissionResult, NMMetaTermColor,
NM_UTILS_LOOKUP_DEFAULT (NM_META_TERM_COLOR_NORMAL),
NM_UTILS_LOOKUP_ITEM (NM_CLIENT_PERMISSION_RESULT_YES, NM_META_TERM_COLOR_GREEN),
NM_UTILS_LOOKUP_ITEM (NM_CLIENT_PERMISSION_RESULT_NO, NM_META_TERM_COLOR_RED),
NM_UTILS_LOOKUP_ITEM (NM_CLIENT_PERMISSION_RESULT_AUTH, NM_META_TERM_COLOR_YELLOW),
_NM_UTILS_LOOKUP_DEFINE (static, permission_result_to_color, NMClientPermissionResult, NMMetaColor,
NM_UTILS_LOOKUP_DEFAULT (NM_META_COLOR_PERMISSION_UNKNOWN),
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_COLOR_PERMISSION_NO),
NM_UTILS_LOOKUP_ITEM (NM_CLIENT_PERMISSION_RESULT_AUTH, NM_META_COLOR_PERMISSION_AUTH),
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) {
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");
goto translate_and_out;
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);
goto clone_and_out;
case NMC_GENERIC_INFO_TYPE_GENERAL_STATUS_STATE:
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);
goto translate_and_out;
case NMC_GENERIC_INFO_TYPE_GENERAL_STATUS_STARTUP:
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");
goto translate_and_out;
case NMC_GENERIC_INFO_TYPE_GENERAL_STATUS_CONNECTIVITY:
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);
goto translate_and_out;
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);
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");
goto translate_and_out;
......@@ -293,11 +297,11 @@ _metagen_general_permissions_get_fcn (const NMMetaEnvironment *environment,
switch (info->info_type) {
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);
case NMC_GENERIC_INFO_TYPE_GENERAL_PERMISSIONS_VALUE:
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);
if (get_type == NM_META_ACCESSOR_GET_TYPE_PRETTY)
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);
NMC_HANDLE_TERMFORMAT (NM_META_TERM_COLOR_NORMAL);
NMC_HANDLE_COLOR (NM_META_COLOR_NONE);
if (!d->initialized) {
d->initialized = TRUE;
......@@ -1087,8 +1091,7 @@ networkmanager_running (NMClient *client, GParamSpec *param, NmCli *nmc)
running = nm_client_get_nm_running (client);
str = nmc_colorize (&nmc->nmc_config,
running ? NM_META_TERM_COLOR_GREEN : NM_META_TERM_COLOR_RED,
NM_META_TERM_FORMAT_NORMAL,
running ? NM_META_COLOR_MANAGER_RUNNING : NM_META_COLOR_MANAGER_STOPPED,
running ? _("NetworkManager has started") : _("NetworkManager has stopped"));
g_print ("%s\n", str);
g_free (str);
......@@ -1128,7 +1131,7 @@ client_connectivity (NMClient *client, GParamSpec *param, NmCli *nmc)
char *str;
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));
g_print ("%s", str);
g_free (str);
......@@ -1141,7 +1144,7 @@ client_state (NMClient *client, GParamSpec *param, NmCli *nmc)
char *str;
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"),
nm_state_to_string (state));
g_print ("%s", str);
......@@ -1184,12 +1187,12 @@ device_overview (NmCli *nmc, NMDevice *device)
if (!nm_device_get_autoconnect (device))
g_string_append_printf (outbuf, "%s, ", _("autoconnect"));
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_free (tmp);
}
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_free (tmp);
}
......@@ -1290,7 +1293,7 @@ do_overview (NmCli *nmc, int argc, char **argv)
NMDevice **devices;
const GPtrArray *p;
NMActiveConnection *ac;
NMMetaTermColor color;
NMMetaColor color;
NMDnsEntry *dns;
char *tmp;
int i;
......@@ -1314,8 +1317,8 @@ do_overview (NmCli *nmc, int argc, char **argv)
continue;
state = nm_active_connection_get_state (ac);
nmc_active_connection_state_to_color (state, &color);
tmp = nmc_colorize (&nmc->nmc_config, color, NM_META_TERM_FORMAT_NORMAL, _("%s VPN connection"),
color = nmc_active_connection_state_to_color (state);
tmp = nmc_colorize (&nmc->nmc_config, color, _("%s VPN connection"),
nm_active_connection_get_id (ac));
g_print ("%s\n", tmp);
g_free (tmp);
......@@ -1326,14 +1329,13 @@ do_overview (NmCli *nmc, int argc, char **argv)
devices = nmc_get_devices_sorted (nmc->client);
for (i = 0; devices[i]; i++) {
NMMetaTermFormat color_fmt;
NMDeviceState state;
ac = nm_device_get_active_connection (devices[i]);
state = nm_device_get_state (devices[i]);
nmc_device_state_to_color (state, &color, &color_fmt);
tmp = nmc_colorize (&nmc->nmc_config, color, color_fmt, "%s: %s%s%s",
color = nmc_device_state_to_color (state);
tmp = nmc_colorize (&nmc->nmc_config, color, "%s: %s%s%s",
nm_device_get_iface (devices[i]),
nmc_device_state_to_string (state),
ac ? " to " : "",
......@@ -1416,7 +1418,7 @@ do_monitor (NmCli *nmc, int argc, char **argv)
if (!nm_client_get_nm_running (nmc->client)) {
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"));
g_print ("%s", str);
g_free (str);
......
......@@ -73,6 +73,42 @@ NmCli nm_cli = {
.complete = FALSE,
.nmc_config.show_secrets = 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_save_confirmation = TRUE,
};
......
......@@ -93,8 +93,7 @@ struct _NmcOutputField {
gboolean value_is_array; /* Whether value is char** instead of char* */
gboolean free_value; /* Whether to free the value */
NmcOfFlags flags; /* Flags - whether and how to print values/field names/headers */
NMMetaTermColor color; /* Use this color to print value */
NMMetaTermFormat color_fmt; /* Use this terminal format to print value */
NMMetaColor color; /* Use this color to print value */
};
typedef struct _NmcConfig {
......@@ -105,6 +104,7 @@ typedef struct _NmcConfig {
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 overview; /* Overview mode (hide default values) */
const char *palette[_NM_META_COLOR_NUM]; /* Color palette */
} NmcConfig;
typedef struct _NmcOutputData {
......
......@@ -86,11 +86,11 @@ _meta_type_nmc_generic_info_get_fcn (const NMMetaAbstractInfo *abstract_info,
if (!NM_IN_SET (get_type,
NM_META_ACCESSOR_GET_TYPE_PARSABLE,
NM_META_ACCESSOR_GET_TYPE_PRETTY,
NM_META_ACCESSOR_GET_TYPE_TERMFORMAT))
NM_META_ACCESSOR_GET_TYPE_COLOR))
g_return_val_if_reached (NULL);
/* omitting the out_to_free value is only allowed for TERMFORMAT. */
nm_assert (out_to_free || NM_IN_SET (get_type, NM_META_ACCESSOR_GET_TYPE_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_COLOR));
if (info->get_fcn) {
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) {
NMC_HANDLE_TERMFORMAT (NM_META_TERM_COLOR_NORMAL);
NMC_HANDLE_COLOR (NM_META_COLOR_NONE);
return info->name;
}
......@@ -121,17 +121,14 @@ const NMMetaType nmc_meta_type_generic_info = {
static const char *
colorize_string (const NmcConfig *nmc_config,
NMMetaTermColor color,
NMMetaTermFormat color_fmt,
NMMetaColor color,
const char *str,
char **out_to_free)
{
const char *out = str;
if ( nmc_config
&& nmc_config->use_colors
&& (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);
if (nmc_config && nmc_config->use_colors) {
*out_to_free = nmc_colorize (nmc_config, color, "%s", str);
out = *out_to_free;
}
......@@ -378,89 +375,24 @@ nmc_terminal_show_progress (const char *str)
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 *
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;
char *str, *colored;
const char *ansi_color, *color_end, *ansi_fmt, *format_end;
static const char *end_seq = "\33[0m";
const char *ansi_seq = NULL;
va_start (args, fmt);
str = g_strdup_vprintf (fmt, args);
va_end (args);
if (!nmc_config->use_colors)
return str;
if (nmc_config->use_colors)
ansi_seq = nmc_config->palette[color];
ansi_color = nmc_term_color_sequence (color);
ansi_fmt = nmc_term_format_sequence (format);