Skip to content

HalfasleepDev/c-modular-cli-menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Modular CLI Menu for c

Overview

This C program provides a simple console-based menu system with navigation and selection capabilities. It allows users to navigate through menu options using numeric keys and select an option using the ENTER key. The selected option is then returned for further processing.

Features

  • Displays a formatted menu with a customizable header.
  • Highlights the selected menu option in purple.
  • Allows navigation using numeric keys.
  • Detects and processes the ENTER key to confirm a selection.
  • Clears the screen upon selection for a seamless user experience.

Files

  • menu.c: Contains the implementation of the menu system.
  • menu.h: (Not provided, but expected to contain function prototypes and necessary macros.)

Usage

Function Definitions

int menu(const char header[], const char menuElements[][50], const char endPhrase[], int numOfElements);

This function displays a menu and allows the user to navigate and select an option.

Parameters:

  • header[]: The title/header of the menu.
  • menuElements[][50]: Array of menu options (strings).
  • endPhrase[]: A closing phrase to be displayed when exiting the menu.
  • numOfElements: Number of menu options available.

Returns:

  • The selected menu option index (1-based).

char getch();

This function captures a single character input without requiring the user to press ENTER.

Returns:

  • The numeric value of the key pressed.

Compilation & Execution

To compile the program, use:

gcc menu.c -o menu

To execute:

./menu

Example Usage

#include "menu.h"

int main() {
    const char header[] = "Main Menu";
    const char menuElements[][50] = {"Option 1", "Option 2", "Exit"};
    const char endPhrase[] = "Goodbye!";
    int numOfElements = 3;

    int choice = menu(header, menuElements, endPhrase, numOfElements);
    printf("You selected option: %d\n", choice);
    //clear buffer when additional input is used (loop)
    //while ((getchar()) != '\n');
    return 0;
}

Dependencies

  • Standard C libraries: <stdio.h>, <termios.h>, <unistd.h>, <stdlib.h>, <string.h>.

Notes

  • The system("clear") command is used to refresh the console, which may need adjustment for non-UNIX systems.
  • The function getch() modifies terminal attributes to allow single-character input.

Author

Created by HalfAsleepDev

Version

  • 1.0 (Last update: February 11, 2025)
  • 2.0 (Last update: February 13, 2025)
    • BUG FIXES Arrow key navigation, Clear buffer fix

TODO:

  • Start working on adv-menu (Last update: February 15, 2025)
    • Make the menu mostly modular
  • Start working on Ver 3.0
  • Seperate simple and advanced menus?
  • Re-orginize files
  • Create new README.md files
  • Seperate basic and advenced menu types

About

A cli menu for c because I'm too lazy to recreate menus for assignments.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages