This commit is contained in:
Mars 2025-04-29 02:26:37 -04:00
parent 33881d007e
commit 854f116f2d
Signed by: pupbrained
GPG key ID: 0FF5B8826803F895
5 changed files with 55 additions and 54 deletions

View file

@ -2,10 +2,8 @@
#include <filesystem> // std::filesystem::{path, operator/, exists, create_directories}
#include <fstream> // std::{ifstream, ofstream, operator<<}
#include <stdexcept> // std::runtime_error
#include <system_error> // std::error_code
#include <toml++/impl/parser.hpp> // toml::{parse_file, parse_result}
#include <utility> // std::pair (Pair)
#include "src/core/util/helpers.hpp"
#include "src/core/util/logging.hpp"
@ -128,8 +126,8 @@ location = "London" # Your city name
try {
const std::string formattedConfig = std::format(defaultConfigTemplate, defaultName);
file << formattedConfig;
} catch (const std::format_error& fmt_err) {
error_log("Failed to format default config string: {}. Using fallback name 'User'.", fmt_err.what());
} catch (const std::format_error& fmtErr) {
error_log("Failed to format default config string: {}. Using fallback name 'User'.", fmtErr.what());
try {
const std::string fallbackConfig = std::format(defaultConfigTemplate, "User");
@ -147,8 +145,8 @@ location = "London" # Your city name
info_log("Created default config file at {}", configPath.string());
return true;
} catch (const fs::filesystem_error& fs_err) {
error_log("Filesystem error during default config creation: {}", fs_err.what());
} catch (const fs::filesystem_error& fsErr) {
error_log("Filesystem error during default config creation: {}", fsErr.what());
return false;
} catch (const Exception& e) {
error_log("Failed to create default config file: {}", e.what());
@ -174,13 +172,13 @@ fn Config::getInstance() -> Config {
try {
const fs::path configPath = GetConfigPath();
std::error_code ec;
std::error_code errc;
const bool exists = fs::exists(configPath, ec);
const bool exists = fs::exists(configPath, errc);
if (ec)
if (errc)
warn_log(
"Failed to check if config file exists at {}: {}. Assuming it doesn't.", configPath.string(), ec.message()
"Failed to check if config file exists at {}: {}. Assuming it doesn't.", configPath.string(), errc.message()
);
if (!exists) {

View file

@ -1,18 +1,19 @@
#pragma once
#ifdef _WIN32
#include <windows.h> // GetUserNameA
#else
#include <pwd.h> // getpwuid, passwd
#include <unistd.h> // getuid
#endif
#include <stdexcept> // std::runtime_error
#include <string> // std::string (String)
#include <toml++/impl/node.hpp> // toml::node
#include <toml++/impl/node_view.hpp> // toml::node_view
#include <toml++/impl/table.hpp> // toml::table
#include <variant> // std::variant
//
#ifdef _WIN32
#include <windows.h> // GetUserNameA
#else
#include <pwd.h> // getpwuid, passwd
#include <unistd.h> // getuid
#include "src/core/util/helpers.hpp"
#endif
#include "src/core/util/defs.hpp"
#include "src/core/util/error.hpp"
@ -50,16 +51,18 @@ struct General {
DWORD size = sizeof(username);
return GetUserNameA(username.data(), &size) ? username.data() : "User";
#else
using util::helpers::GetEnv;
// Try to get the username using getpwuid
if (const passwd* pwd = getpwuid(getuid()))
return pwd->pw_name;
// Try to get the username using environment variables
if (Result<String, DraconisError> envUser = util::helpers::GetEnv("USER"))
if (Result<String, DraconisError> envUser = GetEnv("USER"))
return *envUser;
// Finally, try to get the username using LOGNAME
if (Result<String, DraconisError> envLogname = util::helpers::GetEnv("LOGNAME"))
if (Result<String, DraconisError> envLogname = GetEnv("LOGNAME"))
return *envLogname;
// If all else fails, return a default name

View file

@ -1,12 +1,8 @@
#include "system_data.hpp"
#include <chrono> // std::chrono::{year_month_day, floor, days, system_clock}
#include <exception> // std::exception (Exception)
#include <future> // std::{future, async, launch}
#include <locale> // std::locale
#include <stdexcept> // std::runtime_error
#include <tuple> // std::{tuple, get, make_tuple}
#include <utility> // std::move
#include "src/config/config.hpp"
#include "src/os/os.hpp"
@ -66,6 +62,8 @@ fn SystemData::fetchSystemData(const Config& config) -> SystemData {
.window_manager = time_execution("GetWindowManager", GetWindowManager),
.disk_usage = time_execution("GetDiskUsage", GetDiskUsage),
.shell = time_execution("GetShell", GetShell),
.now_playing = None,
.weather_info = None,
};
if (const Result<MediaInfo, DraconisError>& nowPlayingResult = time_execution("GetNowPlaying", os::GetNowPlaying)) {

View file

@ -4,7 +4,6 @@
#include <filesystem> // std::filesystem::path
#include <format> // std::format
#include <ftxui/screen/color.hpp> // ftxui::Color
#include <mutex> // std::mutex
#include <print> // std::print
#include <source_location> // std::source_location
#include <utility> // std::forward
@ -21,7 +20,7 @@ namespace util::logging {
struct LogLevelConst {
// ANSI color codes
// clang-format off
static constexpr Array<const char*, 16> COLOR_CODE_LITERALS = {
static constexpr Array<StringView, 16> COLOR_CODE_LITERALS = {
"\033[38;5;0m", "\033[38;5;1m", "\033[38;5;2m", "\033[38;5;3m",
"\033[38;5;4m", "\033[38;5;5m", "\033[38;5;6m", "\033[38;5;7m",
"\033[38;5;8m", "\033[38;5;9m", "\033[38;5;10m", "\033[38;5;11m",
@ -71,8 +70,10 @@ namespace util::logging {
* @param color The FTXUI color
* @return Styled string with ANSI codes
*/
inline fn Colorize(const String& text, const ftxui::Color::Palette16& color) -> String {
return String(LogLevelConst::COLOR_CODE_LITERALS[static_cast<int>(color)]) + text + LogLevelConst::RESET_CODE;
inline fn Colorize(StringView text, const ftxui::Color::Palette16& color) -> String {
std::ostringstream oss;
oss << LogLevelConst::COLOR_CODE_LITERALS.at(static_cast<i32>(color)) << text << LogLevelConst::RESET_CODE;
return oss.str();
}
/**
@ -93,14 +94,20 @@ namespace util::logging {
return String(LogLevelConst::ITALIC_START) + String(text) + String(LogLevelConst::ITALIC_END);
}
// Initialize at runtime using the constexpr static values
// This can't be constexpr itself due to the string operations
inline const Array<String, 4> LEVEL_INFO = {
Bold(Colorize(LogLevelConst::DEBUG_STR.data(), LogLevelConst::DEBUG_COLOR)),
Bold(Colorize(LogLevelConst::INFO_STR.data(), LogLevelConst::INFO_COLOR)),
Bold(Colorize(LogLevelConst::WARN_STR.data(), LogLevelConst::WARN_COLOR)),
Bold(Colorize(LogLevelConst::ERROR_STR.data(), LogLevelConst::ERROR_COLOR)),
};
/**
* @brief Returns the pre-formatted and styled log level strings.
* @note Uses function-local static for lazy initialization to avoid
* static initialization order issues and CERT-ERR58-CPP warnings.
*/
inline fn GetLevelInfo() -> const Array<String, 4>& {
static const Array<String, 4> LEVEL_INFO_INSTANCE = {
Bold(Colorize(LogLevelConst::DEBUG_STR, LogLevelConst::DEBUG_COLOR)),
Bold(Colorize(LogLevelConst::INFO_STR, LogLevelConst::INFO_COLOR)),
Bold(Colorize(LogLevelConst::WARN_STR, LogLevelConst::WARN_COLOR)),
Bold(Colorize(LogLevelConst::ERROR_STR, LogLevelConst::ERROR_COLOR)),
};
return LEVEL_INFO_INSTANCE;
}
/**
* @brief Returns FTXUI color representation for a log level
@ -122,12 +129,12 @@ namespace util::logging {
* @param level The log level
* @return String representation
*/
constexpr fn GetLevelString(const LogLevel level) -> String {
constexpr fn GetLevelString(const LogLevel level) -> StringView {
switch (level) {
case LogLevel::Debug: return LogLevelConst::DEBUG_STR.data();
case LogLevel::Info: return LogLevelConst::INFO_STR.data();
case LogLevel::Warn: return LogLevelConst::WARN_STR.data();
case LogLevel::Error: return LogLevelConst::ERROR_STR.data();
case LogLevel::Debug: return LogLevelConst::DEBUG_STR;
case LogLevel::Info: return LogLevelConst::INFO_STR;
case LogLevel::Warn: return LogLevelConst::WARN_STR;
case LogLevel::Error: return LogLevelConst::ERROR_STR;
default: std::unreachable();
}
}
@ -155,29 +162,24 @@ namespace util::logging {
Buffer buffer {};
Buffer::iterator iter = std::format_to(
auto* iter = std::format_to(
buffer.begin(),
LogLevelConst::LOG_FORMAT,
Colorize("[" + timestamp + "]", LogLevelConst::DEBUG_INFO_COLOR),
LEVEL_INFO[static_cast<usize>(level)],
GetLevelInfo().at(static_cast<usize>(level)),
message
);
#ifndef NDEBUG
iter = std::format_to(
iter,
"\n{}",
Italic(Colorize(
LogLevelConst::DEBUG_LINE_PREFIX +
std::format("{}:{}", path(loc.file_name()).lexically_normal().string(), std::to_string(loc.line())),
LogLevelConst::DEBUG_INFO_COLOR
))
);
const String fileLine = std::format("{}:{}", path(loc.file_name()).lexically_normal().string(), loc.line());
const String fullDebugLine = std::format("{}{}", LogLevelConst::DEBUG_LINE_PREFIX, fileLine);
iter = std::format_to(iter, "\n{}", Italic(Colorize(fullDebugLine, LogLevelConst::DEBUG_INFO_COLOR)));
#endif
const usize length = std::distance(buffer.begin(), iter);
std::println("{}", std::string_view(buffer.data(), length));
std::println("{}", StringView(buffer.data(), length));
}
template <typename ErrorType>

View file

@ -6,7 +6,7 @@
#include "src/core/util/types.hpp"
namespace xcb {
using util::types::u8, util::types::i32, util::types::CStr;
using util::types::u8, util::types::i32, util::types::CStr, util::types::None;
using connection_t = xcb_connection_t;
using setup_t = xcb_setup_t;