Skip to content

Commit d4a5cbe

Browse files
this time
1 parent bcf3d05 commit d4a5cbe

File tree

2 files changed

+57
-79
lines changed

2 files changed

+57
-79
lines changed

code/logic/archive.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -580,12 +580,12 @@ void fossil_io_archive_print(fossil_io_archive_t *archive) {
580580

581581
fossil_io_printf("{cyan}Archive:{reset} %s {yellow}(Type: %d, Entries: %zu){reset}\n",
582582
archive->path, archive->type, archive->entry_count);
583-
fossil_io_printf("{green}%-40s %10s %10s %s{reset}\n", "Name", "Size", "Compressed", "Directory");
584-
fossil_io_printf("{green}%-40s %10s %10s %s{reset}\n", "----", "----", "----------", "---------");
583+
fossil_io_printf("{bold,blue}%-40s %10s %10s %s{reset}\n", "Name", "Size", "Compressed", "Directory");
584+
fossil_io_printf("{bold,blue}%-40s %10s %10s %s{reset}\n", "----", "----", "----------", "---------");
585585

586586
for (size_t i = 0; i < archive->entry_count; i++) {
587587
fossil_io_archive_entry_t *entry = &archive->entries[i];
588-
fossil_io_printf("{white}%-40s{reset} {magenta}%10zu{reset} {blue}%10zu{reset} {red}%s{reset}\n",
588+
fossil_io_printf("{white}%-40s{reset} {green}%10zu{reset} {magenta}%10zu{reset} {yellow}%s{reset}\n",
589589
entry->name ? entry->name : "(null)",
590590
entry->size,
591591
entry->compressed_size,

code/logic/output.c

Lines changed: 54 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ int32_t FOSSIL_IO_COLOR_ENABLE = 1; // Flag to enable/disable color output
6666

6767
// Function to apply color
6868
void fossil_io_apply_color(const char *color) {
69-
if (!FOSSIL_IO_COLOR_ENABLE || !color) return;
70-
7169
if (strcmp(color, "red") == 0) {
7270
printf(FOSSIL_IO_COLOR_RED);
7371
} else if (strcmp(color, "green") == 0) {
@@ -98,15 +96,13 @@ void fossil_io_apply_color(const char *color) {
9896
printf(FOSSIL_IO_COLOR_BRIGHT_CYAN);
9997
} else if (strcmp(color, "bright_white") == 0) {
10098
printf(FOSSIL_IO_COLOR_BRIGHT_WHITE);
101-
} else {
99+
} else if (strcmp(color, "reset") == 0) {
102100
printf(FOSSIL_IO_COLOR_RESET); // Reset to default if color not recognized
103101
}
104102
}
105103

106104
// Function to apply text attributes (e.g., bold, underline)
107105
void fossil_io_apply_attribute(const char *attribute) {
108-
if (!attribute) return;
109-
110106
if (strcmp(attribute, "bold") == 0) {
111107
printf(FOSSIL_IO_ATTR_BOLD);
112108
} else if (strcmp(attribute, "underline") == 0) {
@@ -123,35 +119,35 @@ void fossil_io_apply_attribute(const char *attribute) {
123119
printf(FOSSIL_IO_ATTR_ITALIC);
124120
} else if (strcmp(attribute, "strikethrough") == 0) {
125121
printf(FOSSIL_IO_ATTR_STRIKETHROUGH);
122+
} else if (strcmp(attribute, "reset") == 0) {
123+
printf(FOSSIL_IO_ATTR_NORMAL); // Reset to normal if attribute not recognized
126124
}
127125
}
128126

129127
// Function to handle named positions (like top, bottom, left, right)
130128
void fossil_io_apply_position(const char *pos) {
131-
if (!pos) return;
132-
133129
if (strcmp(pos, "top") == 0) {
134130
printf("\033[1;1H"); // Move to top
135131
} else if (strcmp(pos, "bottom") == 0) {
136-
printf("\033[999;1H"); // Move cursor to bottom-left (reduced from 1000)
132+
printf("\033[1000;1H"); // Move cursor to bottom-left
137133
} else if (strcmp(pos, "left") == 0) {
138134
printf("\033[1;1H"); // Move to top-left (as a general left start)
139135
} else if (strcmp(pos, "right") == 0) {
140-
printf("\033[1;999H"); // Move to top-right (reduced from 1000)
136+
printf("\033[1;1000H"); // Move to top-right
141137
} else if (strcmp(pos, "center") == 0) {
142138
printf("\033[25;40H"); // Approximate center for 80x50 terminal
143139
} else if (strcmp(pos, "top-left") == 0) {
144140
printf("\033[1;1H");
145141
} else if (strcmp(pos, "top-right") == 0) {
146-
printf("\033[1;999H");
142+
printf("\033[1;1000H");
147143
} else if (strcmp(pos, "bottom-left") == 0) {
148-
printf("\033[999;1H");
144+
printf("\033[1000;1H");
149145
} else if (strcmp(pos, "bottom-right") == 0) {
150-
printf("\033[999;999H");
146+
printf("\033[1000;1000H");
151147
} else if (strcmp(pos, "middle-left") == 0) {
152148
printf("\033[25;1H"); // Mid vertical, far left
153149
} else if (strcmp(pos, "middle-right") == 0) {
154-
printf("\033[25;999H"); // Mid vertical, far right
150+
printf("\033[25;1000H"); // Mid vertical, far right
155151
} else {
156152
fprintf(stderr, "Unknown position: %s\n", pos);
157153
}
@@ -170,46 +166,41 @@ void fossil_io_print_with_attributes(const char *str) {
170166

171167
while ((start = strchr(current_pos, '{')) != NULL) {
172168
// Output text before '{'
173-
size_t len = start - current_pos;
174-
if (len > 0) {
175-
fwrite(current_pos, 1, len, stdout);
176-
}
169+
fwrite(current_pos, 1, start - current_pos, stdout);
177170

178171
// Find the matching '}'
179172
end = strchr(start, '}');
180173
if (end) {
181174
// Extract attributes inside '{}'
182175
size_t length = end - start - 1;
183-
if (length > 0 && length < 256) { // Reasonable size limit
184-
char attributes[257];
185-
strncpy(attributes, start + 1, length);
186-
attributes[length] = '\0';
187-
188-
// Split by comma to separate color, attribute, or position
189-
char *color = NULL;
190-
char *attribute = NULL;
191-
char *pos = NULL;
192-
char *comma_pos = strchr(attributes, ',');
193-
if (comma_pos) {
194-
*comma_pos = '\0'; // Null-terminate the first part
195-
color = attributes; // Color or position part
196-
attribute = comma_pos + 1; // Attribute part
197-
} else {
198-
color = attributes; // Only one part (could be color, attribute, or position)
199-
}
176+
char attributes[length + 1];
177+
strncpy(attributes, start + 1, length);
178+
attributes[length] = '\0';
179+
180+
// Split by comma to separate color, attribute, or position
181+
char *color = NULL;
182+
char *attribute = NULL;
183+
char *pos = NULL;
184+
char *comma_pos = strchr(attributes, ',');
185+
if (comma_pos) {
186+
*comma_pos = '\0'; // Null-terminate the first part
187+
color = attributes; // Color or position part
188+
attribute = comma_pos + 1; // Attribute part
189+
} else {
190+
color = attributes; // Only one part (could be color, attribute, or position)
191+
}
200192

201-
// Handle positions (like {pos:name})
202-
if (color && strncmp(color, "pos:", 4) == 0) {
203-
pos = color + 4; // Skip the "pos:" prefix
204-
fossil_io_apply_position(pos);
205-
} else {
206-
// Apply color and/or attribute based on flags
207-
if (FOSSIL_IO_COLOR_ENABLE && color) {
208-
fossil_io_apply_color(color);
209-
}
210-
if (attribute) {
211-
fossil_io_apply_attribute(attribute);
212-
}
193+
// Handle positions (like {pos:name})
194+
if (strstr(color, "pos:") == color) {
195+
pos = color + 4; // Skip the "pos:" prefix
196+
fossil_io_apply_position(pos);
197+
} else {
198+
// Apply color and/or attribute based on flags
199+
if (FOSSIL_IO_COLOR_ENABLE && color) {
200+
fossil_io_apply_color(color);
201+
}
202+
if (attribute) {
203+
fossil_io_apply_attribute(attribute);
213204
}
214205
}
215206

@@ -223,17 +214,15 @@ void fossil_io_print_with_attributes(const char *str) {
223214
}
224215

225216
// Output remaining text after last '}'
226-
if (current_pos && *current_pos) {
227-
fputs(current_pos, stdout);
228-
}
217+
fputs(current_pos, stdout);
229218
fflush(stdout);
230219
}
231220

232221
// Function to print a sanitized formatted string to a specific file stream with attributes
233222
void fossil_io_fprint_with_attributes(fossil_fstream_t *stream, const char *str) {
234-
if (str != NULL && stream != NULL && stream->file != NULL) {
223+
if (str != NULL && stream != NULL) {
235224
char sanitized_str[FOSSIL_IO_BUFFER_SIZE];
236-
strncpy(sanitized_str, str, sizeof(sanitized_str) - 1);
225+
strncpy(sanitized_str, str, sizeof(sanitized_str));
237226
sanitized_str[sizeof(sanitized_str) - 1] = '\0'; // Ensure null termination
238227

239228
// Remove attribute/color escape codes for file output
@@ -242,10 +231,7 @@ void fossil_io_fprint_with_attributes(fossil_fstream_t *stream, const char *str)
242231
const char *end = NULL;
243232
while ((start = strchr(current_pos, '{')) != NULL) {
244233
// Write text before '{' to the file
245-
size_t len = start - current_pos;
246-
if (len > 0) {
247-
fwrite(current_pos, 1, len, stream->file);
248-
}
234+
fwrite(current_pos, 1, start - current_pos, stream->file);
249235
end = strchr(start, '}');
250236
if (end) {
251237
// Skip the attribute section
@@ -257,9 +243,9 @@ void fossil_io_fprint_with_attributes(fossil_fstream_t *stream, const char *str)
257243
}
258244
}
259245
// Write remaining text after last '}'
260-
if (current_pos && *current_pos) {
261-
fputs(current_pos, stream->file);
262-
}
246+
fputs(current_pos, stream->file);
247+
} else {
248+
//fputs("cnullptr\n", stderr);
263249
}
264250
}
265251

@@ -271,7 +257,7 @@ void fossil_io_fprint_with_attributes(fossil_fstream_t *stream, const char *str)
271257
void fossil_io_puts(const char *str) {
272258
if (str != NULL) {
273259
char sanitized_str[FOSSIL_IO_BUFFER_SIZE];
274-
strncpy(sanitized_str, str, sizeof(sanitized_str) - 1);
260+
strncpy(sanitized_str, str, sizeof(sanitized_str));
275261
sanitized_str[sizeof(sanitized_str) - 1] = '\0'; // Ensure null termination
276262

277263
// Print the sanitized string with attributes
@@ -288,28 +274,24 @@ void fossil_io_putchar(char c) {
288274

289275
// Function to print sanitized formatted output with attributes
290276
void fossil_io_printf(const char *format, ...) {
291-
if (!format) return;
292-
293277
va_list args;
294278
va_start(args, format);
295279

296280
// Create a buffer to hold the formatted string
297281
char buffer[FOSSIL_IO_BUFFER_SIZE];
298-
int result = vsnprintf(buffer, sizeof(buffer), format, args);
299-
300-
if (result > 0 && result < (int)sizeof(buffer)) {
301-
// Print the sanitized output with attributes
302-
fossil_io_print_with_attributes(buffer);
303-
}
282+
vsnprintf(buffer, sizeof(buffer), format, args);
283+
284+
// Print the sanitized output with attributes
285+
fossil_io_print_with_attributes(buffer);
304286

305287
va_end(args);
306288
}
307289

308290
// Function to print a sanitized string to a specific file stream
309291
void fossil_io_fputs(fossil_fstream_t *stream, const char *str) {
310-
if (str != NULL && stream != NULL && stream->file != NULL) {
292+
if (str != NULL && stream != NULL) {
311293
char sanitized_str[FOSSIL_IO_BUFFER_SIZE];
312-
strncpy(sanitized_str, str, sizeof(sanitized_str) - 1);
294+
strncpy(sanitized_str, str, sizeof(sanitized_str));
313295
sanitized_str[sizeof(sanitized_str) - 1] = '\0'; // Ensure null termination
314296

315297
// Apply color/attributes and sanitize the string before printing
@@ -321,19 +303,15 @@ void fossil_io_fputs(fossil_fstream_t *stream, const char *str) {
321303

322304
// Function to print a sanitized formatted string to a specific file stream
323305
void fossil_io_fprintf(fossil_fstream_t *stream, const char *format, ...) {
324-
if (!format || !stream || !stream->file) return;
325-
326306
va_list args;
327307
va_start(args, format);
328308

329309
// Create a buffer to hold the formatted string
330310
char buffer[FOSSIL_IO_BUFFER_SIZE];
331-
int result = vsnprintf(buffer, sizeof(buffer), format, args);
311+
vsnprintf(buffer, sizeof(buffer), format, args);
332312

333-
if (result > 0 && result < (int)sizeof(buffer)) {
334-
// Print the sanitized formatted string with attributes to the specified stream
335-
fossil_io_fprint_with_attributes(stream, buffer);
336-
}
313+
// Print the sanitized formatted string with attributes to the specified stream
314+
fossil_io_fprint_with_attributes(stream, buffer);
337315

338316
va_end(args);
339317
}

0 commit comments

Comments
 (0)