conio_lt 0.2.0
A lite version of the 'conio.h' library for Unix-like systems.
Loading...
Searching...
No Matches
Typedefs | Functions
conio_lt.h File Reference

conio_lt library is a lightweight adaptation of the <conio.h> library designed for Unix-like systems. More...

#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>

Go to the source code of this file.

Typedefs

typedef unsigned int cpos_t
 An abbreviation from Cursor Position Type.
 

Functions

void gotoxy (const cpos_t __x, const cpos_t __y)
 Moves the cursor to the specified coordinates on the terminal screen.
 
void clrscr (void)
 Clears the terminal screen.
 
void rstscr (void)
 Resets and clears the terminal screen.
 
const int ungetch (const int __c)
 Pushes a character back onto the input stream.
 
const int getch (void)
 Reads a single character from the standard input without echoing it.
 
const int getche (void)
 Reads a single character from the standard input and then echoing it.
 
const cpos_t wherex (void)
 Retrieves the current X-coordinate of the cursor on the terminal screen.
 
const cpos_t wherey (void)
 Retrieves the current Y-coordinate of the cursor on the terminal screen.
 
void wherexy (cpos_t *__x, cpos_t *__y)
 Retrieves the current X and Y coordinates of the cursor on the terminal screen.
 
const int putch (const int __chr)
 Writes a character to the standard output.
 
void gotox (const cpos_t __x)
 Sets the cursor position to the specified X-coordinate, maintaining the current Y-coordinate.
 
void gotoy (const cpos_t __y)
 Sets the cursor position to the specified Y-coordinate, maintaining the current X-coordinate.
 

Detailed Description

conio_lt library is a lightweight adaptation of the <conio.h> library designed for Unix-like systems.

This project aims to bring these functionalities to Unix-like systems and Borland C++, especially for legacy version of Borland C++.

Available APIs

Author
Ryuu Mitsuki
Version
0.2.0
Date
17 Jan 2024

Typedef Documentation

◆ cpos_t

typedef unsigned int cpos_t

An abbreviation from Cursor Position Type.

Custom type definition to represent the cursor position.

Function Documentation

◆ clrscr()

void clrscr ( void )

Clears the terminal screen.

This function clears the terminal screen by sending control sequences to the standard output using printf.

The function uses the following control sequences.

Control sequence Description
"\033[0m" Resets any text formatting or color attributes.
"\033[1J" Clears the screen from the cursor position to the end of the screen.
"\033[H" Moves the cursor to the top-left corner of the screen.

By combining these control sequences in a single printf statement, the function achieves the effect of clearing the terminal screen.

Note
This function does not prevent the screen from scrolling. If you want to reset entire the screen, use the rstscr(void) instead.
Since
0.1.0
See also
rstscr(void)
180 {
181 printf("%s0m%s1J%sH", __prefix, __prefix, __prefix);
182}

◆ getch()

const int getch ( void )

Reads a single character from the standard input without echoing it.

This function reads a single character from the standard input without echoing it.

Returns
Returns the character read from the standard input.
Since
0.1.0
See also
getche(void)
ungetch(int)
243 {
244 return __getch(0); /* 0 means no echo */
245}

◆ getche()

const int getche ( void )

Reads a single character from the standard input and then echoing it.

This function reads a single character from the standard input and then echoing it.

Returns
Returns the character read from the standard input.
Since
0.1.0
See also
getch(void)
ungetch(int)
258 {
259 return __getch(1); /* non-zero means with echo */
260}

◆ gotox()

void gotox ( const cpos_t __x)

Sets the cursor position to the specified X-coordinate, maintaining the current Y-coordinate.

This function serves as an alias for gotoxy(x, wherey()). It allows for flexible cursor manipulation by allowing the user to set the X-coordinate while keeping the current Y-coordinate unchanged.

Parameters
__xThe desired X-coordinate to set the cursor to.
Since
0.2.0
See also
gotoy(cpos_t)
gotoxy(cpos_t, cpos_t)
349 {
350 gotoxy(__x, wherey());
351}
const cpos_t wherey(void)
Retrieves the current Y-coordinate of the cursor on the terminal screen.
Definition conio_lt.h:293
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

◆ gotoxy()

void gotoxy ( const cpos_t __x,
const cpos_t __y )

Moves the cursor to the specified coordinates on the terminal screen.

