feat(console input): implemented clear console function

This commit is contained in:
Mineplay 2025-10-17 17:24:39 -05:00
parent 98129b3260
commit c74b7364f2
4 changed files with 54 additions and 6 deletions

View file

@ -25,6 +25,7 @@
#include "../Utils/Error.h"
IonyError iony_console_print(const char *text, long length);
IonyError iony_console_clear();
IonyError iony_console_get_character(char *character);

View file

@ -26,7 +26,8 @@
typedef enum {
IONY_ERROR_NONE = 0,
IONY_ERROR_INVALID_POINTER = 1,
IONY_ERROR_SYSTEM_CALL_FAILED = 2
IONY_ERROR_SYSTEM_CALL_FAILED = 2,
IONY_ERROR_HANDLE_NOT_FOUND = 3,
} IonyError;
#endif

View file

@ -25,6 +25,7 @@
#if defined(_WIN32)
#include <windows.h>
static HANDLE iony_standard_console_out = 0;
static HANDLE iony_standard_console_in = 0;
#elif defined(__GNUC__)
#include "../../Include/Iony/Utils/System.h"
@ -36,13 +37,15 @@ IonyError iony_console_print(const char *text, const long length) {
}
#if defined(_WIN32)
static HANDLE standard_console_out_handle = 0;
if (standard_console_out_handle == 0) {
standard_console_out_handle = GetStdHandle(STD_OUTPUT_HANDLE);
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 (!WriteFile(standard_console_out_handle, text, length, &characters_written, 0)) {
if (!WriteFile(iony_standard_console_out, text, length, &characters_written, 0)) {
return IONY_ERROR_SYSTEM_CALL_FAILED;
}
#elif defined(__GNUC__)
@ -54,6 +57,41 @@ IonyError iony_console_print(const char *text, const long length) {
return IONY_ERROR_NONE;
}
IonyError iony_console_clear() {
#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;
}
}
CONSOLE_SCREEN_BUFFER_INFO console_info;
DWORD characters_written, cell_count;
COORD cursor_coords = { 0, 0 };
if (!GetConsoleScreenBufferInfo(iony_standard_console_out, &console_info)) {
return IONY_ERROR_SYSTEM_CALL_FAILED;
}
cell_count = console_info.dwSize.X * console_info.dwSize.Y;
if (!FillConsoleOutputCharacterA(iony_standard_console_out, ' ', cell_count, cursor_coords, &characters_written)) {
return IONY_ERROR_SYSTEM_CALL_FAILED;
}
if (!FillConsoleOutputAttribute(iony_standard_console_out, console_info.wAttributes, cell_count, cursor_coords, &characters_written)) {
return IONY_ERROR_SYSTEM_CALL_FAILED;
}
SetConsoleCursorPosition(iony_standard_console_out, cursor_coords);
#elif defined(__GNUC__)
const char clear[] = "\033[2J\033[H";
if (iony_system_call(IONY_SYS_WRITE, 1, (long)clear, sizeof(clear) - 1, 0, 0, 0) < 0) {
return IONY_ERROR_SYSTEM_CALL_FAILED;
}
#endif
return IONY_ERROR_NONE;
}
IonyError iony_console_get_character(char *character) {
if (character == 0) {
return IONY_ERROR_INVALID_POINTER;
@ -62,6 +100,9 @@ IonyError iony_console_get_character(char *character) {
#if defined(_WIN32)
if (iony_standard_console_in == 0) {
iony_standard_console_in = GetStdHandle(STD_INPUT_HANDLE);
if (iony_standard_console_in == INVALID_HANDLE_VALUE) {
return IONY_ERROR_HANDLE_NOT_FOUND;
}
}
DWORD characters_read;
@ -69,7 +110,7 @@ IonyError iony_console_get_character(char *character) {
return IONY_ERROR_SYSTEM_CALL_FAILED;
}
#elif defined(__GNUC__)
if (iony_system_call(IONY_SYS_READ, 0, (long)character, 1, 0, 0, 0)) {
if (iony_system_call(IONY_SYS_READ, 0, (long)character, 1, 0, 0, 0) < 0) {
return IONY_ERROR_SYSTEM_CALL_FAILED;
}
#endif

View file

@ -32,5 +32,10 @@ int main(void) {
return -1;
}
if (iony_console_clear() != IONY_ERROR_NONE) {
return -1;
}
return 0;
}