-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathControlPort.js
More file actions
122 lines (90 loc) · 2.56 KB
/
Copy pathControlPort.js
File metadata and controls
122 lines (90 loc) · 2.56 KB
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
var net = require('net');
function ControlPort(prompt_) {
var p = prompt_ || 'Node';
this.prompt = p+'> \033[22;39m';
this.color = 'yellow';
this.commands = {};
this.menu = '---- Menu ----\n\n';
}
ControlPort.prototype.start = function (port) {
this.port = port;
var self = this;
net.createServer(function (socket) {
socket.write(colorizePrompt(self.prompt,self.color));
socket.on('data', function (data) {
self.handler(data,socket);
});
}).listen(this.port);
this.register('menu',function() {
return this.menu+'\n';
});
}
ControlPort.prototype.handler = function (data,socket) {
var parse = data.toString().substring(0, data.length-1).split(/\s+/g);
var command = parse.shift();
if(parse[0] === '') parse.shift();
if( this.commands[command] === undefined) {
socket.write('\n'+colorizePrompt(this.prompt,this.color));
return;
}
detectTypes(parse);
console.log(parse);
var out = this.commands[command].apply(this, parse);
socket.write(out+'\n'+colorizePrompt(this.prompt,this.color));
}
ControlPort.prototype.addToMenu = function (name, args,descrip) {
var command = ' '+name;
for(var i = 0; i < args.length; i++) {
if(args[i] === '') continue;
command += ' <'+args[i]+'>';
}
command += ' - '+descrip;
this.menu += command+'\n\n';
}
ControlPort.prototype.register = function(name, callback, descrip) {
if(name === 'menu') {
delete this.commands['menu'];
}
this.commands[name] = callback;
if(descrip !== undefined) {
this.addToMenu(name,extractArguments(callback.toString()),descrip);
}
}
//Helper functions
function detectTypes (args) {
for(var i = 0; i < args.length; i++) {
if(typeof args[i] == 'object') continue;
//TODO: this won't detect cases of NumString. parseFloat() chops String and returns Num
if(typeof args[i] == 'string') {
if(!isNaN(parseFloat(args[i]))) {
args[i] = parseFloat(args[i]);
}
}
}
}
function extractArguments (string) {
string = string.replace('function','');
var args = string.substring(0,string.search('{')).replace(/^\s*\(/g,'').replace(/\)\s*$/g, '').split(',');
for(var i = 0; i < args.length; i++) {
args[i] = args[i].replace(/^\s+/g,'').replace(/\s+$/g,'');
}
return args;
}
function colorizePrompt(prompt,color) {
if(Colors[color] === undefined) {
color = 'yellow'
}
return Colors[color] + prompt;
}
//Available Colors
var Colors = {
yellow : '\033[1;33m',
blue : '\033[1;34m',
cyan : '\033[1;36m',
green : '\033[1;32m',
magenta: '\033[1;35m',
red : '\033[1;31m',
white : '\033[1;37m',
grey : '\033[1;90m'
};
exports.ControlPort = ControlPort;