forked from gost-engine/engine
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ansi_terminal.c
95 lines (76 loc) · 2.2 KB
/
ansi_terminal.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#ifdef _WIN32
#define _CRT_SECURE_NO_WARNINGS 1
#include <windows.h>
#else
#include <termios.h>
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
// Some old MinGW/CYGWIN distributions don't define this:
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif
static HANDLE stdoutHandle, stdinHandle;
static DWORD outModeInit, inModeInit;
void setupConsole(void) {
DWORD outMode = 0, inMode = 0;
stdoutHandle = GetStdHandle(STD_OUTPUT_HANDLE);
stdinHandle = GetStdHandle(STD_INPUT_HANDLE);
if(stdoutHandle == INVALID_HANDLE_VALUE || stdinHandle == INVALID_HANDLE_VALUE) {
exit(GetLastError());
}
if(!GetConsoleMode(stdoutHandle, &outMode) || !GetConsoleMode(stdinHandle, &inMode)) {
exit(GetLastError());
}
outModeInit = outMode;
inModeInit = inMode;
// Enable ANSI escape codes
outMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
// Set stdin as no echo and unbuffered
inMode &= ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT);
if(!SetConsoleMode(stdoutHandle, outMode) || !SetConsoleMode(stdinHandle, inMode)) {
exit(GetLastError());
}
}
void restoreConsole(void) {
// Reset colors
printf("\x1b[0m");
// Reset console mode
if(!SetConsoleMode(stdoutHandle, outModeInit) || !SetConsoleMode(stdinHandle, inModeInit)) {
exit(GetLastError());
}
}
#else
static struct termios orig_term;
static struct termios new_term;
void setupConsole(void) {
tcgetattr(STDIN_FILENO, &orig_term);
new_term = orig_term;
new_term.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &new_term);
}
void restoreConsole(void) {
// Reset colors
printf("\x1b[0m");
// Reset console mode
tcsetattr(STDIN_FILENO, TCSANOW, &orig_term);
}
#endif
void getCursorPosition(int *row, int *col) {
printf("\x1b[6n");
char buff[128];
int indx = 0;
for(;;) {
int cc = getchar();
buff[indx] = (char)cc;
indx++;
if(cc == 'R') {
buff[indx + 1] = '\0';
break;
}
}
sscanf(buff, "\x1b[%d;%dR", row, col);
fseek(stdin, 0, SEEK_END);
}