From 164a68c5eed0c5333e1e294b67fac2dcc18e9d6c Mon Sep 17 00:00:00 2001 From: Mars Date: Fri, 21 Jun 2024 23:07:31 -0400 Subject: [PATCH] fixed some more stuff --- meson.build | 2 +- src/config/config.cpp | 10 ++++++++-- src/config/weather.cpp | 45 ++++++++++++++++++++---------------------- src/main.cpp | 11 ++++++----- 4 files changed, 36 insertions(+), 32 deletions(-) diff --git a/meson.build b/meson.build index c4de06b..4eca14b 100644 --- a/meson.build +++ b/meson.build @@ -43,7 +43,7 @@ foreach file : source_file_names endforeach 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 = [] diff --git a/src/config/config.cpp b/src/config/config.cpp index 788f861..275fb3c 100644 --- a/src/config/config.cpp +++ b/src/config/config.cpp @@ -1,6 +1,12 @@ #include "config.h" -fn Config::getInstance() -> const Config& { - static const Config* INSTANCE = new Config(rfl::toml::load("./config.toml").value()); +fn Config::getInstance()->const Config& { +#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(path).value()); return *INSTANCE; } diff --git a/src/config/weather.cpp b/src/config/weather.cpp index d10f6a2..c063ae4 100644 --- a/src/config/weather.cpp +++ b/src/config/weather.cpp @@ -3,11 +3,14 @@ #include #include -#include "../util/result.h" #include "config.h" +using rfl::Error; +using rfl::Nothing; +using rfl::Result; + // Function to read cache from file -fn ReadCacheFromFile() -> Result { +fn ReadCacheFromFile()->Result { #ifdef __WIN32__ const char* tempPath = getenv("TEMP"); const string path = string(tempPath) + "\\weather_cache.json"; @@ -21,23 +24,19 @@ fn ReadCacheFromFile() -> Result { fmt::println("Reading from cache file..."); - WeatherOutput val; + std::stringstream buf; - try { - std::stringstream buf; + buf << ifs.rdbuf(); - buf << ifs.rdbuf(); - - val = rfl::json::read(buf.str()).value(); - } catch (Error& e) { return e; } + Result val = rfl::json::read(buf.str()); fmt::println("Successfully read from cache file."); - return Ok(val); + return val; } // Function to write cache to file -fn WriteCacheToFile(const WeatherOutput& data) -> Result<> { +fn WriteCacheToFile(const WeatherOutput& data)->Result { fmt::println("Writing to cache file..."); #ifdef __WIN32__ @@ -55,17 +54,17 @@ fn WriteCacheToFile(const WeatherOutput& data) -> Result<> { 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 { - size_t totalSize = size * nmemb; +fn WriteCallback(void* contents, const size_t size, const size_t nmemb, string* str)->size_t { + const size_t totalSize = size * nmemb; str->append(static_cast(contents), totalSize); return totalSize; } // Function to make API request -fn MakeApiRequest(const string& url) -> Result { +fn MakeApiRequest(const string& url)->Result { fmt::println("Making API request to URL: {}", url); CURL* curl = curl_easy_init(); @@ -75,33 +74,31 @@ fn MakeApiRequest(const string& url) -> Result { curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseBuffer); - CURLcode res = curl_easy_perform(curl); + const CURLcode res = curl_easy_perform(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))); - } fmt::println("Received response from API. Response size: {}", responseBuffer.size()); fmt::println("Response: {}", responseBuffer); WeatherOutput output = rfl::json::read(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."); } // Core function to get weather information -fn Weather::getWeatherInfo() const -> WeatherOutput { +fn Weather::getWeatherInfo() const->WeatherOutput { using namespace std::chrono; // Check if cache is valid - if (Result data = ReadCacheFromFile(); data.isOk()) { - WeatherOutput dataVal = data.value(); - - if (system_clock::now() - system_clock::time_point(seconds(dataVal.dt)) < + if (Result data = ReadCacheFromFile()) { + if (WeatherOutput dataVal = *data; + system_clock::now() - system_clock::time_point(seconds(dataVal.dt)) < minutes(10)) { // Assuming cache duration is always 10 minutes fmt::println("Cache is valid. Returning cached data."); diff --git a/src/main.cpp b/src/main.cpp index 075c60a..536f4bf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,7 +15,7 @@ constexpr u64 GIB = 1'073'741'824; template <> struct fmt::formatter : formatter { template - fn format(const BytesToGiB BTG, FmtCtx& ctx) -> typename FmtCtx::iterator { + fn format(const BytesToGiB BTG, FmtCtx& ctx)->typename FmtCtx::iterator { typename FmtCtx::iterator out = formatter::format(static_cast(BTG.value) / GIB, ctx); *out++ = 'G'; @@ -25,7 +25,7 @@ struct fmt::formatter : formatter { } }; -fn GetDate() -> string { +fn GetDate()->string { const std::tm localTime = fmt::localtime(time(nullptr)); string date = fmt::format("{:%e}", localTime); @@ -45,7 +45,7 @@ fn GetDate() -> string { return fmt::format("{:%B} {}, {:%-I:%0M %p}", localTime, date, localTime); } -fn main() -> i32 { +fn main()->i32 { using std::future; const Config& config = Config::getInstance(); @@ -62,7 +62,7 @@ fn main() -> i32 { const auto [clouds, - timezone, + tz, visibility, main, coords, @@ -75,7 +75,8 @@ fn main() -> i32 { cod, dt, id, - wind] = weatherFuture.get(); + wind] = weatherFuture.get(); + const i64 temp = std::lround(main.temp); const bool nowPlayingEnabled = nowPlayingEnabledFuture.get();