mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-11-06 13:14:21 +01:00
LocalIP: support --localip-compact-type option (#408)
This commit is contained in:
+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