2021-10-17 12:42:15 +08:00
|
|
|
#include "fastfetch.h"
|
2022-07-25 13:15:01 +02:00
|
|
|
#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
|
|
|
|
2023-06-08 21:04:56 +08:00
|
|
|
static int sortIps(const FFLocalIpResult* left, const FFLocalIpResult* right)
|
2023-02-02 17:50:22 +08:00
|
|
|
{
|
2023-06-08 21:04:56 +08:00
|
|
|
return ffStrbufComp(&left->name, &right->name);
|
2023-02-02 17:50:22 +08:00
|
|
|
}
|
|
|
|
|
|
2023-02-02 22:25:31 +08:00
|
|
|
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}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-08 21:04:56 +08:00
|
|
|
static void printIp(FFLocalIpResult* ip)
|
2023-02-02 22:25:31 +08:00
|
|
|
{
|
2023-06-08 21:04:56 +08:00
|
|
|
bool flag = false;
|
|
|
|
|
if (ip->ipv4.length)
|
2023-02-02 22:25:31 +08:00
|
|
|
{
|
2023-06-08 21:04:56 +08:00
|
|
|
ffStrbufWriteTo(&ip->ipv4, stdout);
|
|
|
|
|
flag = true;
|
2023-02-02 22:25:31 +08:00
|
|
|
}
|
2023-06-08 21:04:56 +08:00
|
|
|
if (ip->ipv6.length)
|
2023-02-02 22:25:31 +08:00
|
|
|
{
|
2023-06-08 21:04:56 +08:00
|
|
|
if (flag) putchar(' ');
|
|
|
|
|
ffStrbufWriteTo(&ip->ipv6, stdout);
|
|
|
|
|
flag = true;
|
|
|
|
|
}
|
|
|
|
|
if (ip->mac.length)
|
|
|
|
|
{
|
|
|
|
|
if (flag)
|
|
|
|
|
printf(" (%s)", ip->mac.chars);
|
|
|
|
|
else
|
|
|
|
|
ffStrbufWriteTo(&ip->mac, stdout);
|
2023-02-02 22:25:31 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-08 22:48:13 +08:00
|
|
|
void ffPrintLocalIp(FFinstance* instance)
|
2021-10-17 12:42:15 +08:00
|
|
|
{
|
2023-02-02 17:50:22 +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);
|
2023-02-02 17:50:22 +08:00
|
|
|
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");
|
2023-02-02 17:50:22 +08:00
|
|
|
return;
|
2021-10-17 12:42:15 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-08 21:04:56 +08:00
|
|
|
ffListSort(&results, (void*) sortIps);
|
2023-02-02 22:25:31 +08:00
|
|
|
|
2023-06-08 21:04:56 +08:00
|
|
|
if (instance->config.localIpShowType & FF_LOCALIP_TYPE_COMPACT_BIT)
|
2022-10-08 22:48:13 +08:00
|
|
|
{
|
2023-06-08 21:04:56 +08:00
|
|
|
ffPrintLogoAndKey(instance, FF_LOCALIP_MODULE_NAME, 0, &instance->config.localIP.key);
|
2021-10-17 12:42:15 +08:00
|
|
|
|
2023-02-02 17:50:22 +08:00
|
|
|
FF_LIST_FOR_EACH(FFLocalIpResult, ip, results)
|
2022-10-08 22:48:13 +08:00
|
|
|
{
|
2023-06-08 21:04:56 +08:00
|
|
|
if ((void*) ip != (void*) results.data)
|
|
|
|
|
fputs(" - ", stdout);
|
|
|
|
|
printIp(ip);
|
2021-10-17 12:42:15 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-02-02 17:50:22 +08:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY key;
|
|
|
|
|
ffStrbufInit(&key);
|
2021-10-17 12:42:15 +08:00
|
|
|
|
2023-02-02 17:50:22 +08:00
|
|
|
FF_LIST_FOR_EACH(FFLocalIpResult, ip, results)
|
|
|
|
|
{
|
2023-02-02 22:25:31 +08:00
|
|
|
formatKey(instance, ip, &key);
|
2023-02-02 17:50:22 +08:00
|
|
|
if(instance->config.localIP.outputFormat.length == 0)
|
|
|
|
|
{
|
|
|
|
|
ffPrintLogoAndKey(instance, key.chars, 0, NULL);
|
2023-06-08 21:04:56 +08:00
|
|
|
printIp(ip);
|
|
|
|
|
putchar('\n');
|
2023-02-02 17:50:22 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ffPrintFormatString(instance, key.chars, 0, NULL, &instance->config.localIP.outputFormat, FF_LOCALIP_NUM_FORMAT_ARGS, (FFformatarg[]){
|
2023-06-08 21:04:56 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &ip->ipv4},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &ip->ipv6},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &ip->mac},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &ip->name},
|
2023-02-02 17:50:22 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-08 21:04:56 +08:00
|
|
|
|
|
|
|
|
FF_LIST_FOR_EACH(FFLocalIpResult, ip, results)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufDestroy(&ip->name);
|
|
|
|
|
ffStrbufDestroy(&ip->ipv4);
|
|
|
|
|
ffStrbufDestroy(&ip->ipv6);
|
|
|
|
|
ffStrbufDestroy(&ip->mac);
|
|
|
|
|
}
|
2021-10-17 12:42:15 +08:00
|
|
|
}
|