Skip to content

About

Simple CLI menu to map nested menu options to functions

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

14 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gomenutree

Simple CLI menu to map nested menu options to functions

Usage

  • Import the package
    import "github.com/mikefrom1974/gomenutree"
  • Create your menu(s)
    mMain := gomenutree.NewMenu("Main", "myPrompt")
  • Add options -> functions
    mMain.AddOption("foo", foo)
  • Create your tree and add menus
    mTree := gomenutree.NewMenuTree(mMain)
    mTree.AddSubMenu(<parentMenu>, <childMenu>)
  • Display your menu
    mTree.Display()
  • Optionally set the current menu prompt
    mTree.SetPrompt("Please select one of the following:")

Notes

  • For simplicity, mapped functions are without parameters (to avoid interfaces and reflections, etc). The user is expected to handle persistent values on their own. If you must have parameters, use a function wrapper (see example).

Sample:

package main

import (
	"fmt"
	
	"github.com/mikefrom1974/gomenutree"
)

func main() {
	prompt := "Please select from the following:"
	
	mMain := gomenutree.NewMenu("test main", prompt, nil)
	mMain.AddOption("foo", foo)
	mMain.AddOption("bar", bar)

	mTree := gomenutree.NewMenuTree(mMain)

	mSub1 := gomenutree.NewMenu("simple sub", "", nil)
	mSub1.AddOption("baz", baz)
	mTree.AddSubMenu(mMain, mSub1)

	mSub2 := gomenutree.NewMenu("func w param", prompt, nil)
	mSub2.AddOption("enter", func() {
		p("staticParam")
	})
	mSub2.AddOption("examine", func() {
		p(mTree.Name() + " " + mTree.Prompt())
	})
	mTree.AddSubMenu(mMain, mSub2)

	mTree.Display()
}

func foo() {
	fmt.Println("foo")
}

func bar() {
	fmt.Println("bar")
}

func baz() {
	fmt.Println("baz")
}

func p(param string) {
	fmt.Println("parameter:", param)
}

Change Log (semantic versioning)

1.0.0

  • Added: Initial release

1.0.1

  • Fixed: incomplete line fill on redraw

1.0.2

  • Fixed: typos

1.0.3

  • Changed: selecting by hotkey now moves selection cursor

1.0.4

  • Fixed: redraw scrambles on small terminals (added toggle)

1.1.0

  • Added: ability to assign function to generate prompt

About

Simple CLI menu to map nested menu options to functions

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages

⌂ 换网址