Skip to content

Commit d288a69

Browse files
Merge pull request #92 from CarterLi/localip
Add new module that shows local IP addresses which are useful with frontend development
2 parents 0b3f966 + 38e2f8c commit d288a69

File tree

7 files changed

+116
-2
lines changed

7 files changed

+116
-2
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ set(SRCS
116116
src/modules/disk.c
117117
src/modules/battery.c
118118
src/modules/locale.c
119+
src/modules/localip.c
119120
src/modules/colors.c
120121
)
121122

completions/bash

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ __fastfetch_complete_help()
2525
"disk-format"
2626
"battery-format"
2727
"locale-format"
28+
"local-ip-format"
2829
)
2930
COMPREPLY=($(compgen -W "${__ff_helps[*]}" -- "$CURRENT_WORD"))
3031
}
@@ -121,6 +122,9 @@ __fastfetch_completion()
121122
"--allow-slow-operations"
122123
"--disable-linewrap"
123124
"--hide-cursor"
125+
"--localip-show-ipv4"
126+
"--localip-show-ipv6"
127+
"--localip-show-loop"
124128
)
125129

126130
local FF_OPTIONS_STRING=(
@@ -179,6 +183,8 @@ __fastfetch_completion()
179183
"--locale-key"
180184
"--disk-folders"
181185
"--disk-key"
186+
"--local-ip-format"
187+
"--local-ip-key"
182188
)
183189

184190
local FF_OPTIONS_PATH=(

src/common/init.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ static void defaultConfig(FFinstance* instance)
165165
ffStrbufInitA(&instance->config.batteryKey, 1);
166166
ffStrbufInitA(&instance->config.localeFormat, 1);
167167
ffStrbufInitA(&instance->config.localeKey, 1);
168+
ffStrbufInitA(&instance->config.localIpKey, 1);
169+
ffStrbufInitA(&instance->config.localIpFormat, 1);
168170

169171
ffStrbufInitA(&instance->config.libPCI, 1);
170172
ffStrbufInitA(&instance->config.libX11, 1);
@@ -178,6 +180,10 @@ static void defaultConfig(FFinstance* instance)
178180
ffStrbufInitA(&instance->config.diskFolders, 1);
179181

180182
ffStrbufInitA(&instance->config.batteryDir, 1);
183+
184+
instance->config.localIpShowIpV4 = true;
185+
instance->config.localIpShowIpV6 = false;
186+
instance->config.localIpShowLoop = false;
181187
}
182188

183189
void ffInitInstance(FFinstance* instance)

