1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-20 08:58:00 +02:00

Correctly set original console colors. Minor code reworking.

This commit is contained in:
Brian Fiete 2020-05-05 08:18:32 -07:00
parent 753a58e1ad
commit e9a1d6990e
2 changed files with 111 additions and 111 deletions

View file

@ -6,36 +6,62 @@ namespace System
{ {
public static class Console public static class Console
{ {
public static Encoding InputEncoding = Encoding.ASCII; static Encoding InputEncoding = Encoding.ASCII;
public static Encoding OutputEncoding = Encoding.ASCII; static Encoding OutputEncoding = Encoding.ASCII;
private static ConsoleColor mForegroundColor = .White; static ConsoleColor sForegroundColor = .White;
private static ConsoleColor mBackgroundColor = .Black; static ConsoleColor sBackgroundColor = .Black;
private static readonly ConsoleColor mOriginalForegroundColor = mForegroundColor; static readonly ConsoleColor sOriginalForegroundColor = sForegroundColor;
private static readonly ConsoleColor mOriginalBackgroundColor = mBackgroundColor; static readonly ConsoleColor sOriginalBackgroundColor = sBackgroundColor;
public static ConsoleColor ForegroundColor public static ConsoleColor ForegroundColor
{ {
get { return mForegroundColor; } get { return sForegroundColor; }
set { mForegroundColor = value; SetColors(); } set { sForegroundColor = value; SetColors(); }
} }
public static ConsoleColor BackgroundColor public static ConsoleColor BackgroundColor
{ {
get { return mBackgroundColor; } get { return sBackgroundColor; }
set { mBackgroundColor = value; SetColors(); } set { sBackgroundColor = value; SetColors(); }
} }
private const uint32 STD_INPUT_HANDLE = (uint32) - 10; const uint32 STD_INPUT_HANDLE = (uint32)-10;
private const uint32 STD_OUTPUT_HANDLE = (uint32) - 11; const uint32 STD_OUTPUT_HANDLE = (uint32)-11;
private const uint32 STD_ERROR_HANDLE = (uint32) - 12; const uint32 STD_ERROR_HANDLE = (uint32)-12;
[Import("kernel32.dll"), CLink] [CRepr]
private static extern bool SetConsoleTextAttribute(void* hConsoleOutput, uint16 wAttributes); struct CONSOLE_SCREEN_BUFFER_INFO
{
public uint16[2] mSize;
public uint16[2] mCursorPosition;
public uint16 mAttributes;
public uint16[4] mWindow;
public uint16[2] mMaximumWindowSize;
}
[Import("kernel32.dll"), CLink] [CLink]
private static extern void* GetStdHandle(uint32 nStdHandle); static extern int SetConsoleTextAttribute(void* hConsoleOutput, uint16 wAttributes);
[CLink]
static extern int GetConsoleScreenBufferInfo(void* hConsoleOutput, out CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);
[CLink]
static extern void* GetStdHandle(uint32 nStdHandle);
#if BF_PLATFORM_WINDOWS
public static this()
{
let handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO consoleInfo = .();
if (GetConsoleScreenBufferInfo(handle, out consoleInfo) != 0)
{
sOriginalForegroundColor.ConsoleTextAttribute = (uint8)(consoleInfo.mAttributes & 0xF);
sOriginalBackgroundColor.ConsoleTextAttribute = (uint8)(consoleInfo.mAttributes >> 4);
}
}
#endif
static StreamWriter OpenStreamWriter(Platform.BfpFileStdKind stdKind, ref StreamWriter outStreamWriter) static StreamWriter OpenStreamWriter(Platform.BfpFileStdKind stdKind, ref StreamWriter outStreamWriter)
{ {
@ -162,20 +188,20 @@ namespace System
public static void ResetColor() public static void ResetColor()
{ {
mForegroundColor = mOriginalForegroundColor; sForegroundColor = sOriginalForegroundColor;
mBackgroundColor = mOriginalBackgroundColor; sBackgroundColor = sOriginalBackgroundColor;
#if !BF_PLATFORM_WINDOWS #if !BF_PLATFORM_WINDOWS
Write("\x1B[0m"); Write("\x1B[0m");
#endif #endif
} }
private static void SetColors() static void SetColors()
{ {
#if BF_PLATFORM_WINDOWS #if BF_PLATFORM_WINDOWS
let handle = GetStdHandle(STD_OUTPUT_HANDLE); let handle = GetStdHandle(STD_OUTPUT_HANDLE);
let fgColor = ForegroundColor.ToConsoleTextAttribute(); let fgColor = ForegroundColor.ConsoleTextAttribute;
let bgColor = BackgroundColor.ToConsoleTextAttribute(); let bgColor = BackgroundColor.ConsoleTextAttribute;
SetConsoleTextAttribute(handle, bgColor * 16 + fgColor); SetConsoleTextAttribute(handle, bgColor * 16 + fgColor);
#else #else
Write("\x1B[{}m", ForegroundColor.ToAnsi()); Write("\x1B[{}m", ForegroundColor.ToAnsi());

View file

@ -2,66 +2,39 @@ namespace System
{ {
public enum ConsoleColor public enum ConsoleColor
{ {
Black, case Black;
DarkBlue, case DarkBlue;
DarkGreen, case DarkGreen;
DarkCyan, case DarkCyan;
DarkRed, case DarkRed;
DarkMagenta, case DarkMagenta;
DarkYellow, case DarkYellow;
DarkGray, case DarkGray;
Gray, case Gray;
Blue, case Blue;
Green, case Green;
Cyan, case Cyan;
Red, case Red;
Magenta, case Magenta;
Yellow, case Yellow;
White case White;
public uint8 ConsoleTextAttribute
{
get
{
return (.)this;
} }
extension ConsoleColor set mut
{ {
public uint8 ToConsoleTextAttribute() this = (.)value;
{
switch (this)
{
case .Black:
return 0;
case .DarkBlue:
return 1;
case .DarkGreen:
return 2;
case .DarkCyan:
return 3;
case .DarkRed:
return 4;
case .DarkMagenta:
return 5;
case .DarkYellow:
return 6;
case .DarkGray:
return 7;
case .Gray:
return 8;
case .Blue:
return 9;
case .Green:
return 10;
case .Cyan:
return 11;
case .Red:
return 12;
case .Magenta:
return 13;
case .Yellow:
return 14;
case .White:
return 15;
} }
} }
public uint8 ToAnsi() public uint8 AnsiCode
{
get
{ {
switch (this) switch (this)
{ {
@ -101,3 +74,4 @@ namespace System
} }
} }
} }
}