diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a7f389f --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.vscode +cmake-build-debug +build \ No newline at end of file diff --git a/draw.c b/draw.c index 35a9fbf..5e6b4c5 100644 --- a/draw.c +++ b/draw.c @@ -1,6 +1,8 @@ #include #include #include +#include +#include #include "draw.h" void drawEdges() { @@ -20,7 +22,9 @@ void drawTitle(char* title) { printf(" "); } 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; } 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; char **lines = malloc(maxLines * sizeof(char*)); diff --git a/draw.h b/draw.h index 591ebe4..f79fe67 100644 --- a/draw.h +++ b/draw.h @@ -41,6 +41,14 @@ void drawTitle(char* title); */ 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); #endif \ No newline at end of file diff --git a/main.c b/main.c index c2c83ee..aa4696c 100644 --- a/main.c +++ b/main.c @@ -1,3 +1,5 @@ +#include +#include #include "draw.h" #include "main.h" @@ -10,24 +12,20 @@ #endif int main() { - // char *buffer = NULL; - // size_t buffsize = 0; - // ssize_t characters; + char output[1024]; - // printf("Python alter leck meine eier? \n"); - // characters = getline(&buffer, &buffsize, stdin); - - // if (characters != -1) { - // printf("you typed %s\n", buffer); - // } - - sleep_ms(500); + getInitialWorries(output); drawEdges(); drawEmptyLines(1); drawTitle(TITLE); drawEmptyLines(1); drawEdges(); + printf("%s", output); return 0; } + +void getInitialWorries(char* output) { + scanf("What's on your mind? %s\n", output); +} diff --git a/main.h b/main.h index ad24457..32216dc 100644 --- a/main.h +++ b/main.h @@ -3,4 +3,6 @@ #define TITLE "Scream your thoughts into the void!" +void getInitialWorries(char* output); + #endif \ No newline at end of file