diff --git a/Include/Iony/Core/Console.h b/Include/Iony/Core/Console.h index d3c8349..03d6c73 100644 --- a/Include/Iony/Core/Console.h +++ b/Include/Iony/Core/Console.h @@ -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 diff --git a/Src/Core/Console.c b/Src/Core/Console.c index 8fd8637..81ba099 100644 --- a/Src/Core/Console.c +++ b/Src/Core/Console.c @@ -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; +} diff --git a/Tests/Main.c b/Tests/Main.c index d66adbc..4d003ac 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -55,5 +55,9 @@ int main(void) { return -1; } + if (iony_console_reset_color() != IONY_ERROR_NONE) { + return -1; + } + return 0; } \ No newline at end of file