Implement user input handling and refine drawing logic

This commit is contained in:
2026-06-07 17:25:33 +02:00
parent 73bb260fec
commit 8121288e57
5 changed files with 97 additions and 13 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
.vscode
cmake-build-debug
build

77
draw.c
View File

@@ -1,6 +1,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <termios.h>
#include <unistd.h>
#include "draw.h" #include "draw.h"
void drawEdges() { void drawEdges() {
@@ -20,7 +22,9 @@ void drawTitle(char* title) {
printf(" "); printf(" ");
} }
printf("%s", title); printf("%s", title);
if (strlen(title) % 2 != WIDTH % 2) {
// we check if either the length of the title or the width is odd.
if ((strlen(title) + WIDTH) % 2 == 1) {
spacing += 1; spacing += 1;
} }
for (int i = 0; i < spacing; i++) { for (int i = 0; i < spacing; i++) {
@@ -40,7 +44,76 @@ void drawEmptyLines(const int amount) {
} }
char** splitByNewline(char *str, int *lineCount) { 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; int maxLines = 100;
char **lines = malloc(maxLines * sizeof(char*)); char **lines = malloc(maxLines * sizeof(char*));

8
draw.h
View File

@@ -41,6 +41,14 @@ void drawTitle(char* title);
*/ */
void drawEmptyLines(int amount); void drawEmptyLines(int amount);
/**
* @brief gets Userinput and puts it in the input var
*
* @param[out] output
* @param[in] displayText
*/
void getUserInput(char* output, char* displayText);
char** splitByNewLine(char *str, int *lineCount); char** splitByNewLine(char *str, int *lineCount);
#endif #endif

20
main.c
View File

@@ -1,3 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
#include "draw.h" #include "draw.h"
#include "main.h" #include "main.h"
@@ -10,24 +12,20 @@
#endif #endif
int main() { int main() {
// char *buffer = NULL; char output[1024];
// size_t buffsize = 0;
// ssize_t characters;
// printf("Python alter leck meine eier? \n"); getInitialWorries(output);
// characters = getline(&buffer, &buffsize, stdin);
// if (characters != -1) {
// printf("you typed %s\n", buffer);
// }
sleep_ms(500);
drawEdges(); drawEdges();
drawEmptyLines(1); drawEmptyLines(1);
drawTitle(TITLE); drawTitle(TITLE);
drawEmptyLines(1); drawEmptyLines(1);
drawEdges(); drawEdges();
printf("%s", output);
return 0; return 0;
} }
void getInitialWorries(char* output) {
scanf("What's on your mind? %s\n", output);
}

2
main.h
View File

@@ -3,4 +3,6 @@
#define TITLE "Scream your thoughts into the void!" #define TITLE "Scream your thoughts into the void!"
void getInitialWorries(char* output);
#endif #endif