-
-
Notifications
You must be signed in to change notification settings - Fork 577
Invoking Commands and terminal methods from Server
The echo command is quite powerful. With it, you can invoke the different commands from the server.
You can call code like this:
var term = $('body').terminal({
foo: function(a, b, c) {
console.log({a, b, c});
}
});
term.echo('[[ foo 1 2 /bar/ ]]');
and foo command will be executed where a
is number 1, b
is number 2 and c
is a regular expression.
this is the same as term.exec('foo 1 2 /bar/');
but the difference is that you can just return the string "[[ foo 1 2 3 ]]"
from JSON-RPC procedure. For you this you need to create interpreter that will have both JSON-RCP and object:
var term = $('body').terminal(["service.php", {
foo: function(a, b, c) {
console.log({a, b, c});
}
}]);
This will create an interpreter with both JSON-RPC service in PHP file and custom object.
With this setup, you can create a procedure "hello"
in JSON-RPC service that will return "[[ foo 1 2 3 ]]"
and when you type "hello" it will run console.log({a, b, c});
.
You can also return exit from JSON-RPC and when that procedure will be executed it will log out from the interpreter if you have a login or call pop if you're in the nested interpreter. There is also one feature that's disabled by default for security reasons (see security section in API reference). To execute terminal method, like for instance shortcut CTRL+R from the server, you can use this syntax:
[[ terminal::invoke_key("CTRL+R") ]]
the same goes to cmd instance methods, you can insert text and change cursor position to the beginning of the line, from server with this string:
[[ cmd::set("hello") ]][[ cmd::position(0) ]]
To enable this feature you need to set invokeMethods: true
option.
The only limitations are that you can't return value from this method, you can't return object and invoke a method on it (so you can't disable history but this may change). The other limitation is that arguments need to be valid JSON so this will not work:
[[ cmd::set('hello') ]]
$('body').terminal([
"service.py",
{
size: function(x) {
this.css('--size', x); // custom properties works in jQuery 3.2
}
}
});
In the above example terminal create two interpreters two objects one from JSON-RPC service and one object with one command size
(more about interpreters in Advanced jQuery Terminal Tutorial). With this setup, the server can return the string [[ size 1.5 ]]
and change the size of the terminal. You can write any method you like and invoke it from the server.
Library for Web-Based Terminal in JavaScript, (jQuery Terminal: git repo)
Copyright (c) 2011-2023 Jakub T. Jankiewicz