user2225961
user2225961

Reputation: 43

JQuery Terminal term.echo from another function?

I am trying to use: http://terminal.jcubic.pl/

I want to be able to call term.echo from another function to be able to place data inside the terminal, but I can not reference it.

Heres the code below:

jQuery(document).ready(function($) {
    var id = 1;
    $('body').terminal(function(command, term) {
        if (command == 'help') {
            term.echo("available commands are mysql, js, test");
        } else{
          term.echo("entered: " + command);
        }

    }, {
        greetings: "Shell",
        onBlur: function() {
            return false;
        }
    });
});

How can I access term.echo externally, so I can from like a button click call term.echo to add in data?

Upvotes: 1

Views: 1355

Answers (2)

jcubic
jcubic

Reputation: 66478

The value returned by the terminal is the same object as in interpter parameter (in both cases is jQuery object with additional terminal methods), so you can do something like this:

jQuery(function($) {
    var terminal = $('body').terminal(function(command, term) {
        if (command == 'help') {
            term.echo("available commands are mysql, js, test");
        } else{
          term.echo("entered: " + command);
        }

    }, {
        greetings: "Shell"
    });

    terminal.echo("Some text");
});

Upvotes: 1

chrmod
chrmod

Reputation: 1445

Simplest way is to use global variable as a reference for a term object. In your example that can look like follows:

// global placeholder for term object
var terminal = null;

jQuery(document).ready(function($) {
    var id = 1;
    $('body').terminal(function(command, term) {

        // setting up global reference
        terminal = term;

        if (command == 'help') {
            term.echo("available commands are mysql, js, test");
        } else{
          term.echo("entered: " + command);
        }

    }, {
        greetings: "Shell",
        onBlur: function() {
            return false;
        }
    });
});

The issue with this code is that it will load terminal after the ready event fired on document and you are never sure when that happens.

After document.ready fired you will be able to use terminal.echo(''); anywhere.

Upvotes: 1

Related Questions