gocliselectv2

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2025 License: MIT Imports: 5 Imported by: 0

README

Golang CLI Select V2

Lightweight interactive CLI selection library

Menu Navigation

Key Action
up arrow / shift + tab Moves the cursor up
down arrow / tab Moves the cursor down
home Moves the cursor to the first item
end Moves the cursor to the last item
page up Moves the cursor up by pageSize steps (configurable)
page down Moves the cursor down by pageSize steps (configurable)
enter Returns the selected item
escape Returns an empty string without error
ctrl+c Returns an empty string with io.EOF error
ctrl+d Returns an empty string with io.EOF error

Menu Options

Field Description Method
selectedColor Defines the selected item text color. Default value is SelectedColor.YELLOW WithSelectedColor
pageSize Defines the number of steps to move the cursor when clicking page up/down. Default value is 3 WithPageSize
printShortcuts If false, then the item text won't be printed with the shortcut key prefix [key]. Default value is true WithPrintShortcuts

Import the package

import "github.com/ameenkh/gocliselectv2"

Usage

Create a new menu, supplying the question as a parameter

menu := gocliselect.NewMenu("Choose an operation")

You can change the menu options:

menu := gocliselectv2.NewMenu("Choose an operation", gocliselectv2.WithSelectedColor(gocliselectv2.RED), gocliselectv2.WithPageSize(3))

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

You can add also a shortcut key to any item (available shortcuts are letters and numbers only: [a-z] or [A-Z] or [0-9]):

menu.AddItemWithShortcutKey("List", "list", gocliselectv2.Key_l)
menu.AddItemWithShortcutKey("Get", "get", gocliselectv2.Key_g)
menu.AddItem("Post", "post")
menu.AddItem("Delete", "delete")

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

choice, err := menu.Display()

Example

package main

import (
	"errors"
	"fmt"
	"github.com/ameenkh/gocliselectv2"
	"io"
)

func main() {
	menu := gocliselectv2.NewMenu("Choose an operation", gocliselectv2.WithSelectedColor(gocliselectv2.RED), gocliselectv2.WithPageSize(3))

	menu.AddItemWithShortcutKey("List", "list", gocliselectv2.Key_l)
	menu.AddItemWithShortcutKey("Get", "get", gocliselectv2.Key_g)
	menu.AddItem("Post", "post")
	menu.AddItem("Delete", "delete")

	choice, err := menu.Display()
	if errors.Is(err, io.EOF) {
		fmt.Printf("interrupted...") // in case of ctrl+c or ctrl+d
	} else {
		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 KeyCode

type KeyCode byte
const (
	Key_a KeyCode
	Key_b
	Key_c
	Key_d
	Key_e
	Key_f
	Key_g
	Key_h
	Key_i
	Key_j
	Key_k
	Key_l
	Key_m
	Key_n
	Key_o
	Key_p
	Key_q
	Key_r
	Key_s
	Key_t
	Key_u
	Key_v
	Key_w
	Key_x
	Key_y
	Key_z

	Key_A
	Key_B
	Key_C
	Key_D
	Key_E
	Key_F
	Key_G
	Key_H
	Key_I
	Key_J
	Key_K
	Key_L
	Key_M
	Key_N
	Key_O
	Key_P
	Key_Q
	Key_R
	Key_S
	Key_T
	Key_U
	Key_V
	Key_W
	Key_X
	Key_Y
	Key_Z

	Key_0
	Key_1
	Key_2
	Key_3
	Key_4
	Key_5
	Key_6
	Key_7
	Key_8
	Key_9
)
type Menu struct {
	Prompt          string
	CursorPos       int
	MenuItems       []*MenuItem
	ShortcutKeysMap map[KeyCode]int
	// contains filtered or unexported fields
}

func NewMenu

func NewMenu(prompt string, opts ...MenuOption) *Menu
func (m *Menu) AddItem(option string, id string) *Menu

AddItem will add a new menu option to the menu list

func (m *Menu) AddItemWithShortcutKey(option string, id string, shortcutKey KeyCode) *Menu

AddItemWithShortcutKey will add a new menu option with shortcut key to the menu list

func (m *Menu) Display() (string, error)

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

type MenuItem struct {
	Text        string
	ID          string
	ShortcutKey KeyCode
}
type MenuOption func(menu *Menu)

func WithPageSize

func WithPageSize(size int) MenuOption

func WithPrintShortcuts

func WithPrintShortcuts(print bool) MenuOption

func WithSelectedColor

func WithSelectedColor(color SelectedColor) MenuOption

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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