javascriptjqueryjquery-terminal

Creating text prompts with jquery Terminal


I've read the docs and the article on jquery Terminal

https://itnext.io/how-to-create-interactive-terminal-like-website-888bb0972288

I'm using it for the GUI basically and not so much for the command line terminal properties. I just want to be able to use it for question/answer. So essentially to echo out a question and then read in an answer. A bit like those games that used to give options and then you would decide what to do next.

In the article the code:

<script>
$('body').terminal({
    hello: function(what) {
        this.echo('Hello, ' + what +
                  '. Wellcome to this terminal.');
    }
}, {
    greetings: 'My First Web Terminal'
});
</script>

Creates a greeting but you would need to know the command 'hello' exists so you could type hello world. And if you type an incorrect command you get an error "Command x not found".

Similarly, the code in the docs for reading in input is:

$('#term').terminal({
   name: function(name) {
      this.read('last name: ', last_name => {
         this.echo('Your name is ' + name + ' ' + last_name);
      });
   }
});

But you would need to know that the name command exists.

Is there an easy way to just setup a question/answer system without knowing what the commands would be in advance?

Also, didn't quite get the different uses for why you might want to create the interpreter as a function/object/string.

Thanks


Solution

  • You can call read outside of the terminal:

      const term = $('...').terminal();
    
      term.read('last name: ', last_name => {
         this.echo('Your name is ' + name + ' ' + last_name);
      });
    

    And if you want something more complex you can check the example Create Settings object from questions.