fixed some more stuff

This commit is contained in:
Mars 2024-06-21 23:07:31 -04:00
parent 3cf13c45bc
commit 164a68c5ee
4 changed files with 36 additions and 32 deletions

View file

@ -43,7 +43,7 @@ foreach file : source_file_names
endforeach endforeach
windows_sdk_lib_dir = 'C:/Program Files (x86)/Windows Kits/10/Lib/10.0.22621.0/um/x64' windows_sdk_lib_dir = 'C:/Program Files (x86)/Windows Kits/10/Lib/10.0.22621.0/um/x64'
link_args = ['-L' + windows_sdk_lib_dir, '-lwindowsapp'] link_args = ['-L' + windows_sdk_lib_dir, '-lwindowsapp', '-static']
deps = [] deps = []

View file

@ -1,6 +1,12 @@
#include "config.h" #include "config.h"
fn Config::getInstance() -> const Config& { fn Config::getInstance()->const Config& {
static const Config* INSTANCE = new Config(rfl::toml::load<Config>("./config.toml").value()); #ifdef __WIN32__
const string path = string(getenv("LOCALAPPDATA")) + "\\draconis++\\config.toml";
#else
const string path = string(getenv("HOME")) + "/.config/draconis++/config.toml";
#endif
// ReSharper disable once CppDFAMemoryLeak
static const Config* INSTANCE = new Config(rfl::toml::load<Config>(path).value());
return *INSTANCE; return *INSTANCE;
} }

View file

