diff --git a/src/config/config.cpp b/src/config/config.cpp index 275fb3c..2735460 100644 --- a/src/config/config.cpp +++ b/src/config/config.cpp @@ -1,12 +1,20 @@ +#include + #include "config.h" -fn Config::getInstance()->const Config& { +using rfl::Result; + +fn Config::getInstance()->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; -} + const Result result = rfl::toml::load(path); + + if (result) + return result.value(); + + fmt::println("Failed to load config file: {}", result.error().value().what()); + return {}; +} \ No newline at end of file diff --git a/src/config/config.h b/src/config/config.h index a131345..082410a 100644 --- a/src/config/config.h +++ b/src/config/config.h @@ -22,7 +22,7 @@ struct Weather { string api_key; string units; - [[nodiscard]] fn getWeatherInfo() const -> WeatherOutput; + [[nodiscard]] fn getWeatherInfo() const->WeatherOutput; }; struct Config { @@ -30,5 +30,5 @@ struct Config { rfl::Field<"now_playing", NowPlaying> now_playing = NowPlaying(); rfl::Field<"weather", Weather> weather = Weather(); - static fn getInstance() -> const Config&; + static fn getInstance()->Config; }; diff --git a/src/config/weather.cpp b/src/config/weather.cpp index c063ae4..bda8cc7 100644 --- a/src/config/weather.cpp +++ b/src/config/weather.cpp @@ -6,7 +6,6 @@ #include "config.h" using rfl::Error; -using rfl::Nothing; using rfl::Result; // Function to read cache from file diff --git a/src/main.cpp b/src/main.cpp index 536f4bf..92fdb47 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -48,7 +48,7 @@ fn GetDate()->string { fn main()->i32 { using std::future; - const Config& config = Config::getInstance(); + const Config config = Config::getInstance(); auto weatherFuture = std::async(std::launch::async, [&config]() { return config.weather.get().getWeatherInfo(); }); diff --git a/src/os/windows.cpp b/src/os/windows.cpp index 6419cb3..5aff406 100644 --- a/src/os/windows.cpp +++ b/src/os/windows.cpp @@ -6,13 +6,13 @@ #include "os.h" -fn GetMemInfo()->u64 { +fn GetMemInfo() -> u64 { u64 mem = 0; GetPhysicallyInstalledSystemMemory(&mem); return mem * 1024; } -fn GetNowPlaying()->string { +fn GetNowPlaying() -> string { using namespace winrt::Windows::Media::Control; using namespace winrt::Windows::Foundation; @@ -27,9 +27,7 @@ fn GetNowPlaying()->string { if (const Session currentSession = sessionManager.GetCurrentSession()) { // Try to get the media properties asynchronously - const IAsyncOperation mediaPropertiesOp = - currentSession.TryGetMediaPropertiesAsync(); - const MediaProperties mediaProperties = mediaPropertiesOp.get(); + const MediaProperties mediaProperties = currentSession.TryGetMediaPropertiesAsync().get(); // Convert the hstring title to string return to_string(mediaProperties.Title()); @@ -40,13 +38,14 @@ fn GetNowPlaying()->string { } catch (...) { return "Failed to get media properties."; } } -fn GetRegistryValue(const HKEY& hKey, const string& subKey, const string& valueName)->string { +fn GetRegistryValue(const HKEY& hKey, const string& subKey, const string& valueName) -> string { HKEY key = nullptr; if (RegOpenKeyExA(hKey, subKey.c_str(), 0, KEY_READ, &key) != ERROR_SUCCESS) return ""; DWORD dataSize = 0; - if (RegQueryValueExA(key, valueName.c_str(), nullptr, nullptr, nullptr, &dataSize) != ERROR_SUCCESS) { + if (RegQueryValueExA(key, valueName.c_str(), nullptr, nullptr, nullptr, &dataSize) != + ERROR_SUCCESS) { RegCloseKey(key); return ""; } @@ -72,7 +71,7 @@ fn GetRegistryValue(const HKEY& hKey, const string& subKey, const string& valueN return value; } -fn GetOSVersion()->string { +fn GetOSVersion() -> string { string productName = GetRegistryValue( HKEY_LOCAL_MACHINE, R"(SOFTWARE\Microsoft\Windows NT\CurrentVersion)", "ProductName" );