This commit is contained in:
Mars 2025-05-01 14:51:41 -04:00
parent 44c2c99468
commit 9a95178095
Signed by: pupbrained
GPG key ID: 874E22DF2F9DFCB5
6 changed files with 118 additions and 43 deletions

View file

@ -1,24 +1,73 @@
#include "system_data.hpp"
#include <chrono> // std::chrono::{year_month_day, days, floor, system_clock}
#include <chrono> // std::chrono::system_clock
#include <ctime> // localtime_r/s, strftime, time_t, tm
#include <format> // std::format
#include <future> // std::async
#include <future> // std::{async, launch}
#include "src/config/config.hpp"
#include "src/config/weather.hpp"
#include "src/core/util/defs.hpp"
#include "src/core/util/error.hpp"
#include "src/core/util/types.hpp"
#include "src/os/os.hpp"
using util::error::DracError, util::error::DracErrorCode;
namespace {
using util::types::i32, util::types::CStr;
fn getOrdinalSuffix(i32 day) -> CStr {
if (day >= 11 && day <= 13)
return "th";
switch (day % 10) {
case 1: return "st";
case 2: return "nd";
case 3: return "rd";
default: return "th";
}
}
fn getDate() -> Result<String, DracError> {
using std::chrono::system_clock;
using util::types::String, util::types::usize, util::types::Err;
const system_clock::time_point nowTp = system_clock::now();
const std::time_t nowTt = system_clock::to_time_t(nowTp);
std::tm nowTm;
#ifdef _WIN32
if (localtime_s(&nowTm, &nowTt) == 0) {
#else
if (localtime_r(&nowTt, &nowTm) != nullptr) {
#endif
i32 day = nowTm.tm_mday;
String monthBuffer(32, '\0');
const usize monthLen = std::strftime(monthBuffer.data(), monthBuffer.size(), "%B", &nowTm);
if (monthLen > 0) {
monthBuffer.resize(monthLen);
CStr suffix = getOrdinalSuffix(day);
try {
return std::format("{} {}{}", monthBuffer, day, suffix);
} catch (const std::format_error& e) { return Err(DracError(DracErrorCode::ParseError, e.what())); }
} else
return Err(DracError(DracErrorCode::ParseError, "Failed to format date"));
} else
return Err(DracError(DracErrorCode::ParseError, "Failed to get local time"));
}
} // namespace
namespace os {
SystemData::SystemData(const Config& config) {
// NOLINTNEXTLINE(misc-include-cleaner) - std::chrono::{days, floor} are inherited from <chrono>
using std::chrono::year_month_day, std::chrono::system_clock, std::chrono::floor, std::chrono::days;
using util::error::DracError, util::error::DracErrorCode;
using util::types::Result, util::types::Err, util::types::Option, util::types::None, util::types::Exception,
util::types::Future;
using weather::Output;
using enum std::launch;
using util::types::Future, util::types::Err;
using weather::Output;
Future<Result<String, DracError>> hostFut = std::async(async, GetHost);
Future<Result<String, DracError>> kernelFut = std::async(async, GetKernelVersion);
@ -36,8 +85,7 @@ namespace os {
return config.weather.getWeatherInfo();
});
// TODO: make this use the user's timezone
this->date = std::format("{:%B %d}", year_month_day { floor<days>(system_clock::now()) });
this->date = getDate();
this->host = hostFut.get();
this->kernelVersion = kernelFut.get();
this->osVersion = osFut.get();

View file

@ -67,18 +67,18 @@ namespace os {
* in order to display it at all at once during runtime.
*/
struct SystemData {
String date; ///< Current date (e.g., "April 26"). Always expected to succeed.
Result<String, DracError> host; ///< Host/product family (e.g., "MacBook Air") or OS util::erroror.
Result<String, DracError> kernelVersion; ///< OS kernel version (e.g., "6.14.4") or OS error.
Result<String, DracError> osVersion; ///< OS pretty name (e.g., "Ubuntu 24.04.2 LTS") or OS error.
Result<u64, DracError> memInfo; ///< Total physical RAM in bytes or OS error.
Result<String, DracError> desktopEnv; ///< Desktop environment (e.g., "KDE") or None if not detected.
Result<String, DracError> windowMgr; ///< Window manager (e.g., "KWin") or None if not detected.
Result<DiskSpace, DracError> diskUsage; ///< Used/Total disk space for root filesystem or OS error.
Result<String, DracError> shell; ///< Name of the current user shell (e.g., "zsh"). None if not detected.
Result<u64, DracError> packageCount; ///< Total number of packages installed or OS error.
Result<MediaInfo, DracError> nowPlaying; ///< Result of fetching media info.
Result<weather::Output, DracError> weather; ///< Result of fetching weather info.
Result<String, DracError> date; ///< Current date (e.g., "April 26th").
Result<String, DracError> host; ///< Host/product family (e.g., "MacBook Air").
Result<String, DracError> kernelVersion; ///< OS kernel version (e.g., "6.14.4").
Result<String, DracError> osVersion; ///< OS pretty name (e.g., "Ubuntu 24.04.2 LTS").
Result<u64, DracError> memInfo; ///< Total physical RAM in bytes.
Result<String, DracError> desktopEnv; ///< Desktop environment (e.g., "KDE").
Result<String, DracError> windowMgr; ///< Window manager (e.g., "KWin").
Result<DiskSpace, DracError> diskUsage; ///< Used/Total disk space for root filesystem.
Result<String, DracError> shell; ///< Name of the current user shell (e.g., "zsh").
Result<u64, DracError> packageCount; ///< Total number of packages installed.
Result<MediaInfo, DracError> nowPlaying; ///< Result of fetching media info.
Result<weather::Output, DracError> weather; ///< Result of fetching weather info.
/**
* @brief Constructs a SystemData object and initializes its members.

View file

@ -1,13 +1,17 @@
#pragma once
#include <chrono> // std::chrono::{days, floor, seconds, system_clock}
#include <filesystem> // std::filesystem::path
#include <format> // std::format
#include <chrono> // std::chrono::{days, floor, seconds, system_clock}
#include <ctime> // For time_t, tm, localtime_s, localtime_r, strftime (needed for cross-platform local time)
#include <filesystem> // std::filesystem::path
#include <format> // std::format
#include <ftxui/screen/color.hpp> // ftxui::Color
#include <print> // std::print
#include <source_location> // std::source_location
#include <utility> // std::forward
#ifndef NDEBUG
#include <source_location> // std::source_location
#endif
#include "src/core/util/defs.hpp"
#include "src/core/util/error.hpp"
#include "src/core/util/types.hpp"
@ -48,7 +52,7 @@ namespace util::logging {
static constexpr ftxui::Color::Palette16 ERROR_COLOR = ftxui::Color::Palette16::Red;
static constexpr ftxui::Color::Palette16 DEBUG_INFO_COLOR = ftxui::Color::Palette16::GrayLight;
static constexpr CStr TIMESTAMP_FORMAT = "{:%X}";
static constexpr CStr TIMESTAMP_FORMAT = "%X";
static constexpr CStr LOG_FORMAT = "{} {} {}";
#ifndef NDEBUG
@ -143,7 +147,7 @@ namespace util::logging {
* @brief Logs a message with the specified log level, source location, and format string.
* @tparam Args Parameter pack for format arguments.
* @param level The log level (DEBUG, INFO, WARN, ERROR).
* @param loc The source location of the log message.
* @param loc The source location of the log message (only in Debug builds).
* @param fmt The format string.
* @param args The arguments for the format string.
*/
@ -161,17 +165,34 @@ namespace util::logging {
using Buffer = Array<char, 512>;
// Dynamic parts (runtime)
const time_point<system_clock, duration<i64>> now = floor<seconds>(system_clock::now());
const auto nowTp = system_clock::now();
const std::time_t nowTt = system_clock::to_time_t(nowTp);
std::tm localTm;
const String timestamp = std::format(LogLevelConst::TIMESTAMP_FORMAT, now);
const String message = std::format(fmt, std::forward<Args>(args)...);
String timestamp;
#ifdef _WIN32
if (localtime_s(&local_tm, &now_tt) == 0) {
#else
if (localtime_r(&nowTt, &localTm) != nullptr) {
#endif
Array<char, 64> timeBuffer;
if (std::strftime(timeBuffer.data(), sizeof(timeBuffer), LogLevelConst::TIMESTAMP_FORMAT, &localTm) > 0)
timestamp = timeBuffer.data();
else
timestamp = "??:??:?? (strf_err)";
} else
timestamp = "??:??:?? (conv_err)";
const String message = std::format(fmt, std::forward<Args>(args)...);
Buffer buffer {};
// Use the locally formatted timestamp string here
auto* iter = std::format_to(
buffer.begin(),
LogLevelConst::LOG_FORMAT,
LogLevelConst::LOG_FORMAT, // "{timestamp} {level} {message}"
Colorize("[" + timestamp + "]", LogLevelConst::DEBUG_INFO_COLOR),
GetLevelInfo().at(static_cast<usize>(level)),
message

View file

@ -4,6 +4,7 @@
#include <ftxui/dom/node.hpp> // ftxui::{Render}
#include <ftxui/screen/color.hpp> // ftxui::Color
#include <ftxui/screen/screen.hpp> // ftxui::{Screen, Dimension::Full}
#include <print> // std::println
#include <ranges> // std::ranges::{iota, to, transform}
#include "src/config/config.hpp"
@ -151,7 +152,10 @@ namespace {
};
// System info rows
content.push_back(createRow(calendarIcon, "Date", data.date));
if (data.date)
content.push_back(createRow(calendarIcon, "Date", *data.date));
else
error_at(data.date.error());
// Weather row
if (weather.enabled && data.weather) {

View file

@ -13,7 +13,7 @@
// clang-format on
using namespace util::types;
using util::error::DracError, util::error::DracErrorCode;
using util::error::DracError;
fn os::GetMemInfo() -> Result<u64, DracError> {
u64 mem = 0;
@ -30,16 +30,16 @@ fn os::GetNowPlaying() -> Result<MediaInfo, DracError> { return GetCurrentPlayin
fn os::GetOSVersion() -> Result<String, DracError> { return GetMacOSVersion(); }
fn os::GetDesktopEnvironment() -> Result<String, DracError> {
return Err(DracError(DracErrorCode::Other, "Not implemented on macOS"));
return "Aqua"; // TODO: Implement
}
fn os::GetWindowManager() -> Result<String, DracError> {
return Err(DracError(DracErrorCode::Other, "Not implemented on macOS"));
return "Yabai"; // TODO: Implement
}
fn os::GetKernelVersion() -> Result<String, DracError> {
std::array<char, 256> kernelVersion {};
usize kernelVersionLen = sizeof(kernelVersion);
Array<char, 256> kernelVersion {};
usize kernelVersionLen = sizeof(kernelVersion);
if (sysctlbyname("kern.osrelease", kernelVersion.data(), &kernelVersionLen, nullptr, 0) == -1)
return Err(DracError::withErrno("Failed to get kernel version"));
@ -48,15 +48,15 @@ fn os::GetKernelVersion() -> Result<String, DracError> {
}
fn os::GetHost() -> Result<String, DracError> {
std::array<char, 256> hwModel {};
size_t hwModelLen = sizeof(hwModel);
Array<char, 256> hwModel {};
usize hwModelLen = sizeof(hwModel);
if (sysctlbyname("hw.model", hwModel.data(), &hwModelLen, nullptr, 0) == -1)
return Err(DracError::withErrno("Failed to get host info"));
// taken from https://github.com/fastfetch-cli/fastfetch/blob/dev/src/detection/host/host_mac.c
// shortened a lot of the entries to remove unnecessary info
std::flat_map<std::string_view, std::string_view> modelNameByHwModel = {
std::flat_map<StringView, StringView> modelNameByHwModel = {
// MacBook Pro
{ "MacBookPro18,3", "MacBook Pro (14-inch, 2021)" },
{ "MacBookPro18,4", "MacBook Pro (14-inch, 2021)" },
@ -225,7 +225,7 @@ fn os::GetDiskUsage() -> Result<DiskSpace, DracError> {
fn os::GetPackageCount() -> Result<u64, DracError> { return shared::GetPackageCount(); }
fn os::GetShell() -> Result<String, DracError> {
return Err(DracError(DracErrorCode::Other, "Not implemented on macOS"));
return "Fish"; // TODO: Implement
}
#endif

View file

@ -3,6 +3,7 @@
#include <SQLiteCpp/Statement.h> // SQLite::Statement
#include <chrono> // std::chrono
#include <filesystem> // std::filesystem
#include <format> // std::format
#include <fstream> // std::{ifstream, ofstream}
#include <glaze/beve/read.hpp> // glz::read_beve
#include <glaze/beve/write.hpp> // glz::write_beve
@ -11,6 +12,7 @@
#include <glaze/core/meta.hpp> // glz::detail::Object
#include <ios> // std::ios::{binary, trunc}, std::ios_base
#include <iterator> // std::istreambuf_iterator
#include <system_error> // std::error_code
#include "src/core/util/defs.hpp"
#include "src/core/util/error.hpp"