gocliselect

package module
v0.0.0-...-1c8d4d7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2025 License: MIT Imports: 4 Imported by: 1

README

Golang CLI Select

Lightweight interactive CLI selection library

Import the package

import "github.com/nexidian/gocliselect"

Usage

Create a new menu, supplying the question as a parameter

menu := gocliselect.NewMenu("Chose a colour")

Add any number of options by calling AddItem() supplying the display text of the option as well as the id

menu.AddItem("Red", "red")
menu.AddItem("Blue", "blue")
menu.AddItem("Green", "green")
menu.AddItem("Yellow", "yellow")
menu.AddItem("Cyan", "cyan")

To display the menu and away the user choice call Display()

choice := menu.Display()

Example

package main

import (
    "fmt"
    "github.com/nexidian/gocliselect"
)

func main() {
    menu := gocliselect.NewMenu("Chose a colour")

    menu.AddItem("Red", "red")
    menu.AddItem("Blue", "blue")
    menu.AddItem("Green", "green")
    menu.AddItem("Yellow", "yellow")
    menu.AddItem("Cyan", "cyan")

    choice := menu.Display()

    fmt.Printf("Choice: %s\n", choice)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ID

type ID interface {
	any
}
type Menu[V ID] struct {
	Prompt             string
	CursorPos          int
	MenuItems          []*MenuItem[V]
	SelectedItemColour int
}

func NewMenu

func NewMenu[V ID](prompt string, selectedItemColour int) *Menu[V]
func (m *Menu[V]) AddItem(option string, id V) *Menu[V]

AddItem will add a new menu option to the menu list

func (m *Menu[V]) Display() (output V)

Display will display the current menu options and awaits user selection It returns the users selected choice

type MenuItem[V ID] struct {
	Text    string
	ID      V
	SubMenu *Menu[V]
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL