index — thing @ cd7480a81a233f6fb52248040b6f7f36ad4a50b1

A little something I'm working on

menu: prototype of menu function
crispy-caesus crispy@crispy-caesus.eu
Thu, 28 May 2026 10:26:35 +0200
commit

cd7480a81a233f6fb52248040b6f7f36ad4a50b1

parent

65cce9dd1077ad9ac9fc29243f564a01ddd0a4cc

1 files changed, 36 insertions(+), 10 deletions(-)

jump to
M main.cmain.c

@@ -5,12 +5,22 @@ #include <poll.h>

#include <termios.h> #include <unistd.h> -void move_cursor(int direction) { +void move_cursor(int direction, uint8_t *pos, int len) { if (direction == -1) { + if (*pos != 0) { + *pos -= 1; + } else { + return; + } printf("\x0D "); printf("\x0D\x1B[1A>"); printf("\x0D"); } else if (direction == 1) { + if (*pos != len - 1) { + *pos += 1; + } else { + return; + } printf("\x0D "); printf("\x0D\x1B[1B>"); printf("\x0D");

@@ -36,18 +46,21 @@ }

void return_term(struct termios *old) { tcsetattr(STDIN_FILENO, TCSANOW, old); } -void menu() { +int menu(char **elements, int len) { struct pollfd poller[1]; poller[0].fd = STDIN_FILENO; poller[0].events = POLLIN; - printf(" hallo\n felix\n lass\n minecraft\n spielen\n"); - printf("\x1B[5A>\x0D"); + for (int i = 0; i < len; i++) { + printf(" %s\n", elements[i]); + } + + printf("\x1B[%dA>\x0D", len); fflush(stdout); - uint8_t run = 1; - while (run) { + uint8_t pos = 0; + while (1) { int events = poll(poller, 1, -1); if (events > 0 && poller[0].revents & POLLIN) { char buffer[10];

@@ -56,14 +69,20 @@ if (length > 0) {

uint8_t i; for (i = 0; i < length; i++) { if (buffer[i] == 'q') - run = 0; + return -1; else if (buffer[i] == 'j') { - move_cursor(1); + move_cursor(1, &pos, len); } else if (buffer[i] == 'k') { - move_cursor(-1); + move_cursor(-1, &pos, len); + } else if (buffer[i] == 'o') { + return pos; } } + } else { + return -1; } + } else { + return -1; } } }

@@ -71,8 +90,15 @@

int main() { struct termios old = setup_term(); - menu(); + static char *elements[] = {"hi", "hello", "what's up"}; + + int seli = menu(elements, 3); + if (seli < 0) { + printf("ERROR: menu failed"); + return 1; + } return_term(&old); + printf("%s", elements[seli]); return 0; }