forked from workshopper/workshopper
-
Notifications
You must be signed in to change notification settings - Fork 5
/
menu.js
70 lines (53 loc) · 1.76 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
const tmenu = require('terminal-menu')
, path = require('path')
, fs = require('fs')
, xtend = require('xtend')
, EventEmitter = require('events').EventEmitter
const repeat = require('./term-util').repeat
, bold = require('./term-util').bold
, italic = require('./term-util').italic
function showMenu (opts) {
var emitter = new EventEmitter()
, menu = tmenu(xtend({
width : opts.width
, x : 3
, y : 2
}, opts.menu))
menu.reset()
menu.write(bold(opts.title) + '\n')
if (typeof opts.subtitle == 'string')
menu.write(italic(opts.subtitle) + '\n')
menu.write(repeat('-', opts.width) + '\n')
opts.problems.forEach(function (name) {
var isDone = opts.completed.indexOf(name) >= 0
, m = '[COMPLETED]'
name = name
if (isDone)
return menu.add(bold('»') + ' ' + name + Array(63 - m.length - name.length + 1).join(' ') + m)
else
menu.add(bold('»') + ' ' + name)
})
menu.write(repeat('-', opts.width) + '\n')
menu.add(bold('HELP'))
if (opts.prerequisites)
menu.add(bold('PREREQUISITES'))
if (opts.credits)
menu.add(bold('CREDITS'))
menu.add(bold('EXIT'))
menu.on('select', function (label) {
var name = label.replace(/(^[^»]+»[^\s]+ )|(\s{2}.*)/g, '')
menu.close()
if (name === bold('EXIT'))
return emitter.emit('exit')
if (name === bold('HELP'))
return emitter.emit('help')
if (name === bold('CREDITS'))
return emitter.emit('credits')
if (name === bold('PREREQUISITES'))
return emitter.emit('prerequisites')
emitter.emit('select', name)
})
menu.createStream().pipe(process.stdout)
return emitter
}
module.exports = showMenu