1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-20 17:08:00 +02:00
This commit is contained in:
Brian Fiete 2021-06-07 06:38:29 -07:00
parent 1e5e263a31
commit c3e6282a8b

View file

@ -7,6 +7,12 @@ namespace System
{
public static class Console
{
public enum CancelKind
{
CtrlC,
CtrlBreak
}
static Encoding InputEncoding = Encoding.ASCII;
static Encoding OutputEncoding = Encoding.ASCII;
@ -16,6 +22,9 @@ namespace System
static readonly ConsoleColor sOriginalForegroundColor = sForegroundColor;
static readonly ConsoleColor sOriginalBackgroundColor = sBackgroundColor;
static Event<delegate void (CancelKind cancelKind, ref bool terminate)> sOnCancel ~ _.Dispose();
static bool sCancelEventRegistered;
public static ConsoleColor ForegroundColor
{
get { return sForegroundColor; }
@ -42,6 +51,31 @@ namespace System
public uint16[2] mMaximumWindowSize;
}
public static ref Event<delegate void (CancelKind cancelKind, ref bool terminate)> OnCancel
{
get
{
if (!sCancelEventRegistered)
{
sCancelEventRegistered = true;
#if BF_PLATFORM_WINDOWS
SetConsoleCtrlHandler(=> ConsoleCtrlHandler, true);
#endif
}
return ref sOnCancel;
}
}
#if BF_PLATFORM_WINDOWS
[CallingConvention(.Stdcall)]
public static Windows.IntBool ConsoleCtrlHandler(int32 ctrlType)
{
bool terminate = true;
if ((ctrlType == 0) || (ctrlType == 1))
sOnCancel((.)ctrlType, ref terminate);
return terminate ? false : true;
}
[CLink, CallingConvention(.Stdcall)]
static extern int SetConsoleTextAttribute(void* hConsoleOutput, uint16 wAttributes);
@ -51,7 +85,11 @@ namespace System
[CLink, CallingConvention(.Stdcall)]
static extern void* GetStdHandle(uint32 nStdHandle);
#if BF_PLATFORM_WINDOWS
[CallingConvention(.Stdcall)]
function Windows.IntBool ConsoleCtrlHandler(int32 ctrlType);
[CLink, CallingConvention(.Stdcall)]
static extern Windows.IntBool SetConsoleCtrlHandler(ConsoleCtrlHandler handler, Windows.IntBool addHandler);
public static this()
{
let handle = GetStdHandle(STD_OUTPUT_HANDLE);