[+] A small hacky TTY interface API

This commit is contained in:
Reece Wilson 2022-05-03 05:31:49 +01:00
parent 5cd9e64992
commit 73173bdaae
9 changed files with 460 additions and 26 deletions

View File

@ -24,7 +24,7 @@ namespace Aurora::Console
AUKN_SYM AuSPtr<Logging::ILogger> GetDefaultLogInterface();
/**
* @brief Async read of the underlying binary stream, unlocalized and potentially being consumed by other users.
* @brief Asynchronous binary read of the underlying stream, unlocalized and potentially being consumed by other users.
* Consider using `Hooks::SetCallbackAndDisableCmdProcessing` for asynchronous utf-8 processed line based input
* @param buffer
* @param length
@ -33,7 +33,7 @@ namespace Aurora::Console
AUKN_SYM AuUInt32 ReadStdIn(void *buffer, AuUInt32 length);
/**
* @brief Synchronous binary write to the applications stdout stream. Consider using `AuLogInfo` for general purpose messaging
* @brief Asynchronous binary write to the applications stdout stream. Consider using `AuLogInfo` for general purpose messaging
* @param buffer
* @param length
* @return
@ -41,12 +41,33 @@ namespace Aurora::Console
AUKN_SYM AuUInt32 WriteStdOut(const void *buffer, AuUInt32 length);
/**
* @brief Simulates an input line from an internal logger/console interface given a UTF-8 string
* @param string
* @return
*/
* @brief Simulates an input line from an internal console interface given a UTF-8 string
* Populates ReadStdIn and notifys the hook callback
* @param string
* @return
*/
AUKN_SYM bool DispatchRawLine(const AuString &string);
AUKN_SYM AuUInt32 TTYWrite(const void *buffer, AuUInt32 length);
AUKN_SYM void TTYWrite(const char *string, EAnsiColor fgColor = EAnsiColor::eEnumCount, EAnsiColor bgColor = EAnsiColor::eEnumCount);
AUKN_SYM void TTYFill(char character, EAnsiColor fgColor = EAnsiColor::eEnumCount, EAnsiColor bgColor = EAnsiColor::eEnumCount);
AUKN_SYM void TTYClearLine(EAnsiColor bgColor = EAnsiColor::eEnumCount);
AUKN_SYM void TTYClearScreen();
AUKN_SYM AuPair<AuUInt32, AuUInt32> TTYScreenSize();
AUKN_SYM void TTYSetPos(AuPair<AuUInt32, AuUInt32> position);
AUKN_SYM void TTYStorePos();
AUKN_SYM void TTYRestorePos();
AUKN_SYM void TTYMoveY(AuInt16 lines);
AUKN_SYM void TTYMoveX(AuInt16 cols);
AUKN_SYM void TTYSetY(AuUInt16 Y);
AUKN_SYM void TTYSetX(AuUInt16 X);
AUKN_SYM void TTYReturnHome();
AUKN_SYM void OpenLateStd();
AUKN_SYM void OpenLateGUI();
}

View File

