128 for(
int i = 0; i < strlen(text); i++) {
137 char* substr =
substring(text, i, offset), *endptr;
141 n = strtol(substr, &endptr, 10);
146 int semicolonCount =
count(substr,
';', 0);
148 if(semicolonCount == 3) {
151 if(semicolonOffset != 1)
155 bool fg = strcmp(
"fg", str) == 0;
160 for(
int j = 0; j <= 2; j++) {
164 char* v =
substring(text, i, semicolonOffset), *endptr_;
165 int n = strtol(v, &endptr_, 10);
167 if(errno == ERANGE || *endptr_ !=
'\0' && endptr_ == NULL)
177 i += j == 2 ? semicolonOffset : semicolonOffset + 1;
185 }
else if((n >= 30 && n <= 37) || (n >= 90 && n <= 97) || (n >= 40 && n <= 47) || (n >= 100 && n <= 107)) {
186 if((n >= 30 && n <= 37) || (n >= 90 && n <= 97)) {
189 }
else if((n >= 40 && n <= 47) || (n >= 100 && n <= 107)) {
193 }
else if(strcmp(substr,
"b") == 0) {
196 }
else if(strcmp(substr,
"i") == 0) {
199 }
else if(strcmp(substr,
"u") == 0) {
202 }
else if(strcmp(substr,
"du") == 0) {
205 }
else if(strcmp(substr,
"bl") == 0) {
208 }
else if(strcmp(substr,
"r") == 0) {
215 printf(
"%c", text[i]);
224 va_start(args, text);
228 for(
int i = 0; i < strlen(text); i++) {
236 else if(text[i] ==
'd' || text[i] ==
'i' || text[i] ==
'u' || text[i] ==
'x' || text[i] ==
'X') {
237 int n = va_arg(args,
int), snLen =
nDigits(n) + 2;
238 char* sn = (
char*)malloc(snLen*
sizeof(
char));
242 snprintf(sn, snLen,
"%d", n);
244 for(
int j = 0; j < strlen(sn); j++)
248 }
else if(text[i] ==
'f' || text[i] ==
'F') {
249 double n = va_arg(args,
double);
254 char* sn = (
char*)malloc(snLen*
sizeof(
char));
258 snprintf(sn, snLen, format, n);
260 for(
int j = 0; j < strlen(sn); j++)
264 }
else if(text[i] ==
's') {
265 char* s = va_arg(args,
char*);
267 for(
int j = 0; j < strlen(s); j++)
270 }
else if(text[i] ==
'n')
286 tcgetattr(STDIN_FILENO, &term);
288 term.c_lflag &= ~(ICANON | ECHO);
289 tcsetattr(STDIN_FILENO, TCSANOW, &term);
292 int rs = read(STDIN_FILENO, &c, 1);
294 tcgetattr(STDIN_FILENO, &term);
296 term.c_lflag |= (ICANON | ECHO);
297 tcsetattr(STDIN_FILENO, TCSANOW, &term);
299 return rs == 1 ? c : -1;
316 if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1) {
324 CONSOLE_SCREEN_BUFFER_INFO csbi;
325 HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
327 if (GetConsoleScreenBufferInfo(hStdout, &csbi)) {
328 *width = csbi.dwSize.X;
329 *height = csbi.dwSize.Y;
332 CloseHandle(hStdout);
395 printf(
"\e[%d;%dH", n, m);
429 int _ = scanf(
"\e[%d;%dR", row, col);
431 CONSOLE_SCREEN_BUFFER_INFO csbi;
432 HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
434 if (GetConsoleScreenBufferInfo(hStdout, &csbi)) {
435 *col = csbi.dwCursorPosition.X + 1;
436 *row = csbi.dwCursorPosition.Y + 1;
439 CloseHandle(hStdout);
472 printf(
"\e[%dm", (
int)foregroundColor);
476 printf(
"\e[38;2;%d;%d;%dm", rgbColor.
r, rgbColor.
g, rgbColor.
b);
484 printf(
"\e[%dm", (
int)backgroundColor);
488 printf(
"\e[48;2;%d;%d;%dm", rgbColor.
r, rgbColor.
g, rgbColor.
b);
void underlined()
Toggles singled underlined text.
rgb buildRgb()
Builds a rgb struct.
void Pause(bool clear)
Waits for the user to press a key without waiting for 'Enter' to be pressed.
void cursorDown(int n)
Moves the cursor down by n rows.
void printgr(const char *text)
Prints a text with the graphic rendition specified with custom graphic rendition format specifiers.
void setColor(color color)
Sets both terminal's foreground color and background color, if specified.
void eraseInDisplay(int n)
Clears part of the screen.
void setForegroundColor(color color)
Sets the terminal's foreground color.
void setRGBBackgroundColor(rgb rgbColor)
Sets the RGB background color.
void deviceStatusReport(int *row, int *col)
Gets the cursor position.
void printGraphicRendition(char *text, graphicRendition rendition)
Prints a text with the graphic rendition specified with the struct. Standard C format specifiers are ...
void defaultBackgroundColor()
Resets the background color.
void eraseInLine(int n)
Erases part of the line.
void graphicReset()
Resets the graphic rendition.
void bold()
Toggles bold text.
ansiStandardColor buildAnsiStandardColor()
Builds a ANSI standard color struct.
ansiRgbColor buildAnsiRgbColor()
Builds a ANSI rgb color struct.
void defaultForegroundColor()
Resets the foreground color.
void cursorUp(int n)
Moves the cursor up by n rows.
void scrollDown(int n)
Scrolls the whole page down by n rows.
void doublyUnderlined()
Toggles doubly underlined text.
void cursorHorizontalAbsolute(int n)
Moves the cursor to column n.
void cursorPosition(int n, int m)
Moves the cursor at n-th row and at the m-th column.
void screenSize(int *width, int *height)
It returns the width and the height of the screen. On unix-like platforms, in case of errors,...
char getChar()
Gets the char pressed on the keyboard without waiting for 'Enter' to be pressed.
void setRGBForegroundColor(rgb rgbColor)
Sets the RGB foreground color.
void clearScreen()
Clears the entire terminal's window's content.
graphicRendition buildGraphicRendition()
Builds a graphic rendition struct.
void cursorBack(int n)
Moves the cursor to the left by n columns.
void italic()
Toggles italic text.
void setBackgroundColor(color color)
Sets the terminal's background color.
void cursorPreviousLine(int n)
Moves the cursor at the beginning of the line n rows up.
void setStandardForegroundColor(ansiStandardColors foregroundColor)
Sets the ANSI standard foreground color.
void scrollUp(int n)
Scrolls the whole page up by n rows.
void setStandardBackgroundColor(ansiStandardColors backgroundColor)
Sets the ANSI standard background color.
void slowBlinking()
Toggles slow blinking.
color buildColor()
Builds a color struct.
void printfgr(char *text,...)
Prints text as printgr but handles standard C format specifiers %, d, i, u, x, X, f,...
void cursorNextLine(int n)
Moves the cursor at the beginning of the line n rows down.
void cursorForward(int n)
Moves the cursor to the right by n columns.
Implemented following https://en.wikipedia.org/wiki/ANSI_escape_code.
#define NUMBER_DECIMALS_DIGITS
Defines how many decimals digits should use printfgr when printing floating-point numbers....
uint8_t ansiStandardColors
Defines the type ansiStandardColors as unsigned integer 1 byte large.
#define EXIT_ALLOC_FAILURE
Exit code when dynamic memory allocation fails.
#define NONE
Used in some structure for meaning that a field is unset, instead of using NULL.
Defines a basic structure for RGB foreground color and background color for a piece of printable text...
rgb bgRgb
Background RGB color field.
rgb fgRgb
Foreground RGB color field.
Defines a basic structure for standard ANSI foreground color and background color for a piece of prin...
ansiStandardColors fgColor
Foreground ANSI standard color field.
ansiStandardColors bgColor
Background ANSI standard color field.
Defines a struct for representing a generic color.
__color color
Color field.
bool isRgbColor
Boolean flag for determining if the color field is an ansiRgbColor or an ansiStandardColor.
Defines a struct for graphic rendition including properties for bold, italic, underlined and colored ...
bool bold
Boolean flag to determine if the text has to be bold.
color color
The color for the text and the its background.
underline underlined
Flag to determine if the text has to be underlined, singly or doubly.
bool italic
Boolean flag to determine if the text has to be italic.
Defines a basic structure for RGB colors.
uint8_t g
Green component field.
uint8_t b
Blue component field.
uint8_t r
Red component field.
data_type * data
Contiguous memory segment with in data is stored.
ansiRgbColor rgbColor
RGB color field.
ansiStandardColor stdColor
ANSI standard color field.
bool containsFrom(const char *string, char character, int startPosition)
Checks if there is an occoruncy of the passed char from the passed position in the passed string.
int count(const char *string, char character, int startPosition)
Counts the number of occoruncies in the string, from a passed position, of the passed char.
bool isDecimalDigit(char character)
Checks if a char is a decimal digit.
int offsetFromNext(const char *string, char character, int startPosition)
Counts the offset in the string between the position and the next occoruncy of the passed char.
int nDigits(int number)
Evalutates the number of digits of a number. The time complexity of this function is constant (O(1)).
char * substring(const char *string, int startPosition, int length)
Copies a substring (heap-allocated) from the passed string. If fails when allocating memory for the s...
void freeVector(vector *const vector)
Frees the memory used from the vector structure.
vector * buildVector()
Builds a vector structure initializing its fields. If vector's allocation or data's block allocation ...
void pushBack(vector *const vector, const data_type element)
Pushes an element at the back of the vector.