Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape sequences are printed when using custom output stream #651

Closed
krzsal opened this issue Feb 15, 2021 · 6 comments
Closed

Escape sequences are printed when using custom output stream #651

krzsal opened this issue Feb 15, 2021 · 6 comments

Comments

@krzsal
Copy link

krzsal commented Feb 15, 2021

Hi,
I am upgrading to jline3 from jline2. I solved most of the issues but I cannot get rid of few cursor control escape sequences:
[?1h= and [?1l>[?1000l.
Those sequences are printed when application is started on linux, macOS or git-bash in windows. Starting application from windows cmd or power shell will not print those escape sequences. The issue occures when custom output stream is used, the System.out works perfectly fine.
In the descibed case I needed to remove >.... but setting terminal size explicitly solved that. Also I am already using option (BRACKETED_PASTE, false).

Terminal terminal = TerminalBuilder.builder()
                .system(isSystem) // is false when issue occures
                .streams(inputStream,outputStream)
                .size(new Size(160,50))
                .build();

LineReader reader = LineReaderBuilder.builder()
                .terminal(terminal)
                .option(LineReader.Option.BRACKETED_PASTE, false)
                .build();

On git-bash the terminal type is xterm and uses ExternalTerminal. For unix, type is xterm-256color and uses PosixPtyTerminal.

I checked other threads but neither solution worked, maybe I missed something.
Is there any other way to remove those escape sequences?

@mattirn
Copy link
Collaborator

mattirn commented Feb 15, 2021

When you use custom streams I think you should create your terminal as

Terminal terminal = TerminalBuilder.builder()
                .streams(inputStream,outputStream)
                .size(new Size(160,50))
                .type(Terminal.TYPE_DUMB)
                .build();

@krzsal
Copy link
Author

krzsal commented Feb 16, 2021

Thanks for the help, setting Terminal.TYPE_DUMB did the trick and escape sequences are no longer displayed. However a side effect appeared. Now the command is printed again in new line, e.g.

prompt> command
command
command_output

Is there any way to disable this echoing?

@mattirn
Copy link
Collaborator

mattirn commented Feb 16, 2021

Ok, dumb terminal is rather primitive to be used in interactive session. You might want to create a real terminal, see an example in ShellFactoryImpl.java#L119-L223.

@krzsal
Copy link
Author

krzsal commented Feb 18, 2021

I tried to create terminal with same attributes/flags as the one that is created by cmd/powershell but the escape sequence characters are still printed. It looks like the different settings are applied depending of the os terminal from which application is started. E.g. starting application from cmd/powershell/2-click sets terminal type to null and the jline works perfect. However, starting from git-bash, sets terminal type to xterm and the problem with escape sequences appears. Also I tried to set terminal type to windows-vtp (this is used when using system streams) but then in both cases get escape character printed. I do not know if this is relevant but the output stream prints to javafx elements. I think that starting application from git-bash sets unix-like properties in the jvm from witch jline gets properties.

@mattirn
Copy link
Collaborator

mattirn commented Feb 19, 2021

If terminal type is not set when building terminal then TerminalBuilder will get type from environment variable TERM. In git-bash TERM default value is xterm and that's why TerminalBuilder builds for you terminal type xterm.

When you build terminal using custom streams and you do not have environment variable TERM (cmd/powershell/2-click) then I think TerminalBuilder will build terminal type ansi. You can print the terminal type to be sure

            System.out.println(terminal.getName() + ": " + terminal.getType());

When using git-bash/unix try to build your terminal as

Terminal terminal = TerminalBuilder.builder()
                .streams(inputStream,outputStream)
                .size(new Size(160,50))
                .type("ansi")
                .build();

@krzsal
Copy link
Author

krzsal commented Feb 19, 2021

The "ansi" type solved all the problems, thanks a lot for help.

When you build terminal using custom streams and you do not have environment variable TERM (cmd/powershell/2-click) then I think TerminalBuilder will build terminal type ansi. You can print the terminal type to be sure

        System.out.println(terminal.getName() + ": " + terminal.getType());

FYI the terminal.getType() returns null and it uses org.jline.terminal.impl.ExternalTerminal. In all other cases the type was returned properly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants