JQTerminal plugins are not hardcoded in the main logic of the application. Even after you have included - and properly initialized -
the terminal, it won't recognize any commands that you haven't explicitly added.
The commands incluided in the demo on the FAQ page are implemented separately. This was done with the intention of giving people the
chance to remove them, should they find these commands do not suit their needs.
First, include the necessary files, namely jqTerminal.css, jqTerminal.js, and optionally default.js if you plan to use the default commands.
Next, initialize the console:
var terminal = new Terminal("root@domain", "$","console-container"); terminal.initialize();
Finally, add the commands you want to use using the addCommandList
method.
For example, assuming you want to use the default commands,use the method like so:
terminal.addCommandList(defaultCommands);
...and you're done!
Alternatively, you can use the addCommand method if you want to add a single command to the terminal.
In order to write your own commands, you have to take two things into consideration.
Every command should have the following signature
function <name of your command> (arguments, self)
arguments
is a list containing the arguments that were used to invocate the command.
self
represents the terminal itself. You will need this if you intend to perform any operations on the console (like, for example writing on it).
Using self, you can access the methods the console exposes, which are the following:
initialize
- initializes the console.
clear
- clears all text on the console except for the promt and username.
write
- prints a given text on the console.
files
- returns a list of the files in the filesystem.
insertNewLine
- inserts a new line in the console.
commands
- returns the list of commands present on the console.
addCommand
- adds a given command to the console.
addCommandList
- adds a given set of commands to the console.
destroy
- destroys the console.
setEnvironmentVariable
- Sets an environment variable
getEnvironmentVariable
- Gets the value of an environment variable
In order for a command to be successfully added to the console, it should have, at least, the following sections
name
- a string containing the name of the command.man
- a string containing a description of what the command does does.run
- a function, containing the code that executes when the console runs the command. You should define your list of commands as follows
customCommands = [ {name:"command1",man:"command1 - do stuff",run:command1}, {name:"command2",man:"command2 - do more stuff",run:command2}, {name:"command3",man:"command3 - do even more stuff",run:command3} ];
And then, in your main page the commands to the list using the addCommandList
method:
terminal.addCommandList(customCommands);
That should be all you need to get your custom defined commands to work.