mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-20 17:08:00 +02:00
Correctly set original console colors. Minor code reworking.
This commit is contained in:
parent
753a58e1ad
commit
e9a1d6990e
2 changed files with 111 additions and 111 deletions
|
@ -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());
|
||||||
|
|
|
@ -2,101 +2,75 @@ 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;
|
||||||
}
|
|
||||||
|
|
||||||
extension ConsoleColor
|
public uint8 ConsoleTextAttribute
|
||||||
{
|
|
||||||
public uint8 ToConsoleTextAttribute()
|
|
||||||
{
|
{
|
||||||
switch (this)
|
get
|
||||||
{
|
{
|
||||||
case .Black:
|
return (.)this;
|
||||||
return 0;
|
}
|
||||||
case .DarkBlue:
|
|
||||||
return 1;
|
set mut
|
||||||
case .DarkGreen:
|
{
|
||||||
return 2;
|
this = (.)value;
|
||||||
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
|
||||||
{
|
{
|
||||||
switch (this)
|
get
|
||||||
{
|
{
|
||||||
case .Black:
|
switch (this)
|
||||||
return 30;
|
{
|
||||||
case .DarkRed:
|
case .Black:
|
||||||
return 31;
|
return 30;
|
||||||
case .DarkGreen:
|
case .DarkRed:
|
||||||
return 32;
|
return 31;
|
||||||
case .DarkYellow:
|
case .DarkGreen:
|
||||||
return 33;
|
return 32;
|
||||||
case .DarkBlue:
|
case .DarkYellow:
|
||||||
return 34;
|
return 33;
|
||||||
case .DarkMagenta:
|
case .DarkBlue:
|
||||||
return 35;
|
return 34;
|
||||||
case .DarkCyan:
|
case .DarkMagenta:
|
||||||
return 36;
|
return 35;
|
||||||
case .Gray:
|
case .DarkCyan:
|
||||||
return 37;
|
return 36;
|
||||||
case .DarkGray:
|
case .Gray:
|
||||||
return 90;
|
return 37;
|
||||||
case .Red:
|
case .DarkGray:
|
||||||
return 91;
|
return 90;
|
||||||
case .Green:
|
case .Red:
|
||||||
return 92;
|
return 91;
|
||||||
case .Yellow:
|
case .Green:
|
||||||
return 93;
|
return 92;
|
||||||
case .Blue:
|
case .Yellow:
|
||||||
return 94;
|
return 93;
|
||||||
case .Magenta:
|
case .Blue:
|
||||||
return 95;
|
return 94;
|
||||||
case .Cyan:
|
case .Magenta:
|
||||||
return 96;
|
return 95;
|
||||||
case .White:
|
case .Cyan:
|
||||||
return 97;
|
return 96;
|
||||||
|
case .White:
|
||||||
|
return 97;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue