Created
June 29, 2019 21:18
-
-
Save khalidelboray/2405e636b3a852048b98007dd8fa03e3 to your computer and use it in GitHub Desktop.
Enable ANSI escape codes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use NativeCall; | |
| use Terminal::ANSIColor; | |
| # Compile the c code on windows | |
| # $ gcc -c setcolor.c | |
| # $ gcc -shared -o setcolor.dll setcolor.o | |
| constant LIBPATH = "$*CWD/setcolor"; | |
| sub restoreConsole() is native(LIBPATH) { * } | |
| sub setupconsole() is native(LIBPATH) { * } | |
| setupconsole(); | |
| my $psych = "Yo, I got your psychedelia right here!" | |
| .words.map({ colored("$_ ", "{(^256).pick} on_{(^256).pick}") }).join; | |
| say $psych; | |
| restoreConsole(); | |
| say "Restore Concole"; | |
| say $psych; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifdef _WIN32 | |
| #include <windows.h> | |
| #endif | |
| #include <stdio.h> | |
| #ifdef _WIN32 | |
| #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING | |
| #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004 | |
| #endif | |
| static HANDLE stdoutHandle; | |
| static DWORD outModeInit; | |
| void setupconsole () { | |
| DWORD outMode = 0; | |
| stdoutHandle = GetStdHandle(STD_OUTPUT_HANDLE); | |
| if(stdoutHandle == INVALID_HANDLE_VALUE) { | |
| exit(GetLastError()); | |
| } | |
| if(!GetConsoleMode(stdoutHandle, &outMode)) { | |
| exit(GetLastError()); | |
| } | |
| outModeInit = outMode; | |
| // Enable ANSI escape codes | |
| outMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; | |
| if(!SetConsoleMode(stdoutHandle, outMode)) { | |
| exit(GetLastError()); | |
| } | |
| } | |
| void restoreConsole() { | |
| // Reset colors | |
| printf("\x1b[0m"); | |
| // Reset console mode | |
| if(!SetConsoleMode(stdoutHandle, outModeInit)) { | |
| exit(GetLastError()); | |
| } | |
| } | |
| #else | |
| void setupConsole(void) {} | |
| void restoreConsole(void) { | |
| printf("\x1b[0m"); | |
| } | |
| #endif |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ref