mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 10:24:58 +02:00
Weather: add new module
This commit is contained in:
@@ -235,6 +235,7 @@ set(LIBFASTFETCH_SRC
|
||||
src/modules/vulkan.c
|
||||
src/modules/localip.c
|
||||
src/modules/publicip.c
|
||||
src/modules/weather.c
|
||||
src/modules/player.c
|
||||
src/modules/song.c
|
||||
src/modules/datetime.c
|
||||
|
||||
@@ -219,6 +219,8 @@ __fastfetch_completion()
|
||||
"--player-name"
|
||||
"--public-ip-url"
|
||||
"--public-ip-timeout"
|
||||
"--weather-output-format"
|
||||
"--weather-timeout"
|
||||
"--os-key"
|
||||
"--os-format"
|
||||
"--os-error"
|
||||
@@ -303,6 +305,9 @@ __fastfetch_completion()
|
||||
"--public-ip-key"
|
||||
"--public-ip-format"
|
||||
"--public-ip-error"
|
||||
"--weather-key"
|
||||
"--weather-format"
|
||||
"--weather-error"
|
||||
"--player-key"
|
||||
"--player-format"
|
||||
"--player-error"
|
||||
|
||||
+1
-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
|
||||
--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
|
||||
|
||||
@@ -188,6 +188,7 @@ static void defaultConfig(FFinstance* instance)
|
||||
initModuleArg(&instance->config.locale);
|
||||
initModuleArg(&instance->config.localIP);
|
||||
initModuleArg(&instance->config.publicIP);
|
||||
initModuleArg(&instance->config.weather);
|
||||
initModuleArg(&instance->config.player);
|
||||
initModuleArg(&instance->config.song);
|
||||
initModuleArg(&instance->config.dateTime);
|
||||
@@ -245,6 +246,9 @@ static void defaultConfig(FFinstance* instance)
|
||||
instance->config.publicIpTimeout = 0;
|
||||
ffStrbufInit(&instance->config.publicIpUrl);
|
||||
|
||||
instance->config.weatherTimeout = 0;
|
||||
ffStrbufInitS(&instance->config.weatherOutputFormat, "%t+-+%C+(%l)");
|
||||
|
||||
ffStrbufInitA(&instance->config.osFile, 0);
|
||||
|
||||
ffStrbufInitA(&instance->config.playerName, 0);
|
||||
@@ -361,6 +365,7 @@ void ffStart(FFinstance* instance)
|
||||
{
|
||||
ffPrepareCPUUsage();
|
||||
ffPreparePublicIp(instance);
|
||||
ffPrepareWeather(instance);
|
||||
|
||||
if(instance->config.multithreading)
|
||||
startDetectionThreads(instance);
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
|
||||
int ffNetworkingSendHttpRequest(const char* host, const char* path, uint32_t timeout)
|
||||
int ffNetworkingSendHttpRequest(const char* host, const char* path, const char* headers, uint32_t timeout)
|
||||
{
|
||||
struct addrinfo hints = {
|
||||
.ai_family = AF_INET,
|
||||
@@ -48,7 +48,9 @@ int ffNetworkingSendHttpRequest(const char* host, const char* path, uint32_t tim
|
||||
ffStrbufAppendS(&command, path);
|
||||
ffStrbufAppendS(&command, " HTTP/1.1\nHost: ");
|
||||
ffStrbufAppendS(&command, host);
|
||||
ffStrbufAppendS(&command, "\r\n\r\n");
|
||||
ffStrbufAppendS(&command, "\r\n");
|
||||
ffStrbufAppendS(&command, headers);
|
||||
ffStrbufAppendS(&command, "\r\n");
|
||||
|
||||
if(send(sockfd, command.chars, command.length, 0) == -1)
|
||||
{
|
||||
@@ -73,9 +75,9 @@ void ffNetworkingRecvHttpResponse(int sockfd, FFstrbuf* buffer)
|
||||
close(sockfd);
|
||||
}
|
||||
|
||||
void ffNetworkingGetHttp(const char* host, const char* path, uint32_t timeout, FFstrbuf* buffer)
|
||||
void ffNetworkingGetHttp(const char* host, const char* path, uint32_t timeout, const char* headers, FFstrbuf* buffer)
|
||||
{
|
||||
int sockfd = ffNetworkingSendHttpRequest(host, path, timeout);
|
||||
int sockfd = ffNetworkingSendHttpRequest(host, path, headers, timeout);
|
||||
if(sockfd > 0)
|
||||
ffNetworkingRecvHttpResponse(sockfd, buffer);
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
|
||||
#include "util/FFstrbuf.h"
|
||||
|
||||
int ffNetworkingSendHttpRequest(const char* host, const char* path, uint32_t timeout);
|
||||
int ffNetworkingSendHttpRequest(const char* host, const char* path, const char* headers, uint32_t timeout);
|
||||
void ffNetworkingRecvHttpResponse(int sock, FFstrbuf* buffer);
|
||||
void ffNetworkingGetHttp(const char* host, const char* path, uint32_t timeout, FFstrbuf* buffer);
|
||||
void ffNetworkingGetHttp(const char* host, const char* path, uint32_t timeout, const char* headers, FFstrbuf* buffer);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -163,6 +163,18 @@
|
||||
# Default is 0 (disabled).
|
||||
#--public-ip-timeout 0
|
||||
|
||||
# Weather output format option:
|
||||
# Sets the weather format to be used. It must be URI encoded.
|
||||
# See: https://github.com/chubin/wttr.in#one-line-output
|
||||
# Default is "%t+-+%C+(%l)".
|
||||
#--weather-output-format "%t+-+%C+(%l)"
|
||||
|
||||
# Weather timeout option:
|
||||
# Sets the time to wait for the weather server (wttr.in) to respond.
|
||||
# Must be a positive integer.
|
||||
# Default is 0 (disabled).
|
||||
#--weather-timeout 0
|
||||
|
||||
# OS file option
|
||||
# Sets the path to the file containing the operating system information.
|
||||
# Should be a valid path to an existing file.
|
||||
@@ -219,6 +231,7 @@
|
||||
#--locale-key Locale
|
||||
#--local-ip-key Local IP ({1})
|
||||
#--public-ip-key Public IP
|
||||
#--weather-key Weather
|
||||
#--player-key Media Player
|
||||
#--song-key Song
|
||||
#--datetime-key Date Time
|
||||
@@ -260,6 +273,7 @@
|
||||
#--locale-format
|
||||
#--local-ip-format
|
||||
#--public-ip-format
|
||||
#--weather-format
|
||||
#--player-format
|
||||
#--song-format
|
||||
#--datetime-format
|
||||
@@ -301,6 +315,7 @@
|
||||
#--locale-error
|
||||
#--local-ip-error
|
||||
#--public-ip-error
|
||||
#--weather-error
|
||||
#--player-error
|
||||
#--song-error
|
||||
#--datetime-error
|
||||
|
||||
@@ -104,6 +104,8 @@ Module specific options:
|
||||
--localip-name-prefix <str>: Show ips with given name prefix only. Default is empty
|
||||
--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)
|
||||
--weather-output-format: The output weather format to be used. It must be URI encoded.
|
||||
--player-name: The name of the player to use
|
||||
--gl <value>: Sets the opengl context creation library to use. Must be auto, egl, glx or osmesa. Default is auto
|
||||
|
||||
|
||||
@@ -1151,6 +1151,12 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
||||
optionParseString(key, value, &instance->config.publicIP.outputFormat);
|
||||
else if(strcasecmp(key, "--public-ip-error") == 0)
|
||||
optionParseString(key, value, &instance->config.publicIP.errorFormat);
|
||||
else if(strcasecmp(key, "--weather-key") == 0)
|
||||
optionParseString(key, value, &instance->config.weather.key);
|
||||
else if(strcasecmp(key, "--weather-format") == 0)
|
||||
optionParseString(key, value, &instance->config.weather.outputFormat);
|
||||
else if(strcasecmp(key, "--weather-error") == 0)
|
||||
optionParseString(key, value, &instance->config.weather.errorFormat);
|
||||
else if(strcasecmp(key, "--player-key") == 0)
|
||||
optionParseString(key, value, &instance->config.player.key);
|
||||
else if(strcasecmp(key, "--player-format") == 0)
|
||||
@@ -1295,6 +1301,10 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
||||
optionParseString(key, value, &instance->config.publicIpUrl);
|
||||
else if(strcasecmp(key, "--public-ip-timeout") == 0)
|
||||
instance->config.publicIpTimeout = optionParseUInt32(key, value);
|
||||
else if(strcasecmp(key, "--weather-output-format") == 0)
|
||||
optionParseString(key, value, &instance->config.weatherOutputFormat);
|
||||
else if(strcasecmp(key, "--weather-timeout") == 0)
|
||||
instance->config.weatherTimeout = optionParseUInt32(key, value);
|
||||
else if(strcasecmp(key, "--gl") == 0)
|
||||
{
|
||||
optionParseEnum(key, value, &instance->config.glType,
|
||||
@@ -1430,6 +1440,8 @@ static void parseStructureCommand(FFinstance* instance, FFdata* data, const char
|
||||
ffPrintLocalIp(instance);
|
||||
else if(strcasecmp(line, "publicip") == 0)
|
||||
ffPrintPublicIp(instance);
|
||||
else if(strcasecmp(line, "weather") == 0)
|
||||
ffPrintWeather(instance);
|
||||
else if(strcasecmp(line, "player") == 0)
|
||||
ffPrintPlayer(instance);
|
||||
else if(strcasecmp(line, "media") == 0 || strcasecmp(line, "song") == 0)
|
||||
|
||||
@@ -120,6 +120,7 @@ typedef struct FFconfig
|
||||
FFModuleArgs locale;
|
||||
FFModuleArgs localIP;
|
||||
FFModuleArgs publicIP;
|
||||
FFModuleArgs weather;
|
||||
FFModuleArgs player;
|
||||
FFModuleArgs song;
|
||||
FFModuleArgs dateTime;
|
||||
@@ -177,6 +178,9 @@ typedef struct FFconfig
|
||||
FFstrbuf publicIpUrl;
|
||||
uint32_t publicIpTimeout;
|
||||
|
||||
FFstrbuf weatherOutputFormat;
|
||||
uint32_t weatherTimeout;
|
||||
|
||||
FFstrbuf osFile;
|
||||
|
||||
FFstrbuf playerName;
|
||||
@@ -238,6 +242,7 @@ void ffLogoBuiltinListAutocompletion();
|
||||
void ffPrintDateTimeFormat(FFinstance* instance, const char* moduleName, const FFModuleArgs* moduleArgs);
|
||||
void ffPrepareCPUUsage();
|
||||
void ffPreparePublicIp(FFinstance* instance);
|
||||
void ffPrepareWeather(FFinstance* instance);
|
||||
|
||||
//Printing
|
||||
|
||||
@@ -278,6 +283,7 @@ void ffPrintDate(FFinstance* instance);
|
||||
void ffPrintTime(FFinstance* instance);
|
||||
void ffPrintLocalIp(FFinstance* instance);
|
||||
void ffPrintPublicIp(FFinstance* instance);
|
||||
void ffPrintWeather(FFinstance* instance);
|
||||
void ffPrintColors(FFinstance* instance);
|
||||
void ffPrintVulkan(FFinstance* instance);
|
||||
void ffPrintOpenGL(FFinstance* instance);
|
||||
|
||||
@@ -53,6 +53,7 @@ int main(int argc, char** argv)
|
||||
//ffPrintOpenGL(&instance);
|
||||
//ffPrintOpenCL(&instance);
|
||||
//ffPrintUsers(&instance);
|
||||
//ffPrintWeather(&instance);
|
||||
ffPrintBreak(&instance);
|
||||
ffPrintColors(&instance);
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ static int sockfd;
|
||||
void ffPreparePublicIp(FFinstance* instance)
|
||||
{
|
||||
if(instance->config.publicIpUrl.length == 0)
|
||||
sockfd = ffNetworkingSendHttpRequest("ipinfo.io", "/ip", instance->config.publicIpTimeout);
|
||||
sockfd = ffNetworkingSendHttpRequest("ipinfo.io", "/ip", NULL, instance->config.publicIpTimeout);
|
||||
else
|
||||
{
|
||||
FFstrbuf host;
|
||||
@@ -27,7 +27,7 @@ void ffPreparePublicIp(FFinstance* instance)
|
||||
host.chars[pathStartIndex] = '\0';
|
||||
}
|
||||
|
||||
sockfd = ffNetworkingSendHttpRequest(host.chars, path.length == 0 ? "/" : path.chars, instance->config.publicIpTimeout);
|
||||
sockfd = ffNetworkingSendHttpRequest(host.chars, path.length == 0 ? "/" : path.chars, NULL, instance->config.publicIpTimeout);
|
||||
|
||||
ffStrbufDestroy(&path);
|
||||
ffStrbufDestroy(&host);
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "common/networking.h"
|
||||
|
||||
#define FF_WEATHER_MODULE_NAME "Weather"
|
||||
#define FF_WEATHER_NUM_FORMAT_ARGS 1
|
||||
|
||||
static int sockfd;
|
||||
|
||||
void ffPrepareWeather(FFinstance* instance)
|
||||
{
|
||||
FFstrbuf path;
|
||||
ffStrbufInitS(&path, "/?format=");
|
||||
ffStrbufAppend(&path, &instance->config.weatherOutputFormat);
|
||||
sockfd = ffNetworkingSendHttpRequest("wttr.in", path.chars, "User-Agent: curl/0.0.0\r\n", instance->config.weatherTimeout);
|
||||
ffStrbufDestroy(&path);
|
||||
}
|
||||
|
||||
void ffPrintWeather(FFinstance* instance)
|
||||
{
|
||||
if(sockfd == 0)
|
||||
ffPrepareWeather(instance);
|
||||
|
||||
if(sockfd < 0)
|
||||
{
|
||||
ffPrintError(instance, FF_WEATHER_MODULE_NAME, 0, &instance->config.weather, "Failed to connect to 'wttr.in'");
|
||||
return;
|
||||
}
|
||||
|
||||
FFstrbuf result;
|
||||
ffStrbufInitA(&result, 4096);
|
||||
ffNetworkingRecvHttpResponse(sockfd, &result);
|
||||
ffStrbufSubstrAfterFirstS(&result, "\r\n\r\n");
|
||||
|
||||
if(result.length == 0)
|
||||
{
|
||||
ffPrintError(instance, FF_WEATHER_MODULE_NAME, 0, &instance->config.weather, "Failed to receive the server response");
|
||||
ffStrbufDestroy(&result);
|
||||
return;
|
||||
}
|
||||
|
||||
if(instance->config.weather.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_WEATHER_MODULE_NAME, 0, &instance->config.weather.key);
|
||||
ffStrbufPutTo(&result, stdout);
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormat(instance, FF_WEATHER_MODULE_NAME, 0, &instance->config.weather, FF_WEATHER_NUM_FORMAT_ARGS, (FFformatarg[]) {
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result}
|
||||
});
|
||||
}
|
||||
|
||||
ffStrbufDestroy(&result);
|
||||
}
|
||||
Reference in New Issue
Block a user