learning: create menu (without functionality)
crispy-caesus crispy@crispy-caesus.eu
Tue, 28 Apr 2026 10:39:57 +0200
1 files changed,
34 insertions(+),
7 deletions(-)
jump to
M
main.c
→
main.c
@@ -5,8 +5,20 @@ #include <poll.h>
#include <termios.h> #include <unistd.h> -int main() { +void move_cursor(int direction) { + if (direction == -1) { + printf("\x0D "); + printf("\x0D\x1B[1A>"); + printf("\x0D"); + } else if (direction == 1) { + printf("\x0D "); + printf("\x0D\x1B[1B>"); + printf("\x0D"); + } + fflush(stdout); +} +struct termios setup_term() { struct termios old, new; tcgetattr(STDIN_FILENO, &old);@@ -19,15 +31,22 @@ new.c_cc[VTIME] = 0; // time before return
tcsetattr(STDIN_FILENO, TCSANOW, &new); + return old; +} + +void return_term(struct termios *old) { tcsetattr(STDIN_FILENO, TCSANOW, old); } + +void menu() { struct pollfd poller[1]; poller[0].fd = STDIN_FILENO; poller[0].events = POLLIN; - printf("Pressed characters:\n"); + printf(" hallo\n felix\n lass\n minecraft\n spielen\n"); + printf("\x1B[5A>\x0D"); + fflush(stdout); uint8_t run = 1; - while (run) { int events = poll(poller, 1, -1); if (events > 0 && poller[0].revents & POLLIN) {@@ -36,16 +55,24 @@ uint16_t length = read(STDIN_FILENO, buffer, sizeof(buffer));
if (length > 0) { uint8_t i; for (i = 0; i < length; i++) { - printf(" %x", buffer[i]); - if (buffer[i] == 'Q') + if (buffer[i] == 'q') run = 0; + else if (buffer[i] == 'j') { + move_cursor(1); + } else if (buffer[i] == 'k') { + move_cursor(-1); + } } - printf("\n"); } } } +} - tcsetattr(STDIN_FILENO, TCSANOW, &old); +int main() { + struct termios old = setup_term(); + menu(); + + return_term(&old); return 0; }