This function moves the cursor on the terminal screen to the specified coordinates. It takes two integer parameters, __x and __y, representing the X and Y coordinates respectively.

Parameters
__xThe X-coordinate to move the cursor to.
__yThe Y-coordinate to move the cursor to.
Since
0.1.0
153 {
154 printf("%s%u;%uf", __prefix, __y, __x); /* "\033[{y};{x}f" */
155}

◆ gotoy()

void gotoy ( const cpos_t __y)

Sets the cursor position to the specified Y-coordinate, maintaining the current X-coordinate.

This function serves as an alias for gotoxy(wherex(), y). It provides flexibility in cursor positioning by allowing the user to set the Y-coordinate while keeping the current X-coordinate unchanged.

Parameters
__yThe desired Y-coordinate to set the cursor to.
Since
0.2.0
See also
gotox(cpos_t)
gotoxy(cpos_t, cpos_t)
367 {
368 gotoxy(wherex(), __y);
369}
const cpos_t wherex(void)
Retrieves the current X-coordinate of the cursor on the terminal screen.
Definition conio_lt.h:274

◆ putch()

const int putch ( const int __chr)

Writes a character to the standard output.

This function writes a character to the standard output. It takes an integer parameter __chr, representing the character to be written.

Parameters
__chrThe character to be written.
Returns
Returns the written character as an integer.
Since
0.1.0
330 {
331 printf("%c", __chr);
332 return __chr;
333}

◆ rstscr()

void rstscr ( void )

Resets and clears the terminal screen.

This function resets any text formatting or color attributes, clears the entire terminal screen, and moves the cursor to the top-left corner. It achieves this effect by sending the appropriate control sequences to the standard output using printf.

The function uses the following control sequences.

Control sequence Description
"\033[0m" Resets any text formatting or color attributes.
"\033c" Resets and clears the entire terminal screen.

By combining these control sequences in a single printf statement, the function achieves the effect of resetting and clearing the terminal screen.

Note
This function prevents the screen from scrolling by clearing the entire screen. If you only want to clear the screen without preventing scrolling, consider using the clrscr(void) function.
Since
0.2.0
See also
clrscr(void)
210 {
211 printf("%s0m\033c", __prefix); /* "\033[0m\033c" */
212}

◆ ungetch()

const int ungetch ( const int __c)

Pushes a character back onto the input stream.

This function pushes a character back onto the input stream. It takes an integer parameter __c, representing the character to be pushed back.

Parameters
__cThe character to be pushed back.
Returns
Returns the pushed-back character on success, or EOF on failure.
Since
0.1.0
See also
getch(void)
getche(void)
228 {
229 return ungetc(__c, stdin);
230}

◆ wherex()

const cpos_t wherex ( void )

Retrieves the current X-coordinate of the cursor on the terminal screen.

This function retrieves the current X-coordinate of the cursor on the terminal screen. The returned coordinate value always positive value.

Returns
Returns the X-coordinate of the cursor.
Since
0.1.0
See also
wherey(void)
wherexy(cpos_t*, cpos_t*)
274 {
275 cpos_t __x = 0, __y = 0;
276 __whereis_xy(&__x, &__y);
277
278 return __x; /* only return the X-coordinate */
279}
unsigned int cpos_t
An abbreviation from Cursor Position Type.
Definition conio_lt.h:58

◆ wherexy()

void wherexy ( cpos_t * __x,
cpos_t * __y )

Retrieves the current X and Y coordinates of the cursor on the terminal screen.

This function stores the current X-coordinate in the variable pointed to by __x, and the Y-coordinate in the variable pointed to by __y.

To use this function, provide the addresses of variables for X and Y to store the coordinates.

Parameters
[in,out]__xPointer to the variable where the X-coordinate will be stored.
[in,out]__yPointer to the variable where the Y-coordinate will be stored.
Since
0.2.0.
314 {
315 __whereis_xy(__x, __y);
316}

◆ wherey()

const cpos_t wherey ( void )

Retrieves the current Y-coordinate of the cursor on the terminal screen.

This function retrieves the current Y-coordinate of the cursor on the terminal screen. The returned coordinate value always positive value.

Returns
Returns the Y-coordinate of the cursor.
Since
0.1.0
See also
wherex(void)
wherexy(cpos_t*, cpos_t*)
293 {
294 cpos_t __x = 0, __y = 0;
295 __whereis_xy(&__x, &__y);
296
297 return __y; /* only return the Y-coordinate */
298}