@ -3,11 +3,14 @@
#include <rfl/json.hpp> #include <rfl/json.hpp>
#include <rfl/json/load.hpp> #include <rfl/json/load.hpp>
#include "../util/result.h"
#include "config.h" #include "config.h"
using rfl::Error;
using rfl::Nothing;
using rfl::Result;
// Function to read cache from file // Function to read cache from file
fn ReadCacheFromFile() -> Result<WeatherOutput> { fn ReadCacheFromFile()->Result<WeatherOutput> {
#ifdef __WIN32__ #ifdef __WIN32__
const char* tempPath = getenv("TEMP"); const char* tempPath = getenv("TEMP");
const string path = string(tempPath) + "\\weather_cache.json"; const string path = string(tempPath) + "\\weather_cache.json";
@ -21,23 +24,19 @@ fn ReadCacheFromFile() -> Result<WeatherOutput> {
fmt::println("Reading from cache file..."); fmt::println("Reading from cache file...");
WeatherOutput val; std::stringstream buf;
try { buf << ifs.rdbuf();
std::stringstream buf;
buf << ifs.rdbuf(); Result<WeatherOutput> val = rfl::json::read<WeatherOutput>(buf.str());
val = rfl::json::read<WeatherOutput>(buf.str()).value();
} catch (Error& e) { return e; }
fmt::println("Successfully read from cache file."); fmt::println("Successfully read from cache file.");
return Ok(val); return val;
} }
// Function to write cache to file // Function to write cache to file
fn WriteCacheToFile(const WeatherOutput& data) -> Result<> { fn WriteCacheToFile(const WeatherOutput& data)->Result<u8> {
fmt::println("Writing to cache file..."); fmt::println("Writing to cache file...");
#ifdef __WIN32__ #ifdef __WIN32__
@ -55,17 +54,17 @@ fn WriteCacheToFile(const WeatherOutput& data) -> Result<> {
fmt::println("Successfully wrote to cache file."); fmt::println("Successfully wrote to cache file.");
return Ok(); return 0;
} }
fn WriteCallback(void* contents, size_t size, size_t nmemb, string* str) -> size_t { fn WriteCallback(void* contents, const size_t size, const size_t nmemb, string* str)->size_t {
size_t totalSize = size * nmemb; const size_t totalSize = size * nmemb;
str->append(static_cast<char*>(contents), totalSize); str->append(static_cast<char*>(contents), totalSize);
return totalSize; return totalSize;
} }
// Function to make API request // Function to make API request
fn MakeApiRequest(const string& url) -> Result<WeatherOutput> { fn MakeApiRequest(const string& url)->Result<WeatherOutput> {
fmt::println("Making API request to URL: {}", url); fmt::println("Making API request to URL: {}", url);
CURL* curl = curl_easy_init(); CURL* curl = curl_easy_init();
@ -75,33 +74,31 @@ fn MakeApiRequest(const string& url) -> Result<WeatherOutput> {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseBuffer); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseBuffer);
CURLcode res = curl_easy_perform(curl); const CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
if (res != CURLE_OK) { if (res != CURLE_OK)
return Error(fmt::format("Failed to perform cURL request: {}", curl_easy_strerror(res))); return Error(fmt::format("Failed to perform cURL request: {}", curl_easy_strerror(res)));
}
fmt::println("Received response from API. Response size: {}", responseBuffer.size()); fmt::println("Received response from API. Response size: {}", responseBuffer.size());
fmt::println("Response: {}", responseBuffer); fmt::println("Response: {}", responseBuffer);
WeatherOutput output = rfl::json::read<WeatherOutput>(responseBuffer).value(); WeatherOutput output = rfl::json::read<WeatherOutput>(responseBuffer).value();
return Ok(output); // Return an empty result for now return output; // Return an empty result for now
} }
return Error("Failed to initialize cURL."); return Error("Failed to initialize cURL.");
} }
// Core function to get weather information // Core function to get weather information
fn Weather::getWeatherInfo() const -> WeatherOutput { fn Weather::getWeatherInfo() const->WeatherOutput {
using namespace std::chrono; using namespace std::chrono;
// Check if cache is valid // Check if cache is valid
if (Result<WeatherOutput> data = ReadCacheFromFile(); data.isOk()) { if (Result<WeatherOutput> data = ReadCacheFromFile()) {
WeatherOutput dataVal = data.value(); if (WeatherOutput dataVal = *data;
system_clock::now() - system_clock::time_point(seconds(dataVal.dt)) <
if (system_clock::now() - system_clock::time_point(seconds(dataVal.dt)) <
minutes(10)) { // Assuming cache duration is always 10 minutes minutes(10)) { // Assuming cache duration is always 10 minutes
fmt::println("Cache is valid. Returning cached data."); fmt::println("Cache is valid. Returning cached data.");

View file

@ -15,7 +15,7 @@ constexpr u64 GIB = 1'073'741'824;
template <> template <>
struct fmt::formatter<BytesToGiB> : formatter<double> { struct fmt::formatter<BytesToGiB> : formatter<double> {
template <typename FmtCtx> template <typename FmtCtx>
fn format(const BytesToGiB BTG, FmtCtx& ctx) -> typename FmtCtx::iterator { fn format(const BytesToGiB BTG, FmtCtx& ctx)->typename FmtCtx::iterator {
typename FmtCtx::iterator out = typename FmtCtx::iterator out =
formatter<double>::format(static_cast<double>(BTG.value) / GIB, ctx); formatter<double>::format(static_cast<double>(BTG.value) / GIB, ctx);
*out++ = 'G'; *out++ = 'G';
@ -25,7 +25,7 @@ struct fmt::formatter<BytesToGiB> : formatter<double> {
} }
}; };
fn GetDate() -> string { fn GetDate()->string {
const std::tm localTime = fmt::localtime(time(nullptr)); const std::tm localTime = fmt::localtime(time(nullptr));
string date = fmt::format("{:%e}", localTime); string date = fmt::format("{:%e}", localTime);
@ -45,7 +45,7 @@ fn GetDate() -> string {
return fmt::format("{:%B} {}, {:%-I:%0M %p}", localTime, date, localTime); return fmt::format("{:%B} {}, {:%-I:%0M %p}", localTime, date, localTime);
} }
fn main() -> i32 { fn main()->i32 {
using std::future; using std::future;
const Config& config = Config::getInstance(); const Config& config = Config::getInstance();
@ -62,7 +62,7 @@ fn main() -> i32 {
const auto const auto
[clouds, [clouds,
timezone, tz,
visibility, visibility,
main, main,
coords, coords,
@ -75,7 +75,8 @@ fn main() -> i32 {
cod, cod,
dt, dt,
id, id,
wind] = weatherFuture.get(); wind] = weatherFuture.get();
const i64 temp = std::lround(main.temp); const i64 temp = std::lround(main.temp);
const bool nowPlayingEnabled = nowPlayingEnabledFuture.get(); const bool nowPlayingEnabled = nowPlayingEnabledFuture.get();