diff --git a/src/config/config.cpp b/src/config/config.cpp index 0ecec8c..a37becd 100644 --- a/src/config/config.cpp +++ b/src/config/config.cpp @@ -2,10 +2,8 @@ #include // std::filesystem::{path, operator/, exists, create_directories} #include // std::{ifstream, ofstream, operator<<} -#include // std::runtime_error #include // std::error_code #include // toml::{parse_file, parse_result} -#include // 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) { diff --git a/src/config/config.hpp b/src/config/config.hpp index e5f136a..135473f 100644 --- a/src/config/config.hpp +++ b/src/config/config.hpp @@ -1,18 +1,19 @@ #pragma once -#ifdef _WIN32 - #include // GetUserNameA -#else - #include // getpwuid, passwd - #include // getuid -#endif - -#include // std::runtime_error #include // std::string (String) #include // toml::node #include // toml::node_view #include // toml::table #include // std::variant +// +#ifdef _WIN32 + #include // GetUserNameA +#else + #include // getpwuid, passwd + #include // 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 envUser = util::helpers::GetEnv("USER")) + if (Result envUser = GetEnv("USER")) return *envUser; // Finally, try to get the username using LOGNAME - if (Result envLogname = util::helpers::GetEnv("LOGNAME")) + if (Result envLogname = GetEnv("LOGNAME")) return *envLogname; // If all else fails, return a default name diff --git a/src/core/system_data.cpp b/src/core/system_data.cpp index 25f5217..3bde682 100644 --- a/src/core/system_data.cpp +++ b/src/core/system_data.cpp @@ -1,12 +1,8 @@ #include "system_data.hpp" #include // std::chrono::{year_month_day, floor, days, system_clock} -#include // std::exception (Exception) -#include // std::{future, async, launch} #include // std::locale #include // std::runtime_error -#include // std::{tuple, get, make_tuple} -#include // 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& nowPlayingResult = time_execution("GetNowPlaying", os::GetNowPlaying)) { diff --git a/src/core/util/logging.hpp b/src/core/util/logging.hpp index f08b777..1518dd5 100644 --- a/src/core/util/logging.hpp +++ b/src/core/util/logging.hpp @@ -4,7 +4,6 @@ #include // std::filesystem::path #include // std::format #include // ftxui::Color -#include // std::mutex #include // std::print #include // std::source_location #include // std::forward @@ -21,7 +20,7 @@ namespace util::logging { struct LogLevelConst { // ANSI color codes // clang-format off - static constexpr Array COLOR_CODE_LITERALS = { + static constexpr Array 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(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(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 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& { + static const Array 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(level)], + GetLevelInfo().at(static_cast(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 diff --git a/src/wrappers/xcb.hpp b/src/wrappers/xcb.hpp index 86f81ae..5af09b7 100644 --- a/src/wrappers/xcb.hpp +++ b/src/wrappers/xcb.hpp @@ -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;