mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2025-02-20 11:43:27 +08:00
LocalIP: add JSON config support
This commit is contained in:
parent
0b96818f6d
commit
7e85db6843
@ -285,7 +285,7 @@ set(LIBFASTFETCH_SRC
|
||||
src/modules/gamepad/gamepad.c
|
||||
src/modules/kernel/kernel.c
|
||||
src/modules/locale/locale.c
|
||||
src/modules/localip.c
|
||||
src/modules/localip/localip.c
|
||||
src/modules/memory.c
|
||||
src/modules/opencl.c
|
||||
src/modules/opengl.c
|
||||
|
@ -90,7 +90,7 @@ static void defaultConfig(FFinstance* instance)
|
||||
ffInitBatteryOptions(&instance->config.battery);
|
||||
initModuleArg(&instance->config.powerAdapter);
|
||||
ffInitLocaleOptions(&instance->config.locale);
|
||||
initModuleArg(&instance->config.localIP);
|
||||
ffInitLocalIpOptions(&instance->config.localIP);
|
||||
initModuleArg(&instance->config.publicIP);
|
||||
initModuleArg(&instance->config.weather);
|
||||
initModuleArg(&instance->config.wifi);
|
||||
@ -138,9 +138,6 @@ static void defaultConfig(FFinstance* instance)
|
||||
|
||||
instance->config.soundType = FF_SOUND_TYPE_MAIN;
|
||||
|
||||
instance->config.localIpShowType = FF_LOCALIP_TYPE_IPV4_BIT;
|
||||
ffStrbufInit(&instance->config.localIpNamePrefix);
|
||||
|
||||
instance->config.publicIpTimeout = 0;
|
||||
ffStrbufInit(&instance->config.publicIpUrl);
|
||||
|
||||
@ -317,7 +314,7 @@ static void destroyConfig(FFinstance* instance)
|
||||
ffDestroyBatteryOptions(&instance->config.battery);
|
||||
destroyModuleArg(&instance->config.powerAdapter);
|
||||
ffDestroyLocaleOptions(&instance->config.locale);
|
||||
destroyModuleArg(&instance->config.localIP);
|
||||
ffDestroyLocalIpOptions(&instance->config.localIP);
|
||||
destroyModuleArg(&instance->config.publicIP);
|
||||
destroyModuleArg(&instance->config.wallpaper);
|
||||
destroyModuleArg(&instance->config.weather);
|
||||
@ -360,7 +357,6 @@ static void destroyConfig(FFinstance* instance)
|
||||
ffStrbufDestroy(&instance->config.libwlanapi);
|
||||
ffStrbufDestroy(&instance->config.libnm);
|
||||
|
||||
ffStrbufDestroy(&instance->config.localIpNamePrefix);
|
||||
ffStrbufDestroy(&instance->config.publicIpUrl);
|
||||
ffStrbufDestroy(&instance->config.weatherOutputFormat);
|
||||
ffStrbufDestroy(&instance->config.playerName);
|
||||
|
@ -13,6 +13,6 @@ typedef struct FFLocalIpResult
|
||||
FFstrbuf mac;
|
||||
} FFLocalIpResult;
|
||||
|
||||
const char* ffDetectLocalIps(const FFinstance* instance, FFlist* results);
|
||||
const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results);
|
||||
|
||||
#endif
|
||||
|
@ -46,7 +46,7 @@ static void addNewIp(FFlist* list, const char* name, const char* addr, int type)
|
||||
}
|
||||
}
|
||||
|
||||
const char* ffDetectLocalIps(const FFinstance* instance, FFlist* results)
|
||||
const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
|
||||
{
|
||||
struct ifaddrs* ifAddrStruct = NULL;
|
||||
if(getifaddrs(&ifAddrStruct) < 0)
|
||||
@ -57,15 +57,15 @@ const char* ffDetectLocalIps(const FFinstance* instance, FFlist* results)
|
||||
if (!ifa->ifa_addr || !(ifa->ifa_flags & IFF_RUNNING))
|
||||
continue;
|
||||
|
||||
if ((ifa->ifa_flags & IFF_LOOPBACK) && !(instance->config.localIpShowType & FF_LOCALIP_TYPE_LOOP_BIT))
|
||||
if ((ifa->ifa_flags & IFF_LOOPBACK) && !(options->showType & FF_LOCALIP_TYPE_LOOP_BIT))
|
||||
continue;
|
||||
|
||||
if (instance->config.localIpNamePrefix.length && strncmp(ifa->ifa_name, instance->config.localIpNamePrefix.chars, instance->config.localIpNamePrefix.length) != 0)
|
||||
if (options->namePrefix.length && strncmp(ifa->ifa_name, options->namePrefix.chars, options->namePrefix.length) != 0)
|
||||
continue;
|
||||
|
||||
if (ifa->ifa_addr->sa_family == AF_INET)
|
||||
{
|
||||
if (!(instance->config.localIpShowType & FF_LOCALIP_TYPE_IPV4_BIT))
|
||||
if (!(options->showType & FF_LOCALIP_TYPE_IPV4_BIT))
|
||||
continue;
|
||||
|
||||
struct sockaddr_in* ipv4 = (struct sockaddr_in*) ifa->ifa_addr;
|
||||
@ -75,7 +75,7 @@ const char* ffDetectLocalIps(const FFinstance* instance, FFlist* results)
|
||||
}
|
||||
else if (ifa->ifa_addr->sa_family == AF_INET6)
|
||||
{
|
||||
if (!(instance->config.localIpShowType & FF_LOCALIP_TYPE_IPV6_BIT))
|
||||
if (!(options->showType & FF_LOCALIP_TYPE_IPV6_BIT))
|
||||
continue;
|
||||
|
||||
struct sockaddr_in6* ipv6 = (struct sockaddr_in6 *)ifa->ifa_addr;
|
||||
@ -86,7 +86,7 @@ const char* ffDetectLocalIps(const FFinstance* instance, FFlist* results)
|
||||
#if defined(__FreeBSD__) || defined(__APPLE__)
|
||||
else if (ifa->ifa_addr->sa_family == AF_LINK)
|
||||
{
|
||||
if (!(instance->config.localIpShowType & FF_LOCALIP_TYPE_MAC_BIT))
|
||||
if (!(options->showType & FF_LOCALIP_TYPE_MAC_BIT))
|
||||
continue;
|
||||
|
||||
char addressBuffer[32];
|
||||
@ -98,7 +98,7 @@ const char* ffDetectLocalIps(const FFinstance* instance, FFlist* results)
|
||||
#else
|
||||
else if (ifa->ifa_addr->sa_family == AF_PACKET)
|
||||
{
|
||||
if (!(instance->config.localIpShowType & FF_LOCALIP_TYPE_MAC_BIT))
|
||||
if (!(options->showType & FF_LOCALIP_TYPE_MAC_BIT))
|
||||
continue;
|
||||
|
||||
char addressBuffer[32];
|
||||
|
@ -37,7 +37,7 @@ static void addNewIp(FFlist* list, const char* name, const char* value, int type
|
||||
}
|
||||
}
|
||||
|
||||
const char* ffDetectLocalIps(const FFinstance* instance, FFlist* results)
|
||||
const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
|
||||
{
|
||||
IP_ADAPTER_ADDRESSES* FF_AUTO_FREE adapter_addresses = NULL;
|
||||
|
||||
@ -51,8 +51,8 @@ const char* ffDetectLocalIps(const FFinstance* instance, FFlist* results)
|
||||
assert(adapter_addresses);
|
||||
|
||||
DWORD error = GetAdaptersAddresses(
|
||||
instance->config.localIpShowType & FF_LOCALIP_TYPE_IPV4_BIT
|
||||
? instance->config.localIpShowType & FF_LOCALIP_TYPE_IPV6_BIT ? AF_UNSPEC : AF_INET
|
||||
options->showType & FF_LOCALIP_TYPE_IPV4_BIT
|
||||
? options->showType & FF_LOCALIP_TYPE_IPV6_BIT ? AF_UNSPEC : AF_INET
|
||||
: AF_INET6,
|
||||
GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER,
|
||||
NULL,
|
||||
@ -71,17 +71,17 @@ const char* ffDetectLocalIps(const FFinstance* instance, FFlist* results)
|
||||
for (IP_ADAPTER_ADDRESSES* adapter = adapter_addresses; adapter; adapter = adapter->Next)
|
||||
{
|
||||
bool isLoop = adapter->IfType == IF_TYPE_SOFTWARE_LOOPBACK;
|
||||
if (isLoop && !(instance->config.localIpShowType & FF_LOCALIP_TYPE_LOOP_BIT))
|
||||
if (isLoop && !(options->showType & FF_LOCALIP_TYPE_LOOP_BIT))
|
||||
continue;
|
||||
|
||||
bool newIp = true;
|
||||
|
||||
char name[128];
|
||||
WideCharToMultiByte(CP_UTF8, 0, adapter->FriendlyName, -1, name, sizeof(name), NULL, NULL);
|
||||
if (instance->config.localIpNamePrefix.length && strncmp(name, instance->config.localIpNamePrefix.chars, instance->config.localIpNamePrefix.length) != 0)
|
||||
if (options->namePrefix.length && strncmp(name, options->namePrefix.chars, options->namePrefix.length) != 0)
|
||||
continue;
|
||||
|
||||
if (instance->config.localIpShowType & FF_LOCALIP_TYPE_MAC_BIT && adapter->PhysicalAddressLength == 6)
|
||||
if (options->showType & FF_LOCALIP_TYPE_MAC_BIT && adapter->PhysicalAddressLength == 6)
|
||||
{
|
||||
char addressBuffer[32];
|
||||
uint8_t* ptr = adapter->PhysicalAddress;
|
||||
|
@ -1011,7 +1011,7 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
||||
else if(ffParseBatteryCommandOptions(&instance->config.battery, key, value)) {}
|
||||
else if(optionParseModuleArgs(key, value, "poweradapter", &instance->config.powerAdapter)) {}
|
||||
else if(ffParseLocaleCommandOptions(&instance->config.locale, key, value)) {}
|
||||
else if(optionParseModuleArgs(key, value, "localip", &instance->config.localIP)) {}
|
||||
else if(ffParseLocalIpCommandOptions(&instance->config.localIP, key, value)) {}
|
||||
else if(optionParseModuleArgs(key, value, "publicip", &instance->config.publicIP)) {}
|
||||
else if(optionParseModuleArgs(key, value, "weather", &instance->config.weather)) {}
|
||||
else if(optionParseModuleArgs(key, value, "player", &instance->config.player)) {}
|
||||
@ -1104,18 +1104,6 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
||||
NULL
|
||||
);
|
||||
}
|
||||
else if(strcasecmp(key, "--localip-show-ipv4") == 0)
|
||||
optionParseBoolean(value) ? (instance->config.localIpShowType |= FF_LOCALIP_TYPE_IPV4_BIT) : (instance->config.localIpShowType &= ~FF_LOCALIP_TYPE_IPV4_BIT);
|
||||
else if(strcasecmp(key, "--localip-show-ipv6") == 0)
|
||||
optionParseBoolean(value) ? (instance->config.localIpShowType |= FF_LOCALIP_TYPE_IPV6_BIT) : (instance->config.localIpShowType &= ~FF_LOCALIP_TYPE_IPV6_BIT);
|
||||
else if(strcasecmp(key, "--localip-show-mac") == 0)
|
||||
optionParseBoolean(value) ? (instance->config.localIpShowType |= FF_LOCALIP_TYPE_MAC_BIT) : (instance->config.localIpShowType &= ~FF_LOCALIP_TYPE_MAC_BIT);
|
||||
else if(strcasecmp(key, "--localip-show-loop") == 0)
|
||||
optionParseBoolean(value) ? (instance->config.localIpShowType |= FF_LOCALIP_TYPE_LOOP_BIT) : (instance->config.localIpShowType &= ~FF_LOCALIP_TYPE_LOOP_BIT);
|
||||
else if(strcasecmp(key, "--localip-compact") == 0)
|
||||
optionParseBoolean(value) ? (instance->config.localIpShowType |= FF_LOCALIP_TYPE_COMPACT_BIT) : (instance->config.localIpShowType &= ~FF_LOCALIP_TYPE_COMPACT_BIT);
|
||||
else if(strcasecmp(key, "--localip-name-prefix") == 0)
|
||||
optionParseString(key, value, &instance->config.localIpNamePrefix);
|
||||
else if(strcasecmp(key, "--player-name") == 0)
|
||||
optionParseString(key, value, &instance->config.playerName);
|
||||
else if(strcasecmp(key, "--publicip-url") == 0)
|
||||
@ -1261,7 +1249,7 @@ static void parseStructureCommand(FFinstance* instance, const char* line)
|
||||
else if(strcasecmp(line, FF_LOCALE_MODULE_NAME) == 0)
|
||||
ffPrintLocale(instance, &instance->config.locale);
|
||||
else if(strcasecmp(line, "localip") == 0)
|
||||
ffPrintLocalIp(instance);
|
||||
ffPrintLocalIp(instance, &instance->config.localIP);
|
||||
else if(strcasecmp(line, "publicip") == 0)
|
||||
ffPrintPublicIp(instance);
|
||||
else if(strcasecmp(line, "wifi") == 0)
|
||||
|
@ -26,13 +26,6 @@ typedef enum FFSoundType
|
||||
FF_SOUND_TYPE_ALL,
|
||||
} FFSoundType;
|
||||
|
||||
typedef enum FFLocalIpCompactType
|
||||
{
|
||||
FF_LOCALIP_COMPACT_TYPE_NONE,
|
||||
FF_LOCALIP_COMPACT_TYPE_MULTILINE,
|
||||
FF_LOCALIP_COMPACT_TYPE_ONELINE,
|
||||
} FFLocalIpCompactType;
|
||||
|
||||
typedef enum FFBinaryPrefixType
|
||||
{
|
||||
FF_BINARY_PREFIX_TYPE_IEC, // 1024 Bytes = 1 KiB, 1024 KiB = 1 MiB, ... (standard)
|
||||
@ -48,17 +41,6 @@ typedef enum FFGLType
|
||||
FF_GL_TYPE_OSMESA
|
||||
} FFGLType;
|
||||
|
||||
typedef enum FFLocalIpType
|
||||
{
|
||||
FF_LOCALIP_TYPE_NONE,
|
||||
FF_LOCALIP_TYPE_LOOP_BIT = 1 << 0,
|
||||
FF_LOCALIP_TYPE_IPV4_BIT = 1 << 1,
|
||||
FF_LOCALIP_TYPE_IPV6_BIT = 1 << 2,
|
||||
FF_LOCALIP_TYPE_MAC_BIT = 1 << 3,
|
||||
|
||||
FF_LOCALIP_TYPE_COMPACT_BIT = 1 << 10,
|
||||
} FFLocalIpType;
|
||||
|
||||
typedef struct FFconfig
|
||||
{
|
||||
FFLogoOptions logo;
|
||||
@ -116,7 +98,7 @@ typedef struct FFconfig
|
||||
FFBatteryOptions battery;
|
||||
FFModuleArgs powerAdapter;
|
||||
FFLocaleOptions locale;
|
||||
FFModuleArgs localIP;
|
||||
FFLocalIpOptions localIP;
|
||||
FFModuleArgs publicIP;
|
||||
FFModuleArgs weather;
|
||||
FFModuleArgs player;
|
||||
@ -160,9 +142,6 @@ typedef struct FFconfig
|
||||
bool shellVersion;
|
||||
bool terminalVersion;
|
||||
|
||||
FFstrbuf localIpNamePrefix;
|
||||
FFLocalIpType localIpShowType;
|
||||
|
||||
FFstrbuf publicIpUrl;
|
||||
uint32_t publicIpTimeout;
|
||||
|
||||
@ -250,7 +229,6 @@ void ffPrintMedia(FFinstance* instance);
|
||||
void ffPrintDateTime(FFinstance* instance, FFDateTimeOptions* options);
|
||||
void ffPrintDate(FFinstance* instance);
|
||||
void ffPrintTime(FFinstance* instance);
|
||||
void ffPrintLocalIp(FFinstance* instance);
|
||||
void ffPrintPublicIp(FFinstance* instance);
|
||||
void ffPrintWeather(FFinstance* instance);
|
||||
void ffPrintWifi(FFinstance* instance);
|
||||
|
@ -84,6 +84,7 @@ static bool parseModuleJsonObject(FFinstance* instance, const char* type, json_o
|
||||
case 'L': {
|
||||
return
|
||||
tryModule(instance, type, module, FF_LOCALE_MODULE_NAME, ffParseLocaleJsonObject) ||
|
||||
tryModule(instance, type, module, FF_LOCALIP_MODULE_NAME, ffParseLocalIpJsonObject) ||
|
||||
false;
|
||||
}
|
||||
|
||||
|
@ -1,119 +0,0 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "detection/localip/localip.h"
|
||||
|
||||
#define FF_LOCALIP_MODULE_NAME "Local IP"
|
||||
#define FF_LOCALIP_NUM_FORMAT_ARGS 2
|
||||
|
||||
static int sortIps(const FFLocalIpResult* left, const FFLocalIpResult* right)
|
||||
{
|
||||
return ffStrbufComp(&left->name, &right->name);
|
||||
}
|
||||
|
||||
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 printIp(FFLocalIpResult* ip)
|
||||
{
|
||||
bool flag = false;
|
||||
if (ip->ipv4.length)
|
||||
{
|
||||
ffStrbufWriteTo(&ip->ipv4, stdout);
|
||||
flag = true;
|
||||
}
|
||||
if (ip->ipv6.length)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
void ffPrintLocalIp(FFinstance* instance)
|
||||
{
|
||||
FF_LIST_AUTO_DESTROY results;
|
||||
ffListInit(&results, sizeof(FFLocalIpResult));
|
||||
|
||||
const char* error = ffDetectLocalIps(instance, &results);
|
||||
|
||||
if(error)
|
||||
{
|
||||
ffPrintError(instance, FF_LOCALIP_MODULE_NAME, 0, &instance->config.localIP, "%s", error);
|
||||
return;
|
||||
}
|
||||
|
||||
if(results.length == 0)
|
||||
{
|
||||
ffPrintError(instance, FF_LOCALIP_MODULE_NAME, 0, &instance->config.localIP, "Failed to detect any IPs");
|
||||
return;
|
||||
}
|
||||
|
||||
ffListSort(&results, (void*) sortIps);
|
||||
|
||||
if (instance->config.localIpShowType & FF_LOCALIP_TYPE_COMPACT_BIT)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_LOCALIP_MODULE_NAME, 0, &instance->config.localIP.key);
|
||||
|
||||
FF_LIST_FOR_EACH(FFLocalIpResult, ip, results)
|
||||
{
|
||||
if ((void*) ip != (void*) results.data)
|
||||
fputs(" - ", stdout);
|
||||
printIp(ip);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
FF_STRBUF_AUTO_DESTROY key;
|
||||
ffStrbufInit(&key);
|
||||
|
||||
FF_LIST_FOR_EACH(FFLocalIpResult, ip, results)
|
||||
{
|
||||
formatKey(instance, ip, &key);
|
||||
if(instance->config.localIP.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, key.chars, 0, NULL);
|
||||
printIp(ip);
|
||||
putchar('\n');
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormatString(instance, key.chars, 0, NULL, &instance->config.localIP.outputFormat, FF_LOCALIP_NUM_FORMAT_ARGS, (FFformatarg[]){
|
||||
{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},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FF_LIST_FOR_EACH(FFLocalIpResult, ip, results)
|
||||
{
|
||||
ffStrbufDestroy(&ip->name);
|
||||
ffStrbufDestroy(&ip->ipv4);
|
||||
ffStrbufDestroy(&ip->ipv6);
|
||||
ffStrbufDestroy(&ip->mac);
|
||||
}
|
||||
}
|
269
src/modules/localip/localip.c
Normal file
269
src/modules/localip/localip.c
Normal file
@ -0,0 +1,269 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "detection/localip/localip.h"
|
||||
#include "modules/localip/localip.h"
|
||||
|
||||
#define FF_LOCALIP_DISPLAY_NAME "Local IP"
|
||||
#define FF_LOCALIP_NUM_FORMAT_ARGS 2
|
||||
#pragma GCC diagnostic ignored "-Wsign-conversion"
|
||||
|
||||
static int sortIps(const FFLocalIpResult* left, const FFLocalIpResult* right)
|
||||
{
|
||||
return ffStrbufComp(&left->name, &right->name);
|
||||
}
|
||||
|
||||
static void formatKey(const FFLocalIpOptions* options, const FFLocalIpResult* ip, FFstrbuf* key)
|
||||
{
|
||||
if(options->moduleArgs.key.length == 0)
|
||||
{
|
||||
if(ip->name.length)
|
||||
ffStrbufSetF(key, FF_LOCALIP_DISPLAY_NAME " (%s)", ip->name.chars);
|
||||
else
|
||||
ffStrbufSetS(key, FF_LOCALIP_DISPLAY_NAME);
|
||||
}
|
||||
else
|
||||
{
|
||||
ffStrbufClear(key);
|
||||
ffParseFormatString(key, &options->moduleArgs.key, 2, (FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &ip->name},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
static void printIp(FFLocalIpResult* ip)
|
||||
{
|
||||
bool flag = false;
|
||||
if (ip->ipv4.length)
|
||||
{
|
||||
ffStrbufWriteTo(&ip->ipv4, stdout);
|
||||
flag = true;
|
||||
}
|
||||
if (ip->ipv6.length)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
void ffPrintLocalIp(FFinstance* instance, FFLocalIpOptions* options)
|
||||
{
|
||||
FF_LIST_AUTO_DESTROY results;
|
||||
ffListInit(&results, sizeof(FFLocalIpResult));
|
||||
|
||||
const char* error = ffDetectLocalIps(options, &results);
|
||||
|
||||
if(error)
|
||||
{
|
||||
ffPrintError(instance, FF_LOCALIP_DISPLAY_NAME, 0, &options->moduleArgs, "%s", error);
|
||||
return;
|
||||
}
|
||||
|
||||
if(results.length == 0)
|
||||
{
|
||||
ffPrintError(instance, FF_LOCALIP_DISPLAY_NAME, 0, &options->moduleArgs, "Failed to detect any IPs");
|
||||
return;
|
||||
}
|
||||
|
||||
ffListSort(&results, (const void*) sortIps);
|
||||
|
||||
if (options->showType & FF_LOCALIP_TYPE_COMPACT_BIT)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_LOCALIP_DISPLAY_NAME, 0, &options->moduleArgs.key);
|
||||
|
||||
FF_LIST_FOR_EACH(FFLocalIpResult, ip, results)
|
||||
{
|
||||
if ((void*) ip != (void*) results.data)
|
||||
fputs(" - ", stdout);
|
||||
printIp(ip);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
FF_STRBUF_AUTO_DESTROY key;
|
||||
ffStrbufInit(&key);
|
||||
|
||||
FF_LIST_FOR_EACH(FFLocalIpResult, ip, results)
|
||||
{
|
||||
formatKey(options, ip, &key);
|
||||
if(options->moduleArgs.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, key.chars, 0, NULL);
|
||||
printIp(ip);
|
||||
putchar('\n');
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormatString(instance, key.chars, 0, NULL, &options->moduleArgs.outputFormat, FF_LOCALIP_NUM_FORMAT_ARGS, (FFformatarg[]){
|
||||
{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},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FF_LIST_FOR_EACH(FFLocalIpResult, ip, results)
|
||||
{
|
||||
ffStrbufDestroy(&ip->name);
|
||||
ffStrbufDestroy(&ip->ipv4);
|
||||
ffStrbufDestroy(&ip->ipv6);
|
||||
ffStrbufDestroy(&ip->mac);
|
||||
}
|
||||
}
|
||||
|
||||
void ffInitLocalIpOptions(FFLocalIpOptions* options)
|
||||
{
|
||||
options->moduleName = FF_LOCALIP_MODULE_NAME;
|
||||
ffOptionInitModuleArg(&options->moduleArgs);
|
||||
|
||||
options->showType = FF_LOCALIP_TYPE_IPV4_BIT;
|
||||
ffStrbufInit(&options->namePrefix);
|
||||
}
|
||||
|
||||
bool ffParseLocalIpCommandOptions(FFLocalIpOptions* options, const char* key, const char* value)
|
||||
{
|
||||
const char* subKey = ffOptionTestPrefix(key, FF_LOCALIP_MODULE_NAME);
|
||||
if (!subKey) return false;
|
||||
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
||||
return true;
|
||||
|
||||
if (strcasecmp(subKey, "show-ipv4") == 0)
|
||||
{
|
||||
if (ffOptionParseBoolean(value))
|
||||
options->showType |= FF_LOCALIP_TYPE_IPV4_BIT;
|
||||
else
|
||||
options->showType &= ~FF_LOCALIP_TYPE_IPV4_BIT;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strcasecmp(subKey, "show-ipv6") == 0)
|
||||
{
|
||||
if (ffOptionParseBoolean(value))
|
||||
options->showType |= FF_LOCALIP_TYPE_IPV6_BIT;
|
||||
else
|
||||
options->showType &= ~FF_LOCALIP_TYPE_IPV6_BIT;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strcasecmp(subKey, "show-mac") == 0)
|
||||
{
|
||||
if (ffOptionParseBoolean(value))
|
||||
options->showType |= FF_LOCALIP_TYPE_MAC_BIT;
|
||||
else
|
||||
options->showType &= ~FF_LOCALIP_TYPE_MAC_BIT;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strcasecmp(subKey, "show-loop") == 0)
|
||||
{
|
||||
if (ffOptionParseBoolean(value))
|
||||
options->showType |= FF_LOCALIP_TYPE_LOOP_BIT;
|
||||
else
|
||||
options->showType &= ~FF_LOCALIP_TYPE_LOOP_BIT;
|
||||
return true;
|
||||
}
|
||||
|
||||
if(strcasecmp(subKey, "compact") == 0)
|
||||
{
|
||||
if (ffOptionParseBoolean(value))
|
||||
options->showType |= FF_LOCALIP_TYPE_COMPACT_BIT;
|
||||
else
|
||||
options->showType &= ~FF_LOCALIP_TYPE_COMPACT_BIT;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strcasecmp(subKey, "name-prefix") == 0)
|
||||
{
|
||||
ffOptionParseString(key, value, &options->namePrefix);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ffDestroyLocalIpOptions(FFLocalIpOptions* options)
|
||||
{
|
||||
ffOptionDestroyModuleArg(&options->moduleArgs);
|
||||
ffStrbufDestroy(&options->namePrefix);
|
||||
}
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
void ffParseLocalIpJsonObject(FFinstance* instance, json_object* module)
|
||||
{
|
||||
FFLocalIpOptions __attribute__((__cleanup__(ffDestroyLocalIpOptions))) options;
|
||||
ffInitLocalIpOptions(&options);
|
||||
|
||||
if (module)
|
||||
{
|
||||
json_object_object_foreach(module, key, val)
|
||||
{
|
||||
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
|
||||
continue;
|
||||
|
||||
if (strcasecmp(key, "showIpv4") == 0)
|
||||
{
|
||||
if (json_object_get_boolean(val))
|
||||
options.showType |= FF_LOCALIP_TYPE_IPV4_BIT;
|
||||
else
|
||||
options.showType &= ~FF_LOCALIP_TYPE_IPV4_BIT;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strcasecmp(key, "showIpv6") == 0)
|
||||
{
|
||||
if (json_object_get_boolean(val))
|
||||
options.showType |= FF_LOCALIP_TYPE_IPV6_BIT;
|
||||
else
|
||||
options.showType &= ~FF_LOCALIP_TYPE_IPV6_BIT;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strcasecmp(key, "showMac") == 0)
|
||||
{
|
||||
if (json_object_get_boolean(val))
|
||||
options.showType |= FF_LOCALIP_TYPE_MAC_BIT;
|
||||
else
|
||||
options.showType &= ~FF_LOCALIP_TYPE_MAC_BIT;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strcasecmp(key, "showLoop") == 0)
|
||||
{
|
||||
if (json_object_get_boolean(val))
|
||||
options.showType |= FF_LOCALIP_TYPE_LOOP_BIT;
|
||||
else
|
||||
options.showType &= ~FF_LOCALIP_TYPE_LOOP_BIT;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strcasecmp(key, "compact") == 0)
|
||||
{
|
||||
if (json_object_get_boolean(val))
|
||||
options.showType |= FF_LOCALIP_TYPE_COMPACT_BIT;
|
||||
else
|
||||
options.showType &= ~FF_LOCALIP_TYPE_COMPACT_BIT;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strcasecmp(key, "namePrefix") == 0)
|
||||
{
|
||||
ffStrbufSetS(&options.namePrefix, json_object_get_string(val));
|
||||
continue;
|
||||
}
|
||||
|
||||
ffPrintError(instance, FF_LOCALIP_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
|
||||
}
|
||||
}
|
||||
|
||||
ffPrintLocalIp(instance, &options);
|
||||
}
|
||||
#endif
|
15
src/modules/localip/localip.h
Normal file
15
src/modules/localip/localip.h
Normal file
@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "fastfetch.h"
|
||||
|
||||
#define FF_LOCALIP_MODULE_NAME "LocalIp"
|
||||
|
||||
void ffPrintLocalIp(FFinstance* instance, FFLocalIpOptions* options);
|
||||
void ffInitLocalIpOptions(FFLocalIpOptions* options);
|
||||
bool ffParseLocalIpCommandOptions(FFLocalIpOptions* options, const char* key, const char* value);
|
||||
void ffDestroyLocalIpOptions(FFLocalIpOptions* options);
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
#include "common/jsonconfig.h"
|
||||
void ffParseLocalIpJsonObject(FFinstance* instance, json_object* module);
|
||||
#endif
|
25
src/modules/localip/option.h
Normal file
25
src/modules/localip/option.h
Normal file
@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
// This file will be included in "fastfetch.h", do NOT put unnecessary things here
|
||||
|
||||
#include "common/option.h"
|
||||
|
||||
typedef enum FFLocalIpType
|
||||
{
|
||||
FF_LOCALIP_TYPE_NONE,
|
||||
FF_LOCALIP_TYPE_LOOP_BIT = 1 << 0,
|
||||
FF_LOCALIP_TYPE_IPV4_BIT = 1 << 1,
|
||||
FF_LOCALIP_TYPE_IPV6_BIT = 1 << 2,
|
||||
FF_LOCALIP_TYPE_MAC_BIT = 1 << 3,
|
||||
|
||||
FF_LOCALIP_TYPE_COMPACT_BIT = 1 << 10,
|
||||
} FFLocalIpType;
|
||||
|
||||
typedef struct FFLocalIpOptions
|
||||
{
|
||||
const char* moduleName;
|
||||
FFModuleArgs moduleArgs;
|
||||
|
||||
FFLocalIpType showType;
|
||||
FFstrbuf namePrefix;
|
||||
} FFLocalIpOptions;
|
@ -24,6 +24,7 @@
|
||||
#include "modules/host/host.h"
|
||||
#include "modules/kernel/kernel.h"
|
||||
#include "modules/locale/locale.h"
|
||||
#include "modules/localip/localip.h"
|
||||
#include "modules/separator/separator.h"
|
||||
#include "modules/title/title.h"
|
||||
#include "modules/jsonconfig/jsonconfig.h"
|
||||
|
@ -21,6 +21,7 @@
|
||||
#include "modules/gpu/option.h"
|
||||
#include "modules/kernel/option.h"
|
||||
#include "modules/locale/option.h"
|
||||
#include "modules/localip/option.h"
|
||||
#include "modules/os/option.h"
|
||||
#include "modules/separator/option.h"
|
||||
#include "modules/title/option.h"
|
||||
|
Loading…
x
Reference in New Issue
Block a user