libnm-core: move detection of UTF-8 capable terminals to clients/

Having it in libnm doesn't make any sense and prevents using it for more
internal functionality.

Too bad nm_utils_wifi_strength_bars() is already a public API.
No problem -- replace it with a compatible yet dumber equivalent.
This commit is contained in:
Lubomir Rintel
2017-12-15 16:46:07 +01:00
parent 7c3e1d926a
commit 9e4de97967
5 changed files with 73 additions and 45 deletions

View File

@@ -831,7 +831,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 = nm_utils_wifi_strength_bars (strength);
sig_bars = nmc_wifi_strength_bars (strength);
security_str = g_string_new (NULL);

View File

@@ -20,6 +20,7 @@
#include "nm-default.h"
#include "nm-client-utils.h"
#include "nm-utils.h"
#include "nm-device-bond.h"
#include "nm-device-bridge.h"
@@ -505,3 +506,58 @@ nmc_activation_get_effective_state (NMActiveConnection *active,
return ac_state;
}
static gboolean
can_show_graphics (void)
{
static gboolean can_show_graphics_set = FALSE;
gboolean can_show_graphics = TRUE;
char *locale_str;
if (G_LIKELY (can_show_graphics_set))
return can_show_graphics;
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 typically doesn't have characters we need */
if (g_strcmp0 (g_getenv ("TERM"), "linux") == 0)
can_show_graphics = FALSE;
return can_show_graphics;
}
/**
* nmc_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 *
nmc_wifi_strength_bars (guint8 strength)
{
if (!can_show_graphics ())
return nm_utils_wifi_strength_bars (strength);
if (strength > 80)
return /* ▂▄▆█ */ "\342\226\202\342\226\204\342\226\206\342\226\210";
else if (strength > 55)
return /* ▂▄▆_ */ "\342\226\202\342\226\204\342\226\206_";
else if (strength > 30)
return /* ▂▄__ */ "\342\226\202\342\226\204__";
else if (strength > 5)
return /* ▂___ */ "\342\226\202___";
else
return /* ____ */ "____";
}

View File

@@ -54,4 +54,6 @@ NMActiveConnectionState nmc_activation_get_effective_state (NMActiveConnection *
NMDevice *device,
const char **reason);
const char *nmc_wifi_strength_bars (guint8 strength);
#endif /* __NM_CLIENT_UTILS_H__ */

View File

@@ -13,7 +13,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2013 Red Hat, Inc.
* Copyright 2013 - 2017 Red Hat, Inc.
*/
/**
@@ -32,6 +32,7 @@
#include "nmtui.h"
#include "nmt-connect-connection-list.h"
#include "nm-client-utils.h"
G_DEFINE_TYPE (NmtConnectConnectionList, nmt_connect_connection_list, NMT_TYPE_NEWT_LISTBOX)
@@ -525,7 +526,7 @@ nmt_connect_connection_list_rebuild (NmtConnectConnectionList *list)
if (nmtconn->ap) {
guint8 strength = nm_access_point_get_strength (nmtconn->ap);
strength_col = nm_utils_wifi_strength_bars (strength);
strength_col = nmc_wifi_strength_bars (strength);
} else
strength_col = NULL;

View File

@@ -3438,59 +3438,28 @@ nm_utils_wifi_5ghz_freqs (void)
* @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.
* strength suitable for printing to stdout.
*
* Previous versions used to take a guess at the terminal type and possibly
* return a wide UTF-8 encoded string. Now it always returns a 7-bit
* clean strings of one to 0 to 4 asterisks. Users that actually need
* the functionality are encouraged to make their implementations instead.
*
* 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;
return "****";
else if (strength > 55)
return strength_high;
return "*** ";
else if (strength > 30)
return strength_med;
return "** ";
else if (strength > 5)
return strength_low;
return "* ";
else
return strength_none;
return " ";
}
/**