feat(console coloring): added printing error

This commit is contained in:
Mineplay 2025-10-19 09:28:17 -05:00
parent 7447d6ab45
commit dad7db99d8
3 changed files with 47 additions and 7 deletions

View file

@ -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);

View file

@ -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

View file

@ -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);
}