libnm-core: add nm_utils_wifi_strength_bars(), use it in nmcli and nmtui

Add nm_utils_wifi_strength_bars(), which figures out whether the
terminal can display graphical wifi strength bars, and converts a
numerical value to the appropriate Unicode or ASCII characters.

This also now takes into consideration the fact that the console font
doesn't contain all of the necessary characters, so we can't display
the graphical bars there. (rh #1131491)
This commit is contained in:
Dan Winship 2014-09-19 16:51:07 -04:00
parent 1b74e77f9f
commit e684f36365
5 changed files with 65 additions and 40 deletions

View file

@ -501,11 +501,6 @@ fill_output_access_point (gpointer data, gpointer user_data)
*bitrate_str, *strength_str, *wpa_flags_str, *rsn_flags_str;
GString *security_str;
char *ap_name;
const char *sig_level_0 = "____";
const char *sig_level_1 = "▂___";
const char *sig_level_2 = "▂▄__";
const char *sig_level_3 = "▂▄▆_";
const char *sig_level_4 = "▂▄▆█";
const char *sig_bars;
if (info->active_bssid) {
@ -540,11 +535,7 @@ fill_output_access_point (gpointer data, gpointer user_data)
strength_str = g_strdup_printf ("%u", strength);
wpa_flags_str = ap_wpa_rsn_flags_to_string (wpa_flags);
rsn_flags_str = ap_wpa_rsn_flags_to_string (rsn_flags);
sig_bars = strength > 80 ? sig_level_4 :
strength > 55 ? sig_level_3 :
strength > 30 ? sig_level_2 :
strength > 5 ? sig_level_1 :
sig_level_0;
sig_bars = nm_utils_wifi_strength_bars (strength);
security_str = g_string_new (NULL);

View file

@ -61,8 +61,6 @@ typedef struct {
GSList *nmt_devices;
} NmtConnectConnectionListPrivate;
static const char *strength_full, *strength_high, *strength_med, *strength_low, *strength_none;
/**
* nmt_connect_connection_list_new:
*
@ -527,16 +525,7 @@ nmt_connect_connection_list_rebuild (NmtConnectConnectionList *list)
if (nmtconn->ap) {
guint8 strength = nm_access_point_get_strength (nmtconn->ap);
if (strength > 80)
strength_col = strength_full;
else if (strength > 55)
strength_col = strength_high;
else if (strength > 30)
strength_col = strength_med;
else if (strength > 5)
strength_col = strength_low;
else
strength_col = strength_none;
strength_col = nm_utils_wifi_strength_bars (strength);
} else
strength_col = NULL;
@ -608,30 +597,12 @@ static void
nmt_connect_connection_list_class_init (NmtConnectConnectionListClass *list_class)
{
GObjectClass *object_class = G_OBJECT_CLASS (list_class);
char *tmp;
g_type_class_add_private (list_class, sizeof (NmtConnectConnectionListPrivate));
/* virtual methods */
object_class->constructed = nmt_connect_connection_list_constructed;
object_class->finalize = nmt_connect_connection_list_finalize;
/* globals */
tmp = nmt_newt_locale_from_utf8 ("\342\226\202\342\226\204\342\226\206\342\226\210");
if (*tmp) {
strength_full = /* ▂▄▆█ */ "\342\226\202\342\226\204\342\226\206\342\226\210";
strength_high = /* ▂▄▆_ */ "\342\226\202\342\226\204\342\226\206_";
strength_med = /* ▂▄__ */ "\342\226\202\342\226\204__";
strength_low = /* ▂___ */ "\342\226\202___";
strength_none = /* ____ */ "____";
} else {
strength_full = "****";
strength_high = "*** ";
strength_med = "** ";
strength_low = "* ";
strength_none = " ";
}
g_free (tmp);
}
/**

View file

@ -2100,6 +2100,66 @@ nm_utils_wifi_is_channel_valid (guint32 channel, const char *band)
return FALSE;
}
/**
* nm_utils_wifi_strength_bars:
* @strength: the access point strength, from 0 to 100
*
* Converts @strength into a 4-character-wide graphical representation of
* strength suitable for printing to stdout. If the current locale and terminal
* support it, this will use unicode graphics characters to represent
* "bars". Otherwise it will use 0 to 4 asterisks.
*
* Returns: the graphical representation of the access point strength
*/
const char *
nm_utils_wifi_strength_bars (guint8 strength)
{
static const char *strength_full, *strength_high, *strength_med, *strength_low, *strength_none;
if (G_UNLIKELY (strength_full == NULL)) {
gboolean can_show_graphics = TRUE;
char *locale_str;
if (!g_get_charset (NULL)) {
/* Non-UTF-8 locale */
locale_str = g_locale_from_utf8 ("\342\226\202\342\226\204\342\226\206\342\226\210", -1, NULL, NULL, NULL);
if (locale_str)
g_free (locale_str);
else
can_show_graphics = FALSE;
}
/* The linux console font doesn't have these characters */
if (g_strcmp0 (g_getenv ("TERM"), "linux") == 0)
can_show_graphics = FALSE;
if (can_show_graphics) {
strength_full = /* ▂▄▆█ */ "\342\226\202\342\226\204\342\226\206\342\226\210";
strength_high = /* ▂▄▆_ */ "\342\226\202\342\226\204\342\226\206_";
strength_med = /* ▂▄__ */ "\342\226\202\342\226\204__";
strength_low = /* ▂___ */ "\342\226\202___";
strength_none = /* ____ */ "____";
} else {
strength_full = "****";
strength_high = "*** ";
strength_med = "** ";
strength_low = "* ";
strength_none = " ";
}
}
if (strength > 80)
return strength_full;
else if (strength > 55)
return strength_high;
else if (strength > 30)
return strength_med;
else if (strength > 5)
return strength_low;
else
return strength_none;
}
/**
* nm_utils_hwaddr_len:
* @type: the type of address; either %ARPHRD_ETHER or %ARPHRD_INFINIBAND

View file

@ -131,6 +131,8 @@ guint32 nm_utils_wifi_channel_to_freq (guint32 channel, const char *band);
guint32 nm_utils_wifi_find_next_channel (guint32 channel, int direction, char *band);
gboolean nm_utils_wifi_is_channel_valid (guint32 channel, const char *band);
const char *nm_utils_wifi_strength_bars (guint8 strength);
/**
* NM_UTILS_HWADDR_LEN_MAX:
*

View file

@ -931,6 +931,7 @@ global:
nm_utils_wifi_find_next_channel;
nm_utils_wifi_freq_to_channel;
nm_utils_wifi_is_channel_valid;
nm_utils_wifi_strength_bars;
nm_utils_wpa_psk_valid;
nm_vlan_flags_get_type;
nm_vlan_priority_map_get_type;