-
Notifications
You must be signed in to change notification settings - Fork 0
/
anisc.c
executable file
·71 lines (56 loc) · 1.63 KB
/
anisc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//reference from https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#example-of-sgr-terminal-sequences
//BOTH anisc.c and anisc.h are the code from
//https://solarianprogrammer.com/2019/04/08/c-programming-ansi-escape-codes-windows-macos-linux-terminals/
//they are not my code they are used for compability purpose only
#ifdef __APPLE__
#include <term.h>
#elif defined _WIN32 || defined _WIN64
#include <wchar.h>
#include <windows.h>
#endif
#include <stdio.h>
#include "anisc.h"
#ifdef __APPLE__
void setupConsole(void) {}
void restoreConsole(void) {
// Reset colors
printf("\x1b[0m");
}
#elif defined _WIN32 || defined _WIN64
// Some old MinGW/CYGWIN distributions don't define this:
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif
static HANDLE stdoutHandle;
static DWORD outModeInit;
void setupConsole(void) {
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(void) {
// Reset colors
printf("\x1b[0m");
// Reset console mode
if(!SetConsoleMode(stdoutHandle, outModeInit)) {
exit(GetLastError());
}
}
#else
void setupConsole(void) {}
void restoreConsole(void) {
// Reset colors
printf("\x1b[0m");
}
#endif