#include "utils/utils.hpp" #include #include #include #include #include #ifndef _WIN32 #include #include #endif #include "utils/log.hpp" auto getFileName(const std::string& path) -> std::string { return std::filesystem::path(path).filename().string(); } namespace { constexpr std::size_t BRACKET_INNER_WIDTH = 8; auto defaultLabel(StatusKind kind) -> std::string_view { switch (kind) { case StatusKind::SUCCESS: return "LOADED"; case StatusKind::INFO: return "INFO"; case StatusKind::ERROR: return "ERROR"; } return {}; } auto colorCode(StatusKind kind) -> std::string_view { switch (kind) { case StatusKind::SUCCESS: return "\033[1;32m"; case StatusKind::INFO: return "\033[1;34m"; case StatusKind::ERROR: return "\033[1;31m"; } return {}; } // Decideix una sola vegada per execució: stderr ha d'admetre ANSI? // A Windows només Git Bash (MSYSTEM) el suporta sense habilitar VT. auto useColor() -> bool { static const bool ENABLED = [] { #ifdef _WIN32 return std::getenv("MSYSTEM") != nullptr; #else return isatty(fileno(stderr)) != 0; #endif }(); return ENABLED; } // Construeix "[ ]" amb el contingut interior centrat dins // d'una amplada mínima de BRACKET_INNER_WIDTH. Si `with_color` és true, // només l'interior porta codis ANSI; els claudàtors queden sense color // (mateix patró que `mini`: `[...]`). auto buildBracket(std::string_view label, StatusKind kind, bool with_color) -> std::string { const std::size_t INNER = std::max(BRACKET_INNER_WIDTH, label.size()); const std::size_t PAD = INNER - label.size(); const std::size_t LEFT = PAD / 2; const std::size_t RIGHT = PAD - LEFT; std::string out; out.push_back('['); if (with_color) { out.append(colorCode(kind)); } out.append(LEFT, ' '); out.append(label); out.append(RIGHT, ' '); if (with_color) { out.append("\033[0m"); } out.push_back(']'); return out; } } // namespace void printStatus(const std::string& tag, const std::string& filename, StatusKind kind, std::string_view label) { const std::string_view EFFECTIVE_LABEL = label.empty() ? defaultLabel(kind) : label; const std::string BRACKET = buildBracket(EFFECTIVE_LABEL, kind, useColor()); const Log::Level LVL = (kind == StatusKind::ERROR) ? Log::Level::ERROR : Log::Level::INFO; Log::logPrefixed(LVL, tag, BRACKET, filename); }