86 lines
1.7 KiB
C++
86 lines
1.7 KiB
C++
/***
|
|
Copyright (c) 2020 Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
File: console.cpp
|
|
Date: 2020-6-9
|
|
Originator: Reece
|
|
Purpose: Lightweight and portable unit test abstraction
|
|
***/
|
|
#include <AuroraCommon.hpp>
|
|
|
|
#include "console.hpp"
|
|
|
|
#if defined(AURORA_PLATFORM_WIN32)
|
|
#include <windows.h>
|
|
#endif
|
|
|
|
#include <string>
|
|
#include <cstdio>
|
|
#include <array>
|
|
|
|
using namespace Aurora::UnitTesting;
|
|
using namespace Aurora::UnitTesting::Console;
|
|
|
|
#if defined(AURORA_PLATFORM_WIN32)
|
|
static bool kSupportsColor = false;
|
|
#else
|
|
static bool kSupportsColor = true;
|
|
#endif
|
|
|
|
static std::array<std::string, static_cast<size_t>(AnsiColor::kCount)> kAnsiCheats {
|
|
"\033[0;31m",
|
|
"\033[1;31m",
|
|
"\033[0;32m",
|
|
"\033[1;32m",
|
|
"\033[0;33m",
|
|
"\033[1;33m",
|
|
"\033[0;34m",
|
|
"\033[1;34m",
|
|
"\033[0;35m",
|
|
"\033[1;35m",
|
|
"\033[0;36m",
|
|
"\033[1;36m",
|
|
"\033[0m"
|
|
};
|
|
|
|
void Console::WriteLine(const std::string &prefix, AnsiColor color, const std::string &msg)
|
|
{
|
|
std::string ret;
|
|
|
|
if (kSupportsColor)
|
|
{
|
|
ret = kAnsiCheats[static_cast<size_t>(color)];
|
|
}
|
|
|
|
ret += "[" + prefix + "]\t| ";
|
|
ret += msg;
|
|
|
|
if (kSupportsColor)
|
|
{
|
|
ret += kAnsiCheats[static_cast<size_t>(AnsiColor::kReset)];
|
|
}
|
|
|
|
puts(ret.c_str());
|
|
}
|
|
|
|
void Console::Init()
|
|
{
|
|
#ifdef AURORA_PLATFORM_WIN32
|
|
auto handle = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
if (handle == INVALID_HANDLE_VALUE)
|
|
{
|
|
puts("couldn't enable color. bad handle");
|
|
return;
|
|
}
|
|
|
|
DWORD mode = 0;
|
|
if (!(GetConsoleMode(handle, &mode) &&
|
|
SetConsoleMode(handle, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)))
|
|
{
|
|
puts("couldn't enable color");
|
|
return;
|
|
}
|
|
|
|
kSupportsColor = true;
|
|
#endif
|
|
} |