src/fastfetch.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ static inline void printHelp()
8989
" --disk-format <format>\n"
9090
" --battery-format <format>\n"
9191
" --locale-format <format>\n"
92+
" --local-ip-format <format>\n"
9293
"\n"
9394
"Key options: Provide a custom key for an output\n"
9495
" --os-key <key>\n"
@@ -113,6 +114,7 @@ static inline void printHelp()
113114
" --disk-key <key>: takes the mount path as format argument\n"
114115
" --battery-key <key>: takes the battery index as format argument\n"
115116
" --locale-key <key>\n"
117+
" --local-ip-key <key>: takes the name of this network interface as format argument\n"
116118
"\n"
117119
"Library optins: Set the path of a library to load\n"
118120
" --lib-PCI <path>\n"
@@ -125,8 +127,11 @@ static inline void printHelp()
125127
" --lib-SQLite <path>\n"
126128
"\n"
127129
"Module specific options:\n"
128-
" --disk-folders <folders>: A colon separated list of folder paths for the disk output. Default is \"/:/home\"\n"
129-
" --battery-dir <folder>: The directory where the battery folders are. Standard: /sys/class/power_supply/\n"
130+
" --disk-folders <folders>: A colon separated list of folder paths for the disk output. Default is \"/:/home\"\n"
131+
" --battery-dir <folder>: The directory where the battery folders are. Standard: /sys/class/power_supply/\n"
132+
" --localip-show-ipv4 <?value>: Show ipv4 addresses in local ip module. Default is true\n"
133+
" --localip-show-ipv6 <?value>: Show ipv6 addresses in local ip module. Default is false\n"
134+
" --localip-show-loop <?value>: Show loop back addresses (127.0.0.1) in local ip module. Default is false\n"
130135
"\n"
131136
"Parsing is not case sensitive. E.g. \"--lib-PCI\" is equal to \"--Lib-Pci\"\n"
132137
"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()
492497
"Icons\n"
493498
"Kernel\n"
494499
"Locale\n"
500+
"LocalIp\n"
495501
"Memory\n"
496502
"OS\n"
497503
"Packages\n"
@@ -914,6 +920,10 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
914920
optionParseString(key, value, &instance->config.localeFormat);
915921
else if(strcasecmp(key, "--locale-key") == 0)
916922
optionParseString(key, value, &instance->config.localeKey);
923+
else if(strcasecmp(key, "--local-ip-key") == 0)
924+
optionParseString(key, value, &instance->config.localIpKey);
925+
else if(strcasecmp(key, "--local-ip-format") == 0)
926+
optionParseString(key, value, &instance->config.localIpFormat);
917927
else if(strcasecmp(key, "--lib-PCI") == 0)
918928
optionParseString(key, value, &instance->config.libPCI);
919929
else if(strcasecmp(key, "--lib-X11") == 0)
@@ -934,6 +944,12 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
934944
optionParseString(key, value, &instance->config.diskFolders);
935945
else if(strcasecmp(key, "--battery-dir") == 0)
936946
optionParseString(key, value, &instance->config.batteryDir);
947+
else if(strcasecmp(key, "--localip-show-ipv4") == 0)
948+
instance->config.localIpShowIpV4 = optionParseBoolean(value);
949+
else if(strcasecmp(key, "--localip-show-ipv6") == 0)
950+
instance->config.localIpShowIpV6 = optionParseBoolean(value);
951+
else if(strcasecmp(key, "--localip-show-loop") == 0)
952+
instance->config.localIpShowLoop = optionParseBoolean(value);
937953
else
938954
{
939955
fprintf(stderr, "Error: unknown option: %s\n", key);
@@ -1063,6 +1079,8 @@ static void parseStructureCommand(FFinstance* instance, FFdata* data, const char
10631079
ffPrintBattery(instance);
10641080
else if(strcasecmp(line, "locale") == 0)
10651081
ffPrintLocale(instance);
1082+
else if(strcasecmp(line, "localip") == 0)
1083+
ffPrintLocalIp(instance);
10661084
else if(strcasecmp(line, "colors") == 0)
10671085
ffPrintColors(instance);
10681086
else

src/fastfetch.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ typedef struct FFconfig
9191
FFstrbuf batteryKey;
9292
FFstrbuf localeFormat;
9393
FFstrbuf localeKey;
94+
FFstrbuf localIpKey;
95+
FFstrbuf localIpFormat;
9496

9597
FFstrbuf libPCI;
9698
FFstrbuf libX11;
@@ -105,6 +107,10 @@ typedef struct FFconfig
105107

106108
FFstrbuf batteryDir;
107109

110+
bool localIpShowLoop;
111+
bool localIpShowIpV4;
112+
bool localIpShowIpV6;
113+
108114
} FFconfig;
109115

110116
typedef struct FFstate
@@ -380,6 +386,7 @@ void ffPrintMemory(FFinstance* instance);
380386
void ffPrintDisk(FFinstance* instance);
381387
void ffPrintBattery(FFinstance* instance);
382388
void ffPrintLocale(FFinstance* instance);
389+
void ffPrintLocalIp(FFinstance* instance);
383390
void ffPrintColors(FFinstance* instance);
384391

385392
#endif

src/flashfetch.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ int main(int argc, char** argv)
4343
ffPrintMemory(&instance);
4444
ffPrintDisk(&instance);
4545
ffPrintBattery(&instance);
46+
ffPrintLocalIp(&instance);
4647
ffPrintLocale(&instance);
4748
ffPrintBreak(&instance);
4849
ffPrintColors(&instance);

src/modules/localip.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include "fastfetch.h"
2+
3+
#define FF_LOCALIP_MODULE_NAME "Local Ip"
4+
#define FF_LOCALIP_NUM_FORMAT_ARGS 1
5+
6+
#include <sys/types.h>
7+
#include <ifaddrs.h>
8+
#include <netinet/in.h>
9+
#include <string.h>
10+
#include <arpa/inet.h>
11+
12+
static void printValue(FFinstance* instance, const char* ifaName, const char* addressBuffer)
13+
{
14+
FF_STRBUF_CREATE(key);
15+
16+
if (instance->config.localIpKey.length == 0) {
17+
ffStrbufSetF(&key, FF_LOCALIP_MODULE_NAME " (%s)", ifaName);
18+
} else {
19+
ffParseFormatString(&key, &instance->config.localIpKey, NULL, 1, (FFformatarg[]){
20+
{FF_FORMAT_ARG_TYPE_STRING, ifaName}
21+
});
22+
}
23+
24+
if (instance->config.localIpFormat.length == 0) {
25+
ffPrintLogoAndKey(instance, FF_LOCALIP_MODULE_NAME, 0, &key);
26+
puts(addressBuffer);
27+
} else {
28+
ffPrintFormatString(instance, FF_LOCALIP_MODULE_NAME, 0, &key, &instance->config.localIpFormat, NULL, FF_LOCALIP_NUM_FORMAT_ARGS, (FFformatarg[]){
29+
{FF_FORMAT_ARG_TYPE_STRING, addressBuffer}
30+
});
31+
}
32+
33+
ffStrbufDestroy(&key);
34+
}
35+
36+
void ffPrintLocalIp(FFinstance* instance)
37+
{
38+
struct ifaddrs* ifAddrStruct = NULL;
39+
int ret = getifaddrs(&ifAddrStruct);
40+
if (ret < 0) {
41+
ffPrintError(instance, FF_LOCALIP_MODULE_NAME, 0, &instance->config.localIpKey, &instance->config.localIpFormat, FF_LOCALIP_NUM_FORMAT_ARGS, "getifaddrs(&ifAddrStruct) < 0 (%i)", ret);
42+
return;
43+
}
44+
45+
for (struct ifaddrs* ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
46+
if (!ifa->ifa_addr)
47+
continue;
48+
49+
// loop back
50+
if (strcmp(ifa->ifa_name, "lo") == 0 && !instance->config.localIpShowLoop)
51+
continue;
52+
53+
if (ifa->ifa_addr->sa_family == AF_INET) { // check it is IP4
54+
// is a valid IP4 Address
55+
if (!instance->config.localIpShowIpV4)
56+
continue;
57+
58+
void* tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
59+
char addressBuffer[INET_ADDRSTRLEN];
60+
inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
61+
printValue(instance, ifa->ifa_name, addressBuffer);
62+
} else if (ifa->ifa_addr->sa_family == AF_INET6) { // check it is IP6
63+
// is a valid IP6 Address
64+
if (!instance->config.localIpShowIpV6)
65+
continue;
66+
67+
void* tmpAddrPtr=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
68+
char addressBuffer[INET6_ADDRSTRLEN];
69+
inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
70+
printValue(instance, ifa->ifa_name, addressBuffer);
71+
}
72+
}
73+
74+
if (ifAddrStruct) freeifaddrs(ifAddrStruct);
75+
}

0 commit comments

Comments
 (0)