Skip to content

Commit c86fcd1

Browse files
Merge pull request #36 from dreamer-coding/add_redirect_output
2 parents cec7f34 + b5e5504 commit c86fcd1

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

code/logic/device.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* -----------------------------------------------------------------------------
1313
*/
1414
#include "fossil/io/device.h"
15+
#include "fossil/io/output.h"
1516
#include <stdio.h>
1617
#include <stdlib.h>
1718
#include <string.h>
@@ -139,7 +140,6 @@ static fossil_io_keyboard_event_t fossil_io_keyboard_get_event(void) {
139140

140141
void fossil_io_keyboard_init(void) {
141142
memset(&keyboard_manager, 0, sizeof(keyboard_manager));
142-
printf("[mouse] Initialized: bindings cleared\n");
143143
#if defined(_WIN32) || defined(_WIN64)
144144
// Windows doesn't require explicit setup for raw mode.
145145
#else
@@ -150,7 +150,6 @@ void fossil_io_keyboard_init(void) {
150150

151151
void fossil_io_keyboard_shutdown(void) {
152152
memset(&keyboard_manager, 0, sizeof(keyboard_manager));
153-
printf("[mouse] Shutdown: bindings released\n");
154153
#if defined(_WIN32) || defined(_WIN64)
155154
// Windows doesn't require explicit cleanup for raw mode.
156155
#else
@@ -168,7 +167,7 @@ void fossil_io_keyboard_register_binding(fossil_io_keyboard_event_t event, fossi
168167
keyboard_manager.bindings[keyboard_manager.count].callback = callback;
169168
keyboard_manager.count++;
170169
} else {
171-
fprintf(stderr, "Max keybindings reached.\n");
170+
fossil_io_fprintf(FOSSIL_STDERR, "Max keybindings reached.\n");
172171
}
173172
}
174173

@@ -185,7 +184,7 @@ void fossil_io_keyboard_unregister_binding(fossil_io_keyboard_event_t event) {
185184
return;
186185
}
187186
}
188-
fprintf(stderr, "No matching keybinding to unregister.\n");
187+
fossil_io_fprintf(FOSSIL_STDERR, "No matching keybinding to unregister.\n");
189188
}
190189

191190
void fossil_io_keyboard_poll_events(void) {
@@ -228,7 +227,7 @@ static void fossil_io_mouse_get_event(fossil_io_mouse_event_t* event) {
228227

229228
void fossil_io_mouse_register_binding(fossil_io_mouse_event_t event, fossil_io_mouse_callback_t callback) {
230229
if (mouse_manager.count >= MAX_MOUSEBINDS) {
231-
fprintf(stderr, "[mouse] Max bindings reached\n");
230+
fossil_io_fprintf(FOSSIL_STDERR, "[mouse] Max bindings reached\n");
232231
return;
233232
}
234233

@@ -244,7 +243,7 @@ void fossil_io_mouse_unregister_binding(fossil_io_mouse_event_t event) {
244243
return;
245244
}
246245
}
247-
fprintf(stderr, "[mouse] Binding not found\n");
246+
fossil_io_fprintf(FOSSIL_STDERR, "[mouse] Binding not found\n");
248247
}
249248

250249
void fossil_io_mouse_poll_events(void) {
@@ -269,12 +268,10 @@ void fossil_io_mouse_clear_bindings(void) {
269268

270269
void fossil_io_mouse_init(void) {
271270
memset(&mouse_manager, 0, sizeof(mouse_manager));
272-
printf("[mouse] Initialized: bindings cleared\n");
273271
}
274272

275273
void fossil_io_mouse_shutdown(void) {
276274
memset(&mouse_manager, 0, sizeof(mouse_manager));
277-
printf("[mouse] Shutdown: bindings released\n");
278275
}
279276

280277
// TOUCH
@@ -300,7 +297,7 @@ static void fossil_io_touch_get_event(fossil_io_touch_event_t* event) {
300297

301298
void fossil_io_touch_register_binding(fossil_io_touch_event_t event, fossil_io_touch_callback_t callback) {
302299
if (touch_manager.count >= MAX_TOUCHBINDS) {
303-
fprintf(stderr, "[touch] Max bindings reached\n");
300+
fossil_io_fprintf(FOSSIL_STDERR, "[touch] Max bindings reached\n");
304301
return;
305302
}
306303

@@ -316,7 +313,7 @@ void fossil_io_touch_unregister_binding(fossil_io_touch_event_t event) {
316313
return;
317314
}
318315
}
319-
fprintf(stderr, "[touch] Binding not found\n");
316+
fossil_io_fprintf(FOSSIL_STDERR, "[touch] Binding not found\n");
320317
}
321318

322319
void fossil_io_touch_poll_events(void) {
@@ -341,10 +338,8 @@ void fossil_io_touch_clear_bindings(void) {
341338

342339
void fossil_io_touch_init(void) {
343340
memset(&touch_manager, 0, sizeof(touch_manager));
344-
printf("[touch] Initialized: bindings cleared\n");
345341
}
346342

347343
void fossil_io_touch_shutdown(void) {
348344
memset(&touch_manager, 0, sizeof(touch_manager));
349-
printf("[touch] Shutdown: bindings released\n");
350345
}

code/logic/fossil/io/output.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
extern "C" {
2222
#endif
2323

24+
extern int32_t FOSSIL_IO_COLOR_ENABLE; // Flag to enable/disable color output
25+
extern int32_t FOSSIL_IO_ATTR_ENABLE; // Flag to enable/disable attribute output
26+
2427
/**
2528
* This code provides a robust set of functions for formatting and manipulating terminal output,
2629
* allowing developers to apply color, text attributes (like bold, underline, etc.), and cursor positioning
@@ -70,6 +73,16 @@ extern "C" {
7073
* needs of more complex applications.
7174
*/
7275

76+
/**
77+
* Redirects the output to a specified stream.
78+
*
79+
* This function allows you to change the default output destination to a custom stream.
80+
* It is useful when you want to redirect output to a file or another output stream.
81+
*
82+
* @param stream The output stream where subsequent output should be redirected.
83+
*/
84+
void fossil_io_redirect_output(fossil_fstream_t *stream);
85+
7386
/**
7487
* Prints a string to the output.
7588
*

code/logic/output.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
#include <string.h>
1919
#include <stdio.h>
2020

21+
int32_t FOSSIL_IO_COLOR_ENABLE = 1; // Flag to enable/disable color output
22+
int32_t FOSSIL_IO_ATTR_ENABLE = 1; // Flag to enable/disable attribute output
23+
2124
// Define color codes for output
2225
#define FOSSIL_IO_COLOR_RESET "\033[0m"
2326
#define FOSSIL_IO_COLOR_RED "\033[31m"
@@ -171,11 +174,11 @@ void fossil_io_print_with_attributes(const char *format, ...) {
171174
pos = color + 4; // Skip the "pos:" prefix
172175
fossil_io_apply_position(pos);
173176
} else {
174-
// Apply color and/or attribute
175-
if (color) {
177+
// Apply color and/or attribute based on flags
178+
if (FOSSIL_IO_COLOR_ENABLE && color) {
176179
fossil_io_apply_color(color);
177180
}
178-
if (attribute) {
181+
if (FOSSIL_IO_ATTR_ENABLE && attribute) {
179182
fossil_io_apply_attribute(attribute);
180183
}
181184
}

0 commit comments

Comments
 (0)