From 7768ba452276f4c6fb2d0f1286a79db18678d37f Mon Sep 17 00:00:00 2001 From: Pasha Bibko <156938226+PashaBibko@users.noreply.github.com> Date: Sun, 20 Jul 2025 13:10:57 +0100 Subject: [PATCH] Added colored text in terminal --- Common/FileRead.h | 20 ++++++++++++++-- Common/IO.h | 60 +++++++++++++++++++++++++++++++++++++++++++++++ Common/LXC.h | 2 +- LXC/LXC.cpp | 27 ++++++++++++--------- 4 files changed, 95 insertions(+), 14 deletions(-) create mode 100644 Common/IO.h diff --git a/Common/FileRead.h b/Common/FileRead.h index e33aa58..1d8157e 100644 --- a/Common/FileRead.h +++ b/Common/FileRead.h @@ -7,6 +7,7 @@ namespace LXC::Util { struct FileReadError { + // Different reasons why the error can occur // enum Reason { FileNotFound, @@ -14,12 +15,27 @@ namespace LXC::Util NotAFile }; + // Constructor to pass arguments to the struct // FileReadError(const std::filesystem::path& _path, Reason _reason) : path(_path), reason(_reason) {} - std::filesystem::path path; - Reason reason; + // Error information // + const std::filesystem::path path; + const Reason reason; + + // Turns the error into a c-string // + inline static const char* const ReasonStr(const Reason& reason) + { + static const char* reasons[] = + { + "File cannot be found", + "File reading permissions are denied", + "Not a file" + }; + + return reasons[reason]; + } }; inline ReturnVal ReadFile(const std::filesystem::path& filepath) diff --git a/Common/IO.h b/Common/IO.h new file mode 100644 index 0000000..d91c120 --- /dev/null +++ b/Common/IO.h @@ -0,0 +1,60 @@ +#pragma once + +// Platform specific includes // +#ifdef _WIN32 + #define NOMINMAX + #define WIN32_LEAN_AND_MEAN + #include +#else + #error "Code is currently only supported on Win32" +#endif // _WIN32 + +#include +#include + +namespace LXC::Util +{ + // Checks if a type can be outputted to std::ostream // + template concept Logable = requires(std::ostream& os, T t) + { + // I have no idea what this part does at all // + { os << t } -> std::same_as; + }; + + // Checks if a list of types can be outputted to std::ostream // + template concept AllLogable = (Logable && ...); + + // Enum to translate to the Win32 code for the colors // + enum class Color : WORD + { + BLACK = 0x00, + BLUE = 0x01, + GREEN = 0x02, + AQUA = 0x03, + RED = 0x04, + PURPLE = 0x05, + YELLOW = 0x06, + LIGHT_GRAY = 0x07, + LIGHT_BLUE = 0x09, + LIGHT_GREEN = 0x0a, + LIGHT_AQUA = 0x0b, + LIGHT_RED = 0x0c, + LIGHT_PURPLE = 0x0d, + LIGHT_YELLOW = 0x0e, + WHITE = 0x0f + }; + + // Prints arguments to the console with the given color // + template + requires AllLogable + inline void PrintAs(Args... args) + { + // Permenant handle to the console // + static HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + + // Prints it to the console and resets the color // + SetConsoleTextAttribute(hConsole, (WORD)col); + (std::cout << ... << args); + SetConsoleTextAttribute(hConsole, (WORD)Color::LIGHT_GRAY); + } +} diff --git a/Common/LXC.h b/Common/LXC.h index c7e0530..d0b131f 100644 --- a/Common/LXC.h +++ b/Common/LXC.h @@ -3,9 +3,9 @@ // Standard libraries // #include -#include // LXC util files // #include #include +#include diff --git a/LXC/LXC.cpp b/LXC/LXC.cpp index 713a8b9..cdd8f2a 100644 --- a/LXC/LXC.cpp +++ b/LXC/LXC.cpp @@ -6,20 +6,25 @@ int main(int argc, char** argv) { using namespace LXC; - // - - Lexer::LexerContext context; - Lexer::Token exampleToken(context, 2); - - // - + // Reads the given file to a string // Util::ReturnVal fileContents = Util::ReadFile("example/example.lx"); + if (fileContents.Failed()) + { + // Stores the error for easier access // + Util::FileReadError& err = fileContents.Error(); - if (fileContents.Suceeded()) - std::cout << fileContents.Result() << std::endl; + // Prints the error to the console // + Util::PrintAs("[LXC]"); + Util::PrintAs(" Error: "); - else - std::cout << fileContents.Error().reason << " | " << fileContents.Error().path << std::endl; + std::cout + << Util::FileReadError::ReasonStr(err.reason) << ' ' + << '[' << std::filesystem::absolute(err.path) << ']' + << std::endl; + + // Returns with default exit code // + return -1; + } return 0; }