small fixups i think
This commit is contained in:
parent
3d4878f930
commit
d5f3c2351f
3 changed files with 89 additions and 21 deletions
|
@ -21,6 +21,12 @@ using util::types::u64, util::types::f64, util::types::String, util::types::Opti
|
||||||
*/
|
*/
|
||||||
struct BytesToGiB {
|
struct BytesToGiB {
|
||||||
u64 value; ///< The byte value to be converted.
|
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
|
/// @brief Conversion factor from bytes to GiB
|
||||||
|
|
|
@ -41,15 +41,15 @@
|
||||||
#include "os.hpp"
|
#include "os.hpp"
|
||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
using namespace util::types;
|
|
||||||
using util::error::DracError, util::error::DracErrorCode;
|
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 {
|
namespace {
|
||||||
fn GetX11WindowManager() -> Result<String> {
|
fn GetX11WindowManager() -> Result<String> {
|
||||||
using namespace xcb;
|
using namespace xcb;
|
||||||
using namespace matchit;
|
using namespace matchit;
|
||||||
using enum ConnError;
|
using enum ConnError;
|
||||||
|
using util::types::StringView;
|
||||||
|
|
||||||
const DisplayGuard conn;
|
const DisplayGuard conn;
|
||||||
|
|
||||||
|
@ -72,6 +72,8 @@ namespace {
|
||||||
);
|
);
|
||||||
|
|
||||||
fn internAtom = [&conn](const StringView name) -> Result<atom_t> {
|
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));
|
const ReplyGuard<intern_atom_reply_t> reply(InternAtomReply(conn.get(), InternAtom(conn.get(), 0, static_cast<u16>(name.size()), name.data()), nullptr));
|
||||||
|
|
||||||
if (!reply)
|
if (!reply)
|
||||||
|
@ -123,6 +125,8 @@ namespace {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn GetWaylandCompositor() -> Result<String> {
|
fn GetWaylandCompositor() -> Result<String> {
|
||||||
|
using util::types::i32, util::types::Array, util::types::isize, util::types::StringView;
|
||||||
|
|
||||||
const wl::DisplayGuard display;
|
const wl::DisplayGuard display;
|
||||||
|
|
||||||
if (!display)
|
if (!display)
|
||||||
|
@ -195,13 +199,15 @@ namespace {
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
namespace os {
|
namespace os {
|
||||||
fn GetOSVersion() -> Result<String> {
|
using util::helpers::GetEnv;
|
||||||
constexpr CStr path = "/etc/os-release";
|
|
||||||
|
|
||||||
std::ifstream file(path);
|
fn GetOSVersion() -> Result<String> {
|
||||||
|
using util::types::StringView;
|
||||||
|
|
||||||
|
std::ifstream file("/etc/os-release");
|
||||||
|
|
||||||
if (!file)
|
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;
|
String line;
|
||||||
constexpr StringView prefix = "PRETTY_NAME=";
|
constexpr StringView prefix = "PRETTY_NAME=";
|
||||||
|
@ -216,14 +222,14 @@ namespace os {
|
||||||
|
|
||||||
if (value.empty())
|
if (value.empty())
|
||||||
return Err(
|
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 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> {
|
fn GetMemInfo() -> Result<u64> {
|
||||||
|
@ -403,6 +409,8 @@ namespace os {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn GetShell() -> Result<String> {
|
fn GetShell() -> Result<String> {
|
||||||
|
using util::types::Pair, util::types::Array, util::types::StringView;
|
||||||
|
|
||||||
if (const Result<String> shellPath = GetEnv("SHELL")) {
|
if (const Result<String> shellPath = GetEnv("SHELL")) {
|
||||||
// clang-format off
|
// clang-format off
|
||||||
constexpr Array<Pair<StringView, StringView>, 5> shellMap {{
|
constexpr Array<Pair<StringView, StringView>, 5> shellMap {{
|
||||||
|
@ -425,6 +433,8 @@ namespace os {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn GetHost() -> Result<String> {
|
fn GetHost() -> Result<String> {
|
||||||
|
using util::types::CStr;
|
||||||
|
|
||||||
constexpr CStr primaryPath = "/sys/class/dmi/id/product_family";
|
constexpr CStr primaryPath = "/sys/class/dmi/id/product_family";
|
||||||
constexpr CStr fallbackPath = "/sys/class/dmi/id/product_name";
|
constexpr CStr fallbackPath = "/sys/class/dmi/id/product_name";
|
||||||
|
|
||||||
|
@ -603,8 +613,9 @@ namespace package {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn CountXbps() -> Result<u64> {
|
fn CountXbps() -> Result<u64> {
|
||||||
const StringView xbpsDbPath = "/var/db/xbps";
|
using util::types::CStr;
|
||||||
const String pmId = "xbps";
|
|
||||||
|
const CStr xbpsDbPath = "/var/db/xbps";
|
||||||
|
|
||||||
if (!fs::exists(xbpsDbPath))
|
if (!fs::exists(xbpsDbPath))
|
||||||
return Err(DracError(DracErrorCode::NotFound, std::format("Xbps database path '{}' does not exist", xbpsDbPath)));
|
return Err(DracError(DracErrorCode::NotFound, std::format("Xbps database path '{}' does not exist", xbpsDbPath)));
|
||||||
|
@ -621,7 +632,7 @@ namespace package {
|
||||||
if (plistPath.empty())
|
if (plistPath.empty())
|
||||||
return Err(DracError(DracErrorCode::NotFound, "No Xbps database found"));
|
return Err(DracError(DracErrorCode::NotFound, "No Xbps database found"));
|
||||||
|
|
||||||
return GetCountFromPlist(pmId, plistPath);
|
return GetCountFromPlist("xbps", plistPath);
|
||||||
}
|
}
|
||||||
} // namespace package
|
} // namespace package
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#include "ui.hpp"
|
#include "ui.hpp"
|
||||||
|
|
||||||
|
#include "src/os/os.hpp"
|
||||||
#include "src/util/types.hpp"
|
#include "src/util/types.hpp"
|
||||||
|
|
||||||
namespace ui {
|
namespace ui {
|
||||||
|
@ -31,12 +32,22 @@ namespace ui {
|
||||||
};
|
};
|
||||||
|
|
||||||
[[maybe_unused]] static constexpr Icons NERD = {
|
[[maybe_unused]] static constexpr Icons NERD = {
|
||||||
.user = " ",
|
.user = " ",
|
||||||
.palette = " ",
|
.palette = " ",
|
||||||
.calendar = " ",
|
.calendar = " ",
|
||||||
.host = " ",
|
.host = " ",
|
||||||
.kernel = " ",
|
.kernel = " ",
|
||||||
.os = " ",
|
#ifdef __linux__
|
||||||
|
.os = " ",
|
||||||
|
#elifdef __APPLE__
|
||||||
|
.os = " ",
|
||||||
|
#elifdef _WIN32
|
||||||
|
.os = " ",
|
||||||
|
#elifdef __FreeBSD__
|
||||||
|
.os = " ",
|
||||||
|
#else
|
||||||
|
.os = " ",
|
||||||
|
#endif
|
||||||
.memory = " ",
|
.memory = " ",
|
||||||
.weather = " ",
|
.weather = " ",
|
||||||
.music = " ",
|
.music = " ",
|
||||||
|
@ -73,6 +84,37 @@ namespace ui {
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace {
|
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 {
|
fn CreateColorCircles() -> Element {
|
||||||
auto colorView =
|
auto colorView =
|
||||||
std::views::iota(0, 16) | std::views::transform([](i32 colorIndex) {
|
std::views::iota(0, 16) | std::views::transform([](i32 colorIndex) {
|
||||||
|
@ -146,14 +188,23 @@ namespace ui {
|
||||||
if (data.host && !data.host->empty())
|
if (data.host && !data.host->empty())
|
||||||
systemInfoRows.push_back({ .icon = hostIcon, .label = "Host", .value = *data.host });
|
systemInfoRows.push_back({ .icon = hostIcon, .label = "Host", .value = *data.host });
|
||||||
|
|
||||||
if (data.osVersion)
|
if (data.osVersion) {
|
||||||
systemInfoRows.push_back({ .icon = osIcon, .label = "OS", .value = *data.osVersion });
|
systemInfoRows.push_back({
|
||||||
|
#ifdef __linux__
|
||||||
|
.icon = GetDistroIcon().value_or(osIcon),
|
||||||
|
#else
|
||||||
|
.icon = osIcon,
|
||||||
|
#endif
|
||||||
|
.label = "OS",
|
||||||
|
.value = *data.osVersion,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (data.kernelVersion)
|
if (data.kernelVersion)
|
||||||
systemInfoRows.push_back({ .icon = kernelIcon, .label = "Kernel", .value = *data.kernelVersion });
|
systemInfoRows.push_back({ .icon = kernelIcon, .label = "Kernel", .value = *data.kernelVersion });
|
||||||
|
|
||||||
if (data.memInfo)
|
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())
|
else if (!data.memInfo.has_value())
|
||||||
debug_at(data.memInfo.error());
|
debug_at(data.memInfo.error());
|
||||||
|
|
||||||
|
@ -162,7 +213,7 @@ namespace ui {
|
||||||
{
|
{
|
||||||
.icon = diskIcon,
|
.icon = diskIcon,
|
||||||
.label = "Disk",
|
.label = "Disk",
|
||||||
.value = std::format("{}/{}", BytesToGiB { data.diskUsage->usedBytes }, BytesToGiB { data.diskUsage->totalBytes }),
|
.value = std::format("{}/{}", BytesToGiB(data.diskUsage->usedBytes), BytesToGiB(data.diskUsage->totalBytes)),
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue