ugghghghgh

This commit is contained in:
Mars 2025-02-17 22:06:51 -05:00
parent f9a9491da3
commit 8cbc00372d
6 changed files with 73 additions and 65 deletions

View file

@ -156,6 +156,8 @@
]
++ deps;
LD_LIBRARY_PATH = "${pkgs.lib.makeLibraryPath deps}";
name = "C++";
};
}

View file

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

View file

@ -6,6 +6,7 @@
#include <algorithm>
#include <cstring>
#include <dirent.h>
#include <expected>
#include <filesystem>
#include <fmt/format.h>
#include <fstream>
@ -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<string> {
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<u64, string> {
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 {

View file

@ -0,0 +1,9 @@
#include "src/os/linux/pkg_count.h"
namespace fs = std::filesystem;
fn GetApkPackageCount() -> std::optional<usize> {
fs::path apkDbPath("/lib/apk/db/installed");
return std::nullopt;
}

View file

@ -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<usize>;
// Get package count from RPM (Red Hat/Fedora/CentOS)
int get_rpm_package_count();
fn GetRpmPackageCount() -> std::optional<usize>;
// Get package count from pacman (Arch Linux)
int get_pacman_package_count();
fn GetPacmanPackageCount() -> std::optional<usize>;
// Get package count from Portage (Gentoo)
int get_portage_package_count();
fn GetPortagePackageCount() -> std::optional<usize>;
// Get package count from zypper (openSUSE)
int get_zypper_package_count();
fn GetZypperPackageCount() -> std::optional<usize>;
// Get package count from apk (Alpine)
fn GetApkPackageCount() -> std::optional<usize>;
// Get package count from flatpak
int get_flatpak_package_count();
fn GetFlatpakPackageCount() -> std::optional<usize>;
// Get package count from snap
int get_snap_package_count();
fn GetSnapPackageCount() -> std::optional<usize>;
// Get package count from AppImage
int get_appimage_package_count();
fn GetAppimagePackageCount() -> std::optional<usize>;
// Get total package count from all available package managers
fn GetTotalPackageCount() -> int;
fn GetTotalPackageCount() -> std::optional<usize>;

View file

@ -1,12 +1,14 @@
#pragma once
#include <expected>
#include "../util/macros.h"
#include "../util/types.h"
/**
* @brief Get the amount of installed RAM in bytes.
*/
fn GetMemInfo() -> u64;
fn GetMemInfo() -> std::expected<u64, string>;
/**
* @brief Get the currently playing song metadata.