climenu

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2024 License: MIT Imports: 11 Imported by: 0

README

climenu

README | 中文文档

GoDoc

The selection menu for the command line program in terminal.

Supports various terminals under Windows, Linux, and MacOS.

Use the up and down arrow keys to select, and use the left and right arrow keys to turn pages. <Esc> and <Ctrl>+C exit, <Enter> confirm.

Key <PageUp> and <PageDown> can also be turn pages.

Input regular characters to filter menu item names. Case insensitive.

pic

Install

go get -u github.com/wels99/climenu

Example

  • Example 1
...
m := climenu.New()
m.Add("name1", "note1", nil, func(e *climenu.Item) error {
    fmt.Println("selected: ", e)
    return nil
})
m.Add("name2", "note2", nil, func(e *climenu.Item) error {
    fmt.Println("selected: ", e)
    return nil
})
m.run()
...
  • Example 2
package main

import (
    "fmt"
    "math/rand"
    "strings"

    "github.com/wels99/climenu"
)

func main() {

    mitems := [][]string{
        {"item001", "note001"},
        {"item002", "note002"},
        {"item003", "note003"},
        {"item004", "note004"},
        {"item005", "note005"},
        {"item006", "note006"},
    }

    m := climenu.New()

    m.SetIndex(true)
    m.SetSelectIcon(" \u27A4 ") // '➤'
    m.SetMessage("select one:")
    m.SetPagesize(5)
    // m.SetSelectedStyle(climenu.Style_Red, climenu.Style_White_bg)
    // m.SetSelectedStyle(climenu.Style_Reverse)
    // m.SetDelimiter("|")
    // m.Seti18n("当前", "页", "方向键移动,回车确认")

    for _, v := range mitems {
        name := v[0]
        note := fmt.Sprintf("%s_%s", v[1], strings.Repeat("🍕", rand.Intn(10)))
        note2 := fmt.Sprintf("%s_%s", v[1], strings.Repeat("🍀", rand.Intn(20)))

        m.Add(name, note, v, func(e *climenu.Item) error {
            fmt.Println("selected: ", e)
            return nil
        })
        m.AddItem(climenu.Item{
            Name: name,
            Note: note2,
            Tags: v,
            Act: func(e *climenu.Item) error {
                fmt.Println("new item selected: ", e)
                return nil
            },
        })
    }

    m.Sort(func(i, j *climenu.Item) bool {
        return i.Name > j.Name
    })

    e, _ := m.Run()
    if e != nil {
        fmt.Println("return:", e)
        e.Act(e)
    }
}

Documentation

Index

Constants

View Source
const (
	Color_min = 30 + iota
	Color_Red
	Color_Green
	Color_Yellow
	Color_Blue
	Color_max
)
View Source
const (
	Style_Reverse   = "\033[7m"
	Style_Black     = "\033[30;1m"
	Style_Red       = "\033[31;1m"
	Style_Green     = "\033[32;1m"
	Style_Yellow    = "\033[33;1m"
	Style_Blue      = "\033[34;1m"
	Style_Purple    = "\033[35;1m"
	Style_Cyan      = "\033[36;1m"
	Style_White     = "\033[37;1m"
	Style_Black_bg  = "\033[40m"
	Style_Red_bg    = "\033[41m"
	Style_Green_bg  = "\033[42m"
	Style_Yellow_bg = "\033[43m"
	Style_Blue_bg   = "\033[44m"
	Style_Purple_bg = "\033[45m"
	Style_Cyan_bg   = "\033[46m"
	Style_White_bg  = "\033[47m"
)
View Source
const (
	KEY_ignore = iota
	KEY_up
	KEY_down
	KEY_left
	KEY_right
	KEY_escape
	KEY_enter
	KEY_ctrlC
	KEY_backspace
	KEY_pageup
	KEY_pagedown
	KEY_filterstring
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Item

type Item struct {
	Name string            // Menu item name, first item displayed
	Note string            // Menu item description, second item displayed
	Tags any               // Attached data
	Act  func(*Item) error // The execution function of menu items. The function parameter is a menu item
	// contains filtered or unexported fields
}

type LangTxt

type LangTxt struct {
	Cur  string // current index
	Page string // page
	Help string // help text

} // Footer information
type Menu struct {
	// contains filtered or unexported fields

} // Menu structure

func New

func New() *Menu

Create a new menu object

func (m *Menu) Add(name, note string, tag any, act func(*Item) error)

Add menu item

tag - Attached parameter; act - Execute function, function parameter is menu item

func (m *Menu) AddItem(i ...Item)

Add menu item

func (m *Menu) Run() (*Item, error)

Run menu, loop endlessly. Exit after selecting a menu item and executing it, or interrupt with CtrlC. Returns the currently selected menu item.

func (m *Menu) SetDelimiter(s string)

Set menu item field separator

func (m *Menu) SetIndex(si bool)

Is the sequence number of menu items displayed

func (m *Menu) SetMessage(msg string)

Set menu prompt messages

func (m *Menu) SetPagesize(n int)

Set the number of menu items displayed per page

func (m *Menu) SetSelectIcon(p string)

Set the indicator for the current selection to be at the front of the menu

func (m *Menu) SetSelectedColor(c int)

Set selected item color.

Deprecated: use 'SetSelectedStyle'.

func (m *Menu) SetSelectedStyle(styles ...string)

Set selected item style. Use regular expression `^\033\[[\d;]+m$` to check.

func (m *Menu) Seti18n(cur, page, help string)

Set language text for foot

func (m *Menu) Sort(s func(i, j *Item) bool)

Sort menu items. If the sort function is not executed, it will be displayed in the order of addition.

Jump to

Keyboard shortcuts

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