Added colored text in terminal

This commit is contained in:
Pasha Bibko
2025-07-20 13:10:57 +01:00
parent c8975f0c20
commit 7768ba4522
4 changed files with 95 additions and 14 deletions

View File

@@ -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<std::string, FileReadError> ReadFile(const std::filesystem::path& filepath)

60
Common/IO.h Normal file
View File

@@ -0,0 +1,60 @@
#pragma once
// Platform specific includes //
#ifdef _WIN32
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#else
#error "Code is currently only supported on Win32"
#endif // _WIN32
#include <iostream>
#include <ostream>
namespace LXC::Util
{
// Checks if a type can be outputted to std::ostream //
template<typename T> concept Logable = requires(std::ostream& os, T t)
{
// I have no idea what this part does at all //
{ os << t } -> std::same_as<std::ostream&>;
};
// Checks if a list of types can be outputted to std::ostream //
template<typename... Args> concept AllLogable = (Logable<Args> && ...);
// 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<Color col, typename... Args>
requires AllLogable<Args...>
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);
}
}

View File

@@ -3,9 +3,9 @@
// Standard libraries //
#include <iostream>
#include <cstdlib>
// LXC util files //
#include <Result.h>
#include <FileRead.h>
#include <IO.h>

View File

@@ -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<Util::Color::WHITE>("[LXC]");
Util::PrintAs<Util::Color::LIGHT_RED>(" 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;
}