-
Notifications
You must be signed in to change notification settings - Fork 7
/
menu.go
54 lines (47 loc) · 1023 Bytes
/
menu.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package ussd
import (
"fmt"
)
// Menu for USSD
type Menu struct {
Header, Footer string
Items []*menuItem
ZeroItem *menuItem
}
type menuItem struct {
Name string
Route route
}
// NewMenu creates a new Menu
func NewMenu() *Menu {
return &Menu{
Items: make([]*menuItem, 0),
}
}
// Add to USSD menu.
func (m *Menu) Add(name, ctrl, action string) *Menu {
item := &menuItem{name, route{ctrl, action}}
m.Items = append(m.Items, item)
return m
}
// AddZero adds an item at the bottom of USSD menu.
// This item always routes to a choice of "0".
func (m *Menu) AddZero(name, ctrl, action string) *Menu {
m.ZeroItem = &menuItem{name, route{ctrl, action}}
return m
}
// render USSD menu.
func (m Menu) render() string {
msg := StrEmpty
if m.Header != StrEmpty {
msg += m.Header + StrNewLine
}
for i, item := range m.Items {
msg += fmt.Sprintf("%d. %v"+StrNewLine, i+1, item.Name)
}
if m.ZeroItem != nil {
msg += "0. " + m.ZeroItem.Name + StrNewLine
}
msg += m.Footer
return msg
}