From dad7db99d8a91aa8c085e37b3ee3c263f0fc7ebd Mon Sep 17 00:00:00 2001 From: Mineplay Date: Sun, 19 Oct 2025 09:28:17 -0500 Subject: [PATCH] feat(console coloring): added printing error --- Include/Iony/Core/Console.h | 3 ++- Src/Core/Console.c | 35 +++++++++++++++++++++++++++++++---- Tests/Main.c | 16 ++++++++++++++-- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/Include/Iony/Core/Console.h b/Include/Iony/Core/Console.h index 03d6c73..9a0cc92 100644 --- a/Include/Iony/Core/Console.h +++ b/Include/Iony/Core/Console.h @@ -43,7 +43,8 @@ typedef enum { IONY_CONSOLE_COLOR_WHITE = 15 } IonyConsoleColor; -IonyError iony_console_print(const char *text, long length); +IonyError iony_console_print(const char *text, unsigned long length); +IonyError iony_console_error(const char *text, unsigned long length); IonyError iony_console_clear(void); IonyError iony_console_get_character(char *character); diff --git a/Src/Core/Console.c b/Src/Core/Console.c index b333647..55fdce5 100644 --- a/Src/Core/Console.c +++ b/Src/Core/Console.c @@ -22,16 +22,17 @@ #include "../../Include/Iony/Core/Console.h" #include "../../Include/Iony/Utils/System.h" +#include "Iony/Utils/Error.h" #if defined(_WIN32) static HANDLE iony_standard_console_out = 0; static HANDLE iony_standard_console_in = 0; +static HANDLE iony_standard_console_error = 0; static WORD iony_original_console_attributes = 0; #elif defined(__GNUC__) - static const char IONY_RESET_COLOR[] = "\033[0m"; -static const char *IONY_ANSI_COLORS[] = { +static const char IONY_ANSI_COLORS[][8] = { "\x1B[0;30m", "\x1B[0;34m", "\x1B[0;32m", @@ -51,7 +52,7 @@ static const char *IONY_ANSI_COLORS[] = { }; #endif -IonyError iony_console_print(const char *text, const long length) { +IonyError iony_console_print(const char *text, const unsigned long length) { if (text == 0) { return IONY_ERROR_INVALID_POINTER; } @@ -69,7 +70,33 @@ IonyError iony_console_print(const char *text, const long length) { return IONY_ERROR_SYSTEM_CALL_FAILED; } #elif defined(__GNUC__) - if (iony_system_call(IONY_SYS_WRITE, 1, (long)text, length, 0, 0, 0) < 0) { + if (iony_system_call(IONY_SYS_WRITE, 1, (long)text, (long)length, 0, 0, 0) < 0) { + return IONY_ERROR_SYSTEM_CALL_FAILED; + } + #endif + + return IONY_ERROR_NONE; +} + +IonyError iony_console_error(const char *text, unsigned long length) { + if (text == 0) { + return IONY_ERROR_INVALID_POINTER; + } + + #if defined(_WIN32) + if (iony_standard_console_error == 0) { + iony_standard_console_error = GetStdHandle(STD_ERROR_HANDLE); + if (iony_standard_console_error == INVALID_HANDLE_VALUE) { + return IONY_ERROR_HANDLE_NOT_FOUND; + } + } + + DWORD characters_written; + if (!WriteFile(iony_standard_console_error, text, length, &characters_written, 0)) { + return IONY_ERROR_SYSTEM_CALL_FAILED; + } + #elif defined(__GNUC__) + if (iony_system_call(IONY_SYS_WRITE, 2, (long)text, (long)length, 0, 0, 0) < 0) { return IONY_ERROR_SYSTEM_CALL_FAILED; } #endif diff --git a/Tests/Main.c b/Tests/Main.c index f3aadc5..3230d4d 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -25,38 +25,50 @@ void _start(void) { if (iony_console_print("Press enter to continue...", 27) != IONY_ERROR_NONE) { + iony_console_error("[!] Failed to print to console.\n", 33); iony_exit_process(-1); } char character = ' '; if (iony_console_get_character(&character) != IONY_ERROR_NONE) { + iony_console_error("[!] Failed to get character.\n", 30); iony_exit_process(-1); } if (iony_console_clear() != IONY_ERROR_NONE) { + iony_console_error("[!] Failed to clear the console.\n", 34); iony_exit_process(-1); } if (iony_console_print("Enter your name: ", 18) != IONY_ERROR_NONE) { + iony_console_error("[!] Failed to print to console.\n", 33); iony_exit_process(-1); } char name_input[256]; - long characters_read = 0; + unsigned long characters_read = 0; if (iony_console_get_line(name_input, 256, &characters_read) != IONY_ERROR_NONE) { + iony_console_error("[!] Failed to get name from console.\n", 38); iony_exit_process(-1); } - iony_console_set_color(IONY_CONSOLE_COLOR_LIGHT_RED); + if (iony_console_set_color(IONY_CONSOLE_COLOR_LIGHT_RED) != IONY_ERROR_NONE) { + iony_console_error("[!] Failed to set console color.\n", 34); + iony_exit_process(-1); + } + if (iony_console_print("Your name is ", 14) != IONY_ERROR_NONE) { + iony_console_error("[!] Failed to print to console.\n", 33); iony_exit_process(-1); } if (iony_console_print(name_input, characters_read) != IONY_ERROR_NONE) { + iony_console_error("[!] Failed to print name to console.\n", 38); iony_exit_process(-1); } if (iony_console_reset_color() != IONY_ERROR_NONE) { + iony_console_error("[!] Failed to reset console color.\n", 36); iony_exit_process(-1); }