Skip to main content

Terminal Instance

Terminal instance is the main API that allow you to interact with the jQuery Terminal library. To access the terminal instance you can use return value from terminal call. Or access it with this keyword inside methods.

const term = $('body').terminal({
echo(...args) {
this.echo(args.join(' '));
}
}, {
checkArity: false,
greetings: false
});

term.echo('Welcome to my terminal');

In above code we call echo() method on terminal instance in term constant. We also use this.echo to implement echo command.

Common Tasks

Here is a list of most common task you can use with the Terminal. The full list of methods are in Reference Guide.

To print the text you use the echo() method. The first argument is a value that you want to echo and the second are options. More about echo method in What you can echo? Guide.

Changing the Command

To change the current command you can use set_command() method:

term.set_command('hello world);

Inserting Text Into the Command

You can also modify the command by inserting text at given cursor position, with term.insert('text');. You can change the cursor position and then insert the string.

term.set_command('heworld');
term.set_position(2);
term.insert('llo ');

See the Pen insert + position by Jakub T. Jankiewicz (@jcubic) on CodePen.

Getting The Command

To get the current command, you can use get_command() method.

See the Pen get_command by Jakub T. Jankiewicz (@jcubic) on CodePen.

Clear the Terminal

To clear the terminal you use clear() method or default clear command. To remove this default command. You can use clear: false option, as second argument to the terminal.

const term = $('body').terminal({}, {
clear: false
});

term.clear();

Destroy the Terminal

To destroy the terminal and clean the DOM, you can use destroy() method.

Reading Text from User

To read the data from the user you can use Nested Interpreter, or a syntax sugar in form of read() method. It returns a promise that resolve into the command the user type:

term.read('name: ').then(name => {
term.echo(`Your name is ${name}`);
});

Updating Lines