mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
LocalIP: cleanup printing code
This commit is contained in:
@@ -33,57 +33,70 @@ static void formatKey(const FFLocalIpOptions* options, FFLocalIpResult* ip, uint
|
||||
}
|
||||
}
|
||||
|
||||
static void printIp(FFLocalIpResult* ip, bool markDefaultRoute)
|
||||
static void appendSpeed(FFLocalIpResult* ip, FFstrbuf* strbuf)
|
||||
{
|
||||
if (ip->speed >= 1000000)
|
||||
{
|
||||
if (instance.config.display.fractionNdigits >= 0)
|
||||
ffStrbufAppendF(strbuf, "%.*f Tbps", instance.config.display.fractionNdigits, ip->speed / 1000000.0);
|
||||
else
|
||||
ffStrbufAppendF(strbuf, "%g Tbps", ip->speed / 1000000.0);
|
||||
}
|
||||
else if (ip->speed >= 1000)
|
||||
{
|
||||
if (instance.config.display.fractionNdigits >= 0)
|
||||
ffStrbufAppendF(strbuf, "%.*f Gbps", instance.config.display.fractionNdigits, ip->speed / 1000.0);
|
||||
else
|
||||
ffStrbufAppendF(strbuf, "%g Gbps", ip->speed / 1000.0);
|
||||
}
|
||||
else
|
||||
ffStrbufAppendF(strbuf, "%u Mbps", (unsigned) ip->speed);
|
||||
}
|
||||
|
||||
static void printIp(FFLocalIpResult* ip, bool markDefaultRoute, FFstrbuf* buffer)
|
||||
{
|
||||
bool flag = false;
|
||||
if (ip->ipv4.length)
|
||||
{
|
||||
ffStrbufWriteTo(&ip->ipv4, stdout);
|
||||
flag = true;
|
||||
ffStrbufAppend(buffer, &ip->ipv4);
|
||||
}
|
||||
if (ip->ipv6.length)
|
||||
{
|
||||
if (flag) putchar(' ');
|
||||
ffStrbufWriteTo(&ip->ipv6, stdout);
|
||||
flag = true;
|
||||
if (buffer->length) ffStrbufAppendC(buffer, ' ');
|
||||
ffStrbufAppend(buffer, &ip->ipv6);
|
||||
}
|
||||
if (ip->mac.length)
|
||||
{
|
||||
if (flag)
|
||||
printf(" (%s)", ip->mac.chars);
|
||||
if (buffer->length)
|
||||
ffStrbufAppendF(buffer, " (%s)", ip->mac.chars);
|
||||
else
|
||||
ffStrbufWriteTo(&ip->mac, stdout);
|
||||
flag = true;
|
||||
ffStrbufAppend(buffer, &ip->mac);
|
||||
}
|
||||
if (ip->mtu > 0 || ip->speed > 0)
|
||||
{
|
||||
bool flag = buffer->length > 0;
|
||||
if (flag)
|
||||
fputs(" [", stdout);
|
||||
ffStrbufAppendS(buffer, " [");
|
||||
if (ip->speed > 0)
|
||||
{
|
||||
if (ip->speed >= 1000000)
|
||||
printf("%g Tbps", ip->speed / 1000000.0);
|
||||
else if (ip->speed >= 1000)
|
||||
printf("Speed %g Gbps", ip->speed / 1000.0);
|
||||
else
|
||||
printf("Speed %u Mbps", (unsigned) ip->speed);
|
||||
|
||||
if (ip->mtu > 0)
|
||||
fputs(" / ", stdout);
|
||||
ffStrbufAppendS(buffer, "Speed ");
|
||||
appendSpeed(ip, buffer);
|
||||
if (ip->mtu > 0)
|
||||
ffStrbufAppendS(buffer, " / MTU ");
|
||||
}
|
||||
if (ip->mtu > 0)
|
||||
printf("MTU %u", (unsigned) ip->mtu);
|
||||
putchar(']');
|
||||
flag = true;
|
||||
ffStrbufAppendF(buffer, "%u", (unsigned) ip->mtu);
|
||||
if (flag)
|
||||
ffStrbufAppendC(buffer, ']');
|
||||
}
|
||||
if (ip->flags.length) {
|
||||
if (flag) fputs(" <", stdout);
|
||||
ffStrbufWriteTo(&ip->flags, stdout);
|
||||
putchar('>');
|
||||
flag = true;
|
||||
if (ip->flags.length)
|
||||
{
|
||||
if (buffer->length) ffStrbufAppendS(buffer, " <");
|
||||
ffStrbufAppend(buffer, &ip->flags);
|
||||
ffStrbufAppendC(buffer, '>');
|
||||
}
|
||||
if (markDefaultRoute && flag && ip->defaultRoute)
|
||||
fputs(" *", stdout);
|
||||
if (markDefaultRoute && ip->defaultRoute)
|
||||
ffStrbufAppendS(buffer, " *");
|
||||
}
|
||||
|
||||
void ffPrintLocalIp(FFLocalIpOptions* options)
|
||||
@@ -106,21 +119,20 @@ void ffPrintLocalIp(FFLocalIpOptions* options)
|
||||
|
||||
ffListSort(&results, (const void*) sortIps);
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
|
||||
|
||||
if (options->showType & FF_LOCALIP_TYPE_COMPACT_BIT)
|
||||
{
|
||||
ffPrintLogoAndKey(FF_LOCALIP_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
||||
|
||||
bool flag = false;
|
||||
|
||||
FF_LIST_FOR_EACH(FFLocalIpResult, ip, results)
|
||||
{
|
||||
if (flag)
|
||||
fputs(" - ", stdout);
|
||||
else
|
||||
flag = true;
|
||||
printIp(ip, false);
|
||||
if (buffer.length)
|
||||
ffStrbufAppendS(&buffer, " - ");
|
||||
printIp(ip, false, &buffer);
|
||||
}
|
||||
putchar('\n');
|
||||
ffStrbufPutTo(&buffer, stdout);
|
||||
ffStrbufClear(&buffer);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -133,21 +145,13 @@ void ffPrintLocalIp(FFLocalIpOptions* options)
|
||||
if(options->moduleArgs.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY);
|
||||
printIp(ip, !(options->showType & FF_LOCALIP_TYPE_DEFAULT_ROUTE_ONLY_BIT));
|
||||
putchar('\n');
|
||||
printIp(ip, !(options->showType & FF_LOCALIP_TYPE_DEFAULT_ROUTE_ONLY_BIT), &buffer);
|
||||
ffStrbufPutTo(&buffer, stdout);
|
||||
}
|
||||
else
|
||||
{
|
||||
FF_STRBUF_AUTO_DESTROY speedStr = ffStrbufCreate();
|
||||
if (ip->speed > 0)
|
||||
{
|
||||
if (ip->speed >= 1000000)
|
||||
ffStrbufSetF(&speedStr, "%g Tbps", ip->speed / 1000000.0);
|
||||
else if (ip->speed >= 1000)
|
||||
ffStrbufSetF(&speedStr, "%g Gbps", ip->speed / 1000.0);
|
||||
else
|
||||
ffStrbufSetF(&speedStr, "%u Mbps", (unsigned) ip->speed);
|
||||
}
|
||||
appendSpeed(ip, &buffer);
|
||||
FF_PRINT_FORMAT_CHECKED(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY, ((FFformatarg[]){
|
||||
FF_FORMAT_ARG(ip->ipv4, "ipv4"),
|
||||
FF_FORMAT_ARG(ip->ipv6, "ipv6"),
|
||||
@@ -155,11 +159,12 @@ void ffPrintLocalIp(FFLocalIpOptions* options)
|
||||
FF_FORMAT_ARG(ip->name, "ifname"),
|
||||
FF_FORMAT_ARG(ip->defaultRoute, "is-default-route"),
|
||||
FF_FORMAT_ARG(ip->mtu, "mtu"),
|
||||
FF_FORMAT_ARG(speedStr, "speed"),
|
||||
FF_FORMAT_ARG(buffer, "speed"),
|
||||
FF_FORMAT_ARG(ip->flags, "flags"),
|
||||
}));
|
||||
}
|
||||
++index;
|
||||
ffStrbufClear(&buffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user