Simple CLI menu to map nested menu options to functions
- 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:")
- 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).
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)
}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