-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
137 lines (110 loc) · 3.11 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
/*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
'use strict'
require('events').EventEmitter.defaultMaxListeners = Infinity
/**
* simple CLI prompt module - with the emphasis on simple and non-invasive
* with command history and command completion
*/
module.exports = function (readline) {
var rl
var commands
function completer (line) {
var ml1 = []
var ml2 = []
var s = line.split(' ')
var keys = Object.keys(commands)
var command = null
var result
// if more than one token
if (s.length > 0) {
// check for exact match
keys.forEach(function (key) {
if (key === s[0]) {
command = key
}
})
// if exact match check sub command entries
if (command) {
if (commands[command].sub) {
commands[command].sub.forEach(function (sub) {
if (sub.indexOf(s[1]) === 0) {
ml2.push(command + ' ' + sub)
}
})
// no sub match so return all possibilities
if (ml2.length === 0) {
commands[command].sub.forEach(function (sub) {
ml2.push(command + ' ' + sub)
})
}
result = [ml2, line]
} else {
result = [[], line]
}
}
}
// command not matched
if (!command) {
keys.forEach(function (key) {
if (key.indexOf(s[0]) === 0) {
ml1.push(key)
}
})
result = [ml1.length ? ml1 : keys, line]
}
return result
}
function match (line) {
var keys = Object.keys(commands)
var command = null
var args = null
var s
line = line.trim()
line = line.replace(/\s+/g, ' ')
s = line.split(' ')
keys.forEach(function (key) {
if (key === s[0]) {
command = commands[key]
s.shift()
args = s
}
})
return {command: command, args: args}
}
function start (prmpt, cmds, cb) {
commands = cmds
rl = readline.createInterface({input: process.stdin,
output: process.stdout,
historySize: 50,
completer: completer,
prompt: prmpt})
rl.on('line', function (line) {
var result = match(line)
cb(result.command ? null : 'invalid command', result.command, result.args, line)
})
return rl
}
function displayPrompt () {
rl.prompt()
}
function stop () {
rl.close()
}
return {
start: start,
displayPrompt: displayPrompt,
stop: stop
}
}