From 3f4b24c259185d1e8b7595b09da0797285ddc568 Mon Sep 17 00:00:00 2001 From: pupbrained Date: Wed, 31 Jul 2024 00:07:40 -0400 Subject: [PATCH] updates/fixes --- flake.nix | 24 +++++++++++++++++------- src/config/weather.cpp | 7 ++++--- src/config/weather.h | 1 - src/main.cpp | 30 ++++++++++++++++++------------ src/os/linux.cpp | 32 ++++++++++++++++++++++---------- 5 files changed, 61 insertions(+), 33 deletions(-) diff --git a/flake.nix b/flake.nix index ce61f32..dae9ff8 100644 --- a/flake.nix +++ b/flake.nix @@ -23,15 +23,15 @@ then pkgs.stdenvAdapters.useMoldLinker pkgs.llvmPackages_18.stdenv else pkgs.llvmPackages_18.stdenv; - reflect-cpp = stdenv.mkDerivation { + reflect-cpp = stdenv.mkDerivation rec { name = "reflect-cpp"; - version = "0.11.1"; + version = "0.13.0"; src = pkgs.fetchFromGitHub { owner = "getml"; repo = "reflect-cpp"; - rev = "1ce78479ac9d04eb396ad972d656858eb06661d2"; - hash = "sha256-8TW2OlCbQZ07HypoYQE/wo29mxJWJwLziK1BpkhdFBo="; + rev = "v${version}"; + hash = "sha256-dEqdPk5ixnNILxTcdSAOhzP8fzeefMu6pqrL/WgnPlE="; }; nativeBuildInputs = with pkgs; [cmake ninja pkg-config]; @@ -43,10 +43,20 @@ ]; }; + sdbus-cpp = pkgs.sdbus-cpp.overrideAttrs rec { + version = "2.0.0"; + src = pkgs.fetchFromGitHub { + owner = "kistler-group"; + repo = "sdbus-cpp"; + rev = "v${version}"; + hash = "sha256-W8V5FRhV3jtERMFrZ4gf30OpIQLYoj2yYGpnYOmH2+g="; + }; + }; + deps = with pkgs.pkgsStatic; [ curl - fmt + fmt_11 libiconv tomlplusplus yyjson @@ -55,8 +65,8 @@ ++ linuxPkgs ++ darwinPkgs; - linuxPkgs = nixpkgs.lib.optionals stdenv.isLinux (with pkgs.pkgsStatic; [ - glib + linuxPkgs = nixpkgs.lib.optionals stdenv.isLinux (with pkgs; [ + pkgsStatic.glib systemdLibs sdbus-cpp valgrind diff --git a/src/config/weather.cpp b/src/config/weather.cpp index 3503697..2d0f75f 100644 --- a/src/config/weather.cpp +++ b/src/config/weather.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -96,9 +97,9 @@ fn Weather::getWeatherInfo() const -> WeatherOutput { // Check if cache is valid 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 + WeatherOutput dataVal = *data; + + if (system_clock::now() - system_clock::time_point(seconds(dataVal.dt)) < minutes(10)) { fmt::println("Cache is valid. Returning cached data."); return dataVal; diff --git a/src/config/weather.h b/src/config/weather.h index 58e2edb..db3e342 100644 --- a/src/config/weather.h +++ b/src/config/weather.h @@ -2,7 +2,6 @@ #include #include -#include #include "../util/types.h" diff --git a/src/main.cpp b/src/main.cpp index 3a43497..47bd442 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,8 @@ #include #include #include +#include +#include #include "config/config.h" #include "os/os.h" @@ -12,26 +14,30 @@ struct BytesToGiB { constexpr u64 GIB = 1'073'741'824; template <> -struct fmt::formatter : formatter { +struct fmt::formatter : fmt::formatter { template - fn format(const BytesToGiB BTG, FmtCtx& ctx) -> typename FmtCtx::iterator { - typename FmtCtx::iterator out = - formatter::format(static_cast(BTG.value) / GIB, ctx); - *out++ = 'G'; - *out++ = 'i'; - *out++ = 'B'; + constexpr auto format(const BytesToGiB& BTG, FmtCtx& ctx) const -> typename FmtCtx::iterator { + auto out = fmt::formatter::format(static_cast(BTG.value) / GIB, ctx); + *out++ = 'G'; + *out++ = 'i'; + *out++ = 'B'; return out; } }; -fn GetDate() -> string { - const std::tm localTime = fmt::localtime(time(nullptr)); +fn GetDate() -> std::string { + // Get current local time + std::time_t now = std::time(nullptr); + std::tm localTime = *std::localtime(&now); - string date = fmt::format("{:%e}", localTime); + // Format the date using fmt::format + std::string date = fmt::format("{:%e}", localTime); + // Remove leading whitespace if (!date.empty() && std::isspace(date.front())) date.erase(date.begin()); + // Append appropriate suffix for the date if (date == "1" || date == "21" || date == "31") date += "st"; else if (date == "2" || date == "22") @@ -41,7 +47,7 @@ fn GetDate() -> string { else date += "th"; - return fmt::format("{:%B} {}, {:%-I:%0M %p}", localTime, date, localTime); + return fmt::format("{:%B} {}", localTime, date); } fn main() -> i32 { @@ -68,7 +74,7 @@ fn main() -> i32 { fmt::println("Hello {}!", name); fmt::println("Today is: {}", date); fmt::println("Installed RAM: {:.2f}", BytesToGiB(memInfo)); - fmt::println("{}", osVersion); + fmt::println("OS: {}", osVersion); if (weather.enabled) fmt::println("It is {}°F in {}", std::lround(weatherInfo.main.temp), weatherInfo.name); diff --git a/src/os/linux.cpp b/src/os/linux.cpp index e21dfb2..feee51c 100644 --- a/src/os/linux.cpp +++ b/src/os/linux.cpp @@ -1,6 +1,7 @@ #ifdef __linux__ #include +#include #include #include #include @@ -66,8 +67,9 @@ fn GetOSVersion() -> string { } fn GetMprisPlayers(sdbus::IConnection& connection) -> std::vector { - const char *dbusInterface = "org.freedesktop.DBus", *dbusObjectPath = "/org/freedesktop/DBus", - *dbusMethodListNames = "ListNames"; + const sdbus::ServiceName dbusInterface = sdbus::ServiceName("org.freedesktop.DBus"); + const sdbus::ObjectPath dbusObjectPath = sdbus::ObjectPath("/org/freedesktop/DBus"); + const char* dbusMethodListNames = "ListNames"; const std::unique_ptr dbusProxy = createProxy(connection, dbusInterface, dbusObjectPath); @@ -80,7 +82,7 @@ fn GetMprisPlayers(sdbus::IConnection& connection) -> std::vector { for (const std::basic_string& name : names) if (const char* mprisInterfaceName = "org.mpris.MediaPlayer2"; - name.contains(mprisInterfaceName)) + name.find(mprisInterfaceName) != std::string::npos) mprisPlayers.push_back(name); return mprisPlayers; @@ -110,17 +112,27 @@ fn GetNowPlaying() -> string { if (activePlayer.empty()) return ""; - std::unique_ptr playerProxy = - sdbus::createProxy(*connection, activePlayer, playerObjectPath); + auto playerProxy = sdbus::createProxy( + *connection, sdbus::ServiceName(activePlayer), sdbus::ObjectPath(playerObjectPath) + ); - std::map metadata = + sdbus::Variant metadataVariant = playerProxy->getProperty("Metadata").onInterface(playerInterfaceName); - if (const auto iter = metadata.find("xesam:title"); + if (metadataVariant.containsValueOfType>()) { + const auto& metadata = metadataVariant.get>(); - iter != metadata.end() && iter->second.containsValueOfType()) - return iter->second.get(); - } catch (const sdbus::Error& e) { std::cerr << "Error: " << e.what() << '\n'; } + auto iter = metadata.find("xesam:title"); + + if (iter != metadata.end() && iter->second.containsValueOfType()) + return iter->second.get(); + } + } catch (const sdbus::Error& e) { + if (e.getName() != "com.github.altdesktop.playerctld.NoActivePlayer") + return fmt::format("Error: {}", e.what()); + + return "No active player"; + } return ""; }