From 38e2f8cf3e70758853f4a0c71f8efbbd6d659244 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sun, 17 Oct 2021 12:42:15 +0800 Subject: [PATCH] LocalIp: add new module --- CMakeLists.txt | 1 + completions/bash | 6 ++++ src/common/init.c | 6 ++++ src/fastfetch.c | 22 +++++++++++-- src/fastfetch.h | 7 ++++ src/flashfetch.c | 1 + src/modules/localip.c | 75 +++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 src/modules/localip.c diff --git a/CMakeLists.txt b/CMakeLists.txt index f6e2c038..83b5e697 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -116,6 +116,7 @@ set(SRCS src/modules/disk.c src/modules/battery.c src/modules/locale.c + src/modules/localip.c src/modules/colors.c ) diff --git a/completions/bash b/completions/bash index dcffc448..bd293a18 100644 --- a/completions/bash +++ b/completions/bash @@ -25,6 +25,7 @@ __fastfetch_complete_help() "disk-format" "battery-format" "locale-format" + "local-ip-format" ) COMPREPLY=($(compgen -W "${__ff_helps[*]}" -- "$CURRENT_WORD")) } @@ -121,6 +122,9 @@ __fastfetch_completion() "--allow-slow-operations" "--disable-linewrap" "--hide-cursor" + "--localip-show-ipv4" + "--localip-show-ipv6" + "--localip-show-loop" ) local FF_OPTIONS_STRING=( @@ -179,6 +183,8 @@ __fastfetch_completion() "--locale-key" "--disk-folders" "--disk-key" + "--local-ip-format" + "--local-ip-key" ) local FF_OPTIONS_PATH=( diff --git a/src/common/init.c b/src/common/init.c index 9564e444..1fa2bb01 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -165,6 +165,8 @@ static void defaultConfig(FFinstance* instance) ffStrbufInitA(&instance->config.batteryKey, 1); ffStrbufInitA(&instance->config.localeFormat, 1); ffStrbufInitA(&instance->config.localeKey, 1); + ffStrbufInitA(&instance->config.localIpKey, 1); + ffStrbufInitA(&instance->config.localIpFormat, 1); ffStrbufInitA(&instance->config.libPCI, 1); ffStrbufInitA(&instance->config.libX11, 1); @@ -178,6 +180,10 @@ static void defaultConfig(FFinstance* instance) ffStrbufInitA(&instance->config.diskFolders, 1); ffStrbufInitA(&instance->config.batteryDir, 1); + + instance->config.localIpShowIpV4 = true; + instance->config.localIpShowIpV6 = false; + instance->config.localIpShowLoop = false; } void ffInitInstance(FFinstance* instance) diff --git a/src/fastfetch.c b/src/fastfetch.c index a894db47..2012746f 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -89,6 +89,7 @@ static inline void printHelp() " --disk-format \n" " --battery-format \n" " --locale-format \n" + " --local-ip-format \n" "\n" "Key options: Provide a custom key for an output\n" " --os-key \n" @@ -113,6 +114,7 @@ static inline void printHelp() " --disk-key : takes the mount path as format argument\n" " --battery-key : takes the battery index as format argument\n" " --locale-key \n" + " --local-ip-key : takes the name of this network interface as format argument\n" "\n" "Library optins: Set the path of a library to load\n" " --lib-PCI \n" @@ -125,8 +127,11 @@ static inline void printHelp() " --lib-SQLite \n" "\n" "Module specific options:\n" - " --disk-folders : A colon separated list of folder paths for the disk output. Default is \"/:/home\"\n" - " --battery-dir : The directory where the battery folders are. Standard: /sys/class/power_supply/\n" + " --disk-folders : A colon separated list of folder paths for the disk output. Default is \"/:/home\"\n" + " --battery-dir : The directory where the battery folders are. Standard: /sys/class/power_supply/\n" + " --localip-show-ipv4 : Show ipv4 addresses in local ip module. Default is true\n" + " --localip-show-ipv6 : Show ipv6 addresses in local ip module. Default is false\n" + " --localip-show-loop : Show loop back addresses (127.0.0.1) in local ip module. Default is false\n" "\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" @@ -492,6 +497,7 @@ static inline void printAvailableModules() "Icons\n" "Kernel\n" "Locale\n" + "LocalIp\n" "Memory\n" "OS\n" "Packages\n" @@ -914,6 +920,10 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con optionParseString(key, value, &instance->config.localeFormat); else if(strcasecmp(key, "--locale-key") == 0) 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) optionParseString(key, value, &instance->config.libPCI); 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); else if(strcasecmp(key, "--battery-dir") == 0) 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 { fprintf(stderr, "Error: unknown option: %s\n", key); @@ -1063,6 +1079,8 @@ static void parseStructureCommand(FFinstance* instance, FFdata* data, const char ffPrintBattery(instance); else if(strcasecmp(line, "locale") == 0) ffPrintLocale(instance); + else if(strcasecmp(line, "localip") == 0) + ffPrintLocalIp(instance); else if(strcasecmp(line, "colors") == 0) ffPrintColors(instance); else diff --git a/src/fastfetch.h b/src/fastfetch.h index 2a023356..49231ed0 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -91,6 +91,8 @@ typedef struct FFconfig FFstrbuf batteryKey; FFstrbuf localeFormat; FFstrbuf localeKey; + FFstrbuf localIpKey; + FFstrbuf localIpFormat; FFstrbuf libPCI; FFstrbuf libX11; @@ -105,6 +107,10 @@ typedef struct FFconfig FFstrbuf batteryDir; + bool localIpShowLoop; + bool localIpShowIpV4; + bool localIpShowIpV6; + } FFconfig; typedef struct FFstate @@ -380,6 +386,7 @@ void ffPrintMemory(FFinstance* instance); void ffPrintDisk(FFinstance* instance); void ffPrintBattery(FFinstance* instance); void ffPrintLocale(FFinstance* instance); +void ffPrintLocalIp(FFinstance* instance); void ffPrintColors(FFinstance* instance); #endif diff --git a/src/flashfetch.c b/src/flashfetch.c index 5a293bce..0faa0144 100644 --- a/src/flashfetch.c +++ b/src/flashfetch.c @@ -43,6 +43,7 @@ int main(int argc, char** argv) ffPrintMemory(&instance); ffPrintDisk(&instance); ffPrintBattery(&instance); + ffPrintLocalIp(&instance); ffPrintLocale(&instance); ffPrintBreak(&instance); ffPrintColors(&instance); diff --git a/src/modules/localip.c b/src/modules/localip.c new file mode 100644 index 00000000..8e27ad68 --- /dev/null +++ b/src/modules/localip.c @@ -0,0 +1,75 @@ +#include "fastfetch.h" + +#define FF_LOCALIP_MODULE_NAME "Local Ip" +#define FF_LOCALIP_NUM_FORMAT_ARGS 1 + +#include +#include +#include +#include +#include + +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); +}