Added CTRL+C handling.
This commit is contained in:
parent
b3796ddd97
commit
3fb853d7b7
1 changed files with 56 additions and 1 deletions
57
source/app.d
57
source/app.d
|
@ -1,5 +1,60 @@
|
|||
import std.stdio;
|
||||
import core.thread;
|
||||
import core.stdc.signal;
|
||||
import core.sys.windows.windows;
|
||||
|
||||
__gshared bool hasCancelled;
|
||||
|
||||
version(Posix) {
|
||||
extern(C) void signalHander(int num) nothrow @nogc @system {
|
||||
if(num == SIGINT)
|
||||
hasCancelled = true;
|
||||
}
|
||||
}
|
||||
|
||||
version(Windows) {
|
||||
extern(Windows) BOOL ctrlHandler(DWORD fdwCtrlType) nothrow @nogc @system {
|
||||
switch(fdwCtrlType) {
|
||||
case CTRL_C_EVENT:
|
||||
case CTRL_CLOSE_EVENT:
|
||||
hasCancelled = true;
|
||||
return TRUE;
|
||||
|
||||
default:
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
writeln("meow");
|
||||
import std.range : padLeft;
|
||||
import std.string : format;
|
||||
|
||||
version(Windows)
|
||||
if(!SetConsoleCtrlHandler(&ctrlHandler, TRUE))
|
||||
stderr.writeln("Failed to register Windows CTRL handler.");
|
||||
|
||||
version(Posix)
|
||||
signal(SIGINT, &signalHander);
|
||||
|
||||
if(hasCancelled) return;
|
||||
|
||||
writeln(r" __ ____ __ __ ");
|
||||
writeln(r" / |/ (_) /______ / /_____ ");
|
||||
writeln(r" / /|_/ / / //_/ __ \/ __/ __ \");
|
||||
writeln(r" / / / / / ,< / /_/ / /_/ /_/ /");
|
||||
writeln(r"/_/ /_/_/_/|_|\____/\__/\____/ ");
|
||||
|
||||
debug writeln("== %s ========= DEBUG ==".format("aabbccddee"));
|
||||
else writeln("v0.0.0".padLeft(' ', 30));
|
||||
|
||||
if(hasCancelled) return;
|
||||
|
||||
for(;;) {
|
||||
Thread.sleep(1.seconds);
|
||||
if(hasCancelled)
|
||||
break;
|
||||
}
|
||||
|
||||
writeln("Exiting!");
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue