/
menu.go
204 lines (166 loc) · 4.07 KB
/
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package utils
import (
"fmt"
"strconv"
"strings"
"github.com/Cristofori/kmud/types"
)
const decorator = "-=-=-"
type Menu struct {
actions []action
title string
exit bool
exitHandler func()
}
func ExecMenu(title string, comm types.Communicable, build func(*Menu)) {
pageIndex := 0
pageCount := 1
filter := ""
for {
var menu Menu
menu.title = title
menu.exit = false
build(&menu)
pageIndex = Bound(pageIndex, 0, pageCount-1)
pageCount = menu.Print(comm, pageIndex, filter)
filter = ""
prompt := ""
if pageCount > 1 {
prompt = fmt.Sprintf("Page %v of %v (<, >, <<, >>)\r\n> ", pageIndex+1, pageCount)
} else {
prompt = "> "
}
input := comm.GetInput(types.Colorize(types.ColorWhite, prompt))
if input == "" {
if menu.exitHandler != nil {
menu.exitHandler()
}
return
}
if input == ">" {
pageIndex++
} else if input == "<" {
pageIndex--
} else if input == ">>" {
pageIndex = pageCount - 1
} else if input == "<<" {
pageIndex = 0
} else if input[0] == '/' {
filter = input[1:]
} else {
action := menu.getAction(input)
if action.handler != nil {
action.handler()
if menu.exit {
if menu.exitHandler != nil {
menu.exitHandler()
}
return
}
} else if input != "?" && input != "help" {
comm.WriteLine(types.Colorize(types.ColorRed, "Invalid selection"))
}
}
}
}
type action struct {
key string
text string
data types.Id
handler func()
}
func (self *Menu) AddAction(key string, text string, handler func()) {
if self.HasAction(key) {
panic(fmt.Sprintf("Duplicate action added to menu: %s %s", key, text))
}
self.actions = append(self.actions,
action{key: strings.ToLower(key),
text: text,
handler: handler,
})
}
func (self *Menu) AddActionI(index int, text string, handler func()) {
self.AddAction(strconv.Itoa(index+1), text, handler)
}
func (self *Menu) SetTitle(title string) {
self.title = title
}
func (self *Menu) Exit() {
self.exit = true
}
func (self *Menu) OnExit(handler func()) {
self.exitHandler = handler
}
func (self *Menu) GetData(choice string) types.Id {
for _, action := range self.actions {
if action.key == choice {
return action.data
}
}
return nil
}
func (self *Menu) getAction(key string) action {
key = strings.ToLower(key)
for _, action := range self.actions {
if action.key == key {
return action
}
}
return action{}
}
func (self *Menu) HasAction(key string) bool {
action := self.getAction(key)
return action.key != ""
}
func filterActions(actions []action, filter string) []action {
var filtered []action
for _, action := range actions {
if FilterItem(action.text, filter) {
filtered = append(filtered, action)
}
}
return filtered
}
func (self *Menu) Print(comm types.Communicable, page int, filter string) int {
border := types.Colorize(types.ColorWhite, decorator)
title := types.Colorize(types.ColorBlue, self.title)
header := fmt.Sprintf("%s %s %s", border, title, border)
if filter != "" {
header = fmt.Sprintf("%s (/%s)", header, filter)
}
comm.WriteLine(header)
filteredActions := filterActions(self.actions, filter)
options := make([]string, len(filteredActions))
for i, action := range filteredActions {
index := strings.Index(strings.ToLower(action.text), action.key)
actionText := ""
if index == -1 {
actionText = fmt.Sprintf("%s[%s%s%s]%s%s",
types.ColorDarkBlue,
types.ColorBlue,
strings.ToUpper(action.key),
types.ColorDarkBlue,
types.ColorWhite,
action.text)
} else {
keyLength := len(action.key)
actionText = fmt.Sprintf("%s%s[%s%s%s]%s%s",
action.text[:index],
types.ColorDarkBlue,
types.ColorBlue,
action.text[index:index+keyLength],
types.ColorDarkBlue,
types.ColorWhite,
action.text[index+keyLength:])
}
options[i] = fmt.Sprintf(" %s", actionText)
}
width, height := comm.GetWindowSize()
pages := Paginate(options, width, height/2)
if len(options) == 0 && filter != "" {
comm.WriteLine("No items match your search")
} else {
comm.Write(pages[page])
}
return len(pages)
}