javascripthtmlprojectjquery-terminal

How can I make terminal prompt on jquery terminal save in local storage?


Im trying to find a way that when you try this command:

custprmt: function(pstr, ifbold) {

if(ifbold === '-makebold') {
     localStorage.setItem('myNEWprompt', pstr);
this.set_prompt('[[b;var;]' + pstr +' ')
}

else  {
             localStorage.setItem('myNEWprompt', pstr);
       this.set_prompt(pstr +' ')
       }

},

It will make a custom prompt based off what the user types, and save it to local storage. Then by using this code:

$(document).ready(function () {
   if (myNEWprompt) {
      set_prompt(myNEWprompt)
       } else {
        set_prompt('guest$ ')
      }
      });

It will load the custom prompt on page load. My issue is that the second set of code isnt loading the prompt that is saved. I feel like it might be because of the set_prompt but im not sure, ive tried this.set_prompt and prompt: but none works. Im not really sure what to do anymore, and I dont know if its even possible, so please help, your assistance will be much appreciated :)


Solution

  • It looks like you want to have command with options. There is helper methods for that $.terminal.parse_options().

    You're code can look like this:

    $('body').terminal({
      custprmt(...args) {
        const options = $.terminal.parse_options(args);
        if (options._.length !== 1) {
          this.error('Usage: custprmt prompt [options]');
        } else {
          let prompt = options._[0];
          if (options.makebold) {
            prompt = '[[b;white;]' + prompt + ']';
          }
          localStorage.setItem('myNEWprompt', prompt);
          this.set_prompt(prompt);
        }
      }
    }, {
        checkArity: false,
        greetings: false,
        completion: true,
        prompt: localStorage.getItem('myNEWprompt') ?? 'guest$ '
    });
    

    but note that options with long names require a double dash. So you need to type:

    custprmt "hello> " --makebold
    

    Quotes for the prompt are needed if you want to have a space after the prompt.

    Here is a working demo.

    When you parse options you have a similar syntax to Yargs.

    You have an object with each option as a field if the option is a flag it has a true or false value or if it's an option with an argument it has a string as a value. In case of a boolean option like in your case you can use a second argument to indicate that the option is boolean so it can be used before the normal argument.

    Example:

    $.terminal.parse_options(args, {boolean: ["makebold"]});
    

    When using this you can have a command that looks like this:

    custprmt --makebold "hello> "
    

    And the output will be the same, the makebold option will be a boolean flag.