cowsay_intro_for_bash/main.c

148 lines
4.3 KiB
C
Raw Normal View History

2026-09-29 21:50:14 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// Eigene Module einbinden
#include "sprueche.h"
#include "htmlTranslator.h"
#include "asciiArts.h"
#define MAX_WRAP_LINES 100
#define WRAP_LIMIT 80
int visible_len(const char *s, int len) {
int v_len = 0;
int in_ansi = 0;
for (int i = 0; i < len && s[i] != '\0'; i++) {
if (s[i] == '\033') { in_ansi = 1; continue; }
if (in_ansi) {
if (s[i] == 'm') in_ansi = 0;
continue;
}
if ((s[i] & 0xC0) != 0x80) v_len++;
}
return v_len;
}
int main() {
srand(time(NULL));
char *text_data = malloc(sprueche_txt_len + 1);
memcpy(text_data, sprueche_txt, sprueche_txt_len);
text_data[sprueche_txt_len] = '\0';
int total_quotes = 0;
char *quotes[5000];
char *line = strtok(text_data, "\n");
while (line != NULL && total_quotes < 5000) {
if (strlen(line) > 0) quotes[total_quotes++] = line;
line = strtok(NULL, "\n");
}
if (total_quotes == 0) {
free(text_data);
return 1;
}
char *selected_quote = quotes[rand() % total_quotes];
char *translated_quote = html_to_bash_format(selected_quote);
char *lines_to_wrap[500];
int num_initial_lines = 0;
char *ptr = translated_quote;
lines_to_wrap[num_initial_lines++] = ptr;
while (*ptr) {
if (*ptr == '\n') {
*ptr = '\0';
lines_to_wrap[num_initial_lines++] = ptr + 1;
}
ptr++;
}
char *wrapped_lines[MAX_WRAP_LINES];
int num_lines = 0;
for (int i = 0; i < num_initial_lines; i++) {
char *start = lines_to_wrap[i];
char *last_space = NULL;
if (*start == '\0') {
wrapped_lines[num_lines++] = start;
continue;
}
for (char *curr = start; *curr != '\0'; curr++) {
if (*curr == ' ') last_space = curr;
if (visible_len(start, curr - start) >= WRAP_LIMIT) {
if (last_space != NULL && last_space > start) {
*last_space = '\0';
wrapped_lines[num_lines++] = start;
start = last_space + 1;
} else {
char temp = *curr;
*curr = '\0';
wrapped_lines[num_lines++] = start;
*curr = temp;
start = curr;
}
last_space = NULL;
}
}
if (*start != '\0') {
wrapped_lines[num_lines++] = start;
}
}
int max_len = 0;
for (int i = 0; i < num_lines; i++) {
int vlen = visible_len(wrapped_lines[i], strlen(wrapped_lines[i]));
if (vlen > max_len) max_len = vlen;
}
// --- RENDER LOGIK ---
printf(" ");
for (int i = 0; i < max_len + 2; i++) printf("_");
printf("\n");
int is_think = rand() % 2;
for (int i = 0; i < num_lines; i++) {
char left = '|', right = '|';
if (num_lines == 1) { left = is_think ? '(' : '<'; right = is_think ? ')' : '>'; }
else if (i == 0) { left = is_think ? '(' : '/'; right = is_think ? ')' : '\\'; }
else if (i == num_lines - 1) { left = is_think ? '(' : '\\'; right = is_think ? ')' : '/'; }
else if (is_think) { left = '('; right = ')'; }
int vlen = visible_len(wrapped_lines[i], strlen(wrapped_lines[i]));
int pad = max_len - vlen;
printf("%c %s", left, wrapped_lines[i]);
for (int p = 0; p < pad; p++) printf(" ");
printf("\033[0m %c\n", right);
}
printf(" ");
for (int i = 0; i < max_len + 2; i++) printf("-");
printf("\n");
int face = rand() % 9;
char thought_char = is_think ? 'o' : '\\';
int num_templates = sizeof(templates) / sizeof(templates[0]);
const char *tpl = templates[rand() % num_templates];
for (int i = 0; tpl[i] != '\0'; i++) {
if (tpl[i] == '$') {
if (strncmp(&tpl[i], "$thoughts", 9) == 0) { putchar(thought_char); i += 8; }
else if (strncmp(&tpl[i], "$eyes", 5) == 0) { printf("%s", eyes[face]); i += 4; }
else if (strncmp(&tpl[i], "$tongue", 7) == 0) { printf("%s", tongues[face]); i += 6; }
else putchar(tpl[i]);
} else putchar(tpl[i]);
}
free(translated_quote);
free(text_data);
return 0;
}