javaconsole

How to delete text in powershell command line with java?


On the console window I would like to have a single monitoring line, which is constantly updated/overprinted. After each output the remaining space of the line must be erased, as the previous output possibly had more characters than the current one. First I tried to use the corresponding ANSI control code. When this failed, I tried to emulate Ctrl-End with the Robot class, but to no avail. Any idea?

import java.awt.*;
import java.awt.event.*;

public class Ctrl_End {

  public static void main(String[] args) {
    char BS= 8, ESC= 27;
    String ERASE_EOL= "[0K"; // ANSI erase till end of line.
    System.out.println("ABC"+BS+BS+"DE"); // Overprints
    System.out.println("ABC"+BS+BS+ESC+ERASE_EOL);
    System.out.println();

    Robot rob= null;
    try {
      rob= new Robot();
    }
    catch(AWTException ex) {
      System.out.println(ex);
    }
    System.out.print("First ouput, longer than the following");
    rob.keyPress(KeyEvent.VK_CONTROL);
    rob.keyPress(KeyEvent.VK_END);
    rob.keyRelease(KeyEvent.VK_END);
    rob.keyRelease(KeyEvent.VK_CONTROL);
    System.out.print("\rSecond ouput");
}



EDIT:
What I currently receive as output is:

ADE
A←[0K

Second ouput longer than the following

The first line is ok (Backspace test).
The second line shows that the ESC character is not properly recognized, but displayed as an arrow, hence it is no wonder that the remainder of the control string is not interpreted as intended.
The last line shows that print() and \r work, but the line should read Second ouput only; longer than the following stems from the previous print(...) and needs to be deleted.

By the way: Whether using Microsoft's older command window (cmd.exe) or PowerShell, the output is the same. I would, however, prefer a solution on PowerShell.


Solution

  • I found in the net that processing of ANSI control sequences is not necessarily enabled by default on Windows consoles. (I am running Win10.) I checked my registry and found that the required key didn't exist:

    [HKEY_CURRENT_USER\Console]"VirtualTerminalLevel"=dword:00000001

    After defining the key and starting a new PowerShell everything was fine.

    The final demo code now looks as under

    import java.awt.*;
    import java.awt.event.*;
    
    public class Ctrl_End {
    
      public static void main(String[] args) {
        char BS= 8, ESC= 27;
        String ERASE_EOL= "[0K"; // ANSI erase till end of line.
        System.out.println("ABC"+BS+BS+"D"); // Overprints
        System.out.println("ABC"+BS+BS+ESC+ERASE_EOL);
        System.out.println();
        System.out.print("First output, longer than the following");
        waitSomeTime(2000);
        System.out.print("\rSecond output"+ESC+ERASE_EOL);
        waitSomeTime(2000);
        System.out.print("\rThird output"+ESC+ERASE_EOL);
      }
    
    
      public static void waitSomeTime(int ms) {
        try {
          Thread.sleep(ms);
        }
        catch (InterruptedException e) {
          System.out.println(e.toString());
        }
      }
    
    }