From 8cbc00372d493e8be379f02fff2b2b2b0638b591 Mon Sep 17 00:00:00 2001 From: Mars Date: Mon, 17 Feb 2025 22:06:51 -0500 Subject: [PATCH] ugghghghgh --- flake.nix | 2 + src/main.cpp | 2 +- src/os/linux.cpp | 98 +++++++++++++++++--------------------- src/os/linux/pkg_count.cpp | 9 ++++ src/os/linux/pkg_count.h | 23 +++++---- src/os/os.h | 4 +- 6 files changed, 73 insertions(+), 65 deletions(-) diff --git a/flake.nix b/flake.nix index c340c63..e841796 100644 --- a/flake.nix +++ b/flake.nix @@ -156,6 +156,8 @@ ] ++ deps; + LD_LIBRARY_PATH = "${pkgs.lib.makeLibraryPath deps}"; + name = "C++"; }; } diff --git a/src/main.cpp b/src/main.cpp index c34f49d..a6577c4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -80,7 +80,7 @@ namespace { const string& host = GetHost(); const string& kernelVersion = GetKernelVersion(); const string& osVersion = GetOSVersion(); - const u64 memInfo = GetMemInfo(); + const u64 memInfo = GetMemInfo().value_or(0); const string& desktopEnvironment = GetDesktopEnvironment(); const string& windowManager = GetWindowManager(); const bool nowPlayingEnabled = config.now_playing.get().enabled; diff --git a/src/os/linux.cpp b/src/os/linux.cpp index 1d6c038..6ffc349 100644 --- a/src/os/linux.cpp +++ b/src/os/linux.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -25,59 +26,6 @@ namespace fs = std::filesystem; enum SessionType : u8 { Wayland, X11, TTY, Unknown }; namespace { - fn MeminfoParse() -> u64 { - constexpr const char* path = "/proc/meminfo"; - - std::ifstream input(path); - if (!input.is_open()) { - ERROR_LOG("Failed to open {}", path); - return 0; - } - - string line; - while (std::getline(input, line)) { - if (line.starts_with("MemTotal")) { - const size_t colonPos = line.find(':'); - if (colonPos == std::string::npos) { - ERROR_LOG("Invalid MemTotal line: no colon found"); - return 0; - } - - std::string_view view(line); - view.remove_prefix(colonPos + 1); - - // Trim leading whitespace - const size_t firstNonSpace = view.find_first_not_of(' '); - if (firstNonSpace == std::string_view::npos) { - ERROR_LOG("No number found after colon in MemTotal line"); - return 0; - } - view.remove_prefix(firstNonSpace); - - // Find the end of the numeric part - const size_t end = view.find_first_not_of("0123456789"); - if (end != std::string_view::npos) - view = view.substr(0, end); - - // Get pointers via iterators - const char* startPtr = &*view.begin(); // Safe iterator-to-pointer conversion - const char* endPtr = &*view.end(); // No manual arithmetic - - u64 value = 0; - const auto result = std::from_chars(startPtr, endPtr, value); - if (result.ec != std::errc() || result.ptr != endPtr) { - ERROR_LOG("Failed to parse number in MemTotal line"); - return 0; - } - - return value; - } - } - - ERROR_LOG("MemTotal line not found in {}", path); - return 0; - } - fn GetMprisPlayers(sdbus::IConnection& connection) -> std::vector { const sdbus::ServiceName dbusInterface = sdbus::ServiceName("org.freedesktop.DBus"); const sdbus::ObjectPath dbusObjectPath = sdbus::ObjectPath("/org/freedesktop/DBus"); @@ -450,7 +398,49 @@ fn GetOSVersion() -> string { return ""; } -fn GetMemInfo() -> u64 { return MeminfoParse() * 1024; } +fn GetMemInfo() -> std::expected { + constexpr const char* path = "/proc/meminfo"; + + std::ifstream input(path); + if (!input.is_open()) + return std::unexpected("Failed to open " + std::string(path)); + + std::string line; + while (std::getline(input, line)) { + if (line.starts_with("MemTotal")) { + const size_t colonPos = line.find(':'); + if (colonPos == std::string::npos) + return std::unexpected("Invalid MemTotal line: no colon found"); + + std::string_view view(line); + view.remove_prefix(colonPos + 1); + + // Trim leading whitespace + const size_t firstNonSpace = view.find_first_not_of(' '); + if (firstNonSpace == std::string_view::npos) + return std::unexpected("No number found after colon in MemTotal line"); + view.remove_prefix(firstNonSpace); + + // Find the end of the numeric part + const size_t end = view.find_first_not_of("0123456789"); + if (end != std::string_view::npos) + view = view.substr(0, end); + + // Get pointers via iterators + const char* startPtr = &*view.begin(); + const char* endPtr = &*view.end(); + + u64 value = 0; + const auto result = std::from_chars(startPtr, endPtr, value); + if (result.ec != std::errc() || result.ptr != endPtr) + return std::unexpected("Failed to parse number in MemTotal line"); + + return value * 1024; + } + } + + return std::unexpected("MemTotal line not found in " + std::string(path)); +} fn GetNowPlaying() -> string { try { diff --git a/src/os/linux/pkg_count.cpp b/src/os/linux/pkg_count.cpp index e69de29..573b5f2 100644 --- a/src/os/linux/pkg_count.cpp +++ b/src/os/linux/pkg_count.cpp @@ -0,0 +1,9 @@ +#include "src/os/linux/pkg_count.h" + +namespace fs = std::filesystem; + +fn GetApkPackageCount() -> std::optional { + fs::path apkDbPath("/lib/apk/db/installed"); + + return std::nullopt; +} diff --git a/src/os/linux/pkg_count.h b/src/os/linux/pkg_count.h index 3f71bbc..3283dbc 100644 --- a/src/os/linux/pkg_count.h +++ b/src/os/linux/pkg_count.h @@ -1,28 +1,33 @@ #pragma once +#include "src/util/macros.h" + // Get package count from dpkg (Debian/Ubuntu) -int get_dpkg_package_count(); +fn GetDpkgPackageCount() -> std::optional; // Get package count from RPM (Red Hat/Fedora/CentOS) -int get_rpm_package_count(); +fn GetRpmPackageCount() -> std::optional; // Get package count from pacman (Arch Linux) -int get_pacman_package_count(); +fn GetPacmanPackageCount() -> std::optional; // Get package count from Portage (Gentoo) -int get_portage_package_count(); +fn GetPortagePackageCount() -> std::optional; // Get package count from zypper (openSUSE) -int get_zypper_package_count(); +fn GetZypperPackageCount() -> std::optional; + +// Get package count from apk (Alpine) +fn GetApkPackageCount() -> std::optional; // Get package count from flatpak -int get_flatpak_package_count(); +fn GetFlatpakPackageCount() -> std::optional; // Get package count from snap -int get_snap_package_count(); +fn GetSnapPackageCount() -> std::optional; // Get package count from AppImage -int get_appimage_package_count(); +fn GetAppimagePackageCount() -> std::optional; // Get total package count from all available package managers -fn GetTotalPackageCount() -> int; +fn GetTotalPackageCount() -> std::optional; diff --git a/src/os/os.h b/src/os/os.h index d240a3b..a38ec86 100644 --- a/src/os/os.h +++ b/src/os/os.h @@ -1,12 +1,14 @@ #pragma once +#include + #include "../util/macros.h" #include "../util/types.h" /** * @brief Get the amount of installed RAM in bytes. */ -fn GetMemInfo() -> u64; +fn GetMemInfo() -> std::expected; /** * @brief Get the currently playing song metadata.