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

bug report: CLEAR_SCREEN is not portable #68

Closed
PiprTuff opened this issue Apr 27, 2022 · 4 comments
Closed

bug report: CLEAR_SCREEN is not portable #68

PiprTuff opened this issue Apr 27, 2022 · 4 comments
Assignees

Comments

@PiprTuff
Copy link

PiprTuff commented Apr 27, 2022

Current implementation of the CLEAR_SCREEN command returns only \033[2J which works on Windows but on not in many other modern terminals.

Most of the terminals used today are VT terminals, which includes most of the modern Linux and Mac terminals and the Windows Terminal available since Windows 10+. The escape sequence for clearing the screen and the scroll-back in a VT terminal is \033c. This should also work in the Windows Command Prompt if VT (support for ANSI) is enabled. This works exactly like calling clear in Linux or cls in Windows.

Some other Linux terminals, like the KDE Console (Konsole) and XTerm support \033[3J for clearing the screen and resetting the scroll-back.

In my opinion, \033c will be the most platform-independent implementation. But users should be able to provide custom escape sequence implementation for the CLEAR_SCREEN command.

Source: https://stackoverflow.com/a/5367075

@dialex dialex self-assigned this Apr 30, 2022
@dialex
Copy link
Owner

dialex commented May 8, 2022

So you say that \033[2J works on Windows, but not on modern terminals.

  • I tried IntelliJ's terminal in MacOS and it works.
  • I tried iTerm2 in MacOS and it works.

I consider iTerm2 a modern terminal. Can you give an example of a terminal where this code doesn't work?


This source confirms that the ANSI escape code I used is right. This other source contradicts what you said, that the code works on every OS except Windows (unless you enable VT). 🤔

Note that Windows 10 users already have to enable VT for ANSI escape codes to work on cmd.exe. If they use Windows Terminal everything works out of the box.


I did notice something not quite right. That 2J escape code is adding new lines to push the text up and therefore clear the screen. That's not the behaviour I expect. I would like the text to be pushed up without adding empty space. So that needs tweaking.

So I tested lots of different flavours and these were the results

        // Actually deletes the previous text from the screen
//        char escapeChar = 27;
//        String cmd = Character.toString(escapeChar) + 'c';
//        System.out.println(cmd);

        // Actually deletes the previous text from the screen
//        System.out.print("\033\143");

        // Pushes the previous text up, by adding multiple new lines, and does not place new text at the top
//        String cmd = Ansi.generateCode(CLEAR_SCREEN());
//        System.out.println(cmd);

        // Pushes the previous text up, no new lines, new text at the top. Perfection.
//        System.out.print("\033[H\033[2J");
//        System.out.flush();

        // Pushes the previous text up, no new lines, new text at the top. Perfection.
//        System.out.print("\033[H\033[2J");

        // Does nothing
//        Runtime.getRuntime().exec("clear");

Notice that the first example is your suggestion. I can't use it as it deletes all text previous displayed.


tl;dr
I've decided to fix my ANSI escape code from "\033[2J" to "\033[H\033[2J" as that is producing the result I'm looking for. This works on any terminal that supports ANSI escape codes. As mentioned, Windows users can either use a compatible terminal (Windows Terminal) or do the workaround.

@PiprTuff
Copy link
Author

PiprTuff commented May 8, 2022

Notice that the first example is your suggestion. I can't use it as it deletes all text previous displayed.

That's exactly the point! That's why I said that your escape sequence does not work on modern terminals, because I thought that by the command CLEAR_SCREEN, you meant to clear all previous texts, permanently. If you mean just pushing the previous texts up by the word 'CLEAR', then you are right from your perspective. Go ahead. Good luck. 👍

@PiprTuff
Copy link
Author

PiprTuff commented May 8, 2022

// Pushes the previous text up, by adding multiple new lines, and does not place new text at the top
// String cmd = Ansi.generateCode(CLEAR_SCREEN());
// System.out.println(cmd);

That's what I also faced. That was not the expected behaviour. I expected that the CLEAR_SCREEN command should do the same like clear in Unix or cls in Windows. That's why I thought that the escape sequence is not working for me, then I tried some other terminals, and the same happened.

@dialex dialex closed this as completed in 0187cd8 May 8, 2022
@dialex
Copy link
Owner

dialex commented May 8, 2022

Released: https://github.com/dialex/JColor/releases/tag/v5.4.1
(hopefully it works, because I don't want to spend more of my Sunday fixing it 🤞😬)

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

No branches or pull requests

2 participants