-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
91 lines (87 loc) · 2.8 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
const Table = require("cli-table3")
const _ = require("underscore")
const getWidthPercentage = function (percentage) {
const terminalWidth = process.stdout.columns || 100
return Math.floor(terminalWidth * percentage / 100) - 1
}
const tableOpts = {
head: ['Command pattern', 'Example', 'Description'],
style: {
head: [], // disable colors in header cells
border: [] // disable colors for the border
},
colWidths: [
getWidthPercentage(30),
getWidthPercentage(30),
getWidthPercentage(40)
],
wordWrap: true
}
const getTableRow = function (command, example, description) {
return [
{ vAlign: 'center', content: command ? command.toString() : "" },
{ vAlign: 'center', content: example ? example.toString() : "" },
{ vAlign: 'top', content: description ? description.toString() : "" },
]
}
module.exports = function(angel){
angel.on("help", function(angel, next){
var $handlers = angel.reactor.$handlers
var table = new Table(tableOpts);
for(var i = 0; i<$handlers.length; i++) {
var originalPattern = $handlers[i].originalPattern
table.push(getTableRow(
originalPattern,
$handlers[i].example,
$handlers[i].description
))
}
console.log(table.toString())
next(null, table)
})
.example("$ angel help")
.description("brings short hand help with examples")
angel.on(/help (.*)$/, function(angel, next){
var $handlers = angel.reactor.$handlers
var table = new Table(tableOpts);
for(var i = 0; i<$handlers.length; i++) {
if($handlers[i].originalPattern.toString().match(angel.cmdData[1])) {
var originalPattern = $handlers[i].originalPattern
table.push(getTableRow(
originalPattern,
$handlers[i].example,
$handlers[i].description
))
}
}
console.log(table.toString())
next(null, table)
})
.example("$ angel help command")
.description("brings description of given command pattern (.*)")
angel.on("help.json", function(angel, next){
var $handlers = angel.reactor.$handlers
var json = []
for(var i = 0; i<$handlers.length; i++) {
var helpText = {}
var originalPattern = $handlers[i].originalPattern
helpText[originalPattern] = {
example: $handlers[i].example || "",
description: $handlers[i].description || ""
}
json.push(helpText)
}
console.log(JSON.stringify(json))
next(null, json)
})
.example("$ angel help.json")
.description("brings short hand help with examples in json")
angel.addDefaultHandler(function (input) {
if (input) {
console.info('(!) "' + input + '" was not found in available commands, run $ angel help for more info')
} else {
console.info('(!) command is required')
}
throw new Error('failed to execute: ' + input)
})
}