cli/trivial: move code

This commit is contained in:
Thomas Haller 2017-04-06 14:43:44 +02:00
parent 01b1656f36
commit ce28918dbf
2 changed files with 121 additions and 115 deletions

View file

@ -35,6 +35,88 @@
#include "devices.h"
#include "connections.h"
/*****************************************************************************/
static const char *
nm_state_to_string (NMState state)
{
switch (state) {
case NM_STATE_ASLEEP:
return _("asleep");
case NM_STATE_CONNECTING:
return _("connecting");
case NM_STATE_CONNECTED_LOCAL:
return _("connected (local only)");
case NM_STATE_CONNECTED_SITE:
return _("connected (site only)");
case NM_STATE_CONNECTED_GLOBAL:
return _("connected");
case NM_STATE_DISCONNECTING:
return _("disconnecting");
case NM_STATE_DISCONNECTED:
return _("disconnected");
case NM_STATE_UNKNOWN:
default:
return _("unknown");
}
}
static NMMetaTermColor
state_to_color (NMState state)
{
switch (state) {
case NM_STATE_CONNECTING:
return NM_META_TERM_COLOR_YELLOW;
case NM_STATE_CONNECTED_LOCAL:
case NM_STATE_CONNECTED_SITE:
case NM_STATE_CONNECTED_GLOBAL:
return NM_META_TERM_COLOR_GREEN;
case NM_STATE_DISCONNECTING:
return NM_META_TERM_COLOR_YELLOW;
case NM_STATE_ASLEEP:
case NM_STATE_DISCONNECTED:
return NM_META_TERM_COLOR_RED;
default:
return NM_META_TERM_COLOR_NORMAL;
}
}
static const char *
nm_connectivity_to_string (NMConnectivityState connectivity)
{
switch (connectivity) {
case NM_CONNECTIVITY_NONE:
return _("none");
case NM_CONNECTIVITY_PORTAL:
return _("portal");
case NM_CONNECTIVITY_LIMITED:
return _("limited");
case NM_CONNECTIVITY_FULL:
return _("full");
case NM_CONNECTIVITY_UNKNOWN:
default:
return _("unknown");
}
}
static NMMetaTermColor
connectivity_to_color (NMConnectivityState connectivity)
{
switch (connectivity) {
case NM_CONNECTIVITY_NONE:
return NM_META_TERM_COLOR_RED;
case NM_CONNECTIVITY_PORTAL:
case NM_CONNECTIVITY_LIMITED:
return NM_META_TERM_COLOR_YELLOW;
case NM_CONNECTIVITY_FULL:
return NM_META_TERM_COLOR_GREEN;
default:
return NM_META_TERM_COLOR_NORMAL;
}
}
/*****************************************************************************/
static const NmcMetaGenericInfo *const nmc_fields_nm_status[] = {
NMC_META_GENERIC ("RUNNING"), /* 0 */
NMC_META_GENERIC ("VERSION"), /* 1 */
@ -230,84 +312,6 @@ quit (void)
g_main_loop_quit (loop); /* quit main loop */
}
static const char *
nm_state_to_string (NMState state)
{
switch (state) {
case NM_STATE_ASLEEP:
return _("asleep");
case NM_STATE_CONNECTING:
return _("connecting");
case NM_STATE_CONNECTED_LOCAL:
return _("connected (local only)");
case NM_STATE_CONNECTED_SITE:
return _("connected (site only)");
case NM_STATE_CONNECTED_GLOBAL:
return _("connected");
case NM_STATE_DISCONNECTING:
return _("disconnecting");
case NM_STATE_DISCONNECTED:
return _("disconnected");
case NM_STATE_UNKNOWN:
default:
return _("unknown");
}
}
static NMMetaTermColor
state_to_color (NMState state)
{
switch (state) {
case NM_STATE_CONNECTING:
return NM_META_TERM_COLOR_YELLOW;
case NM_STATE_CONNECTED_LOCAL:
case NM_STATE_CONNECTED_SITE:
case NM_STATE_CONNECTED_GLOBAL:
return NM_META_TERM_COLOR_GREEN;
case NM_STATE_DISCONNECTING:
return NM_META_TERM_COLOR_YELLOW;
case NM_STATE_ASLEEP:
case NM_STATE_DISCONNECTED:
return NM_META_TERM_COLOR_RED;
default:
return NM_META_TERM_COLOR_NORMAL;
}
}
static const char *
nm_connectivity_to_string (NMConnectivityState connectivity)
{
switch (connectivity) {
case NM_CONNECTIVITY_NONE:
return _("none");
case NM_CONNECTIVITY_PORTAL:
return _("portal");
case NM_CONNECTIVITY_LIMITED:
return _("limited");
case NM_CONNECTIVITY_FULL:
return _("full");
case NM_CONNECTIVITY_UNKNOWN:
default:
return _("unknown");
}
}
static NMMetaTermColor
connectivity_to_color (NMConnectivityState connectivity)
{
switch (connectivity) {
case NM_CONNECTIVITY_NONE:
return NM_META_TERM_COLOR_RED;
case NM_CONNECTIVITY_PORTAL:
case NM_CONNECTIVITY_LIMITED:
return NM_META_TERM_COLOR_YELLOW;
case NM_CONNECTIVITY_FULL:
return NM_META_TERM_COLOR_GREEN;
default:
return NM_META_TERM_COLOR_NORMAL;
}
}
static gboolean
show_nm_status (NmCli *nmc, const char *pretty_header_name, const char *print_flds)
{

View file

@ -98,6 +98,45 @@ const NMMetaType nmc_meta_type_generic_info = {
/*****************************************************************************/
static gboolean
use_colors (NmcColorOption color_option)
{
if (color_option == NMC_USE_COLOR_AUTO) {
static NmcColorOption cached = NMC_USE_COLOR_AUTO;
if (G_UNLIKELY (cached == NMC_USE_COLOR_AUTO)) {
if ( g_strcmp0 (g_getenv ("TERM"), "dumb") == 0
|| !isatty (fileno (stdout)))
cached = NMC_USE_COLOR_NO;
else
cached = NMC_USE_COLOR_YES;
}
return cached == NMC_USE_COLOR_YES;
}
return color_option == NMC_USE_COLOR_YES;
}
static const char *
colorize_string (NmcColorOption color_option,
NMMetaTermColor color,
NMMetaTermFormat color_fmt,
const char *str,
char **out_to_free)
{
const char *out = str;
if ( use_colors (color_option)
&& (color != NM_META_TERM_COLOR_NORMAL || color_fmt != NM_META_TERM_FORMAT_NORMAL)) {
*out_to_free = nmc_colorize (color_option, color, color_fmt, "%s", str);
out = *out_to_free;
}
return out;
}
/*****************************************************************************/
static gboolean
parse_global_arg (NmCli *nmc, const char *arg)
{
@ -422,25 +461,6 @@ nmc_term_format_sequence (NMMetaTermFormat format)
}
}
static gboolean
use_colors (NmcColorOption color_option)
{
if (color_option == NMC_USE_COLOR_AUTO) {
static NmcColorOption cached = NMC_USE_COLOR_AUTO;
if (G_UNLIKELY (cached == NMC_USE_COLOR_AUTO)) {
if ( g_strcmp0 (g_getenv ("TERM"), "dumb") == 0
|| !isatty (fileno (stdout)))
cached = NMC_USE_COLOR_NO;
else
cached = NMC_USE_COLOR_YES;
}
return cached == NMC_USE_COLOR_YES;
}
return color_option == NMC_USE_COLOR_YES;
}
char *
nmc_colorize (NmcColorOption color_option, NMMetaTermColor color, NMMetaTermFormat format, const char *fmt, ...)
{
@ -992,24 +1012,6 @@ nmc_empty_output_fields (NmcOutputData *output_data)
g_ptr_array_remove_range (output_data->output_data, 0, output_data->output_data->len);
}
static const char *
colorize_string (NmcColorOption color_option,
NMMetaTermColor color,
NMMetaTermFormat color_fmt,
const char *str,
char **out_to_free)
{
const char *out = str;
if ( use_colors (color_option)
&& (color != NM_META_TERM_COLOR_NORMAL || color_fmt != NM_META_TERM_FORMAT_NORMAL)) {
*out_to_free = nmc_colorize (color_option, color, color_fmt, "%s", str);
out = *out_to_free;
}
return out;
}
static const char *
get_value_to_print (NmcColorOption color_option,
const NmcOutputField *field,