Added colored text in terminal
This commit is contained in:
@@ -7,6 +7,7 @@ namespace LXC::Util
|
|||||||
{
|
{
|
||||||
struct FileReadError
|
struct FileReadError
|
||||||
{
|
{
|
||||||
|
// Different reasons why the error can occur //
|
||||||
enum Reason
|
enum Reason
|
||||||
{
|
{
|
||||||
FileNotFound,
|
FileNotFound,
|
||||||
@@ -14,12 +15,27 @@ namespace LXC::Util
|
|||||||
NotAFile
|
NotAFile
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Constructor to pass arguments to the struct //
|
||||||
FileReadError(const std::filesystem::path& _path, Reason _reason)
|
FileReadError(const std::filesystem::path& _path, Reason _reason)
|
||||||
: path(_path), reason(_reason)
|
: path(_path), reason(_reason)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
std::filesystem::path path;
|
// Error information //
|
||||||
Reason reason;
|
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)
|
inline ReturnVal<std::string, FileReadError> ReadFile(const std::filesystem::path& filepath)
|
||||||
|
|||||||
60
Common/IO.h
Normal file
60
Common/IO.h
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,9 +3,9 @@
|
|||||||
// Standard libraries //
|
// Standard libraries //
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cstdlib>
|
|
||||||
|
|
||||||
// LXC util files //
|
// LXC util files //
|
||||||
|
|
||||||
#include <Result.h>
|
#include <Result.h>
|
||||||
#include <FileRead.h>
|
#include <FileRead.h>
|
||||||
|
#include <IO.h>
|
||||||
|
|||||||
27
LXC/LXC.cpp
27
LXC/LXC.cpp
@@ -6,20 +6,25 @@ int main(int argc, char** argv)
|
|||||||
{
|
{
|
||||||
using namespace LXC;
|
using namespace LXC;
|
||||||
|
|
||||||
//
|
// Reads the given file to a string //
|
||||||
|
|
||||||
Lexer::LexerContext context;
|
|
||||||
Lexer::Token exampleToken(context, 2);
|
|
||||||
|
|
||||||
//
|
|
||||||
|
|
||||||
Util::ReturnVal fileContents = Util::ReadFile("example/example.lx");
|
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())
|
// Prints the error to the console //
|
||||||
std::cout << fileContents.Result() << std::endl;
|
Util::PrintAs<Util::Color::WHITE>("[LXC]");
|
||||||
|
Util::PrintAs<Util::Color::LIGHT_RED>(" Error: ");
|
||||||
|
|
||||||
else
|
std::cout
|
||||||
std::cout << fileContents.Error().reason << " | " << fileContents.Error().path << std::endl;
|
<< Util::FileReadError::ReasonStr(err.reason) << ' '
|
||||||
|
<< '[' << std::filesystem::absolute(err.path) << ']'
|
||||||
|
<< std::endl;
|
||||||
|
|
||||||
|
// Returns with default exit code //
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user