2

I've been looking through the documentation for jQuery Terminal but found nothing. Basically, I'm trying to echo the following:

jQuery Terminal

Here v0.8.7 should be a link, but when I try to echo the string using .echo(string,{raw:true}) the ASCII art gets distorted:

term.echo(
    '      __ _____                     ________                              __\n'+
    '     / // _  /__ __ _____ ___ __ _/__  ___/__ ___ ______ __ __  __ ___  / /\n'+
    ' __ / // // // // // _  // _// // / / // _  // _//     // //  \\/ // _ \\/ /\n'+
    '/  / // // // // // ___// / / // / / // ___// / / / / // // /\\  // // / /__\n'+
    '\\___//____ \\\\___//____//_/ _\\_  / /_//____//_/ /_/_/_//_//_/ /_/ \\__\\_\\___/\n'+
    '          \\/              /____/                                     <a href="http://terminal.jcubic.pl/" target="_blank" title="jQueryTerminal Website">v'+term.version()+'</a>',
    {raw:true}
);

I tried using 2 separate echo calls, but the version number is pushed to a new line. How can I add the version number at the end of the ASCII art without it going into a new line?

Here is my current code:

term.echo(
    '      __ _____                     ________                              __\n'+
    '     / // _  /__ __ _____ ___ __ _/__  ___/__ ___ ______ __ __  __ ___  / /\n'+
    ' __ / // // // // // _  // _// // / / // _  // _//     // //  \\/ // _ \\/ /\n'+
    '/  / // // // // // ___// / / // / / // ___// / / / / // // /\\  // // / /__\n'+
    '\\___//____ \\\\___//____//_/ _\\_  / /_//____//_/ /_/ /_//_//_/ /_/ \\__\\_\\___/\n'+
    '          \\/              /____/                                     '
);
term.echo('<a href="http://terminal.jcubic.pl/" target="_blank" title="jQueryTerminal Website">v'+term.version()+'</a>',{raw:true});

Live demo: http://suchmail.eu/Hunpony/djdavid98/cli
(use the command ver)

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103

3 Answers3

3

Okay this was quite interesting. Basically the jQuery Terminal echo function only operates in a few different modes that don’t mix methods within an echo call.

But there is a way to effectively do what you are looking for by using the finalize callback function as explained in the documentation for echo:

finalize — which is callback function with one argument the div container

That is key. If you look at the source HTML that is output from jQuery Terminal when it echo’s content you can see it is all basically a pile of <div> elements with a width of 100% and the ASCII inside of it.

<div style="width: 100%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;__&nbsp;_____&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;________&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;__</div>

So when you try to echo on the next line line this:

term.echo('<a href="http://terminal.jcubic.pl/" target="_blank" title="jQueryTerminal Website">v'+term.version()+'</a>',{raw:true});

What happens is another <div style="width: 100%;"> is generated which knocks the line down. And because it is raw:true it means that it cannot be mixed with the echo above it.

But by using finalize and acting on the div that contains the link you can do this:

term.echo('<a href="http://terminal.jcubic.pl/" target="_blank" title="jQueryTerminal Website">v'+term.version()+'</a>',
     { raw:true,
       finalize: function(div) {
          div
         .css("width", "720px")
         .css("text-align", "right")
         .css("margin-top", "-1em")
         ;
       }
     });

The magic is all CSS and jQuery setting the CSS. The width gets set to 720px, the text-align is set to the right now and then the margin-top is set to -1em so the line is pushed up exactly 1 line. Here is a screenshot of what it looks like with these adjustments in place:

enter image description here

So for all intents and purposes, it visually looks the same. The caveat of this method is you can never tell how browsers will behave to CSS like this. So be sure to test it in a few browsers. But if it chokes, just tweak the CSS that gets set in finalize and you should be able to come up with a combo that works well across browsers.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • Using the `finalize` option I found a solution without any need for extra CSS. Thank you for your help! –  Jun 22 '14 at 08:55
  • *(by the way, you're missing an "H" from "What happens")* –  Jun 22 '14 at 09:01
  • @DJDavid98 Fixed! Happy this worked for you! Rememeber, if this answer helped you, please reemmeber to up vote it. And if it is the answer that solved your problem, please remember to check it off as the answer. – Giacomo1968 Jun 22 '14 at 16:37
2

After reading through @Giacomo1968's answer, I managed to find the ultimate solution without any need for extra CSS.

term.echo(
    '      __ _____                     ________                              __\n'+
    '     / // _  /__ __ _____ ___ __ _/__  ___/__ ___ ______ __ __  __ ___  / /\n'+
    ' __ / // // // // // _  // _// // / / // _  // _//     // //  \\/ // _ \\/ /\n'+
    '/  / // // // // // ___// / / // / / // ___// / / / / // // /\\  // // / /__\n'+
    '\\___//____ \\\\___//____//_/ _\\_  / /_//____//_/ /_/_/_//_//_/ /_/ \\__\\_\\___/\n'+
    '          \\/              /____/                                           '.replace(new RegExp(" {" + (term.version().length+1) + "}$"), ''),
    {finalize: function($div){
        $div.children().last().append('<a href="http://terminal.jcubic.pl/" target="_blank" title="jQueryTerminal Website">v'+term.version()+'</a>')
    }}
);

Using the finalize option mentioned by him, I was able to add some additional HTML to the last <div> using jQuery.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
SeinopSys
  • 8,787
  • 10
  • 62
  • 110
  • You will get different looking version if string version change to `0.10.10`. In source code there this: `replace(new RegExp(" {" + version_string.length + "}$"), ' ');` so the version is always aligned to the right. – jcubic Jun 22 '14 at 13:56
2

The reason why this is happening is because \n in html is ignored, all whitespace characters are replaced by one space. In order to have this in one echo is to use <br /> instead of \n or wrap each line with <div>. This is how inserting html work, because terminal just append raw data. The other option is to use finalize, to alter text text, as other answers suggest.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
jcubic
  • 61,973
  • 54
  • 229
  • 402
  • 1
    This is 100% incorrect: `“The reason why this is happening is because \n in html is ignored, all whitespace characters are replaced by one space. In order to have this in one echo is to use
    instead of \n or wrap each line with
    .”` Did you read my answer or view the source of JQuery Terminal? It outputs each line with a `
    ` and deals with each space individually as ` `. Additionally when output is `raw` then it does none of that. But even then the user of jQuery Terminal cannot add `
    ` or a `
    ` to that raw output. Best option is `finalize`.
    – Giacomo1968 Jun 22 '14 at 16:52
  • @JakeGould He made jQuery Terminal –  Jun 22 '14 at 19:41
  • @DJDavid Oh, I didn't know that. The answer is still confusing to me in the way it is worded. – Giacomo1968 Jun 22 '14 at 20:00
  • @JakeGould the code simple ignore everything it do in echo and just insert html to the DOM (when there is raw option), and standard behavior in html is to merge all whitespaces on render (the newlines will not be ignored when there's css rule like for pre tag). – jcubic Jun 23 '14 at 09:02
  • @jcubic Fair enough. Thank you for the clarification. – Giacomo1968 Jun 23 '14 at 14:20