Skip to content

Instantly share code, notes, and snippets.

@sammaji
Created March 10, 2025 04:49
Show Gist options
  • Select an option

  • Save sammaji/0e55dbc63d44145ad6fbf28ca4cf1b16 to your computer and use it in GitHub Desktop.

Select an option

Save sammaji/0e55dbc63d44145ad6fbf28ca4cf1b16 to your computer and use it in GitHub Desktop.
Create a dropdown menu (tui) in c
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#define CLEAR_SCREEN "\033[2J\033[H"
#define CURSOR_UP "\033[A"
#define CURSOR_DOWN "\033[B"
#define CLEAR_LINE "\033[K"
#define HIGHLIGHT "\033[7m" // Reverse video (highlight)
#define RESET "\033[0m" // Reset attributes
#define KEY_UP 65 // code of 'A' (UP arrow)
#define KEY_DOWN 66 // code of 'B' (DOWN arrow)
#define KEY_ENTER 10 // code of 'Enter'
#define KEY_ESC 27 // code of 'ESC'
#define KEY_Q 113 // code of 'q'
typedef struct {
char **options;
int num_options;
int selected;
char *title;
} Dropdown;
void init_terminal() {
struct termios term;
tcgetattr(STDIN_FILENO, &term);
term.c_lflag &= ~(ICANON | ECHO); // Disable canonical mode and echo
tcsetattr(STDIN_FILENO, TCSANOW, &term);
}
void reset_terminal() {
struct termios term;
tcgetattr(STDIN_FILENO, &term);
term.c_lflag |= (ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &term);
}
Dropdown *create_dropdown(char *title, char **options, int num_options) {
Dropdown *dropdown = (Dropdown *)malloc(sizeof(Dropdown));
if (!dropdown)
return NULL;
dropdown->options = options;
dropdown->num_options = num_options;
dropdown->selected = 0;
dropdown->title = title;
return dropdown;
}
void display_dropdown(Dropdown *dropdown) {
printf(CLEAR_SCREEN);
printf("%s\n\n", dropdown->title);
for (int i = 0; i < dropdown->num_options; i++) {
if (i == dropdown->selected) {
printf("%s> %s%s\n", HIGHLIGHT, dropdown->options[i], RESET);
} else {
printf(" %s\n", dropdown->options[i]);
}
}
}
int read_key() {
char c;
if (read(STDIN_FILENO, &c, 1) == 1) {
return c;
}
return 0;
}
void handle_arrow_key(Dropdown *dropdown) {
switch (read_key()) {
case KEY_UP:
if (dropdown->selected > 0) {
dropdown->selected--;
display_dropdown(dropdown);
}
break;
case KEY_DOWN:
if (dropdown->selected < dropdown->num_options - 1) {
dropdown->selected++;
display_dropdown(dropdown);
}
break;
}
}
int run_dropdown(Dropdown *dropdown) {
bool is_running = true;
display_dropdown(dropdown);
while (is_running) {
switch (read_key()) {
case KEY_ESC: // Check if this is an escape sequence for arrow keys
if (read_key() == '[') {
handle_arrow_key(dropdown);
} else {
// Plain ESC key was pressed - cancel selection
dropdown->selected = -1;
is_running = false;
}
break;
case KEY_ENTER:
// Confirm selection
is_running = false;
break;
case KEY_Q:
// Cancel selection
dropdown->selected = -1;
is_running = false;
break;
}
}
return dropdown->selected;
}
void free_dropdown(Dropdown *dropdown) { free(dropdown); }
int main() {
init_terminal();
char title[] = "Select an option:";
char *options[] = {"Option 1", "Option 2", "Option 3", "Option 4",
"Option 5"};
int num_options = sizeof(options) / sizeof(options[0]);
Dropdown *dropdown = create_dropdown(title, options, num_options);
int selected = run_dropdown(dropdown);
// Clear screen and show result
printf(CLEAR_SCREEN);
if (selected >= 0)
printf("You selected: %s\n", options[selected]);
else
printf("Selection cancelled\n");
free_dropdown(dropdown);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment