feat(console coloring): added reset color for console

This commit is contained in:
Mineplay 2025-10-18 10:00:59 -05:00
parent 65ea3ab572
commit 20db36cd82
3 changed files with 51 additions and 4 deletions

View file

@ -47,8 +47,9 @@ IonyError iony_console_print(const char *text, long length);
IonyError iony_console_clear(void);
IonyError iony_console_get_character(char *character);
IonyError iony_console_get_line(char *character_string, long max_length, long *characters_read);
IonyError iony_console_get_line(char *character_string, long max_length, unsigned long *characters_read);
IonyError iony_console_set_color(IonyConsoleColor color);
IonyError iony_console_reset_color(void);
#endif

View file

@ -27,9 +27,12 @@
static HANDLE iony_standard_console_out = 0;
static HANDLE iony_standard_console_in = 0;
static WORD iony_original_console_attributes = 0;
#elif defined(__GNUC__)
#include "../../Include/Iony/Utils/System.h"
static const char IONY_RESET_COLOR[] = "\033[0m";
static const char *IONY_ANSI_COLORS[] = {
"\x1B[0;30m",
"\x1B[0;34m",
@ -46,7 +49,7 @@ static const char *IONY_ANSI_COLORS[] = {
"\x1B[1;31m",
"\x1B[1;35m",
"\x1B[1;33m",
"\x1B[1;37m"
"\x1B[1;37m",
};
#endif
@ -143,7 +146,7 @@ IonyError iony_console_get_character(char *character) {
return IONY_ERROR_NONE;
}
IonyError iony_console_get_line(char *character_string, const long max_length, long *characters_read) {
IonyError iony_console_get_line(char *character_string, const long max_length, unsigned long *characters_read) {
if (character_string == 0) {
return IONY_ERROR_INVALID_POINTER;
}
@ -172,7 +175,7 @@ IonyError iony_console_get_line(char *character_string, const long max_length, l
}
IonyError iony_console_set_color(IonyConsoleColor color) {
#if defined(_WIN32)
#if defined(_WIN32)
if (iony_standard_console_out == 0) {
iony_standard_console_out = GetStdHandle(STD_OUTPUT_HANDLE);
if (iony_standard_console_out == INVALID_HANDLE_VALUE) {
@ -180,6 +183,15 @@ IonyError iony_console_set_color(IonyConsoleColor color) {
}
}
if (iony_original_console_attributes == 0) {
CONSOLE_SCREEN_BUFFER_INFO console_info;
if (!GetConsoleScreenBufferInfo(iony_standard_console_out, &console_info)) {
return IONY_ERROR_SYSTEM_CALL_FAILED;
}
iony_original_console_attributes = console_info.wAttributes;
}
if (SetConsoleTextAttribute(iony_standard_console_out, color) == 0) {
return IONY_ERROR_SYSTEM_CALL_FAILED;
}
@ -191,3 +203,33 @@ IonyError iony_console_set_color(IonyConsoleColor color) {
return IONY_ERROR_NONE;
}
IonyError iony_console_reset_color(void) {
#if defined(_WIN32)
if (iony_standard_console_out == 0) {
iony_standard_console_out = GetStdHandle(STD_OUTPUT_HANDLE);
if (iony_standard_console_out == INVALID_HANDLE_VALUE) {
return IONY_ERROR_HANDLE_NOT_FOUND;
}
}
if (iony_original_console_attributes == 0) {
CONSOLE_SCREEN_BUFFER_INFO console_info;
if (!GetConsoleScreenBufferInfo(iony_standard_console_out, &console_info)) {
return IONY_ERROR_SYSTEM_CALL_FAILED;
}
iony_original_console_attributes = console_info.wAttributes;
}
if (SetConsoleTextAttribute(iony_standard_console_out, iony_original_console_attributes) == 0) {
return IONY_ERROR_SYSTEM_CALL_FAILED;
}
#elif defined(__GNUC__)
if (iony_system_call(IONY_SYS_WRITE, 1, (long)IONY_RESET_COLOR, sizeof(IONY_RESET_COLOR), 0, 0, 0) < 0) {
return IONY_ERROR_SYSTEM_CALL_FAILED;
}
#endif
return IONY_ERROR_NONE;
}

View file

@ -55,5 +55,9 @@ int main(void) {
return -1;
}
if (iony_console_reset_color() != IONY_ERROR_NONE) {
return -1;
}
return 0;
}