Skip to content

Commit

Permalink
Add an example of how to use multiple menus
Browse files Browse the repository at this point in the history
fixes #15
  • Loading branch information
Will Dixon committed Jan 2, 2018
1 parent 0a2aad3 commit 04748e3
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ menu := wmenu.NewMenu("What is your favorite food?")
menu.Action(func (opts []wmenu.Opt) error {fmt.Printf(opts[0].Text + " is your favorite food."); return nil})
menu.Option("Pizza", nil, true, nil)
menu.Option("Ice Cream", nil, false, nil)
menu.Option("Tacos", nil, false, func() error {
menu.Option("Tacos", nil, false, func(opt wmenu.Opt) error {
fmt.Printf("Tacos are great")
})
err := menu.Run()
Expand Down
76 changes: 76 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//package main is to show how multiple menus can be used with eachother
package main

import (
"errors"
"fmt"
"os"

"github.com/dixonwille/wmenu"
)

type menuItem int

const (
pizza menuItem = iota
iceCream
tacos
)

var menuItemStrings = map[menuItem]string{
pizza: "Pizza",
iceCream: "Ice Cream",
tacos: "Tacos",
}

func main() {
mm := mainMenu()
err := mm.Run()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func mainMenu() *wmenu.Menu {
menu := wmenu.NewMenu("What is your favorite food?")
menu.Option(menuItemStrings[pizza], pizza, true, nil)
menu.Option(menuItemStrings[iceCream], iceCream, false, nil)
menu.Option(menuItemStrings[tacos], tacos, false, func(opt wmenu.Opt) error {
fmt.Printf("Tacos are great!\n")
return nil
})
menu.Action(func(opts []wmenu.Opt) error {
if len(opts) != 1 {
return errors.New("wrong number of options chosen")
}

tm := toppingsMenu(opts[0].Value.(menuItem))
return tm.Run()
})
return menu
}

func toppingsMenu(favorite menuItem) *wmenu.Menu {
menu := wmenu.NewMenu(fmt.Sprintf("What is your favorite topping for %s?", menuItemStrings[favorite]))
if favorite == pizza {
menu.Option("Meat", nil, true, nil)
menu.Option("Cheese", nil, false, nil)
menu.Option("Vegitables", nil, false, nil)
}
if favorite == iceCream {
menu.Option("Fruit", nil, true, nil)
menu.Option("Chocolate Syrup", nil, false, nil)
menu.Option("Caramel Syrup", nil, false, nil)
}
menu.Action(func(opts []wmenu.Opt) error {
if len(opts) != 1 {
return errors.New("wrong number of options chosen")
}

fmt.Printf("Your favorite food is %s with %s on top.\n", menuItemStrings[favorite], opts[0].Text)

return nil
})
return menu
}

0 comments on commit 04748e3

Please sign in to comment.