small fixups i think

This commit is contained in:
Mars 2025-05-08 14:58:46 -04:00
parent 3d4878f930
commit d5f3c2351f
Signed by: pupbrained
GPG key ID: 0FF5B8826803F895
3 changed files with 89 additions and 21 deletions

View file

@ -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

View file

@ -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<String> {
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<atom_t> {
using util::types::u16;
const ReplyGuard<intern_atom_reply_t> reply(InternAtomReply(conn.get(), InternAtom(conn.get(), 0, static_cast<u16>(name.size()), name.data()), nullptr));
if (!reply)
@ -123,6 +125,8 @@ namespace {
}
fn GetWaylandCompositor() -> Result<String> {
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<String> {
constexpr CStr path = "/etc/os-release";
using util::helpers::GetEnv;
std::ifstream file(path);
fn GetOSVersion() -> Result<String> {
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<u64> {
@ -403,6 +409,8 @@ namespace os {
}
fn GetShell() -> Result<String> {
using util::types::Pair, util::types::Array, util::types::StringView;
if (const Result<String> shellPath = GetEnv("SHELL")) {
// clang-format off
constexpr Array<Pair<StringView, StringView>, 5> shellMap {{
@ -425,6 +433,8 @@ namespace os {
}
fn GetHost() -> Result<String> {
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<u64> {
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

View file

@ -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<Pair<String, String>, 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<StringView> {
using namespace matchit;
const Result<String> 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)),
}
);