From d5f3c2351f76e92eb68c3afe2b2066e00e3e5750 Mon Sep 17 00:00:00 2001 From: pupbrained Date: Thu, 8 May 2025 14:58:46 -0400 Subject: [PATCH] small fixups i think --- src/core/system_data.hpp | 6 ++++ src/os/linux.cpp | 33 ++++++++++++------- src/ui/ui.cpp | 71 ++++++++++++++++++++++++++++++++++------ 3 files changed, 89 insertions(+), 21 deletions(-) diff --git a/src/core/system_data.hpp b/src/core/system_data.hpp index 3ec5f9a..ecbbb23 100644 --- a/src/core/system_data.hpp +++ b/src/core/system_data.hpp @@ -21,6 +21,12 @@ using util::types::u64, util::types::f64, util::types::String, util::types::Opti */ struct BytesToGiB { u64 value; ///< The byte value to be converted. + + /** + * @brief Constructor for BytesToGiB. + * @param value The byte value to be converted. + */ + explicit constexpr BytesToGiB(u64 value) : value(value) {} }; /// @brief Conversion factor from bytes to GiB diff --git a/src/os/linux.cpp b/src/os/linux.cpp index 06a4783..aa02ef9 100644 --- a/src/os/linux.cpp +++ b/src/os/linux.cpp @@ -41,15 +41,15 @@ #include "os.hpp" // clang-format on -using namespace util::types; using util::error::DracError, util::error::DracErrorCode; -using util::helpers::GetEnv; +using util::types::String, util::types::Result, util::types::Err, util::types::usize; namespace { fn GetX11WindowManager() -> Result { using namespace xcb; using namespace matchit; using enum ConnError; + using util::types::StringView; const DisplayGuard conn; @@ -72,6 +72,8 @@ namespace { ); fn internAtom = [&conn](const StringView name) -> Result { + using util::types::u16; + const ReplyGuard reply(InternAtomReply(conn.get(), InternAtom(conn.get(), 0, static_cast(name.size()), name.data()), nullptr)); if (!reply) @@ -123,6 +125,8 @@ namespace { } fn GetWaylandCompositor() -> Result { + using util::types::i32, util::types::Array, util::types::isize, util::types::StringView; + const wl::DisplayGuard display; if (!display) @@ -195,13 +199,15 @@ namespace { } // namespace namespace os { - fn GetOSVersion() -> Result { - constexpr CStr path = "/etc/os-release"; + using util::helpers::GetEnv; - std::ifstream file(path); + fn GetOSVersion() -> Result { + using util::types::StringView; + + std::ifstream file("/etc/os-release"); if (!file) - return Err(DracError(DracErrorCode::NotFound, std::format("Failed to open {}", path))); + return Err(DracError(DracErrorCode::NotFound, std::format("Failed to open /etc/os-release"))); String line; constexpr StringView prefix = "PRETTY_NAME="; @@ -216,14 +222,14 @@ namespace os { if (value.empty()) return Err( - DracError(DracErrorCode::ParseError, std::format("PRETTY_NAME value is empty or only quotes in {}", path)) + DracError(DracErrorCode::ParseError, std::format("PRETTY_NAME value is empty or only quotes in /etc/os-release")) ); return value; } } - return Err(DracError(DracErrorCode::NotFound, std::format("PRETTY_NAME line not found in {}", path))); + return Err(DracError(DracErrorCode::NotFound, "PRETTY_NAME line not found in /etc/os-release")); } fn GetMemInfo() -> Result { @@ -403,6 +409,8 @@ namespace os { } fn GetShell() -> Result { + using util::types::Pair, util::types::Array, util::types::StringView; + if (const Result shellPath = GetEnv("SHELL")) { // clang-format off constexpr Array, 5> shellMap {{ @@ -425,6 +433,8 @@ namespace os { } fn GetHost() -> Result { + using util::types::CStr; + constexpr CStr primaryPath = "/sys/class/dmi/id/product_family"; constexpr CStr fallbackPath = "/sys/class/dmi/id/product_name"; @@ -603,8 +613,9 @@ namespace package { } fn CountXbps() -> Result { - const StringView xbpsDbPath = "/var/db/xbps"; - const String pmId = "xbps"; + using util::types::CStr; + + const CStr xbpsDbPath = "/var/db/xbps"; if (!fs::exists(xbpsDbPath)) return Err(DracError(DracErrorCode::NotFound, std::format("Xbps database path '{}' does not exist", xbpsDbPath))); @@ -621,7 +632,7 @@ namespace package { if (plistPath.empty()) return Err(DracError(DracErrorCode::NotFound, "No Xbps database found")); - return GetCountFromPlist(pmId, plistPath); + return GetCountFromPlist("xbps", plistPath); } } // namespace package diff --git a/src/ui/ui.cpp b/src/ui/ui.cpp index d12ffcf..b1e2be1 100644 --- a/src/ui/ui.cpp +++ b/src/ui/ui.cpp @@ -1,5 +1,6 @@ #include "ui.hpp" +#include "src/os/os.hpp" #include "src/util/types.hpp" namespace ui { @@ -31,12 +32,22 @@ namespace ui { }; [[maybe_unused]] static constexpr Icons NERD = { - .user = "  ", - .palette = "  ", - .calendar = "  ", - .host = " 󰌢 ", - .kernel = "  ", - .os = "  ", + .user = "  ", + .palette = "  ", + .calendar = "  ", + .host = " 󰌢 ", + .kernel = "  ", +#ifdef __linux__ + .os = " 󰌽 ", +#elifdef __APPLE__ + .os = "  ", +#elifdef _WIN32 + .os = "  ", +#elifdef __FreeBSD__ + .os = "  ", +#else + .os = "  ", +#endif .memory = "  ", .weather = "  ", .music = "  ", @@ -73,6 +84,37 @@ namespace ui { }; namespace { +#ifdef __linux__ + // clang-format off + constexpr Array, 13> distro_icons {{ + { "NixOS", "  " }, + { "Zorin", "  " }, + { "Debian", "  " }, + { "Fedora", "  " }, + { "Gentoo", "  " }, + { "Ubuntu", "  " }, + { "Manjaro", "  " }, + { "Pop!_OS", "  " }, + { "Arch Linux", "  " }, + { "Linux Mint", "  " }, + { "Void Linux", "  " }, + { "Alpine Linux", "  " }, + }}; + // clang-format on + + fn GetDistroIcon() -> Option { + using namespace matchit; + + const Result distro = os::GetOSVersion(); + + for (const auto& [distroName, distroIcon] : distro_icons) + if (distro->contains(distroName)) + return distroIcon; + + return None; + } +#endif + fn CreateColorCircles() -> Element { auto colorView = std::views::iota(0, 16) | std::views::transform([](i32 colorIndex) { @@ -146,14 +188,23 @@ namespace ui { if (data.host && !data.host->empty()) systemInfoRows.push_back({ .icon = hostIcon, .label = "Host", .value = *data.host }); - if (data.osVersion) - systemInfoRows.push_back({ .icon = osIcon, .label = "OS", .value = *data.osVersion }); + if (data.osVersion) { + systemInfoRows.push_back({ +#ifdef __linux__ + .icon = GetDistroIcon().value_or(osIcon), +#else + .icon = osIcon, +#endif + .label = "OS", + .value = *data.osVersion, + }); + } if (data.kernelVersion) systemInfoRows.push_back({ .icon = kernelIcon, .label = "Kernel", .value = *data.kernelVersion }); if (data.memInfo) - systemInfoRows.push_back({ .icon = memoryIcon, .label = "RAM", .value = std::format("{}", BytesToGiB { *data.memInfo }) }); + systemInfoRows.push_back({ .icon = memoryIcon, .label = "RAM", .value = std::format("{}", BytesToGiB(*data.memInfo)) }); else if (!data.memInfo.has_value()) debug_at(data.memInfo.error()); @@ -162,7 +213,7 @@ namespace ui { { .icon = diskIcon, .label = "Disk", - .value = std::format("{}/{}", BytesToGiB { data.diskUsage->usedBytes }, BytesToGiB { data.diskUsage->totalBytes }), + .value = std::format("{}/{}", BytesToGiB(data.diskUsage->usedBytes), BytesToGiB(data.diskUsage->totalBytes)), } );