2

I'm currently using a piped input/output stream principe to read from and write to a shell channel using JSCH.

Problem: I'm getting weird characters in my jQuery terminal when I transport the response of the shell to jQuery terminal.

Weird characters (jQuery terminal): enter image description here

I tried reading the piped input stream in different ways to solve the problem:

Try 1:

private synchronized String readResponse() throws IOException {
    byte[] array = new byte[pin.available()];
    pin.read(array);
    return new String(array, Charset.forName("UTF-8"));
}

Try 2:

private synchronized String readResponse() throws IOException {
        final StringBuilder s = new StringBuilder();
        while(pin.available() > 0) {
            s.append((char) pin.read());
        }
        return s.toString();
    }

Sadly this problem still persist. Can anyone help me?

Update: I just found out that, when I print the same string in my Java output console, it works.

Java's System.out: enter image description here

Update 2: I've imported the unix_formatting.js file and it's almost fixed. The thing with this file is that it has limited support for unix escape codes. Link to js: https://unpkg.com/jquery.terminal@1.23.2/js/unix_formatting.js

Currently looks like this: enter image description here

How can I fix this problem in jQuery terminal?

Displee
  • 670
  • 8
  • 20
  • 1
    Because jQuery Terminal supports limited number of ANSI escapes codes, you can just include unix_formatting.js file. – jcubic Nov 29 '18 at 15:40
  • @MartinPrikryl you can reopen because issue is in front-end and duplicate is for UI text field not related to html. – jcubic Nov 29 '18 at 17:07
  • I closed this issue because the possible duplicated thread led me to jQuery's terminal unix_formatting.js. I imported the file and it's kind of fixed. But it's not completely fixed because the unix_formatting.js supports a limited amount of ANSI escape codes. – Displee Nov 29 '18 at 18:59
  • Reposting the link: See [Displaying Midnight Commander screen in JTextPane](https://stackoverflow.com/q/28211577/850848). – Martin Prikryl Nov 29 '18 at 19:42
  • I've updated the OP. – Displee Nov 29 '18 at 20:20
  • 1
    With unix_formatting you can only have static output, if you want real tty you can try xterm.js see my answer. – jcubic Nov 29 '18 at 20:38

1 Answers1

2

The problem you're having is that by default jQuery Terminal don't support ANSI escape codes but you can enable basic support by including unix_formatting.js file, the file is created using UMD so you can import it and have it in Webpack or Rollup bundle or include it using script tag:

Simplest is using unpkg.com:

<script src="https://unpkg.com/jquery.terminal/js/unix_formatting.js"></script>

you can also use same CDN as for main files (jsDeliver or cdnJS)

I think that cdn.rawgit.com will work until October 2019 so it's better to not use that one.

But, the one thing that jQuery Terminal can't do so is that you will never be able to display interactive shell command in Terminal in browser. The library was created for writing your own commands in JavaScript with basic support for shell command in unix_formatting file.

If you want to have full unix terminal in browser the better choice will be xterm.js which is real tty. If you can run the server, which you probably can if you're running java then xterm.js will be much better. You can even run vi or emacs -nw with it.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
jcubic
  • 61,973
  • 54
  • 229
  • 402
  • Thanks for your xterm reference. I think that's the solution. However I tried implementing it, but I end up with a weird situation. Whenever I type one key in the terminal, it directly writes it to the server. Here's a gif: https://i.imgur.com/gP82rwX.gif This is how I've setup my terminal: https://hastebin.com/udobequheh.html What am I doing wrong? – Displee Nov 29 '18 at 23:35
  • Never mind, I got everything working like a charm. I ended up with this script: https://hastebin.com/yowigijipe.html Using xterm is the right solution. – Displee Nov 30 '18 at 00:15