Add settitle, setcurshape, gettermsize, getch and getche (#3)

* added Makefile, make functions static

* added settitle, setcurshape, gettermsize

* added getch and getche
This commit is contained in:
Koxiaet 2019-10-23 18:41:19 +01:00 committed by Rahul M. Juliato
parent 1dd26dafd9
commit 145091f801
6 changed files with 220 additions and 44 deletions

BIN
.DS_Store vendored

Binary file not shown.

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.DS_Store
demo

16
Makefile Normal file
View File

@ -0,0 +1,16 @@
CC::=cc
PREFIX::=/usr/local
all:
install: nocurses.h
install -CD nocurses.h ${PREFIX}/include/nocurses.h
uninstall:
rm ${PREFIX}/include/nocurses.h
demo: demo.c nocurses.h
${CC} -pthread -lm -o demo demo.c
clean:
rm demo

View File

@ -10,18 +10,23 @@ Here's a demo (demo.c file avaiable in the same repo):
![nocurses.h](img/nocurses.gif)
## Functions Provided
| Function | Description | Example |
|--------------------------|:--------------------------------------------------------------------------------------:|:--------------------:|
| pause() | Waits for the user to hit [ENTER]. | pause(); |
| clrscr() | Clears the screen. | clrscr(); |
| gotoxy(x, y) | Sets the cursor do the position x, y. Where x is the row number and y the line number. | gotoxy(10,25); |
| setfontcolor(color_name) | Sets the text color to one of the colors described on the color table below. | setfontcolor(RED); |
| setbgrcolor(color_name) | Sets the background color to one of the colors described on the color table below. | setbgrcolor(BLUE); |
| setfontbold(status) | Sets the bold attribute on or off. status can be TRUE or FALSE. | setfontbold(TRUE); |
| setunderline(status) | Sets the underline attribute on or off. status can be TRUE or FALSE. | setunderline(FALSE); |
| setblink(status) | Sets the blink attribute on or off. status can be TRUE or FALSE. | setblink(TRUE); |
| clrline() | Clears the row contents. | clrline(); |
| resetcolors() | Reset terminal to default colors. | clrline(); |
| Function | Description | Example |
|--------------------------|:--------------------------------------------------------------------------------------:|:------------------------:|
| wait() | Waits for the user to hit [ENTER]. | wait(); |
| clrscr() | Clears the screen. | clrscr(); |
| gotoxy(x, y) | Sets the cursor do the position x, y. Where x is the row number and y the line number. | gotoxy(10,25); |
| setfontcolor(color_name) | Sets the text color to one of the colors described on the color table below. | setfontcolor(RED); |
| setbgrcolor(color_name) | Sets the background color to one of the colors described on the color table below. | setbgrcolor(BLUE); |
| setfontbold(status) | Sets the bold attribute on or off. status can be TRUE or FALSE. | setfontbold(TRUE); |
| setunderline(status) | Sets the underline attribute on or off. status can be TRUE or FALSE. | setunderline(FALSE); |
| setblink(status) | Sets the blink attribute on or off. status can be TRUE or FALSE. | setblink(TRUE); |
| settitle(title) | Sets the title of the terminal. | settitle("Hello World"); |
| setcurshape(shape_name) | Sets the shape of the cursor in the terminal in the shape table below. | setcurshape(BAR); |
| gettermsize() | Gets the columns and rows of the terminal. | gettermsize(); |
| getch() | Gets a character without waiting for enter. | getch(); |
| getche() | Gets a character and echoes it without waiting for enter. | getche(); |
| clrline() | Clears the row contents. | clrline(); |
| resetcolors() | Reset terminal to default colors. | clrline(); |
## color_name
@ -39,4 +44,16 @@ Valid color names are:
| CYAN |
| WHITE |
## shape_name
Valid shape names are:
| shape_name |
|:---------------:|
| BLOCK_BLINK |
| BLOCK |
| UNDERLINE_BLINK |
| UNDERLINE |
| BAR_BLINK |
| BAR |

45
demo.c
View File

@ -3,7 +3,7 @@
nocurses.h - nocurses library demo
Compile and run with: $gcc -pthread -lm -o demo demo.c && ./demo
Compile and run with: $ make demo && ./demo
*/
@ -15,7 +15,7 @@ int i;
int waiting(){
printf("\n\nHit ENTER to continue...");
pause();
wait();
clrscr();
return 0;
}
@ -56,21 +56,22 @@ int main(void) {
setfontcolor(WHITE);
printf("\n\n\nHello! Welcome to nocurses.h demo!\nLet me present you its features... \n");
waiting();
printf("The nocurses.h provides these set of functions:\n\n\
pause()\n\
wait()\n\
clrscr()\n\
setfontcolor(COLOR_NAME)\n\
setbgrcolor(COLOR_NAME)\n\
gotoxy(XPOS,YPOS)\n\
setfontbold(STATE)\n\
setunderline(STATE)\n\
setblink(STATE)\n");
setblink(STATE)\n\
settitle(TITLE)\n\
gettermsize()\n");
waiting();
printf("I am using the pause() function to wait for your\ncommand.");
printf("I am using the wait() function to wait for your\ncommand.");
waiting();
printf("See it? The screen was cleared with the clrscr() funciton.");
@ -105,10 +106,24 @@ setblink(STATE)\n");
resetcolors();
clrscr();
printf("Now I used resetcolors()");
waiting();
struct termsize size = gettermsize();
printf("I can detect your terminal size.\n\nThis terminal is %d columns and %d rows, so one line should look like this:\n\n", size.cols, size.rows);
for (int i=0;i<size.cols;i++) {
printf("=");
}
waiting();
printf("I have getch() and getche()! Here is getch() until you press q:\n\nPress q to exit:");
while (getch() != 'q');
clrscr();
printf("Here is getche() until you press q:\n\nPress q to exit:");
while (getche() != 'q');
clrscr();
printf("Now some fancy stuff. \n\nThis may or may not work in your terminal emulator,\n so I am going to TRY stuff, ok?\n\n");
waiting();
@ -140,6 +155,22 @@ setblink(STATE)\n");
printf("Blinking cursor is set to FALSE --> setblink(FALSE)\n\n");
waiting();
settitle("nocurses demo");
printf("Title is set to \"nocurses demo\" --> settitle(\"nocurses demo\")\n\n");
waiting();
setcurshape(BLOCK);
printf("Cursor is set to BLOCK --> setcurshape(BLOCK)\n\n");
waiting();
setcurshape(UNDERLINE);
printf("Cursor is set to UNDERLINE --> setcurshape(UNDERLINE)\n\n");
waiting();
setcurshape(BAR);
printf("Cursor is set to BAR --> setcurshape(BAR)\n\n");
waiting();
printf("\n\nThat's all! Simple nocurses.h\n\nContribute with it on https://github.com/LionyxML/nocurses/\n\n");
waiting();

View File

@ -9,8 +9,19 @@
*/
#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"
#define ESC 27
#define BLACK 0
#define RED 1
#define GREEN 2
@ -20,69 +31,168 @@
#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
int bg_color = BLACK,
font_color = WHITE,
font_bold = FALSE;
struct termsize {
int cols;
int rows;
};
void pause(){
fgetc(stdin);
static int bg_color = BLACK,
font_color = WHITE,
font_bold = FALSE;
static void wait(){
while (fgetc(stdin) != '\n');
}
void clrscr(){
printf("%c[2J%c[?6h", ESC, ESC);
static void clrscr(){
printf(ESC"[2J"ESC"[?6h");
}
void gotoxy(int x, int y){
printf("%c[%d;%dH", ESC, y, x);
static void gotoxy(int x, int y){
printf(ESC"[%d;%dH", y, x);
}
void setfontcolor(int color){
printf("%c[3%dm", ESC, color);
static void setfontcolor(int color){
printf(ESC"[3%dm", color);
font_color = color;
}
void setbgrcolor(int color){
printf("%c[4%dm", ESC, color);
static void setbgrcolor(int color){
printf(ESC"[4%dm", color);
bg_color = color;
}
void setfontbold(int status){
printf("%c[%dm", ESC, status);
static void setfontbold(int status){
printf(ESC"[%dm", status);
font_bold = status;
setfontcolor(font_color);
setbgrcolor(bg_color);
}
void setunderline(int status){
static void setunderline(int status){
if (status) status = 4;
printf("%c[%dm", ESC, status);
printf(ESC"[%dm", status);
setfontcolor(font_color);
setbgrcolor(bg_color);
setfontbold(font_bold);
}
void setblink(int status){
static void setblink(int status){
if (status) status = 5;
printf("%c[%dm", ESC, status);
printf(ESC"[%dm", status);
setfontcolor(font_color);
setbgrcolor(bg_color);
setfontbold(font_bold);
}
void clrline(){
printf("%c[2K%cE", ESC, ESC);
static void settitle(char const* title) {
printf(ESC"]0;%s\x7", title);
}
void resetcolors(){
printf("%c001b%c[0m", ESC, ESC);
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");
}
static void resetcolors(){
printf(ESC"001b"ESC"[0m");
}