Menu

Home

alhambra1

Welcome to jsTerminal wiki!

How to Add Commands at Initialization
Escaping HTML
Environment Variables Within Added Commands
Batch Files



HOW TO ADD COMMANDS AT INITIALIZATION

Examples:

(1) Terminal initialization with added command "myCommand", eval method:

When the command's "eval" property is set to true, more than one variable parameter may be declared between the function's initial parentheses. The same eval method is used by the terminal for commands inserted dynamically.

var terminal = new terminal({
    width: 500,
    height: 200,
    welcomeMessage: 'Welcome to our terminal!'
})

(Command object names must be in lowercase)

terminal.C.Terminal.System.cmd.command.mycommand = {
  name: 'mycommand',
  summary: 'returns a+b',
  help: 'Returns a+b.\nSyntax: myCommand A B',
  eval: true,
  execute: function(a,b){
             return a+b
           }
}



(2) Terminal initialization with added command "myCommand", default method:

When the command's "eval" property is not set, parameters after the command-name are passed by the terminal to the function as one array. (Any statement between double quotes after the command name is passed as one element in the parameter-array, and the outer quotes are removed, allowing for escaped double quotes.)

var terminal = new terminal({
    width: 500,
    height: 200,
    welcomeMessage: 'Welcome to our terminal!'
})

terminal.C.Terminal.System.cmd.command.mycommand = {
  name: 'mycommand',
  summary: 'returns a+b',
  help: 'Returns a+b.\nSyntax: myCommand A B',
  execute: function(parameter_array){
             return Number(parameter_array[0]) + Number(parameter_array[1])
           }
}



(3) Terminal initialization with added command "myCommand", whole-line-as-parameter method:

When the command's "passWholeLineAsParameter" property is set to true, text after the command-name is passed by the terminal to the function as one string (only available in default method, not when using param object).

var terminal = new terminal({
    width: 500,
    height: 200,
    welcomeMessage: 'Welcome to our terminal!'
})

terminal.C.Terminal.System.cmd.command.mycommand = {
  name: 'mycommand',
  summary: 'Prints "[Statement] is what I always wanted to do!"',
  help: 'Prints "[Statement] is what I always wanted to do!"'

        + '\nSyntax: myCommand [Statement]',
  passWholeLineAsParameter: true,
  execute: function(string){
             return string + '  is what I always wanted to do!'
           }
}



(4) Terminal initialization with added command "myCommand", with param object:

When the command's "param" object is populated an extra parameter is assessed by the terminal before passing values to the function, in effect allowing for two-word commands.

var terminal = new terminal({
    width: 500,
    height: 200,
    welcomeMessage: 'Welcome to our terminal!'
})

terminal.C.Terminal.System.cmd.command.mycommand = {
  name: 'mycommand',
  summary: 'with parameter ADD, returns a+b"',
  help: 'With parameter ADD, returns a+b"'

        + '\nSyntax: myCommand ADD A B',
  param: {
    add: function(parameter_array){
           return Number(parameter_array[0]) + Number(parameter_array[1])
         }
  }
}




ESCAPING HTML

Opening HTML tags ("<") and ampersands ("&") in strings returned by terminal functions are converted to html codes ("&lt;" and "&amp;") for div output unless escaped by "HTML" (the "HTML" escape is removed). For example:

terminal.C.Terminal.System.cmd.command.mycommand = {
  name: 'mycommand',
  summary: 'prints an HTML example',
  help: 'Prints an HTML example.\nSyntax: myCommand',
  execute: function(){
             return 'This is aHTML<br />break'
           }
}




ACCESSING TERMINAL ENVIRONMENT VARIABLES WITHIN ADDED COMMANDS

Terminal converts defined environment variables (enclosed in '%') to their assigned value before passing parameters to commands. To access terminal environment variables from inside a command's execute or param object, call the parseVariable function. For example:

terminal.C.Terminal.System.cmd.command.mycommand = {
  name: 'mycommand',
  summary: 'prints value of MyVariable',
  help: 'Prints value of MyVariable.\nSyntax: myCommand',
  execute: function(){
             return 'The value of MyVariable is: ' + parseVariable('%MyVariable%')

             /*
             Or alternatively:
             return parseVariable('The value of MyVariable is: %MyVariable%')
             */
           }
}




BATCH FILES

DOS style batch files can be created and processed by Terminal. Batch files can be created at initialization, or dynamically within the Terminal by opening a new file in the editor or creating a string-type object method with the ending ".bat".

Batch Dynamic Example

cd Window:\terminal
EDIT my_batch_file.bat

echo Welcome to my batch file
set /p name=What is your name?
echo Hi %name%! & echo. & echo Goodbye.

[Ctrl+Q to save file and exit editor]

or

EVAL terminal['my_batch_file.bat'] = 'echo Welcome to my batch file\n' +
                                     'set /p name=What is your name? \n' +
                                     'echo Hi %name%! & echo. & echo Goodbye.'



To run the batch file, you could type:

cd Window:\terminal
my_batch_file.bat


Batch at Initialization

var terminal = new terminal({
    width: 500,
    height: 200,
    welcomeMessage: 'Welcome to our terminal!'
})

terminal['my_batch_file.bat'] = 'echo Welcome to my batch file\n' +
                                'set /p name=What is your name? \n' +
                                'echo Hi %name%! & echo. & echo Goodbye.'




Project Admins:


MongoDB Logo MongoDB