@ -0,0 +1,111 @@
/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: ColorConvert.hpp
Date: 2022-5-2
Author: Reece
***/
#pragma once
namespace Aurora::Console
{
static const AuArray<AuString, static_cast<AuUInt>(EAnsiColor::eEnumCount)> &kAnsiColorForegroundToVirtualEscape =
{
"\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[0;39m"
};
static const AuArray<AuString, static_cast<AuUInt>(EAnsiColor::eEnumCount)> kAnsiColorBackgroundToVirtualEscape
{
"\033[0;41m",
"\033[1;41m",
"\033[0;42m",
"\033[1;42m",
"\033[0;43m",
"\033[1;43m",
"\033[0;44m",
"\033[1;44m",
"\033[0;45m",
"\033[1;45m",
"\033[0;46m",
"\033[1;46m",
"\033[0;49m"
};
#if defined(AURORA_IS_MODERNNT_DERIVED)
#define FOREGROUND_WHITE (FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN)
#define FOREGROUND_YELLOW (FOREGROUND_RED | FOREGROUND_GREEN)
#define FOREGROUND_CYAN (FOREGROUND_BLUE | FOREGROUND_GREEN)
#define FOREGROUND_MAGENTA (FOREGROUND_RED | FOREGROUND_BLUE)
#define FOREGROUND_BLACK 0
#define FOREGROUND_INTENSE_RED (FOREGROUND_RED | FOREGROUND_INTENSITY)
#define FOREGROUND_INTENSE_GREEN (FOREGROUND_GREEN | FOREGROUND_INTENSITY)
#define FOREGROUND_INTENSE_BLUE (FOREGROUND_BLUE | FOREGROUND_INTENSITY)
#define FOREGROUND_INTENSE_WHITE (FOREGROUND_WHITE | FOREGROUND_INTENSITY)
#define FOREGROUND_INTENSE_YELLOW (FOREGROUND_YELLOW | FOREGROUND_INTENSITY)
#define FOREGROUND_INTENSE_CYAN (FOREGROUND_CYAN | FOREGROUND_INTENSITY)
#define FOREGROUND_INTENSE_MAGENTA (FOREGROUND_MAGENTA | FOREGROUND_INTENSITY)
#define BACKGROUND_WHITE (BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN)
#define BACKGROUND_YELLOW (BACKGROUND_RED | BACKGROUND_GREEN)
#define BACKGROUND_CYAN (BACKGROUND_BLUE | BACKGROUND_GREEN)
#define BACKGROUND_MAGENTA (BACKGROUND_RED | BACKGROUND_BLUE)
#define BACKGROUND_BLACK 0
#define BACKGROUND_INTENSE_RED (BACKGROUND_RED | BACKGROUND_INTENSITY)
#define BACKGROUND_INTENSE_GREEN (BACKGROUND_GREEN | BACKGROUND_INTENSITY)
#define BACKGROUND_INTENSE_BLUE (BACKGROUND_BLUE | BACKGROUND_INTENSITY)
#define BACKGROUND_INTENSE_WHITE (BACKGROUND_WHITE | BACKGROUND_INTENSITY)
#define BACKGROUND_INTENSE_YELLOW (BACKGROUND_YELLOW | BACKGROUND_INTENSITY)
#define BACKGROUND_INTENSE_CYAN (BACKGROUND_CYAN | BACKGROUND_INTENSITY)
#define BACKGROUND_INTENSE_MAGENTA (BACKGROUND_MAGENTA | BACKGROUND_INTENSITY)
static const AuArray<DWORD, static_cast<AuUInt>(EAnsiColor::eEnumCount)> kAnsiColorForegroundToNT
{
FOREGROUND_RED,
FOREGROUND_INTENSE_RED,
FOREGROUND_GREEN,
FOREGROUND_INTENSE_GREEN,
FOREGROUND_YELLOW,
FOREGROUND_INTENSE_YELLOW,
FOREGROUND_BLUE,
FOREGROUND_INTENSE_BLUE,
FOREGROUND_MAGENTA,
FOREGROUND_INTENSE_MAGENTA,
FOREGROUND_CYAN,
FOREGROUND_INTENSE_CYAN,
FOREGROUND_WHITE,
};
static const AuArray<DWORD, static_cast<AuUInt>(EAnsiColor::eEnumCount)> kAnsiColorBackgroundToNT
{
BACKGROUND_RED,
BACKGROUND_INTENSE_RED,
BACKGROUND_GREEN,
BACKGROUND_INTENSE_GREEN,
BACKGROUND_YELLOW,
BACKGROUND_INTENSE_YELLOW,
BACKGROUND_BLUE,
BACKGROUND_INTENSE_BLUE,
BACKGROUND_MAGENTA,
BACKGROUND_INTENSE_MAGENTA,
BACKGROUND_CYAN,
BACKGROUND_INTENSE_CYAN,
BACKGROUND_WHITE
};
#endif
}

View File

