nocurses/nocurses.h

199 lines
4.0 KiB
C
Raw Normal View History

2019-10-18 03:38:07 +03:00
/*
nocurses.h - Provides a basic 'VT100 ESC sequences' printing without
the need to use ncurses.
This is inspired by Borland conio.h
Author - Rahul M. Juliato
Original - 25 jun 2005
2019-10-22 06:44:20 +03:00
Revision - oct 2019
2019-10-18 03:38:07 +03:00
*/
#include <stdio.h>
#ifdef _WIN32
# 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"
2019-10-18 03:38:07 +03:00
#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
2019-10-18 03:38:07 +03:00
#define TRUE 1
#define FALSE 0
struct termsize {
int cols;
int rows;
};
2019-10-18 03:38:07 +03:00
static int bg_color = BLACK,
font_color = WHITE,
font_bold = FALSE;
2019-10-18 03:38:07 +03:00
static void wait(){
while (fgetc(stdin) != '\n');
2019-10-18 03:38:07 +03:00
}
static void clrscr(){
printf(ESC"[2J"ESC"[?6h");
2019-10-18 03:38:07 +03:00
}
static void gotoxy(int x, int y){
printf(ESC"[%d;%dH", y, x);
2019-10-18 03:38:07 +03:00
}
static void setfontcolor(int color){
printf(ESC"[3%dm", color);
2019-10-22 06:44:20 +03:00
font_color = color;
2019-10-18 03:38:07 +03:00
}
static void setbgrcolor(int color){
printf(ESC"[4%dm", color);
2019-10-22 06:44:20 +03:00
bg_color = color;
2019-10-18 03:38:07 +03:00
}
static void setfontbold(int status){
printf(ESC"[%dm", status);
2019-10-22 06:44:20 +03:00
font_bold = status;
setfontcolor(font_color);
setbgrcolor(bg_color);
2019-10-18 03:38:07 +03:00
}
static void setunderline(int status){
2019-10-18 03:38:07 +03:00
if (status) status = 4;
printf(ESC"[%dm", status);
2019-10-22 06:44:20 +03:00
setfontcolor(font_color);
setbgrcolor(bg_color);
setfontbold(font_bold);
2019-10-18 03:38:07 +03:00
}
static void setblink(int status){
2019-10-18 03:38:07 +03:00
if (status) status = 5;
printf(ESC"[%dm", status);
2019-10-22 06:44:20 +03:00
setfontcolor(font_color);
setbgrcolor(bg_color);
setfontbold(font_bold);
2019-10-18 03:38:07 +03:00
}
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 (h == 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 (h == 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");
2019-10-18 03:38:07 +03:00
}
2019-10-18 03:44:02 +03:00
static void resetcolors(){
printf(ESC"001b"ESC"[0m");
2019-10-22 06:44:20 +03:00
}