-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
263 lines (229 loc) · 7.67 KB
/
index.js
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// menube function to create a menu using the file specified in menuFile
module.exports = function (menuFile, settings) {
settings = settings || {};
var path = require('path');
var events = require('events');
var emitter = new events.EventEmitter();
var menu = null; // the loaded menu
var activeMenu = menu; // reference to active menu branch
var currentSelect = [0]; // index of currently selected menu items
var topLine = 0; // the top most line of the menu to display
// Notes on currentSelect:
// The currentSelect array keeps track of both the currently selected menu item
// and the branch. The integer is the selected item and the depth of the array
// is the point out on a branch.
// I.E. The first element in currentSelect is an integer of the selected item
// in the root branch. Assuming the selected item is a submenu then activating
// this item will push a new integer onto the currentSelect array with the new
// integer being the item selected within the submenu.
// load the menu json file with recursive calls if needed
function loadMenu(menuFile) {
// load the menu file json
var menu = require(menuFile[0] === path.sep ? menuFile : process.cwd() + path.sep + menuFile);
// function to recursively check menu branches for further menu files to load
var loadMenuFiles = function (menu, i) {
if (i >= menu.length) {
// finished loading this menu file
return;
}
if (menu[i].menuFile) {
// this menu item is defined by another menu file
menu[i].menu = loadMenu(menu[i].menuFile);
}
// next menu item
loadMenuFiles(menu, i + 1);
};
if (Array.isArray(menu)) {
// load any menuFile items in this menu item array
loadMenuFiles(menu, 0);
}
return menu;
}
// activate the selected menu item
function activateSelect() {
var s = getCurrentSelect();
if (!s) {
return; // no menu item to activate
}
// submenu
if (s.menu) {
// submenu selected
currentSelect.push(0);
emitter.emit('menu_changed');
}
// execute command
else if (s.command) {
// shell command selected
require('child_process').exec(s.command, function (err, stdout, stderr) {
if (s.emit) {
emitter.emit(s.emit, err, stdout, stderr);
}
emitter.emit('menu_command');
});
}
// emit event
else if (s.emit) {
// emit event selected
var args = [];
if (typeof s.emit === "string") {
// simple event
args.push(s.emit);
}
else {
// complex event
args.push(s.emit.name);
if (s.emit.arguments) {
args = args.concat(s.emit.arguments);
}
}
emitter.emit.apply(emitter, args);
emitter.emit('menu_emit');
}
// dynamic options menu item from script
else if (s.options) {
// shell command to get options
require('child_process').exec(s.options, function (err, stdout, stderr) {
var options = stdout.split("\n");
var optionsMenu = [];
options.forEach(function (option) {
if (option.length) {
optionsMenu.push({ label: option, optionsItem: true });
}
})
// splice in the options at our options item
var am = getActiveBranch();
am.splice(currentSelect[currentSelect.length - 1], 0, {
label: s.label,
selectScript: s.selectScript,
emit: s.selectEmit,
menu: optionsMenu,
optionsMenu: true
});
activateSelect();
});
}
// options item selected
else if (s.optionsItem) {
var cs = getCurrentSelect();
var ps = getParentSelect();
_menuBack();
require('child_process').exec(ps.selectScript + ' ' + cs.label, function (err, stdout, stderr) {
if (ps.emit) {
emitter.emit(ps.emit, err, stdout, stderr);
}
emitter.emit('menu_command');
});
}
}
// move menu selection back from sub-menu
function menuBack() {
if (currentSelect.length > 1) {
_menuBack();
emitter.emit('menu_changed');
return true;
}
return false;
}
function _menuBack() {
var ps = getParentSelect();
currentSelect.pop();
if (ps.optionsMenu) {
// clean up dynamic options menu
var am = getActiveBranch();
am.splice(currentSelect[currentSelect.length -1], 1);
}
}
// move menu selection up through menu
function menuUp() {
var i = currentSelect[currentSelect.length - 1] - 1;
if (i < 0) {
i = 0;
return false;
}
currentSelect[currentSelect.length - 1] = i;
emitter.emit('menu_changed');
return true;
}
// move menu selection down through menu
function menuDown() {
var i = currentSelect[currentSelect.length - 1] + 1;
if (i >= getActiveBranch().length) {
i -= 1;
return false;
}
currentSelect[currentSelect.length - 1] = i;
emitter.emit('menu_changed');
return true;
}
// get the active menu branch
function getActiveBranch() {
return currentSelect.reduce(function (cur, val, ci) {
// before the last select item return the indexed menu branch, if at the end then return the final menu
return (ci + 1 < currentSelect.length ? cur.menu[val] : cur.menu);
}, {menu: menu});
}
// get active menu, trimmed if necessary
function getActiveMenu() {
var selected = currentSelect[currentSelect.length - 1];
var activeMenu = getActiveBranch();
activeMenu = JSON.parse(JSON.stringify(activeMenu));
activeMenu.map(function (item, index) {
if (index === selected) {
item.selected = true;
}
return item;
});
if (!settings.displayLines || activeMenu.length <= settings.displayLines) {
return activeMenu;
}
else {
return trimMenu(activeMenu, selected, settings.displayLines);
}
}
// trim menu to fit display
function trimMenu(targetMenu, selected, menuLength) {
// top trim at selection only if selected is past second item
var menuStart = selected > 1 ? selected : 0;
// prevent top trim from under displaying menu
menuStart = menuStart + menuLength - 1 <= targetMenu.length ? menuStart : targetMenu.length - menuLength + 1;
var finalMenu = (menuStart > 1 ? [{ label: (settings.moreUpLabel ? settings.moreUpLabel : "..."), menuMore: true }] : []);
finalMenu = finalMenu.concat(targetMenu.slice(menuStart, menuStart + menuLength - finalMenu.length));
if (finalMenu.length + menuStart <= targetMenu.length) {
finalMenu[finalMenu.length - 1] = { label: (settings.moreDownLabel ? settings.moreDownLabel : "..."), menuMore: true };
}
return finalMenu;
}
// get the parent selected menu item
function getParentSelect() {
if (currentSelect.length === 1) {
// at root, no parent
return null;
}
return currentSelect.reduce(function (cur, val, ci) {
return (
ci + 2 < currentSelect.length ?
cur.menu[val] : (
ci + 2 === currentSelect.length ?
cur.menu :
cur[currentSelect[currentSelect.length - 2]]
)
);
}, {menu: menu});
}
// get the currently selected menu item
function getCurrentSelect() {
return getActiveBranch()[currentSelect[currentSelect.length - 1]];
}
// export functions
emitter.menuUp = menuUp;
emitter.menuDown = menuDown;
emitter.activateSelect = activateSelect;
emitter.menuBack = menuBack;
emitter.getCurrentSelect = getCurrentSelect;
emitter.getParentSelect = getParentSelect;
emitter.getActiveMenu = getActiveMenu;
emitter.getActiveBranch = getActiveBranch;
// load menu in this instance
menu = loadMenu(menuFile);
return emitter;
};