jline / jline3

JLine is a Java library for handling console input.
Other
1.46k stars 215 forks source link

How can I avoid escape characters in reader #190

Closed rdxnaor closed 6 years ago

rdxnaor commented 6 years ago

Hi, It seems that all the escape characters causing things at my end to get complicated. when I'm connecting to my app using ssh I'm getting special characters at the beginning of each promp, before every print I make and at the end of any print I make. can I somehow don't print them?

gnodet commented 6 years ago

Can you be a bit more specific ? Which characters are printed ? Can you make a copy/paste of the output (either copy the text or a picture maybe) ?

rdxnaor commented 6 years ago

for example - ^[[?1h^[=^[[?2004h another one is - ^[[?1l^[

image

rdxnaor commented 6 years ago

Ok, so I just overrided some problematic functions of LineReaderImpl and it seems ok. anyway I still have 2 questions about it :

  1. what does those character do and do you know what will happen if I remove them?
  2. Do I have another way to make those escape to disappear?

Thanks a lot!

gnodet commented 6 years ago

You clearly have a configuration problem. Those characters are ansi escape sequences that are used to control the terminal (cursor movement, etc...). The reason may be that you did not configure the ssh client or server correctly, as some informations need to be set correctly in order for jline to recognize the terminal and behave correctly. If you use Mina SSHD, you can find some very useful code at: https://github.com/jline/jline3/tree/master/remote-ssh/src/main/java/org/jline/builtins/ssh

rdxnaor commented 6 years ago

I'm not sure that I can change the ssh configuration (and I'm not sure what to change even if I could). please let me be more clear about how I use jline in my project. I have a remote machine that have Ubuntu on it. written down a cli using jline. afterwards I configured a user and when the user ssh inside the machine my program comes up and he gets into the cli.

I didn't use any of ssh java client and I'm not sure what I need to do in order to use it. Do you have an example to see how terminal, reader and ssh are combined and what is the actual result from it?

Thanks

gnodet commented 6 years ago

If you use a native ssh client, this part should be ok. It's the server side you need to care about. You need to setup JLine correctly according to the SSH session: https://github.com/jline/jline3/blob/master/remote-ssh/src/main/java/org/jline/builtins/ssh/ShellFactoryImpl.java#L119-L225

rdxnaor commented 6 years ago

Thanks! We are actually using JSch on the client side. any special configuration in this case?

gnodet commented 6 years ago

Not sure, you absolutely need to pass the TERM environement variable along with the pty modes. You also need to make sure your real terminal is in raw mode. You can also set callbacks to forward signals (terminal resize, ^C, etc...) https://github.com/jline/jline3/blob/master/remote-ssh/src/main/java/org/jline/builtins/ssh/Ssh.java#L168-L254

I haven't done any integration with JSCh, but maybe you'll find some code. Or you need to adapt the above.

Worst case, set the terminal type to dumb, but you'll loose most of the jline goodness.

Are you using a real terminal on the client side ?

rdxnaor commented 6 years ago

client side includes virtual and real terminal...

I have to say that I didn't really understand the ssh class part in all of this. my program includes mainly the following commands:

///////////////////////////////// Terminal terminal = TerminalBuilder.builder().signalHandler(Terminal.SignalHandler.SIG_IGN).build(); LineReader reader = LineReaderBuilder.builder() .completer(myCompleter) .terminal(terminal) .build(); String line = reader.readLine(promper.getPrompt(context.getCurrentDirectory())); ///////////////////////////////

After I get the line I do all the things I want to do. I understand that the reader have to get the terminal, and the reader does the actual read. I don't understand where the ssh part in all of this?

gnodet commented 6 years ago

Is the above code for the client or server side ? If from the server side, it's broken, because you need to use the streams from the ssh session. If from the client side, then you need to configure your terminal in raw mode before giving them to JSCh.

I suggest you study the jline demo a bit and try to replicate it with JSch if you really need to use it. Correctly integrating JLine with ssh is not an easy task to get right.