Implement user input handling and refine drawing logic
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
.vscode
|
||||
cmake-build-debug
|
||||
build
|
||||
77
draw.c
77
draw.c
@@ -1,6 +1,8 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#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*));
|
||||
|
||||
|
||||
8
draw.h
8
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
|
||||
20
main.c
20
main.c
@@ -1,3 +1,5 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user