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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
#pragma once
#include <stdio.h>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
# include <windows.h>
#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__) || defined(__linux__)
# ifndef __unix__
# define __unix__
# endif
# include <sys/ioctl.h>
# include <termios.h>
# include <unistd.h>
#endif
#define ESC "\x1b"
#define BLACK 0
#define RED 1
#define GREEN 2
#define YELLOW 3
#define BLUE 4
#define MAGENTA 5
#define CYAN 6
#define WHITE 7
#define BLOCK_BLINK 1
#define BLOCK 2
#define UNDERLINE_BLINK 3
#define UNDERLINE 4
#define BAR_BLINK 5
#define BAR 6
#define TRUE 1
#define FALSE 0
struct termsize {
int cols;
int rows;
};
static int bg_color = BLACK,
font_color = WHITE,
font_bold = FALSE;
static void wait() {
while (fgetc(stdin) != '\n');
}
static void clrscr() {
printf(ESC "[2J" ESC "[?6h");
}
static void gotoxy(int x, int y) {
printf(ESC "[%d;%dH", y, x);
}
static void setfontcolor(int color) {
printf(ESC "[3%dm", color);
font_color = color;
}
static void setbgrcolor(int color) {
printf(ESC "[4%dm", color);
bg_color = color;
}
static void setfontbold(int status) {
printf(ESC "[%dm", status);
font_bold = status;
setfontcolor(font_color);
setbgrcolor(bg_color);
}
static void setunderline(int status) {
if (status) status = 4;
printf(ESC "[%dm", status);
setfontcolor(font_color);
setbgrcolor(bg_color);
setfontbold(font_bold);
}
static void setblink(int status) {
if (status) status = 5;
printf(ESC "[%dm", status);
setfontcolor(font_color);
setbgrcolor(bg_color);
setfontbold(font_bold);
}
static void settitle(char const* title) {
printf(ESC"]0;%s\x7", title);
}
static void setcurshape(int shape) {
// vt520/xterm-style; linux terminal uses ESC[?1;2;3c, not implemented
printf(ESC "[%d q", shape);
}
static struct termsize gettermsize() {
struct termsize size;
#ifdef _WIN32
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
size.cols = csbi.srWindow.Right - csbi.srWindow.Left + 1;
size.rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
#elif defined(__unix__)
struct winsize win;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &win);
size.cols = win.ws_col;
size.rows = win.ws_row;
#else
size.cols = 0;
size.rows = 0;
#endif
return size;
}
static int getch() {
#ifdef _WIN32
HANDLE input = GetStdHandle(STD_INPUT_HANDLE);
if (input == NULL) return EOF;
DWORD oldmode;
GetConsoleMode(input, &oldmode);
DWORD newmode = oldmode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
SetConsoleMode(input, newmode);
#elif defined(__unix__)
struct termios oldattr, newattr;
tcgetattr(STDIN_FILENO, &oldattr);
newattr = oldattr;
newattr.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newattr);
#endif
int ch = getc(stdin);
#ifdef _WIN32
SetConsoleMode(input, oldmode);
#elif defined(__unix__)
tcsetattr(STDIN_FILENO, TCSANOW, &oldattr);
#endif
return ch;
}
static int getche() {
#ifdef _WIN32
HANDLE input = GetStdHandle(STD_INPUT_HANDLE);
if (input == NULL) return EOF;
DWORD oldmode;
GetConsoleMode(input, &oldmode);
DWORD newmode = oldmode & ~ENABLE_LINE_INPUT;
SetConsoleMode(input, newmode);
#elif defined(__unix__)
struct termios oldattr, newattr;
tcgetattr(STDIN_FILENO, &oldattr);
newattr = oldattr;
newattr.c_lflag &= ~ICANON;
tcsetattr(STDIN_FILENO, TCSANOW, &newattr);
#endif
int ch = getc(stdin);
#ifdef _WIN32
SetConsoleMode(input, oldmode);
#elif defined(__unix__)
tcsetattr(STDIN_FILENO, TCSANOW, &oldattr);
#endif
return ch;
}
static void clrline() {
printf(ESC "[2K" ESC "E");
}
static void resetcolors() {
printf(ESC "001b" ESC "[0m");
}
| |