Write your commands

prtksxna edited this page Sep 13, 2010 · 1 revision
Clone this wiki locally

How to write you own commands?

Its pretty easy to write your own commands once you understand how the Terminal object parses the input and how other commands are written. The Terminal class has a Terminal.commands hash object which has hash objects inside it containing information about the command. The default commands are written into the object when the Terminal.addDefaultCommands() method is run after initialization. You can either write you command’s code inside this method or modify the Terminal.commands from your own methods.

Thus to make a new command you write -

  this.commands.set('command_name',{<attributes>};

Command Attributes

Name Value Description
help String Give a small description about the usage of the command. This is what will show up when the `help` function is run by the user
exec function(arguments) This is where you’ll write your command’s code. If your commands was preceded by any arguments, for example “git log” or “git diff SHA1”, the arguments will be passed to the function inside an array
Any other function function() If your commands needs multiple functions you can also make it a part of this hash

Thus, the whois command was a written like this -

  this.commands.set('whois',{help:'Gives you information about the user.',exec: function(){
    var whois = new Element('div',{class:'info'}).update(git.userdata.user.name+", "+git.userdata.user.company+" ("+git.userdata.user.login+") \t <a href=\'"+git.userdata.user.blog+"\'>"+git.userdata.user.blog+"<\/a> <br> <a class=\'green\' href=\'mailto:"+git.userdata.user.email+"\'>"+git.userdata.user.email+"<\/a>");
    git.addData(whois);
	}});

The git.addData(elem), method takes an elements which it renders onto the terminal. Here git is the name of the object that has inherited from Terminal, it has been set as global variable so that callbacks can be made easily when a commands uses the API.