Skip to content

Commit

Permalink
Added automatic menu creation
Browse files Browse the repository at this point in the history
  • Loading branch information
maddenpj committed Nov 30, 2011
1 parent 511c5f4 commit f9e3f7b
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions ControlPort.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ function ControlPort(prompt_) {

this.prompt = '\033[1;33m'+p+'> \033[22;39m';
this.commands = {};
this.menu = '---- Menu ----\n';
}


Expand All @@ -17,7 +18,10 @@ ControlPort.prototype.start = function (port) {
self.handler(data,socket);
});
}).listen(this.port);


this.register('menu',function() {
return this.menu+'\n';
});
}


Expand All @@ -36,9 +40,35 @@ ControlPort.prototype.handler = function (data,socket) {
socket.write(out+'\n'+this.prompt);
}

ControlPort.prototype.addToMenu = function (name, args,descrip) {
var command = ' '+name;
for(var i = 0; i < args.length; i++) {
command += ' <'+args[i]+'>';
}
command += ' - '+descrip;
this.menu += command+'\n\n';
}

ControlPort.prototype.register = function(name, callback, descrip) {

if(name === 'menu') {
delete this.commands['menu'];
}

ControlPort.prototype.register = function(name, callback) {
this.commands[name] = callback;
if(descrip !== undefined) {
this.addToMenu(name,extractArguments(callback.toString()),descrip);
}
}

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;
}


exports.ControlPort = ControlPort;

0 comments on commit f9e3f7b

Please sign in to comment.