From ec3cd6cc68afbacc57bb72c33cf6e1ea4223e611 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 2 Feb 2023 22:25:31 +0800 Subject: [PATCH] LocalIP: support multiline compact mode --- src/common/init.c | 1 + src/data/help.txt | 3 +- src/fastfetch.c | 7 +-- src/fastfetch.h | 6 +-- src/modules/localip.c | 100 ++++++++++++++++++++++++++++-------------- 5 files changed, 78 insertions(+), 39 deletions(-) diff --git a/src/common/init.c b/src/common/init.c index cd1105534..cf4a46735 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -174,6 +174,7 @@ static void defaultConfig(FFinstance* instance) instance->config.localIpShowIpV4 = true; instance->config.localIpShowIpV6 = false; instance->config.localIpShowLoop = false; + instance->config.localIpV6First = false; ffStrbufInit(&instance->config.localIpNamePrefix); instance->config.localIpCompactType = FF_LOCALIP_COMPACT_TYPE_NONE; diff --git a/src/data/help.txt b/src/data/help.txt index b8cf324f7..789bfb390 100644 --- a/src/data/help.txt +++ b/src/data/help.txt @@ -124,7 +124,8 @@ Module specific options: --localip-show-ipv6 : Show IPv6 addresses in local ip module. Default is false --localip-show-loop : Show loop back addresses (127.0.0.1) in local ip module. Default is false --localip-name-prefix : Show IPs with given name prefix only. Default is empty - --localip-compact-type : Show IPs in one line. Should be either none (disable one-line mode), default (don't sort the result), v4first (print ipv4 first) and v6first. Default is none + --localip-compact-type : Show IPs in one line. Should be either none, oneline, multiline. Default is none + --localip-v6first : Set if ipv6 should be printed first. Default is false --public-ip-timeout: Time in milliseconds to wait for the public ip server to respond. Default is disabled (0) --public-ip-url: The URL of public IP detection server to be used. --weather-timeout: Time in milliseconds to wait for the weather server to respond. Default is disabled (0) diff --git a/src/fastfetch.c b/src/fastfetch.c index 594c5c87b..b5ebaf0ca 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -1282,6 +1282,8 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con optionParseString(key, value, &instance->config.batteryDir); else if(strcasecmp(key, "--separator-string") == 0) optionParseString(key, value, &instance->config.separatorString); + else if(strcasecmp(key, "--localip-v6first") == 0) + instance->config.localIpV6First = optionParseBoolean(value); else if(strcasecmp(key, "--localip-show-ipv4") == 0) instance->config.localIpShowIpV4 = optionParseBoolean(value); else if(strcasecmp(key, "--localip-show-ipv6") == 0) @@ -1294,9 +1296,8 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con { optionParseEnum(key, value, &instance->config.localIpCompactType, "none", FF_LOCALIP_COMPACT_TYPE_NONE, - "default", FF_LOCALIP_COMPACT_TYPE_DEFAULT, - "v4first", FF_LOCALIP_COMPACT_TYPE_V4FIRST, - "v6first", FF_LOCALIP_COMPACT_TYPE_V6FIRST, + "oneline", FF_LOCALIP_COMPACT_TYPE_ONELINE, + "multiline", FF_LOCALIP_COMPACT_TYPE_MULTILINE, NULL ); } diff --git a/src/fastfetch.h b/src/fastfetch.h index f31562208..627748c16 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -44,9 +44,8 @@ typedef enum FFSoundType typedef enum FFLocalIpCompactType { FF_LOCALIP_COMPACT_TYPE_NONE, - FF_LOCALIP_COMPACT_TYPE_DEFAULT, - FF_LOCALIP_COMPACT_TYPE_V4FIRST, - FF_LOCALIP_COMPACT_TYPE_V6FIRST, + FF_LOCALIP_COMPACT_TYPE_MULTILINE, + FF_LOCALIP_COMPACT_TYPE_ONELINE, } FFLocalIpCompactType; typedef enum FFBinaryPrefixType @@ -209,6 +208,7 @@ typedef struct FFconfig FFstrbuf separatorString; bool localIpShowLoop; + bool localIpV6First; bool localIpShowIpV4; bool localIpShowIpV6; FFstrbuf localIpNamePrefix; diff --git a/src/modules/localip.c b/src/modules/localip.c index d751cbdba..f0429e0a0 100644 --- a/src/modules/localip.c +++ b/src/modules/localip.c @@ -7,6 +7,10 @@ static int sortIpsV4First(const FFLocalIpResult* left, const FFLocalIpResult* right) { + int name = ffStrbufComp(&left->name, &right->name); + if (name != 0) + return name; + if (left->ipv6 != right->ipv6) return left->ipv6 == false ? -1 : 1; @@ -15,12 +19,62 @@ static int sortIpsV4First(const FFLocalIpResult* left, const FFLocalIpResult* ri static int sortIpsV6First(const FFLocalIpResult* left, const FFLocalIpResult* right) { + int name = ffStrbufComp(&left->name, &right->name); + if (name != 0) + return name; + if (left->ipv6 != right->ipv6) return left->ipv6 == true ? -1 : 1; return ffStrbufComp(&left->addr, &right->addr); } +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 printIpsOneline(FFlist* ips) +{ + uint32_t index = 0; + FF_LIST_FOR_EACH(FFLocalIpResult, ip, *ips) + { + if (index++ > 0) + putchar(' '); + ffStrbufWriteTo(&ip->addr, stdout); + } +} + +static void printIpsWithKey(FFinstance* instance, FFlist* ips) +{ + FFLocalIpResult* ip = (FFLocalIpResult*) ffListGet(ips, 0); + if (instance->config.localIpCompactType == FF_LOCALIP_COMPACT_TYPE_MULTILINE) + { + FF_STRBUF_AUTO_DESTROY key; + ffStrbufInit(&key); + formatKey(instance, ip, &key); + ffPrintLogoAndKey(instance, key.chars, 0, NULL); + } + printIpsOneline(ips); + if (instance->config.localIpCompactType == FF_LOCALIP_COMPACT_TYPE_ONELINE) + printf(" (%s) ", ip->name.chars); + else + putchar('\n'); +} + void ffPrintLocalIp(FFinstance* instance) { FF_LIST_AUTO_DESTROY results; @@ -40,30 +94,26 @@ void ffPrintLocalIp(FFinstance* instance) return; } + ffListSort(&results, instance->config.localIpV6First ? (void*) sortIpsV6First : (void*) sortIpsV4First); + if (instance->config.localIpCompactType != FF_LOCALIP_COMPACT_TYPE_NONE) { - ffPrintLogoAndKey(instance, FF_LOCALIP_MODULE_NAME, 0, &instance->config.localIP.key); + if (instance->config.localIpCompactType == FF_LOCALIP_COMPACT_TYPE_ONELINE) + ffPrintLogoAndKey(instance, FF_LOCALIP_MODULE_NAME, 0, &instance->config.localIP.key); - switch (instance->config.localIpCompactType) - { - case FF_LOCALIP_COMPACT_TYPE_V4FIRST: - ffListSort(&results, (void*) sortIpsV4First); - break; - case FF_LOCALIP_COMPACT_TYPE_V6FIRST: - ffListSort(&results, (void*) sortIpsV6First); - break; - default: - break; - } + FF_LIST_AUTO_DESTROY ips; + ffListInit(&ips, sizeof(FFLocalIpResult)); // weak references - uint32_t index = 0; FF_LIST_FOR_EACH(FFLocalIpResult, ip, results) { - if (index++ > 0) - putchar(' '); - ffStrbufWriteTo(&ip->addr, stdout); + if (ips.length > 0 && !ffStrbufEqual(&ip->name, &ip[-1].name)) + { + printIpsWithKey(instance, &ips); + ips.length = 0; + } + *(FFLocalIpResult*)ffListAdd(&ips) = *ip; } - putchar('\n'); + printIpsWithKey(instance, &ips); } else { @@ -72,21 +122,7 @@ void ffPrintLocalIp(FFinstance* instance) 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} - }); - } - + formatKey(instance, ip, &key); if(instance->config.localIP.outputFormat.length == 0) { ffPrintLogoAndKey(instance, key.chars, 0, NULL);