@ -7,27 +7,10 @@
***/
#include <Source/RuntimeInternal.hpp>
#include "ConsoleMessage.hpp"
#include "ColorConvert.hpp"
namespace Aurora::Console
{
static const AuArray<AuString, static_cast<AuUInt>(EAnsiColor::eEnumCount)> 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",
"\033[0m"
};
static AuString StringifyTimeEx(const ConsoleMessage &msg, bool simple, bool utc)
{
try
@ -106,12 +89,12 @@ namespace Aurora::Console
{
return fmt::format("{}[{}] {:<8} | {}{}",
static_cast<EAnsiColor>(color) <= EAnsiColor::eEnumCount ?
kAnsiCheats[static_cast<AuUInt>(color)] :
kAnsiColorForegroundToVirtualEscape[static_cast<AuUInt>(color)] :
"",
StringifyTime(gRuntimeConfig.console.stdOutShortTime),
GetWrappedTag(),
line,
kAnsiCheats[static_cast<AuUInt>(EAnsiColor::eReset)]);
kAnsiColorForegroundToVirtualEscape[static_cast<AuUInt>(EAnsiColor::eReset)]);
}
catch (...)
{

View File

@ -275,6 +275,14 @@ namespace Aurora::Console::ConsoleStd
return gStdoutBuffer.Write(data, length);
}
AuUInt32 WriteStdOutBlocking2(const void *data, AuUInt32 length)
{
AU_LOCK_GUARD(gRingLock);
WriteStdOutBlocking(gStdoutBuffer.begin(), gStdoutBuffer.size());
gStdoutBuffer.clear();
return WriteStdOutBlocking(data, length);
}
static void FlushStdOut()
{
AU_LOCK_GUARD(gRingLock);

View File

@ -15,6 +15,7 @@ namespace Aurora::Console::ConsoleStd
void Flush();
void Start();
AuUInt32 WriteStdOutBlocking2(const void *data, AuUInt32 length);
AuUInt32 ReadStdIn(void *data, AuUInt32 length);
AuUInt32 WriteStdOut(const void *data, AuUInt32 length);

View File

@ -0,0 +1,7 @@
/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: ConsoleTTY.Linux.cpp
Date: 2022-5-2
Author: Reece
***/

View File

@ -0,0 +1,8 @@
/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: ConsoleTTY.Linux.hpp
Date: 2022-5-2
Author: Reece
***/
#pragma once

View File

@ -0,0 +1,287 @@
/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: ConsoleTTY.NT.cpp
Date: 2022-5-2
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "Console.hpp"
#include "ConsoleTTY.NT.hpp"
#include "ColorConvert.hpp"
#include <Source/Console/ConsoleStd/ConsoleStd.hpp>
namespace Aurora::Console
{
static COORD gSavedCoord {};
AUKN_SYM void TTYClearLine(EAnsiColor bgColor)
{
HANDLE hConsole;
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if (!GetConsoleScreenBufferInfo(hConsole, &csbi))
{
return;
}
if (!FillConsoleOutputCharacterW(hConsole,
L' ',
csbi.dwSize.X,
COORD {0, csbi.dwCursorPosition.Y},
&cCharsWritten))
{
return;
}
if (!FillConsoleOutputAttribute(hConsole,
bgColor == EAnsiColor::eEnumCount ? csbi.wAttributes : kAnsiColorBackgroundToNT[AuStaticCast<AuUInt>(bgColor)],
csbi.dwSize.X,
COORD {0, csbi.dwCursorPosition.Y},
&cCharsWritten))
{
return;
}
SetConsoleCursorPosition(hConsole, {0, csbi.dwCursorPosition.Y});
}
AUKN_SYM void TTYClearScreen()
{
HANDLE hConsole;
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if (!GetConsoleScreenBufferInfo(hConsole, &csbi))
{
return;
}
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
if (!FillConsoleOutputCharacterW(hConsole,
L' ',
dwConSize,
COORD {0, 0},
&cCharsWritten))
{
return;
}
if (!GetConsoleScreenBufferInfo(hConsole, &csbi))
{
return;
}
if (!FillConsoleOutputAttribute(hConsole,
csbi.wAttributes,
dwConSize,
COORD {0, 0},
&cCharsWritten))
{
return;
}
SetConsoleCursorPosition(hConsole, {0, 0});
}
AUKN_SYM void TTYFill(char character, EAnsiColor fgColor, EAnsiColor bgColor)
{
DWORD attrib {0}, cCharsWritten {};
CONSOLE_SCREEN_BUFFER_INFO csbi;
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if (!GetConsoleScreenBufferInfo(hConsole, &csbi))
{
return;
}
if (fgColor != EAnsiColor::eEnumCount)
{
attrib |= kAnsiColorForegroundToNT[AuStaticCast<AuUInt>(fgColor)];
}
else if (bgColor != EAnsiColor::eEnumCount)
{
attrib = FOREGROUND_WHITE;
}
if (bgColor != EAnsiColor::eEnumCount)
{
attrib |= kAnsiColorBackgroundToNT[AuStaticCast<AuUInt>(bgColor)];
}
if (!FillConsoleOutputCharacterW(hConsole,
L' ',
csbi.dwSize.X * csbi.dwSize.Y,
COORD {0, 0},
&cCharsWritten))
{
return;
}
if (attrib)
{
if (!FillConsoleOutputAttribute(hConsole,
attrib,
csbi.dwSize.X * csbi.dwSize.Y,
COORD {0, 0},
&cCharsWritten))
{
//return;
}
}
}
AUKN_SYM AuUInt32 TTYWrite(const void *buffer, AuUInt32 length)
{
return ConsoleStd::WriteStdOutBlocking2(buffer, length);
}
AUKN_SYM void TTYWrite(const char *string, EAnsiColor fgColor, EAnsiColor bgColor)
{
DWORD attrib {};
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if (fgColor != EAnsiColor::eEnumCount)
{
attrib |= kAnsiColorForegroundToNT[AuStaticCast<AuUInt>(fgColor)];
}
else
{
attrib = FOREGROUND_WHITE;
}
if (bgColor != EAnsiColor::eEnumCount)
{
attrib |= kAnsiColorBackgroundToNT[AuStaticCast<AuUInt>(bgColor)];
}
else
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (!GetConsoleScreenBufferInfo(hConsole, &csbi))
{
return;
}
attrib |= csbi.wAttributes & (BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY);
}
SetConsoleTextAttribute(hConsole, attrib);
TTYWrite(string, strlen(string));
SetConsoleTextAttribute(hConsole, FOREGROUND_WHITE);
}
AUKN_SYM void TTYReturnHome()
{
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {0, 0});
}
AUKN_SYM AuPair<AuUInt32, AuUInt32> TTYScreenSize()
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
{
return {};
}
return AuMakePair(AuStaticCast<AuUInt32>(csbi.dwSize.X), AuStaticCast<AuUInt32>(csbi.dwSize.Y));
}
AUKN_SYM void TTYStorePos()
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
{
return;
}
gSavedCoord = csbi.dwCursorPosition;
}
AUKN_SYM void TTYRestorePos()
{
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), gSavedCoord);
}
AUKN_SYM void TTYMoveY(AuInt16 lines)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if (!GetConsoleScreenBufferInfo(hConsole, &csbi))
{
return;
}
csbi.dwCursorPosition.Y += lines;
SetConsoleCursorPosition(hConsole, gSavedCoord);
}
AUKN_SYM void TTYMoveX(AuInt16 lines)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if (!GetConsoleScreenBufferInfo(hConsole, &csbi))
{
return;
}
csbi.dwCursorPosition.X += lines;
SetConsoleCursorPosition(hConsole, gSavedCoord);
}
AUKN_SYM void TTYSetY(AuUInt16 Y)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if (!GetConsoleScreenBufferInfo(hConsole, &csbi))
{
return;
}
csbi.dwCursorPosition.Y = Y;
SetConsoleCursorPosition(hConsole, gSavedCoord);
}
AUKN_SYM void TTYSetX(AuUInt16 X)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if (!GetConsoleScreenBufferInfo(hConsole, &csbi))
{
return;
}
csbi.dwCursorPosition.X = X;
SetConsoleCursorPosition(hConsole, gSavedCoord);
}
AUKN_SYM void TTYSetPos(AuPair<AuUInt32, AuUInt32> position)
{
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), COORD {AuStaticCast<short>(position.first), AuStaticCast<short>(position.second)});
}
}

View File

@ -0,0 +1,8 @@
/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: ConsoleTTY.NT.hpp
Date: 2022-5-2
Author: Reece
***/
#pragma once