Files
fastfetch/src/modules/localip.c
T

108 lines
3.2 KiB
C
Raw Normal View History

2021-10-17 12:42:15 +08:00
#include "fastfetch.h"
#include "common/printing.h"
2022-10-08 22:48:13 +08:00
#include "detection/localip/localip.h"
2021-10-17 12:42:15 +08:00
2022-01-10 20:20:18 +01:00
#define FF_LOCALIP_MODULE_NAME "Local IP"
2022-10-08 22:48:13 +08:00
#define FF_LOCALIP_NUM_FORMAT_ARGS 2
2021-10-17 12:42:15 +08:00
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);
}
2022-10-08 22:48:13 +08:00
void ffPrintLocalIp(FFinstance* instance)
2021-10-17 12:42:15 +08:00
{
FF_LIST_AUTO_DESTROY results;
2022-10-08 22:48:13 +08:00
ffListInit(&results, sizeof(FFLocalIpResult));
2021-10-17 12:42:15 +08:00
2022-10-08 22:48:13 +08:00
const char* error = ffDetectLocalIps(instance, &results);
2021-10-17 12:42:15 +08:00
2022-10-08 22:48:13 +08:00
if(error)
2022-06-17 18:15:36 +02:00
{
2022-10-08 22:48:13 +08:00
ffPrintError(instance, FF_LOCALIP_MODULE_NAME, 0, &instance->config.localIP, "%s", error);
return;
2021-10-17 12:42:15 +08:00
}
2022-10-08 22:48:13 +08:00
if(results.length == 0)
{
ffPrintError(instance, FF_LOCALIP_MODULE_NAME, 0, &instance->config.localIP, "Failed to detect any IPs");
return;
2021-10-17 12:42:15 +08:00
}
if (instance->config.localIpCompactType != FF_LOCALIP_COMPACT_TYPE_NONE)
2022-10-08 22:48:13 +08:00
{
ffPrintLogoAndKey(instance, FF_LOCALIP_MODULE_NAME, 0, &instance->config.localIP.key);
2021-10-17 12:42:15 +08:00
switch (instance->config.localIpCompactType)
2022-10-08 22:48:13 +08:00
{
case FF_LOCALIP_COMPACT_TYPE_V4FIRST:
ffListSort(&results, (void*) sortIpsV4First);
break;
case FF_LOCALIP_COMPACT_TYPE_V6FIRST:
ffListSort(&results, (void*) sortIpsV6First);
break;
default:
break;
2022-10-08 22:48:13 +08:00
}
2021-10-17 12:42:15 +08:00
uint32_t index = 0;
FF_LIST_FOR_EACH(FFLocalIpResult, ip, results)
2022-10-08 22:48:13 +08:00
{
if (index++ > 0)
putchar(' ');
ffStrbufWriteTo(&ip->addr, stdout);
2021-10-17 12:42:15 +08:00
}
putchar('\n');
2021-10-17 12:42:15 +08:00
}
else
{
FF_STRBUF_AUTO_DESTROY key;
ffStrbufInit(&key);
2021-10-17 12:42:15 +08:00
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}
});
}
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"}
});
}
2022-10-08 22:48:13 +08:00
ffStrbufDestroy(&ip->name);
ffStrbufDestroy(&ip->addr);
}
}
2021-10-17 12:42:15 +08:00
}