mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2025-02-20 11:43:27 +08:00
LocalIp: add new module
This commit is contained in:
parent
0b3f96610b
commit
38e2f8cf3e
@ -116,6 +116,7 @@ set(SRCS
|
|||||||
src/modules/disk.c
|
src/modules/disk.c
|
||||||
src/modules/battery.c
|
src/modules/battery.c
|
||||||
src/modules/locale.c
|
src/modules/locale.c
|
||||||
|
src/modules/localip.c
|
||||||
src/modules/colors.c
|
src/modules/colors.c
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,6 +25,7 @@ __fastfetch_complete_help()
|
|||||||
"disk-format"
|
"disk-format"
|
||||||
"battery-format"
|
"battery-format"
|
||||||
"locale-format"
|
"locale-format"
|
||||||
|
"local-ip-format"
|
||||||
)
|
)
|
||||||
COMPREPLY=($(compgen -W "${__ff_helps[*]}" -- "$CURRENT_WORD"))
|
COMPREPLY=($(compgen -W "${__ff_helps[*]}" -- "$CURRENT_WORD"))
|
||||||
}
|
}
|
||||||
@ -121,6 +122,9 @@ __fastfetch_completion()
|
|||||||
"--allow-slow-operations"
|
"--allow-slow-operations"
|
||||||
"--disable-linewrap"
|
"--disable-linewrap"
|
||||||
"--hide-cursor"
|
"--hide-cursor"
|
||||||
|
"--localip-show-ipv4"
|
||||||
|
"--localip-show-ipv6"
|
||||||
|
"--localip-show-loop"
|
||||||
)
|
)
|
||||||
|
|
||||||
local FF_OPTIONS_STRING=(
|
local FF_OPTIONS_STRING=(
|
||||||
@ -179,6 +183,8 @@ __fastfetch_completion()
|
|||||||
"--locale-key"
|
"--locale-key"
|
||||||
"--disk-folders"
|
"--disk-folders"
|
||||||
"--disk-key"
|
"--disk-key"
|
||||||
|
"--local-ip-format"
|
||||||
|
"--local-ip-key"
|
||||||
)
|
)
|
||||||
|
|
||||||
local FF_OPTIONS_PATH=(
|
local FF_OPTIONS_PATH=(
|
||||||
|
@ -165,6 +165,8 @@ static void defaultConfig(FFinstance* instance)
|
|||||||
ffStrbufInitA(&instance->config.batteryKey, 1);
|
ffStrbufInitA(&instance->config.batteryKey, 1);
|
||||||
ffStrbufInitA(&instance->config.localeFormat, 1);
|
ffStrbufInitA(&instance->config.localeFormat, 1);
|
||||||
ffStrbufInitA(&instance->config.localeKey, 1);
|
ffStrbufInitA(&instance->config.localeKey, 1);
|
||||||
|
ffStrbufInitA(&instance->config.localIpKey, 1);
|
||||||
|
ffStrbufInitA(&instance->config.localIpFormat, 1);
|
||||||
|
|
||||||
ffStrbufInitA(&instance->config.libPCI, 1);
|
ffStrbufInitA(&instance->config.libPCI, 1);
|
||||||
ffStrbufInitA(&instance->config.libX11, 1);
|
ffStrbufInitA(&instance->config.libX11, 1);
|
||||||
@ -178,6 +180,10 @@ static void defaultConfig(FFinstance* instance)
|
|||||||
ffStrbufInitA(&instance->config.diskFolders, 1);
|
ffStrbufInitA(&instance->config.diskFolders, 1);
|
||||||
|
|
||||||
ffStrbufInitA(&instance->config.batteryDir, 1);
|
ffStrbufInitA(&instance->config.batteryDir, 1);
|
||||||
|
|
||||||
|
instance->config.localIpShowIpV4 = true;
|
||||||
|
instance->config.localIpShowIpV6 = false;
|
||||||
|
instance->config.localIpShowLoop = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ffInitInstance(FFinstance* instance)
|
void ffInitInstance(FFinstance* instance)
|
||||||
|
@ -89,6 +89,7 @@ static inline void printHelp()
|
|||||||
" --disk-format <format>\n"
|
" --disk-format <format>\n"
|
||||||
" --battery-format <format>\n"
|
" --battery-format <format>\n"
|
||||||
" --locale-format <format>\n"
|
" --locale-format <format>\n"
|
||||||
|
" --local-ip-format <format>\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Key options: Provide a custom key for an output\n"
|
"Key options: Provide a custom key for an output\n"
|
||||||
" --os-key <key>\n"
|
" --os-key <key>\n"
|
||||||
@ -113,6 +114,7 @@ static inline void printHelp()
|
|||||||
" --disk-key <key>: takes the mount path as format argument\n"
|
" --disk-key <key>: takes the mount path as format argument\n"
|
||||||
" --battery-key <key>: takes the battery index as format argument\n"
|
" --battery-key <key>: takes the battery index as format argument\n"
|
||||||
" --locale-key <key>\n"
|
" --locale-key <key>\n"
|
||||||
|
" --local-ip-key <key>: takes the name of this network interface as format argument\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Library optins: Set the path of a library to load\n"
|
"Library optins: Set the path of a library to load\n"
|
||||||
" --lib-PCI <path>\n"
|
" --lib-PCI <path>\n"
|
||||||
@ -125,8 +127,11 @@ static inline void printHelp()
|
|||||||
" --lib-SQLite <path>\n"
|
" --lib-SQLite <path>\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Module specific options:\n"
|
"Module specific options:\n"
|
||||||
" --disk-folders <folders>: A colon separated list of folder paths for the disk output. Default is \"/:/home\"\n"
|
" --disk-folders <folders>: A colon separated list of folder paths for the disk output. Default is \"/:/home\"\n"
|
||||||
" --battery-dir <folder>: The directory where the battery folders are. Standard: /sys/class/power_supply/\n"
|
" --battery-dir <folder>: The directory where the battery folders are. Standard: /sys/class/power_supply/\n"
|
||||||
|
" --localip-show-ipv4 <?value>: Show ipv4 addresses in local ip module. Default is true\n"
|
||||||
|
" --localip-show-ipv6 <?value>: Show ipv6 addresses in local ip module. Default is false\n"
|
||||||
|
" --localip-show-loop <?value>: Show loop back addresses (127.0.0.1) in local ip module. Default is false\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Parsing is not case sensitive. E.g. \"--lib-PCI\" is equal to \"--Lib-Pci\"\n"
|
"Parsing is not case sensitive. E.g. \"--lib-PCI\" is equal to \"--Lib-Pci\"\n"
|
||||||
"If a value starts with a ?, it is optional. \"true\" will be used if not set.\n"
|
"If a value starts with a ?, it is optional. \"true\" will be used if not set.\n"
|
||||||
@ -492,6 +497,7 @@ static inline void printAvailableModules()
|
|||||||
"Icons\n"
|
"Icons\n"
|
||||||
"Kernel\n"
|
"Kernel\n"
|
||||||
"Locale\n"
|
"Locale\n"
|
||||||
|
"LocalIp\n"
|
||||||
"Memory\n"
|
"Memory\n"
|
||||||
"OS\n"
|
"OS\n"
|
||||||
"Packages\n"
|
"Packages\n"
|
||||||
@ -914,6 +920,10 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
|||||||
optionParseString(key, value, &instance->config.localeFormat);
|
optionParseString(key, value, &instance->config.localeFormat);
|
||||||
else if(strcasecmp(key, "--locale-key") == 0)
|
else if(strcasecmp(key, "--locale-key") == 0)
|
||||||
optionParseString(key, value, &instance->config.localeKey);
|
optionParseString(key, value, &instance->config.localeKey);
|
||||||
|
else if(strcasecmp(key, "--local-ip-key") == 0)
|
||||||
|
optionParseString(key, value, &instance->config.localIpKey);
|
||||||
|
else if(strcasecmp(key, "--local-ip-format") == 0)
|
||||||
|
optionParseString(key, value, &instance->config.localIpFormat);
|
||||||
else if(strcasecmp(key, "--lib-PCI") == 0)
|
else if(strcasecmp(key, "--lib-PCI") == 0)
|
||||||
optionParseString(key, value, &instance->config.libPCI);
|
optionParseString(key, value, &instance->config.libPCI);
|
||||||
else if(strcasecmp(key, "--lib-X11") == 0)
|
else if(strcasecmp(key, "--lib-X11") == 0)
|
||||||
@ -934,6 +944,12 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
|||||||
optionParseString(key, value, &instance->config.diskFolders);
|
optionParseString(key, value, &instance->config.diskFolders);
|
||||||
else if(strcasecmp(key, "--battery-dir") == 0)
|
else if(strcasecmp(key, "--battery-dir") == 0)
|
||||||
optionParseString(key, value, &instance->config.batteryDir);
|
optionParseString(key, value, &instance->config.batteryDir);
|
||||||
|
else if(strcasecmp(key, "--localip-show-ipv4") == 0)
|
||||||
|
instance->config.localIpShowIpV4 = optionParseBoolean(value);
|
||||||
|
else if(strcasecmp(key, "--localip-show-ipv6") == 0)
|
||||||
|
instance->config.localIpShowIpV6 = optionParseBoolean(value);
|
||||||
|
else if(strcasecmp(key, "--localip-show-loop") == 0)
|
||||||
|
instance->config.localIpShowLoop = optionParseBoolean(value);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Error: unknown option: %s\n", key);
|
fprintf(stderr, "Error: unknown option: %s\n", key);
|
||||||
@ -1063,6 +1079,8 @@ static void parseStructureCommand(FFinstance* instance, FFdata* data, const char
|
|||||||
ffPrintBattery(instance);
|
ffPrintBattery(instance);
|
||||||
else if(strcasecmp(line, "locale") == 0)
|
else if(strcasecmp(line, "locale") == 0)
|
||||||
ffPrintLocale(instance);
|
ffPrintLocale(instance);
|
||||||
|
else if(strcasecmp(line, "localip") == 0)
|
||||||
|
ffPrintLocalIp(instance);
|
||||||
else if(strcasecmp(line, "colors") == 0)
|
else if(strcasecmp(line, "colors") == 0)
|
||||||
ffPrintColors(instance);
|
ffPrintColors(instance);
|
||||||
else
|
else
|
||||||
|
@ -91,6 +91,8 @@ typedef struct FFconfig
|
|||||||
FFstrbuf batteryKey;
|
FFstrbuf batteryKey;
|
||||||
FFstrbuf localeFormat;
|
FFstrbuf localeFormat;
|
||||||
FFstrbuf localeKey;
|
FFstrbuf localeKey;
|
||||||
|
FFstrbuf localIpKey;
|
||||||
|
FFstrbuf localIpFormat;
|
||||||
|
|
||||||
FFstrbuf libPCI;
|
FFstrbuf libPCI;
|
||||||
FFstrbuf libX11;
|
FFstrbuf libX11;
|
||||||
@ -105,6 +107,10 @@ typedef struct FFconfig
|
|||||||
|
|
||||||
FFstrbuf batteryDir;
|
FFstrbuf batteryDir;
|
||||||
|
|
||||||
|
bool localIpShowLoop;
|
||||||
|
bool localIpShowIpV4;
|
||||||
|
bool localIpShowIpV6;
|
||||||
|
|
||||||
} FFconfig;
|
} FFconfig;
|
||||||
|
|
||||||
typedef struct FFstate
|
typedef struct FFstate
|
||||||
@ -380,6 +386,7 @@ void ffPrintMemory(FFinstance* instance);
|
|||||||
void ffPrintDisk(FFinstance* instance);
|
void ffPrintDisk(FFinstance* instance);
|
||||||
void ffPrintBattery(FFinstance* instance);
|
void ffPrintBattery(FFinstance* instance);
|
||||||
void ffPrintLocale(FFinstance* instance);
|
void ffPrintLocale(FFinstance* instance);
|
||||||
|
void ffPrintLocalIp(FFinstance* instance);
|
||||||
void ffPrintColors(FFinstance* instance);
|
void ffPrintColors(FFinstance* instance);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -43,6 +43,7 @@ int main(int argc, char** argv)
|
|||||||
ffPrintMemory(&instance);
|
ffPrintMemory(&instance);
|
||||||
ffPrintDisk(&instance);
|
ffPrintDisk(&instance);
|
||||||
ffPrintBattery(&instance);
|
ffPrintBattery(&instance);
|
||||||
|
ffPrintLocalIp(&instance);
|
||||||
ffPrintLocale(&instance);
|
ffPrintLocale(&instance);
|
||||||
ffPrintBreak(&instance);
|
ffPrintBreak(&instance);
|
||||||
ffPrintColors(&instance);
|
ffPrintColors(&instance);
|
||||||
|
75
src/modules/localip.c
Normal file
75
src/modules/localip.c
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
#include "fastfetch.h"
|
||||||
|
|
||||||
|
#define FF_LOCALIP_MODULE_NAME "Local Ip"
|
||||||
|
#define FF_LOCALIP_NUM_FORMAT_ARGS 1
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <ifaddrs.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
|
static void printValue(FFinstance* instance, const char* ifaName, const char* addressBuffer)
|
||||||
|
{
|
||||||
|
FF_STRBUF_CREATE(key);
|
||||||
|
|
||||||
|
if (instance->config.localIpKey.length == 0) {
|
||||||
|
ffStrbufSetF(&key, FF_LOCALIP_MODULE_NAME " (%s)", ifaName);
|
||||||
|
} else {
|
||||||
|
ffParseFormatString(&key, &instance->config.localIpKey, NULL, 1, (FFformatarg[]){
|
||||||
|
{FF_FORMAT_ARG_TYPE_STRING, ifaName}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (instance->config.localIpFormat.length == 0) {
|
||||||
|
ffPrintLogoAndKey(instance, FF_LOCALIP_MODULE_NAME, 0, &key);
|
||||||
|
puts(addressBuffer);
|
||||||
|
} else {
|
||||||
|
ffPrintFormatString(instance, FF_LOCALIP_MODULE_NAME, 0, &key, &instance->config.localIpFormat, NULL, FF_LOCALIP_NUM_FORMAT_ARGS, (FFformatarg[]){
|
||||||
|
{FF_FORMAT_ARG_TYPE_STRING, addressBuffer}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
ffStrbufDestroy(&key);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ffPrintLocalIp(FFinstance* instance)
|
||||||
|
{
|
||||||
|
struct ifaddrs* ifAddrStruct = NULL;
|
||||||
|
int ret = getifaddrs(&ifAddrStruct);
|
||||||
|
if (ret < 0) {
|
||||||
|
ffPrintError(instance, FF_LOCALIP_MODULE_NAME, 0, &instance->config.localIpKey, &instance->config.localIpFormat, FF_LOCALIP_NUM_FORMAT_ARGS, "getifaddrs(&ifAddrStruct) < 0 (%i)", ret);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (struct ifaddrs* ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
|
||||||
|
if (!ifa->ifa_addr)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// loop back
|
||||||
|
if (strcmp(ifa->ifa_name, "lo") == 0 && !instance->config.localIpShowLoop)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (ifa->ifa_addr->sa_family == AF_INET) { // check it is IP4
|
||||||
|
// is a valid IP4 Address
|
||||||
|
if (!instance->config.localIpShowIpV4)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
void* tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
|
||||||
|
char addressBuffer[INET_ADDRSTRLEN];
|
||||||
|
inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
|
||||||
|
printValue(instance, ifa->ifa_name, addressBuffer);
|
||||||
|
} else if (ifa->ifa_addr->sa_family == AF_INET6) { // check it is IP6
|
||||||
|
// is a valid IP6 Address
|
||||||
|
if (!instance->config.localIpShowIpV6)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
void* tmpAddrPtr=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
|
||||||
|
char addressBuffer[INET6_ADDRSTRLEN];
|
||||||
|
inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
|
||||||
|
printValue(instance, ifa->ifa_name, addressBuffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ifAddrStruct) freeifaddrs(ifAddrStruct);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user