conio_lt 0.2.0
A lite version of the 'conio.h' library for Unix-like systems.
Loading...
Searching...
No Matches
conio_lt.h
Go to the documentation of this file.
1/*======================================================================
2 * conio_lt, a lightweight version of <conio.h> library for Unix-like systems.
3 * Copyright (C) 2023-2024 Ryuu Mitsuki
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 *======================================================================*/
18
48#ifndef CONIO_LT_H_
49#define CONIO_LT_H_
50
51
52#include <stdio.h>
53#include <stdint.h>
54#include <unistd.h>
55#include <fcntl.h>
56#include <termios.h>
57
58typedef unsigned int cpos_t;
62static const char* __prefix = "\033[";
80static const int __getch(uint8_t __echo) {
81 struct termios __oldt, __newt;
82 int __c;
83
84 tcgetattr(STDIN_FILENO, &__oldt);
85 __newt = __oldt;
86 __newt.c_lflag &= ~ICANON;
87
88 if (__echo != 0) { /* No echoing */
89 __newt.c_lflag &= ECHO;
90 } else { /* Echoing */
91 __newt.c_lflag &= ~ECHO;
92 }
93
94 tcsetattr(STDIN_FILENO, TCSANOW, &__newt);
95 __c = getchar(); /* Retrieve the character */
96 tcsetattr(STDIN_FILENO, TCSANOW, &__oldt);
97
98 return __c;
99}
100
113static void __whereis_xy(cpos_t* __x, cpos_t* __y) {
114 int in;
115 cpos_t x = 0, y = 0;
116 printf("%s6n", __prefix);
117
118 /* If the character does not same as '\x1b' nor '\x5b',
119 * immediately return and leaving the __x and __y references unchanged
120 */
121 if (__getch(0) != '\x1B') {
122 return;
123 } else if (__getch(0) != '\x5B') {
124 return;
125 }
126
127 while ((in = __getch(0)) != ';') {
128 y = y * 10 + in - '0';
129 }
130
131 while ((in = __getch(0)) != 'R') {
132 x = x * 10 + in - '0';
133 }
134
135 /* Store and assign the cursor position */
136 *__x = x;
137 *__y = y;
138}
139
140
141
153void gotoxy(const cpos_t __x, const cpos_t __y) {
154 printf("%s%u;%uf", __prefix, __y, __x); /* "\033[{y};{x}f" */
155}
156
180void clrscr(void) {
181 printf("%s0m%s1J%sH", __prefix, __prefix, __prefix);
182}
183
210void rstscr(void) {
211 printf("%s0m\033c", __prefix); /* "\033[0m\033c" */
212}
213
228const int ungetch(const int __c) {
229 return ungetc(__c, stdin);
230}
231
243const int getch(void) {
244 return __getch(0); /* 0 means no echo */
245}
246
258const int getche(void) {
259 return __getch(1); /* non-zero means with echo */
260}
261
274const cpos_t wherex(void) {
275 cpos_t __x = 0, __y = 0;
276 __whereis_xy(&__x, &__y);
277
278 return __x; /* only return the X-coordinate */
279}
280
293const cpos_t wherey(void) {
294 cpos_t __x = 0, __y = 0;
295 __whereis_xy(&__x, &__y);
296
297 return __y; /* only return the Y-coordinate */
298}
299
314void wherexy(cpos_t* __x, cpos_t* __y) {
315 __whereis_xy(__x, __y);
316}
317
330const int putch(const int __chr) {
331 printf("%c", __chr);
332 return __chr;
333}
334
349void gotox(const cpos_t __x) {
350 gotoxy(__x, wherey());
351}
352
367void gotoy(const cpos_t __y) {
368 gotoxy(wherex(), __y);
369}
370
371#endif /* CONIO_LT_H_ */
void wherexy(cpos_t *__x, cpos_t *__y)
Retrieves the current X and Y coordinates of the cursor on the terminal screen.
Definition conio_lt.h:314
void clrscr(void)
Clears the terminal screen.
Definition conio_lt.h:180
const cpos_t wherey(void)
Retrieves the current Y-coordinate of the cursor on the terminal screen.
Definition conio_lt.h:293
void rstscr(void)
Resets and clears the terminal screen.
Definition conio_lt.h:210
const cpos_t wherex(void)
Retrieves the current X-coordinate of the cursor on the terminal screen.
Definition conio_lt.h:274
unsigned int cpos_t
An abbreviation from Cursor Position Type.
Definition conio_lt.h:58
const int putch(const int __chr)
Writes a character to the standard output.
Definition conio_lt.h:330
const int getch(void)
Reads a single character from the standard input without echoing it.
Definition conio_lt.h:243
void gotox(const cpos_t __x)
Sets the cursor position to the specified X-coordinate, maintaining the current Y-coordinate.
Definition conio_lt.h:349
void gotoy(const cpos_t __y)
Sets the cursor position to the specified Y-coordinate, maintaining the current X-coordinate.
Definition conio_lt.h:367
const int getche(void)
Reads a single character from the standard input and then echoing it.
Definition conio_lt.h:258
const int ungetch(const int __c)
Pushes a character back onto the input stream.
Definition conio_lt.h:228
void gotoxy(const cpos_t __x, const cpos_t __y)
Moves the cursor to the specified coordinates on the terminal screen.
Definition conio_lt.h:153