forked from Pomax/terminal-menu-program
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.js
203 lines (194 loc) · 5.55 KB
/
menu.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
var createMenu = require("terminal-menu");
var is = require("is");
var copy = function(o) { return JSON.parse(JSON.stringify(o)); };
var Menu = function(name, program) {
this.setup(name, program);
};
Menu.prototype = {
setup: function(name, program) {
this.name = name;
this.program = program;
this.operations = [];
this.selectables = [];
this.radios = {};
this._radios = {};
this.options = {};
this._options = {};
},
schedule: function(fn) {
setTimeout(fn.bind(this), 1);
},
cacheOptions: function() {
this._options = copy(this.options);
this._radios = copy(this.radios);
},
restoreOptions: function() {
this.options = copy(this._options);
this.radios = copy(this._radios);
this.selectables.forEach(function(op) {
if (op.type == 'checkbox') {
op.toggled = this.options[op.content];
} else if (op.type == 'radio') {
op.toggled = this.radios[op.content];
}
}.bind(this));
},
start: function() {
this.menu = createMenu({
width: process.stdout.columns - 4,
height: process.stdout.rows - 4,
x: 0,
y: 0
});
this.menu.reset();
this.menu.createStream().pipe(process.stdout);
},
reset: function() {
if(this.menu) {
this.menu.reset();
this.setup(this.name, this.program);
}
},
close: function() {
if(this.menu) {
this.menu.reset();
this.menu.close(true); // keep input tied to the menu system
}
},
text: function(s) {
var label = s + "\n";
this.operations.push({
type: "text",
content: label
});
},
spacer: function(s) {
this.text('');
},
option: function(s, n, callback) {
var op = { type: "option", content: s, data: s, screen: n, callback: callback };
if (is.object(s) && 'label' in s) {
op.content = s.label;
} else {
op.content = s;
}
this.addSelectable(op);
},
check: function(s, toggled, callback) {
if(typeof toggled === "function") {
callback = toggled;
toggled = false;
}
var op = { type: "checkbox", content: s, toggled: toggled, callback: callback };
this.addSelectable(op);
this.options[s] = toggled;
},
radio: function(s, toggled, callback) {
if(typeof toggled === "function") {
callback = toggled;
toggled = false;
}
var op = { type: "radio", content: s, toggled: toggled, callback: callback };
this.addSelectable(op);
this.radios[s] = toggled;
},
confirm: function(label, nextScreen, callback) {
if(typeof nextScreen === "function") {
callback = nextScreen;
nextScreen = false;
}
var op = { type: "confirm", content: label, screen: nextScreen, callback: callback };
this.addSelectable(op);
},
cancel: function(label, prevScreen, callback) {
if(typeof prevScreen === "function") {
callback = prevScreen;
prevScreen = false;
}
var op = { type: "cancel", content: label, screen: prevScreen, callback: callback };
this.addSelectable(op);
},
addSelectable: function(op) {
this.operations.push(op);
this.selectables.push(op);
},
handler: function(label) {
var entry, idx;
this.selectables.forEach(function(op, i) {
if(op.label === label) { entry = op; idx = i; }
});
if(entry) {
if(entry.type === "checkbox") {
entry.toggled = !entry.toggled;
this.options[entry.content] = entry.toggled;
if(entry.callback) { entry.callback(entry.toggled); }
return this.draw(idx);
}
if(entry.type === "radio") {
for (var key in this.radios) {
this.radios[key] = false;
}
this.selectables.forEach(function (op) {
if (op.type === "radio") {
op.toggled = false;
}
});
entry.toggled = true;
this.radios[entry.content] = true;
if(entry.callback) { entry.callback(entry.toggled); }
return this.draw(idx);
}
if(entry.type === "option") {
this.departureidx = idx;
if(entry.screen) {
this.schedule(function() {
this.program.run(entry.screen);
});
}
if(entry.callback) {
entry.callback(entry.data);
}
}
if(entry.type === "confirm") {
if(entry.callback) {
entry.callback(copy({options: this.options, radios: this.radios}));
}
if(entry.screen) {
this.program.run(entry.screen);
}
}
if(entry.type === "cancel") {
this.restoreOptions();
if(entry.callback) {
entry.callback(copy(this.options));
}
if(entry.screen) {
this.program.run(entry.screen);
}
}
}
},
draw: function(optionidx) {
this.close();
this.start();
var menu = this.menu;
if(arguments[0] === undefined) { this.cacheOptions(); }
optionidx = optionidx || this.departureidx || this.defaultidx || 0;
delete this.departureidx;
this.operations.forEach(function(op, idx) {
var label = op.content;
switch(op.type) {
case "text": menu.write(label); break;
case "option":
case "confirm":
case "cancel": label = "[" + label + "]"; menu.add(label); break;
case "checkbox": label = "[" + (op.toggled ? '√' : ' ') + "] " + label; menu.add(label); break;
case "radio": label = "[" + (op.toggled ? '●' : ' ') + "] " + label; menu.add(label); break;
}
op.label = label;
if(idx === optionidx) { menu.jump(idx); }
});
menu.on('select', this.handler.bind(this));
}
};
module.exports = Menu;