diff --git a/Include/Iony/Core/Console.h b/Include/Iony/Core/Console.h index a52ae21..d3c8349 100644 --- a/Include/Iony/Core/Console.h +++ b/Include/Iony/Core/Console.h @@ -24,10 +24,31 @@ #include "../Utils/Error.h" +typedef enum { + IONY_CONSOLE_COLOR_BLACK = 0, + IONY_CONSOLE_COLOR_BLUE = 1, + IONY_CONSOLE_COLOR_GREEN = 2, + IONY_CONSOLE_COLOR_CYAN = 3, + IONY_CONSOLE_COLOR_RED = 4, + IONY_CONSOLE_COLOR_MAGENTA = 5, + IONY_CONSOLE_COLOR_BROWN = 6, + IONY_CONSOLE_COLOR_LIGHT_GRAY = 7, + IONY_CONSOLE_COLOR_DARK_GRAY = 8, + IONY_CONSOLE_COLOR_LIGHT_BLUE = 9, + IONY_CONSOLE_COLOR_LIGHT_GREEN = 10, + IONY_CONSOLE_COLOR_LIGHT_CYAN = 11, + IONY_CONSOLE_COLOR_LIGHT_RED = 12, + IONY_CONSOLE_COLOR_LIGHT_MAGENTA = 13, + IONY_CONSOLE_COLOR_YELLOW = 14, + IONY_CONSOLE_COLOR_WHITE = 15 +} IonyConsoleColor; + 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_set_color(IonyConsoleColor color); + #endif diff --git a/Src/Core/Console.c b/Src/Core/Console.c index b2f2ce8..f34d57f 100644 --- a/Src/Core/Console.c +++ b/Src/Core/Console.c @@ -29,6 +29,25 @@ static HANDLE iony_standard_console_out = 0; static HANDLE iony_standard_console_in = 0; #elif defined(__GNUC__) #include "../../Include/Iony/Utils/System.h" + +static const char *IONY_ANSI_COLORS[] = { + "\x1B[0;30m", + "\x1B[0;34m", + "\x1B[0;32m", + "\x1B[0;36m", + "\x1B[0;31m", + "\x1B[0;35m", + "\x1B[0;33m", + "\x1B[0;37m", + "\x1B[1;30m", + "\x1B[1;34m", + "\x1B[1;32m", + "\x1B[1;36m", + "\x1B[1;31m", + "\x1B[1;35m", + "\x1B[1;33m", + "\x1B[1;37m" +}; #endif IonyError iony_console_print(const char *text, const long length) { @@ -146,3 +165,25 @@ IonyError iony_console_get_line(char *character_string, const long max_length, l return IONY_ERROR_NONE; } + +IonyError iony_console_set_color(IonyConsoleColor color) { + #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; + } + } + + DWORD characters_written; + if (SetConsoleTextAttribute(iony_standard_console_out, color) == 0) { + return IONY_ERROR_SYSTEM_CALL_FAILED; + } + #elif defined(__GNUC__) + if (iony_system_call(IONY_SYS_WRITE, 1, (long)IONY_ANSI_COLORS[color], sizeof(IONY_ANSI_COLORS[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 e4ea0fc..d66adbc 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -46,6 +46,7 @@ int main(void) { return -1; } + iony_console_set_color(IONY_CONSOLE_COLOR_LIGHT_RED); if (iony_console_print("Your name is ", 14) != IONY_ERROR_NONE) { return -1; }