Skip to content

Commit 9cf9562

Browse files
committed
Weather: add new module
1 parent c72c6c4 commit 9cf9562

File tree

13 files changed

+113
-9
lines changed

13 files changed

+113
-9
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ set(LIBFASTFETCH_SRC
235235
src/modules/vulkan.c
236236
src/modules/localip.c
237237
src/modules/publicip.c
238+
src/modules/weather.c
238239
src/modules/player.c
239240
src/modules/song.c
240241
src/modules/datetime.c

completions/bash

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ __fastfetch_completion()
219219
"--player-name"
220220
"--public-ip-url"
221221
"--public-ip-timeout"
222+
"--weather-output-format"
223+
"--weather-timeout"
222224
"--os-key"
223225
"--os-format"
224226
"--os-error"
@@ -303,6 +305,9 @@ __fastfetch_completion()
303305
"--public-ip-key"
304306
"--public-ip-format"
305307
"--public-ip-error"
308+
"--weather-key"
309+
"--weather-format"
310+
"--weather-error"
306311
"--player-key"
307312
"--player-format"
308313
"--player-error"

presets/all

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
--structure Title:Separator:OS:Host:Kernel:Uptime:Processes:Packages:Shell:Resolution:DE:WM:WMTheme:Theme:Icons:Font:Cursor:Terminal:TerminalFont:CPU:GPU:Memory:Swap:Disk:Battery:PowerAdapter:Player:Song:PublicIP:LocalIP:DateTime:Locale:Vulkan:OpenGL:OpenCL:Users:Break:Colors
1+
--structure Title:Separator:OS:Host:Kernel:Uptime:Processes:Packages:Shell:Resolution:DE:WM:WMTheme:Theme:Icons:Font:Cursor:Terminal:TerminalFont:CPU:GPU:Memory:Swap:Disk:Battery:PowerAdapter:Player:Song:PublicIP:LocalIP:DateTime:Locale:Vulkan:OpenGL:OpenCL:Users:Weather:Break:Colors

src/common/init.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ static void defaultConfig(FFinstance* instance)
188188
initModuleArg(&instance->config.locale);
189189
initModuleArg(&instance->config.localIP);
190190
initModuleArg(&instance->config.publicIP);
191+
initModuleArg(&instance->config.weather);
191192
initModuleArg(&instance->config.player);
192193
initModuleArg(&instance->config.song);
193194
initModuleArg(&instance->config.dateTime);
@@ -245,6 +246,9 @@ static void defaultConfig(FFinstance* instance)
245246
instance->config.publicIpTimeout = 0;
246247
ffStrbufInit(&instance->config.publicIpUrl);
247248

249+
instance->config.weatherTimeout = 0;
250+
ffStrbufInitS(&instance->config.weatherOutputFormat, "%t+-+%C+(%l)");
251+
248252
ffStrbufInitA(&instance->config.osFile, 0);
249253

250254
ffStrbufInitA(&instance->config.playerName, 0);
@@ -361,6 +365,7 @@ void ffStart(FFinstance* instance)
361365
{
362366
ffPrepareCPUUsage();
363367
ffPreparePublicIp(instance);
368+
ffPrepareWeather(instance);
364369

365370
if(instance->config.multithreading)
366371
startDetectionThreads(instance);

src/common/networking.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <sys/socket.h>
77
#include <netdb.h>
88

9-
int ffNetworkingSendHttpRequest(const char* host, const char* path, uint32_t timeout)
9+
int ffNetworkingSendHttpRequest(const char* host, const char* path, const char* headers, uint32_t timeout)
1010
{
1111
struct addrinfo hints = {
1212
.ai_family = AF_INET,
@@ -48,7 +48,9 @@ int ffNetworkingSendHttpRequest(const char* host, const char* path, uint32_t tim
4848
ffStrbufAppendS(&command, path);
4949
ffStrbufAppendS(&command, " HTTP/1.1\nHost: ");
5050
ffStrbufAppendS(&command, host);
51-
ffStrbufAppendS(&command, "\r\n\r\n");
51+
ffStrbufAppendS(&command, "\r\n");
52+
ffStrbufAppendS(&command, headers);
53+
ffStrbufAppendS(&command, "\r\n");
5254

5355
if(send(sockfd, command.chars, command.length, 0) == -1)
5456
{
@@ -73,9 +75,9 @@ void ffNetworkingRecvHttpResponse(int sockfd, FFstrbuf* buffer)
7375
close(sockfd);
7476
}
7577

76-
void ffNetworkingGetHttp(const char* host, const char* path, uint32_t timeout, FFstrbuf* buffer)
78+
void ffNetworkingGetHttp(const char* host, const char* path, uint32_t timeout, const char* headers, FFstrbuf* buffer)
7779
{
78-
int sockfd = ffNetworkingSendHttpRequest(host, path, timeout);
80+
int sockfd = ffNetworkingSendHttpRequest(host, path, headers, timeout);
7981
if(sockfd > 0)
8082
ffNetworkingRecvHttpResponse(sockfd, buffer);
8183
}

src/common/networking.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
#include "util/FFstrbuf.h"
77

8-
int ffNetworkingSendHttpRequest(const char* host, const char* path, uint32_t timeout);
8+
int ffNetworkingSendHttpRequest(const char* host, const char* path, const char* headers, uint32_t timeout);
99
void ffNetworkingRecvHttpResponse(int sock, FFstrbuf* buffer);
10-
void ffNetworkingGetHttp(const char* host, const char* path, uint32_t timeout, FFstrbuf* buffer);
10+
void ffNetworkingGetHttp(const char* host, const char* path, uint32_t timeout, const char* headers, FFstrbuf* buffer);
1111

1212
#endif

src/data/config_user.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,18 @@
163163
# Default is 0 (disabled).
164164
#--public-ip-timeout 0
165165

166+
# Weather output format option:
167+
# Sets the weather format to be used. It must be URI encoded.
168+
# See: https://github.com/chubin/wttr.in#one-line-output
169+
# Default is "%t+-+%C+(%l)".
170+
#--weather-output-format "%t+-+%C+(%l)"
171+
172+
# Weather timeout option:
173+
# Sets the time to wait for the weather server (wttr.in) to respond.
174+
# Must be a positive integer.
175+
# Default is 0 (disabled).
176+
#--weather-timeout 0
177+
166178
# OS file option
167179
# Sets the path to the file containing the operating system information.
168180
# Should be a valid path to an existing file.
@@ -219,6 +231,7 @@
219231
#--locale-key Locale
220232
#--local-ip-key Local IP ({1})
221233
#--public-ip-key Public IP
234+
#--weather-key Weather
222235
#--player-key Media Player
223236
#--song-key Song
224237
#--datetime-key Date Time
@@ -260,6 +273,7 @@
260273
#--locale-format
261274
#--local-ip-format
262275
#--public-ip-format
276+
#--weather-format
263277
#--player-format
264278
#--song-format
265279
#--datetime-format
@@ -301,6 +315,7 @@
301315
#--locale-error
302316
#--local-ip-error
303317
#--public-ip-error
318+
#--weather-error
304319
#--player-error
305320
#--song-error
306321
#--datetime-error

src/data/help.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ Module specific options:
104104
--localip-name-prefix <str>: Show ips with given name prefix only. Default is empty
105105
--public-ip-timeout: Time in milliseconds to wait for the public ip server to respond. Default is disabled (0)
106106
--public-ip-url: The URL of public IP detection server to be used.
107+
--weather-timeout: Time in milliseconds to wait for the weather server to respond. Default is disabled (0)
108+
--weather-output-format: The output weather format to be used. It must be URI encoded.
107109
--player-name: The name of the player to use
108110
--gl <value>: Sets the opengl context creation library to use. Must be auto, egl, glx or osmesa. Default is auto
109111

src/fastfetch.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,12 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
11511151
optionParseString(key, value, &instance->config.publicIP.outputFormat);
11521152
else if(strcasecmp(key, "--public-ip-error") == 0)
11531153
optionParseString(key, value, &instance->config.publicIP.errorFormat);
1154+
else if(strcasecmp(key, "--weather-key") == 0)
1155+
optionParseString(key, value, &instance->config.weather.key);
1156+
else if(strcasecmp(key, "--weather-format") == 0)
1157+
optionParseString(key, value, &instance->config.weather.outputFormat);
1158+
else if(strcasecmp(key, "--weather-error") == 0)
1159+
optionParseString(key, value, &instance->config.weather.errorFormat);
11541160
else if(strcasecmp(key, "--player-key") == 0)
11551161
optionParseString(key, value, &instance->config.player.key);
11561162
else if(strcasecmp(key, "--player-format") == 0)
@@ -1295,6 +1301,10 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
12951301
optionParseString(key, value, &instance->config.publicIpUrl);
12961302
else if(strcasecmp(key, "--public-ip-timeout") == 0)
12971303
instance->config.publicIpTimeout = optionParseUInt32(key, value);
1304+
else if(strcasecmp(key, "--weather-output-format") == 0)
1305+
optionParseString(key, value, &instance->config.weatherOutputFormat);
1306+
else if(strcasecmp(key, "--weather-timeout") == 0)
1307+
instance->config.weatherTimeout = optionParseUInt32(key, value);
12981308
else if(strcasecmp(key, "--gl") == 0)
12991309
{
13001310
optionParseEnum(key, value, &instance->config.glType,
@@ -1430,6 +1440,8 @@ static void parseStructureCommand(FFinstance* instance, FFdata* data, const char
14301440
ffPrintLocalIp(instance);
14311441
else if(strcasecmp(line, "publicip") == 0)
14321442
ffPrintPublicIp(instance);
1443+
else if(strcasecmp(line, "weather") == 0)
1444+
ffPrintWeather(instance);
14331445
else if(strcasecmp(line, "player") == 0)
14341446
ffPrintPlayer(instance);
14351447
else if(strcasecmp(line, "media") == 0 || strcasecmp(line, "song") == 0)

src/fastfetch.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ typedef struct FFconfig
120120
FFModuleArgs locale;
121121
FFModuleArgs localIP;
122122
FFModuleArgs publicIP;
123+
FFModuleArgs weather;
123124
FFModuleArgs player;
124125
FFModuleArgs song;
125126
FFModuleArgs dateTime;
@@ -177,6 +178,9 @@ typedef struct FFconfig
177178
FFstrbuf publicIpUrl;
178179
uint32_t publicIpTimeout;
179180

181+
FFstrbuf weatherOutputFormat;
182+
uint32_t weatherTimeout;
183+
180184
FFstrbuf osFile;
181185

182186
FFstrbuf playerName;
@@ -238,6 +242,7 @@ void ffLogoBuiltinListAutocompletion();
238242
void ffPrintDateTimeFormat(FFinstance* instance, const char* moduleName, const FFModuleArgs* moduleArgs);
239243
void ffPrepareCPUUsage();
240244
void ffPreparePublicIp(FFinstance* instance);
245+
void ffPrepareWeather(FFinstance* instance);
241246

242247
//Printing
243248

@@ -278,6 +283,7 @@ void ffPrintDate(FFinstance* instance);
278283
void ffPrintTime(FFinstance* instance);
279284
void ffPrintLocalIp(FFinstance* instance);
280285
void ffPrintPublicIp(FFinstance* instance);
286+
void ffPrintWeather(FFinstance* instance);
281287
void ffPrintColors(FFinstance* instance);
282288
void ffPrintVulkan(FFinstance* instance);
283289
void ffPrintOpenGL(FFinstance* instance);

0 commit comments

Comments
 (0)