How can I get a fully working 'echo' command in a Jquery terminal?

113 views Asked by At

Im trying to get a echo command to echo what the user types in a JQuery termial.

This is the code I have

<link href="https://unpkg.com/jquery.terminal/css/jquery.terminal.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://unpkg.com/jquery.terminal/js/jquery.terminal.min.js"></script>

<script>
$('body').terminal({

  echo: function(...args) {
      this.echo(args)
  },


}, {
    checkArity: false,
   
    prompt: '>>> ',
      strings: {
      commandNotFound: "-Error: unknown command: %s",
      echoCommand: true,
      wrongArity: "'%s' is missing one or more arguments",
    },
    pasteImage: true,
      
});
</script>

I tried to use the ...args method but when I do that, it will display what I typed, but add a bunch of spaces between them. For example, if i type: "echo hi bro how are you :)" it will return what I said in a this.echo saying the same thing but with 4 spaces between each word.

How can I remove those spaces so that it will directly show what the user typed.

2

There are 2 answers

1
Leroy On BEST ANSWER

Looking at the jQuery Terminal library, it looks like it behaves as expected.

Whatever you type after your command is considered an argument. The echo function of jQuery terminal converts that object to a readable output (tab separated).

If you type the following, each word is considered an argument of the echo command

>>> echo this is just a text
`this` is the first argument
`is` is the second argument
`just` is the third argument
etc...

You either have to quote the text you want to echo, just like any terminal.

echo "this is just a text"

Or you have to join all your arguments, but it deduped spaces between words.

echo: function(...args) {
   this.echo(args.join(" "));
}

This will product the following outputs

>>> echo this is just a text
this is just a text

>>> echo this   is   just    a text
this is just a text

>>> echo "this    is just    a"     text
this    is just    a text
0
jcubic On

@Leroy answer is correct the object interpreter with methods, parses the command for you and each command line argument is method argument.

You have two options use

  • args.join(' '), and rest operator (...args). For it to work, it may be required to use processArguments: false option, so arguments like numbers are not converted to real numbers.
  • this.get_command() and parse the command yourself if you care about whitespace between arguments.

You can use this code to parse the command. If you care about whitespace it may be easier to use function as interpreter, where you have more control.

<link href="https://unpkg.com/jquery.terminal/css/jquery.terminal.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://unpkg.com/jquery.terminal/js/jquery.terminal.min.js"></script>

<script>
$('body').terminal(function(command) {
    const cmd = $.terminal.parse_command(command);
    if (cmd.name == 'echo') {
        this.echo(cmd.rest);
    } else {
        this.error(`-Error: unknown command: ${cmd.name}`);
    }
}, {
    checkArity: false,
   
    prompt: '>>> ',
      strings: {
      commandNotFound: "-Error: unknown command: %s",
      echoCommand: true,
      wrongArity: "'%s' is missing one or more arguments",
    },
    pasteImage: true,
      
});
</script>