mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
LocalIP: support --localip-compact-type option (#408)
This commit is contained in:
@@ -175,6 +175,7 @@ static void defaultConfig(FFinstance* instance)
|
||||
instance->config.localIpShowIpV6 = false;
|
||||
instance->config.localIpShowLoop = false;
|
||||
ffStrbufInit(&instance->config.localIpNamePrefix);
|
||||
instance->config.localIpCompactType = FF_LOCALIP_COMPACT_TYPE_NONE;
|
||||
|
||||
instance->config.publicIpTimeout = 0;
|
||||
ffStrbufInit(&instance->config.publicIpUrl);
|
||||
|
||||
@@ -124,6 +124,7 @@ 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
|
||||
--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)
|
||||
|
||||
@@ -1290,6 +1290,16 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
||||
instance->config.localIpShowLoop = optionParseBoolean(value);
|
||||
else if(strcasecmp(key, "--localip-name-prefix") == 0)
|
||||
optionParseString(key, value, &instance->config.localIpNamePrefix);
|
||||
else if(strcasecmp(key, "--localip-compact-type") == 0)
|
||||
{
|
||||
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,
|
||||
NULL
|
||||
);
|
||||
}
|
||||
else if(strcasecmp(key, "--os-file") == 0)
|
||||
optionParseString(key, value, &instance->config.osFile);
|
||||
else if(strcasecmp(key, "--player-name") == 0)
|
||||
|
||||
@@ -41,6 +41,14 @@ typedef enum FFSoundType
|
||||
FF_SOUND_TYPE_ALL,
|
||||
} FFSoundType;
|
||||
|
||||
typedef enum FFLocalIpCompactType
|
||||
{
|
||||
FF_LOCALIP_COMPACT_TYPE_NONE,
|
||||
FF_LOCALIP_COMPACT_TYPE_DEFAULT,
|
||||
FF_LOCALIP_COMPACT_TYPE_V4FIRST,
|
||||
FF_LOCALIP_COMPACT_TYPE_V6FIRST,
|
||||
} FFLocalIpCompactType;
|
||||
|
||||
typedef enum FFBinaryPrefixType
|
||||
{
|
||||
FF_BINARY_PREFIX_TYPE_IEC, // 1024 Bytes = 1 KiB, 1024 KiB = 1 MiB, ... (standard)
|
||||
@@ -204,6 +212,7 @@ typedef struct FFconfig
|
||||
bool localIpShowIpV4;
|
||||
bool localIpShowIpV6;
|
||||
FFstrbuf localIpNamePrefix;
|
||||
FFLocalIpCompactType localIpCompactType;
|
||||
|
||||
FFstrbuf publicIpUrl;
|
||||
uint32_t publicIpTimeout;
|
||||
|
||||
+73
-36
@@ -5,9 +5,25 @@
|
||||
#define FF_LOCALIP_MODULE_NAME "Local IP"
|
||||
#define FF_LOCALIP_NUM_FORMAT_ARGS 2
|
||||
|
||||
static int sortIpsV4First(const FFLocalIpResult* left, const FFLocalIpResult* right)
|
||||
{
|
||||
if (left->ipv6 != right->ipv6)
|
||||
return left->ipv6 == false ? -1 : 1;
|
||||
|
||||
return ffStrbufComp(&left->addr, &right->addr);
|
||||
}
|
||||
|
||||
static int sortIpsV6First(const FFLocalIpResult* left, const FFLocalIpResult* right)
|
||||
{
|
||||
if (left->ipv6 != right->ipv6)
|
||||
return left->ipv6 == true ? -1 : 1;
|
||||
|
||||
return ffStrbufComp(&left->addr, &right->addr);
|
||||
}
|
||||
|
||||
void ffPrintLocalIp(FFinstance* instance)
|
||||
{
|
||||
FFlist results;
|
||||
FF_LIST_AUTO_DESTROY results;
|
||||
ffListInit(&results, sizeof(FFLocalIpResult));
|
||||
|
||||
const char* error = ffDetectLocalIps(instance, &results);
|
||||
@@ -15,56 +31,77 @@ void ffPrintLocalIp(FFinstance* instance)
|
||||
if(error)
|
||||
{
|
||||
ffPrintError(instance, FF_LOCALIP_MODULE_NAME, 0, &instance->config.localIP, "%s", error);
|
||||
goto exit;
|
||||
return;
|
||||
}
|
||||
|
||||
if(results.length == 0)
|
||||
{
|
||||
ffPrintError(instance, FF_LOCALIP_MODULE_NAME, 0, &instance->config.localIP, "Failed to detect any IPs");
|
||||
goto exit;
|
||||
return;
|
||||
}
|
||||
|
||||
FFstrbuf key;
|
||||
ffStrbufInit(&key);
|
||||
|
||||
for(uint32_t i = 0; i < results.length; ++i)
|
||||
if (instance->config.localIpCompactType != FF_LOCALIP_COMPACT_TYPE_NONE)
|
||||
{
|
||||
FFLocalIpResult* ip = (FFLocalIpResult*) ffListGet(&results, i);
|
||||
ffPrintLogoAndKey(instance, FF_LOCALIP_MODULE_NAME, 0, &instance->config.localIP.key);
|
||||
|
||||
if(instance->config.localIP.key.length == 0)
|
||||
switch (instance->config.localIpCompactType)
|
||||
{
|
||||
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}
|
||||
});
|
||||
case FF_LOCALIP_COMPACT_TYPE_V4FIRST:
|
||||
ffListSort(&results, (void*) sortIpsV4First);
|
||||
break;
|
||||
case FF_LOCALIP_COMPACT_TYPE_V6FIRST:
|
||||
ffListSort(&results, (void*) sortIpsV6First);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if(instance->config.localIP.outputFormat.length == 0)
|
||||
uint32_t index = 0;
|
||||
FF_LIST_FOR_EACH(FFLocalIpResult, ip, results)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, key.chars, 0, NULL);
|
||||
ffStrbufPutTo(&ip->addr, stdout);
|
||||
if (index++ > 0)
|
||||
putchar(' ');
|
||||
ffStrbufWriteTo(&ip->addr, stdout);
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormatString(instance, key.chars, 0, NULL, &instance->config.localIP.outputFormat, FF_LOCALIP_NUM_FORMAT_ARGS, (FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &ip->addr},
|
||||
{FF_FORMAT_ARG_TYPE_STRING, ip->ipv6 ? "IPv6" : "IPv4"}
|
||||
});
|
||||
}
|
||||
|
||||
ffStrbufDestroy(&ip->name);
|
||||
ffStrbufDestroy(&ip->addr);
|
||||
putchar('\n');
|
||||
}
|
||||
else
|
||||
{
|
||||
FF_STRBUF_AUTO_DESTROY key;
|
||||
ffStrbufInit(&key);
|
||||
|
||||
ffStrbufDestroy(&key);
|
||||
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}
|
||||
});
|
||||
}
|
||||
|
||||
exit:
|
||||
ffListDestroy(&results);
|
||||
if(instance->config.localIP.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, key.chars, 0, NULL);
|
||||
ffStrbufPutTo(&ip->addr, stdout);
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormatString(instance, key.chars, 0, NULL, &instance->config.localIP.outputFormat, FF_LOCALIP_NUM_FORMAT_ARGS, (FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &ip->addr},
|
||||
{FF_FORMAT_ARG_TYPE_STRING, ip->ipv6 ? "IPv6" : "IPv4"}
|
||||
});
|
||||
}
|
||||
|
||||
ffStrbufDestroy(&ip->name);
|
||||
ffStrbufDestroy(&ip->addr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user