gocliselect

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2025 License: MIT Imports: 8 Imported by: 0

README

Golang CLI Select

Lightweight interactive CLI selection library

Import the package

import "github.com/mfutselaar/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")

If you wish to add a keybinding to an item, you can use AddItemWithInput() with the same parameters as AddItem(), but with an added rune.

menu.AddItemWithInput("Red", "red", 'r')
menu.AddItemWithInput("Blue", "blue", 'b')
menu.AddItemWithInput("Green", "green", 'g')
menu.AddItemWithInput("Yellow", "yellow", 'y')
menu.AddItemWithInput("Cyan", "cyan", 'c')

You can also add a divider by calling `AddDivider()

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")
    menu.AddDivider()
    menu.AddItem("Quit", "quit", 'q')

    choice, err := menu.Display()
    if err != nil {
        fmt.Printf("Error: %v\n", err)
    }

    if choice == "quit" {
        fmt.Println("You choose to quit")
    } else {
        fmt.Printf("Choice: %s\n", choice)
    }
}

Documentation

Index

Constants

View Source
const (
	ShowCursor = "\033[?25h"
	HideCursor = "\033[?25l"
	// CursorUpFormat Requires formatting with number of lines
	CursorUpFormat = "\033[%dA"
	ClearLine      = "\r\033[K"
	KeyUp          = byte(65)
	KeyDown        = byte(66)
	KeyEscape      = byte(27)
	KeyEnter       = byte(13)
)

Terminal control codes

Variables

View Source
var (
	ErrNoMenuItems = errors.New("menu has no items to display")
)
View Source
var NavigationKeys = map[byte]bool{
	KeyUp:   true,
	KeyDown: true,
}

NavigationKeys defines a map of specific byte keycodes related to navigation functionality, such as up or down actions.

Functions

This section is empty.

Types

type Menu struct {
	Prompt       string
	CursorPos    int
	ScrollOffset int
	MenuItems    []*MenuItem
}

func NewMenu

func NewMenu(prompt string) *Menu
func (m *Menu) AddDivider() *Menu
func (m *Menu) AddItem(option string, id interface{}) *Menu

AddItem will add a new menu option to the menu list

func (m *Menu) AddItemWithInput(option string, id interface{}, key rune) *Menu
func (m *Menu) Display() (interface{}, error)

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

type MenuItem struct {
	Text    string
	ID      interface{}
	SubMenu *Menu
	Divider bool
	Key     rune
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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