mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-22 17:48:01 +02:00
Merge pull request #216 from RogueMacro/master
Add color support to System.Console
This commit is contained in:
commit
753a58e1ad
2 changed files with 154 additions and 0 deletions
|
@ -9,6 +9,34 @@ namespace System
|
|||
public static Encoding InputEncoding = Encoding.ASCII;
|
||||
public static Encoding OutputEncoding = Encoding.ASCII;
|
||||
|
||||
private static ConsoleColor mForegroundColor = .White;
|
||||
private static ConsoleColor mBackgroundColor = .Black;
|
||||
|
||||
private static readonly ConsoleColor mOriginalForegroundColor = mForegroundColor;
|
||||
private static readonly ConsoleColor mOriginalBackgroundColor = mBackgroundColor;
|
||||
|
||||
public static ConsoleColor ForegroundColor
|
||||
{
|
||||
get { return mForegroundColor; }
|
||||
set { mForegroundColor = value; SetColors(); }
|
||||
}
|
||||
|
||||
public static ConsoleColor BackgroundColor
|
||||
{
|
||||
get { return mBackgroundColor; }
|
||||
set { mBackgroundColor = value; SetColors(); }
|
||||
}
|
||||
|
||||
private const uint32 STD_INPUT_HANDLE = (uint32) - 10;
|
||||
private const uint32 STD_OUTPUT_HANDLE = (uint32) - 11;
|
||||
private const uint32 STD_ERROR_HANDLE = (uint32) - 12;
|
||||
|
||||
[Import("kernel32.dll"), CLink]
|
||||
private static extern bool SetConsoleTextAttribute(void* hConsoleOutput, uint16 wAttributes);
|
||||
|
||||
[Import("kernel32.dll"), CLink]
|
||||
private static extern void* GetStdHandle(uint32 nStdHandle);
|
||||
|
||||
static StreamWriter OpenStreamWriter(Platform.BfpFileStdKind stdKind, ref StreamWriter outStreamWriter)
|
||||
{
|
||||
if (outStreamWriter == null)
|
||||
|
@ -131,5 +159,28 @@ namespace System
|
|||
obj.ToString(str);
|
||||
WriteLine(str);
|
||||
}
|
||||
|
||||
public static void ResetColor()
|
||||
{
|
||||
mForegroundColor = mOriginalForegroundColor;
|
||||
mBackgroundColor = mOriginalBackgroundColor;
|
||||
|
||||
#if !BF_PLATFORM_WINDOWS
|
||||
Write("\x1B[0m");
|
||||
#endif
|
||||
}
|
||||
|
||||
private static void SetColors()
|
||||
{
|
||||
#if BF_PLATFORM_WINDOWS
|
||||
let handle = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
let fgColor = ForegroundColor.ToConsoleTextAttribute();
|
||||
let bgColor = BackgroundColor.ToConsoleTextAttribute();
|
||||
SetConsoleTextAttribute(handle, bgColor * 16 + fgColor);
|
||||
#else
|
||||
Write("\x1B[{}m", ForegroundColor.ToAnsi());
|
||||
Write("\x1B[{}m", BackgroundColor.ToAnsi() + 10);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
103
BeefLibs/corlib/src/ConsoleColor.bf
Normal file
103
BeefLibs/corlib/src/ConsoleColor.bf
Normal file
|
@ -0,0 +1,103 @@
|
|||
namespace System
|
||||
{
|
||||
public enum ConsoleColor
|
||||
{
|
||||
Black,
|
||||
DarkBlue,
|
||||
DarkGreen,
|
||||
DarkCyan,
|
||||
DarkRed,
|
||||
DarkMagenta,
|
||||
DarkYellow,
|
||||
DarkGray,
|
||||
Gray,
|
||||
Blue,
|
||||
Green,
|
||||
Cyan,
|
||||
Red,
|
||||
Magenta,
|
||||
Yellow,
|
||||
White
|
||||
}
|
||||
|
||||
extension ConsoleColor
|
||||
{
|
||||
public uint8 ToConsoleTextAttribute()
|
||||
{
|
||||
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()
|
||||
{
|
||||
switch (this)
|
||||
{
|
||||
case .Black:
|
||||
return 30;
|
||||
case .DarkRed:
|
||||
return 31;
|
||||
case .DarkGreen:
|
||||
return 32;
|
||||
case .DarkYellow:
|
||||
return 33;
|
||||
case .DarkBlue:
|
||||
return 34;
|
||||
case .DarkMagenta:
|
||||
return 35;
|
||||
case .DarkCyan:
|
||||
return 36;
|
||||
case .Gray:
|
||||
return 37;
|
||||
case .DarkGray:
|
||||
return 90;
|
||||
case .Red:
|
||||
return 91;
|
||||
case .Green:
|
||||
return 92;
|
||||
case .Yellow:
|
||||
return 93;
|
||||
case .Blue:
|
||||
return 94;
|
||||
case .Magenta:
|
||||
return 95;
|
||||
case .Cyan:
|
||||
return 96;
|
||||
case .White:
|
||||
return 97;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue