osme stuf

This commit is contained in:
Mars 2024-07-02 05:54:06 -04:00
parent 4f039f9c79
commit dff976ed0e
7 changed files with 44 additions and 48 deletions

View file

@ -5,7 +5,7 @@ project(
'cpp_std=c++23', 'cpp_std=c++23',
'default_library=static', 'default_library=static',
'warning_level=everything', 'warning_level=everything',
'buildtype=debugoptimized' 'buildtype=release'
] ]
) )

View file

@ -4,12 +4,18 @@
using rfl::Result; using rfl::Result;
fn Config::getInstance() -> Config { inline fn GetConfigPath() -> string {
return getenv(
#ifdef __WIN32__ #ifdef __WIN32__
const string path = string(getenv("LOCALAPPDATA")) + "\\draconis++\\config.toml"; "LOCALAPPDATA"
#else #else
const string path = string(getenv("HOME")) + "/.config/draconis++/config.toml"; "HOME"
#endif #endif
);
}
fn Config::getInstance() -> Config {
const string path = GetConfigPath() + "\\draconis++\\config.toml";
const Result<Config> result = rfl::toml::load<Config>(path); const Result<Config> result = rfl::toml::load<Config>(path);
if (result) if (result)
@ -18,4 +24,3 @@ fn Config::getInstance() -> Config {
fmt::println("Failed to load config file: {}", result.error().value().what()); fmt::println("Failed to load config file: {}", result.error().value().what());
return {}; return {};
} }

View file

@ -24,7 +24,7 @@ struct Weather {
string api_key; string api_key;
string units; string units;
[[nodiscard]] fn getWeatherInfo() const->WeatherOutput; [[nodiscard]] fn getWeatherInfo() const -> WeatherOutput;
}; };
struct Config { struct Config {
@ -32,5 +32,5 @@ struct Config {
rfl::Field<"now_playing", NowPlaying> now_playing = NowPlaying(); rfl::Field<"now_playing", NowPlaying> now_playing = NowPlaying();
rfl::Field<"weather", Weather> weather = Weather(); rfl::Field<"weather", Weather> weather = Weather();
static fn getInstance()->Config; static fn getInstance() -> Config;
}; };

View file

@ -9,7 +9,7 @@ using rfl::Error;
using rfl::Result; 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";
@ -35,7 +35,7 @@ fn ReadCacheFromFile()->Result<WeatherOutput> {
} }
// Function to write cache to file // Function to write cache to file
fn WriteCacheToFile(const WeatherOutput& data)->Result<u8> { fn WriteCacheToFile(const WeatherOutput& data) -> Result<u8> {
fmt::println("Writing to cache file..."); fmt::println("Writing to cache file...");
#ifdef __WIN32__ #ifdef __WIN32__
@ -56,14 +56,14 @@ fn WriteCacheToFile(const WeatherOutput& data)->Result<u8> {
return 0; return 0;
} }
fn WriteCallback(void* contents, const size_t size, const size_t nmemb, string* str)->size_t { fn WriteCallback(void* contents, const size_t size, const size_t nmemb, string* str) -> size_t {
const 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();
@ -91,7 +91,7 @@ fn MakeApiRequest(const string& url)->Result<WeatherOutput> {
} }
// 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

View file

@ -1,7 +1,6 @@
#include <ctime> #include <ctime>
#include <fmt/chrono.h> #include <fmt/chrono.h>
#include <fmt/core.h> #include <fmt/core.h>
#include <future>
#include "config/config.h" #include "config/config.h"
#include "os/os.h" #include "os/os.h"
@ -46,53 +45,41 @@ fn GetDate() -> string {
} }
fn main() -> i32 { fn main() -> i32 {
using std::future; const Config& config = Config::getInstance();
const Config config = Config::getInstance(); // Fetching weather information
auto weatherInfo = config.weather.get().getWeatherInfo();
auto weatherFuture = // Fetching OS version
std::async(std::launch::async, [&config]() { return config.weather.get().getWeatherInfo(); }); std::string osVersion = GetOSVersion();
auto osVersionFuture = std::async(std::launch::async, GetOSVersion); // Checking if now playing is enabled
auto nowPlayingEnabledFuture = bool nowPlayingEnabled = config.now_playing.get().enabled;
std::async(std::launch::async, [&config]() { return config.now_playing.get().enabled; });
future<string> dateFuture = std::async(std::launch::async, GetDate); // Fetching current date
future<u64> memInfoFuture = std::async(std::launch::async, GetMemInfo); std::string date = GetDate();
const bool nowPlayingEnabled = nowPlayingEnabledFuture.get(); // Fetching memory info
const string version = osVersionFuture.get(); u64 memInfo = GetMemInfo();
const string date = dateFuture.get();
const string name = config.general.get().name.get(); const std::string& name = config.general.get().name.get();
const u64 mem = memInfoFuture.get();
fmt::println("Hello {}!", name); fmt::println("Hello {}!", name);
fmt::println("Today is: {}", date); fmt::println("Today is: {}", date);
fmt::println("Installed RAM: {:.2f}", BytesToGiB(mem)); fmt::println("Installed RAM: {:.2f}", BytesToGiB(memInfo));
fmt::println("{}", version); fmt::println("{}", osVersion);
if (config.weather.get().enabled) { if (config.weather.get().enabled) {
const auto const auto& [clouds, tz, visibility, main, coords, rain, snow, base, townName, weather, sys, cod, dt, id, wind] =
[clouds, weatherInfo;
tz, i64 temp = std::lround(main.temp);
visibility,
main,
coords,
rain,
snow,
base,
townName,
weather,
sys,
cod,
dt,
id,
wind] = weatherFuture.get();
const i64 temp = std::lround(main.temp);
fmt::println("It is {}°F in {}", temp, townName); fmt::println("It is {}°F in {}", temp, townName);
} }
if (nowPlayingEnabled) if (nowPlayingEnabled) {
fmt::println("{}", GetNowPlaying()); fmt::println("{}", GetNowPlaying());
}
return 0;
} }

View file

@ -21,6 +21,6 @@ fn GetNowPlaying() -> string {
return "No song playing"; return "No song playing";
} }
fn GetOSVersion() -> const char* { return GetMacOSVersion(); }; fn GetOSVersion() -> const char* { return GetMacOSVersion(); }
#endif #endif

View file

@ -1,3 +1,5 @@
#ifdef __WIN32__
#include <exception> #include <exception>
#include <windows.h> #include <windows.h>
#include <winrt/Windows.Foundation.h> #include <winrt/Windows.Foundation.h>
@ -105,3 +107,5 @@ fn GetOSVersion() -> string {
return ""; return "";
} }
#endif