um i think this owrks

This commit is contained in:
Mars 2025-04-23 21:47:25 -04:00
parent cf51e3e569
commit 2219182539
9 changed files with 306 additions and 334 deletions

View file

@ -17,9 +17,10 @@
#ifdef None
#undef None
#define None std::nullopt
#endif
#define None std::nullopt
namespace term {
enum class Emphasis : u8 { none = 0, bold = 1, italic = 2 };
@ -49,16 +50,14 @@ namespace term {
struct FgColor {
Color col;
constexpr explicit FgColor(Color color) : col(color) {}
constexpr explicit FgColor(const Color color) : col(color) {}
[[nodiscard]] fn ansiCode() const -> String { return std::format("\033[{}m", static_cast<int>(col)); }
};
constexpr fn Fg(Color color) -> FgColor { return FgColor(color); }
struct Style {
Emphasis emph = Emphasis::none;
FgColor fg_col = FgColor(static_cast<Color>(-1)); // Invalid color
FgColor fg_col = FgColor(static_cast<Color>(-1));
[[nodiscard]] fn ansiCode() const -> String {
String result;
@ -77,8 +76,13 @@ namespace term {
}
};
constexpr fn operator|(Emphasis emph, FgColor fgColor)->Style { return { .emph = emph, .fg_col = fgColor }; }
constexpr fn operator|(FgColor fgColor, Emphasis emph)->Style { return { .emph = emph, .fg_col = fgColor }; }
constexpr fn operator|(const Emphasis emph, const FgColor fgColor)->Style {
return { .emph = emph, .fg_col = fgColor };
}
constexpr fn operator|(const FgColor fgColor, const Emphasis emph)->Style {
return { .emph = emph, .fg_col = fgColor };
}
constexpr CStr reset = "\033[0m";
@ -122,19 +126,11 @@ fn LogImpl(const LogLevel level, const std::source_location& loc, std::format_st
const auto [color, levelStr] = [&] {
switch (level) {
case LogLevel::DEBUG:
return std::make_pair(log_colors::debug, "DEBUG");
case LogLevel::INFO:
return std::make_pair(log_colors::info, "INFO ");
case LogLevel::WARN:
return std::make_pair(log_colors::warn, "WARN ");
case LogLevel::ERROR:
return std::make_pair(log_colors::error, "ERROR");
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wcovered-switch-default"
default:
std::unreachable();
#pragma clang diagnostic pop
case LogLevel::DEBUG: return std::make_pair(log_colors::debug, "DEBUG");
case LogLevel::INFO: return std::make_pair(log_colors::info, "INFO ");
case LogLevel::WARN: return std::make_pair(log_colors::warn, "WARN ");
case LogLevel::ERROR: return std::make_pair(log_colors::error, "ERROR");
default: std::unreachable();
}
}();
@ -142,20 +138,22 @@ fn LogImpl(const LogLevel level, const std::source_location& loc, std::format_st
using namespace term;
Print(Fg(log_colors::timestamp), "[{:%H:%M:%S}] ", now);
Print(Emphasis::bold | Fg(color), "{} ", levelStr);
Print(FgColor(log_colors::timestamp), "[{:%H:%M:%S}] ", now);
Print(Emphasis::bold | FgColor(color), "{} ", levelStr);
Print(fmt, std::forward<Args>(args)...);
#ifndef NDEBUG
Print(Fg(log_colors::file_info), "\n{:>14} ", "╰──");
Print(Emphasis::italic | Fg(log_colors::file_info), "{}:{}", filename, loc.line());
Print(FgColor(log_colors::file_info), "\n{:>14} ", "╰──");
Print(Emphasis::italic | FgColor(log_colors::file_info), "{}:{}", filename, loc.line());
#endif
Print("\n");
}
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-macros"
#endif
#ifdef NDEBUG
#define DEBUG_LOG(...) static_cast<void>(0)
#else
@ -164,4 +162,6 @@ fn LogImpl(const LogLevel level, const std::source_location& loc, std::format_st
#define INFO_LOG(...) LogImpl(LogLevel::INFO, std::source_location::current(), __VA_ARGS__)
#define WARN_LOG(...) LogImpl(LogLevel::WARN, std::source_location::current(), __VA_ARGS__)
#define ERROR_LOG(...) LogImpl(LogLevel::ERROR, std::source_location::current(), __VA_ARGS__)
#ifdef __clang__
#pragma clang diagnostic pop
#endif

View file

@ -1,15 +1,12 @@
#pragma once
#include <array>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <expected>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
@ -183,8 +180,8 @@ using Option = std::optional<Tp>;
* @typedef Array
* @brief Represents a fixed-size array.
*/
template <typename Tp, std::size_t nm>
using Array = std::array<Tp, nm>;
template <typename Tp, usize sz>
using Array = std::array<Tp, sz>;
/**
* @typedef Vec
@ -264,18 +261,20 @@ enum class EnvError : u8 { NotFound, AccessError };
inline auto GetEnv(const String& name) -> Result<String, EnvError> {
#ifdef _WIN32
char* rawPtr = nullptr;
size_t bufferSize = 0;
char* rawPtr = nullptr;
usize bufferSize = 0;
if (_dupenv_s(&rawPtr, &bufferSize, name.c_str()) != 0)
return std::unexpected(EnvError::AccessError);
const i32 err = _dupenv_s(&rawPtr, &bufferSize, name.c_str());
if (!rawPtr)
return std::unexpected(EnvError::NotFound);
const UniquePointer<char, decltype(&free)> ptrManager(rawPtr, free);
const String result(rawPtr);
free(rawPtr);
return result;
if (err != 0)
return Err(EnvError::AccessError);
if (!ptrManager)
return Err(EnvError::NotFound);
return ptrManager.get();
#else
CStr value = std::getenv(name.c_str());