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
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 = []

View file

@ -1,6 +1,12 @@
#include "config.h"
fn Config::getInstance() -> const Config& {
static const Config* INSTANCE = new Config(rfl::toml::load<Config>("./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<Config>(path).value());
return *INSTANCE;
}

View file

@ -3,11 +3,14 @@
#include <rfl/json.hpp>
#include <rfl/json/load.hpp>
#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<WeatherOutput> {
fn ReadCacheFromFile()->Result<WeatherOutput> {
#ifdef __WIN32__
const char* tempPath = getenv("TEMP");
const string path = string(tempPath) + "\\weather_cache.json";
@ -21,23 +24,19 @@ fn ReadCacheFromFile() -> Result<WeatherOutput> {
fmt::println("Reading from cache file...");
WeatherOutput val;
try {
std::stringstream buf;
buf << ifs.rdbuf();
val = rfl::json::read<WeatherOutput>(buf.str()).value();
} catch (Error& e) { return e; }
Result<WeatherOutput> val = rfl::json::read<WeatherOutput>(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<u8> {
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<char*>(contents), totalSize);
return totalSize;
}
// 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);
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_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<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.");
}
// 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<WeatherOutput> data = ReadCacheFromFile(); data.isOk()) {
WeatherOutput dataVal = data.value();
if (system_clock::now() - system_clock::time_point(seconds(dataVal.dt)) <
if (Result<WeatherOutput> 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.");

View file

@ -15,7 +15,7 @@ constexpr u64 GIB = 1'073'741'824;
template <>
struct fmt::formatter<BytesToGiB> : formatter<double> {
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 =
formatter<double>::format(static_cast<double>(BTG.value) / GIB, ctx);
*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));
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,
@ -76,6 +76,7 @@ fn main() -> i32 {
dt,
id,
wind] = weatherFuture.get();
const i64 temp = std::lround(main.temp);
const bool nowPlayingEnabled = nowPlayingEnabledFuture.get();