mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-30 00:37:23 +01:00
LocalIP: support multiline compact mode
This commit is contained in:
+68
-32
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user