LocalIP: support multiline compact mode

This commit is contained in:
李通洲
2023-02-02 22:25:31 +08:00
parent cae9fee30a
commit ec3cd6cc68
5 changed files with 78 additions and 39 deletions
+1
View File
@@ -174,6 +174,7 @@ static void defaultConfig(FFinstance* instance)
instance->config.localIpShowIpV4 = true;
instance->config.localIpShowIpV6 = false;
instance->config.localIpShowLoop = false;
instance->config.localIpV6First = false;
ffStrbufInit(&instance->config.localIpNamePrefix);
instance->config.localIpCompactType = FF_LOCALIP_COMPACT_TYPE_NONE;
+2 -1
View File
@@ -124,7 +124,8 @@ Module specific options:
--localip-show-ipv6 <?value>: Show IPv6 addresses in local ip module. Default is false
--localip-show-loop <?value>: Show loop back addresses (127.0.0.1) in local ip module. Default is false
--localip-name-prefix <str>: Show IPs with given name prefix only. Default is empty
--localip-compact-type <str>: Show IPs in one line. Should be either none (disable one-line mode), default (don't sort the result), v4first (print ipv4 first) and v6first. Default is none
--localip-compact-type <str>: Show IPs in one line. Should be either none, oneline, multiline. Default is none
--localip-v6first <?value>: Set if ipv6 should be printed first. Default is false
--public-ip-timeout: Time in milliseconds to wait for the public ip server to respond. Default is disabled (0)
--public-ip-url: The URL of public IP detection server to be used.
--weather-timeout: Time in milliseconds to wait for the weather server to respond. Default is disabled (0)
+4 -3
View File
@@ -1282,6 +1282,8 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
optionParseString(key, value, &instance->config.batteryDir);
else if(strcasecmp(key, "--separator-string") == 0)
optionParseString(key, value, &instance->config.separatorString);
else if(strcasecmp(key, "--localip-v6first") == 0)
instance->config.localIpV6First = optionParseBoolean(value);
else if(strcasecmp(key, "--localip-show-ipv4") == 0)
instance->config.localIpShowIpV4 = optionParseBoolean(value);
else if(strcasecmp(key, "--localip-show-ipv6") == 0)
@@ -1294,9 +1296,8 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
{
optionParseEnum(key, value, &instance->config.localIpCompactType,
"none", FF_LOCALIP_COMPACT_TYPE_NONE,
"default", FF_LOCALIP_COMPACT_TYPE_DEFAULT,
"v4first", FF_LOCALIP_COMPACT_TYPE_V4FIRST,
"v6first", FF_LOCALIP_COMPACT_TYPE_V6FIRST,
"oneline", FF_LOCALIP_COMPACT_TYPE_ONELINE,
"multiline", FF_LOCALIP_COMPACT_TYPE_MULTILINE,
NULL
);
}
+3 -3
View File
@@ -44,9 +44,8 @@ typedef enum FFSoundType
typedef enum FFLocalIpCompactType
{
FF_LOCALIP_COMPACT_TYPE_NONE,
FF_LOCALIP_COMPACT_TYPE_DEFAULT,
FF_LOCALIP_COMPACT_TYPE_V4FIRST,
FF_LOCALIP_COMPACT_TYPE_V6FIRST,
FF_LOCALIP_COMPACT_TYPE_MULTILINE,
FF_LOCALIP_COMPACT_TYPE_ONELINE,
} FFLocalIpCompactType;
typedef enum FFBinaryPrefixType
@@ -209,6 +208,7 @@ typedef struct FFconfig
FFstrbuf separatorString;
bool localIpShowLoop;
bool localIpV6First;
bool localIpShowIpV4;
bool localIpShowIpV6;
FFstrbuf localIpNamePrefix;
+68 -32
View File
@@ -7,6 +7,10 @@
static int sortIpsV4First(const FFLocalIpResult* left, const FFLocalIpResult* right)
{
int name = ffStrbufComp(&left->name, &right->name);
if (name != 0)
return name;
if (left->ipv6 != right->ipv6)
return left->ipv6 == false ? -1 : 1;
@@ -15,12 +19,62 @@ static int sortIpsV4First(const FFLocalIpResult* left, const FFLocalIpResult* ri
static int sortIpsV6First(const FFLocalIpResult* left, const FFLocalIpResult* right)
{
int name = ffStrbufComp(&left->name, &right->name);
if (name != 0)
return name;
if (left->ipv6 != right->ipv6)
return left->ipv6 == true ? -1 : 1;
return ffStrbufComp(&left->addr, &right->addr);
}
static void formatKey(const FFinstance* instance, const FFLocalIpResult* ip, FFstrbuf* key)
{
if(instance->config.localIP.key.length == 0)
{
if(ip->name.length)
ffStrbufSetF(key, FF_LOCALIP_MODULE_NAME " (%*s)", ip->name.length, ip->name.chars);
else
ffStrbufSetS(key, FF_LOCALIP_MODULE_NAME);
}
else
{
ffStrbufClear(key);
ffParseFormatString(key, &instance->config.localIP.key, 1, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRBUF, &ip->name}
});
}
}
static void printIpsOneline(FFlist* ips)
{
uint32_t index = 0;
FF_LIST_FOR_EACH(FFLocalIpResult, ip, *ips)
{
if (index++ > 0)
putchar(' ');
ffStrbufWriteTo(&ip->addr, stdout);
}
}
static void printIpsWithKey(FFinstance* instance, FFlist* ips)
{
FFLocalIpResult* ip = (FFLocalIpResult*) ffListGet(ips, 0);
if (instance->config.localIpCompactType == FF_LOCALIP_COMPACT_TYPE_MULTILINE)
{
FF_STRBUF_AUTO_DESTROY key;
ffStrbufInit(&key);
formatKey(instance, ip, &key);
ffPrintLogoAndKey(instance, key.chars, 0, NULL);
}
printIpsOneline(ips);
if (instance->config.localIpCompactType == FF_LOCALIP_COMPACT_TYPE_ONELINE)
printf(" (%s) ", ip->name.chars);
else
putchar('\n');
}
void ffPrintLocalIp(FFinstance* instance)
{
FF_LIST_AUTO_DESTROY results;
@@ -40,30 +94,26 @@ void ffPrintLocalIp(FFinstance* instance)
return;
}
ffListSort(&results, instance->config.localIpV6First ? (void*) sortIpsV6First : (void*) sortIpsV4First);
if (instance->config.localIpCompactType != FF_LOCALIP_COMPACT_TYPE_NONE)
{
ffPrintLogoAndKey(instance, FF_LOCALIP_MODULE_NAME, 0, &instance->config.localIP.key);
if (instance->config.localIpCompactType == FF_LOCALIP_COMPACT_TYPE_ONELINE)
ffPrintLogoAndKey(instance, FF_LOCALIP_MODULE_NAME, 0, &instance->config.localIP.key);
switch (instance->config.localIpCompactType)
{
case FF_LOCALIP_COMPACT_TYPE_V4FIRST:
ffListSort(&results, (void*) sortIpsV4First);
break;
case FF_LOCALIP_COMPACT_TYPE_V6FIRST:
ffListSort(&results, (void*) sortIpsV6First);
break;
default:
break;
}
FF_LIST_AUTO_DESTROY ips;
ffListInit(&ips, sizeof(FFLocalIpResult)); // weak references
uint32_t index = 0;
FF_LIST_FOR_EACH(FFLocalIpResult, ip, results)
{
if (index++ > 0)
putchar(' ');
ffStrbufWriteTo(&ip->addr, stdout);
if (ips.length > 0 && !ffStrbufEqual(&ip->name, &ip[-1].name))
{
printIpsWithKey(instance, &ips);
ips.length = 0;
}
*(FFLocalIpResult*)ffListAdd(&ips) = *ip;
}
putchar('\n');
printIpsWithKey(instance, &ips);
}
else
{
@@ -72,21 +122,7 @@ void ffPrintLocalIp(FFinstance* instance)
FF_LIST_FOR_EACH(FFLocalIpResult, ip, results)
{
if(instance->config.localIP.key.length == 0)
{
if(ip->name.length)
ffStrbufSetF(&key, FF_LOCALIP_MODULE_NAME " (%*s)", ip->name.length, ip->name.chars);
else
ffStrbufSetS(&key, FF_LOCALIP_MODULE_NAME);
}
else
{
ffStrbufClear(&key);
ffParseFormatString(&key, &instance->config.localIP.key, 1, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRBUF, &ip->name}
});
}
formatKey(instance, ip, &key);
if(instance->config.localIP.outputFormat.length == 0)
{
ffPrintLogoAndKey(instance, key.chars, 0, NULL);