forked from workshopper/workshopper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.js
103 lines (77 loc) · 2.38 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
const tmenu = require('terminal-menu')
, path = require('path')
, fs = require('fs')
, xtend = require('xtend')
, EventEmitter = require('events').EventEmitter
, chalk = require('chalk')
, util = require('util')
const repeat = require('./util').repeat
, applyTextMarker = require('./util').applyTextMarker
, maxListenersPerEvent = 10
function showMenu (opts, i18n) {
var emitter = new EventEmitter()
, menu = tmenu(xtend({
width : opts.width
, x : 3
, y : 2
}, opts.menu))
, __ = i18n.__
, __n = i18n.__n
, menuStream
function writeLine() {
menu.write(repeat('\u2500', opts.width) + '\n')
}
function emit(event, value) {
return process.nextTick.bind(process, emitter.emit.bind(emitter, event, value))
}
function addEntry(entry) {
menu.add(applyTextMarker(entry.name, entry.marker || '', opts.width), emit(entry.event, entry.payload))
}
function addVariableEntry(variableEntry) {
if (!variableEntry)
return
if (util.isArray(variableEntry))
return variableEntry.forEach(addVariableEntry)
if (variableEntry.separator)
return writeLine()
addEntry(variableEntry)
}
menu.reset()
menu.write(chalk.bold(__('title')) + '\n')
if (i18n.has('subtitle'))
menu.write(chalk.italic(__('subtitle')) + '\n')
writeLine()
menu.setMaxListeners(opts.entries.length * maxListenersPerEvent)
opts.entries.forEach(addVariableEntry)
function regexpEncode(str) {
return str.replace(/([\.\*\+\?\{\}\[\]\- \(\)\|\^\$\\])/g, "\\$1")
}
function passDataToMenu(data) {
// Node 0.10 fix
menuStream.write(data)
}
menu.on('select', function (label) {
menu.y = 0
menu.reset()
menu.close()
process.stdin.pause()
process.stdin.removeListener('data', passDataToMenu)
menuStream.unpipe(process.stdout)
process.stdin.unpipe(menuStream)
process.stdin.setRawMode(false)
})
menuStream = menu.createStream()
process.stdin
.on("data", passDataToMenu)
menuStream.pipe(process.stdout, {end: false})
if(!process.stdin.isTTY) {
menu.reset()
console.error(__('error.notty'))
process.exit(1)
} else {
process.stdin.setRawMode(true)
}
process.stdin.resume()
return emitter
}
module.exports = showMenu