Files
ScreamIntoTheVoid/draw.c

137 lines
3.8 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include "draw.h"
void drawEdges() {
for (int i = 0; i < WIDTH; i++) {
printf("%c", SEPERATION_CHAR);
}
printf("\n");
}
void drawTitle(char* title) {
// put seperation_char at start and end
printf("%c", SEPERATION_CHAR);
// calculate amount of spaces before and after the word
int spacing = (WIDTH - 2 - strlen(title)) / 2;
for (int i = 0; i < spacing; i++) {
printf(" ");
}
printf("%s", title);
// we check if either the length of the title or the width is odd.
if ((strlen(title) + WIDTH) % 2 == 1) {
spacing += 1;
}
for (int i = 0; i < spacing; i++) {
printf(" ");
}
printf("%c\n", SEPERATION_CHAR);
}
void drawEmptyLines(const int amount) {
for (int i = 0; i < amount; i++) {
printf("%c", SEPERATION_CHAR);
for (int j = 0; j < WIDTH - 2; j++) {
printf(" ");
}
printf("%c\n", SEPERATION_CHAR);
}
}
void getUserInput(char* output, char* displayText) {
// 1. Print the prompt line
printf("%c %s", SEPERATION_CHAR, displayText);
int prompt_len = strlen(displayText);
int padding = WIDTH - prompt_len - 3;
for (int i = 0; i < padding; i++) {
printf(" ");
}
printf("%c\n", SEPERATION_CHAR);
// 2. Start the input line with fixed borders
printf("%c ", SEPERATION_CHAR);
for (int i = 0; i < WIDTH - 4; i++) printf(" ");
printf(" %c", SEPERATION_CHAR);
printf("\033[%dG", 3); // Move cursor to column 3
fflush(stdout);
// 3. Set up terminal for raw input (no echo, no canonical mode)
struct termios oldt, newt;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
int i = 0;
int col = 3; // Current terminal column
char ch;
while (1) {
if (read(STDIN_FILENO, &ch, 1) <= 0) break;
if (ch == '\n' || ch == '\r') {
output[i] = '\0';
// Move cursor to the end of the line and print newline
printf("\033[%dG", WIDTH + 1);
printf("\n");
break;
} else if (ch == 127 || ch == 8) { // Backspace
if (i > 0) {
if (col > 3) {
i--;
col--;
printf("\b \b");
}
// Note: Backspacing across lines is omitted for simplicity
// but could be added with more complex cursor management.
}
} else {
// Check if we have space in output (assume output is large enough or handle gracefully)
output[i++] = ch;
printf("%c", ch);
col++;
// If we reach the end of the text area (WIDTH - 1 is the space before #)
if (col == WIDTH - 1) {
// Move to next line and draw new borders
printf("\n%c ", SEPERATION_CHAR);
for (int j = 0; j < WIDTH - 4; j++) printf(" ");
printf(" %c", SEPERATION_CHAR);
printf("\033[%dG", 3); // Move to column 3
col = 3;
}
}
fflush(stdout);
}
// 4. Restore terminal settings
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
}
char** splitByNewLine(char *str, int *lineCount) {
int maxLines = 100;
char **lines = malloc(maxLines * sizeof(char*));
int count = 0;
char *token = strtok(str, "\n");
while (token != NULL) {
lines[count] = token; // Store the pointer to this line
count++;
if (count >= maxLines) {
break; // Safety check so we don't overflow our array
}
token = strtok(NULL, "\n");
}
*lineCount = count; // Pass the total count back to the caller
return lines;
}