0

I am using JLine 2 to write a console application in Java. I need to make a password visible on the console for say 10 seconds, then erase it (on a GNU terminal).

I tried different things, amongst which :

putString(pass); 
Thread.sleep(10*1000); 
resetLine();

but no luck. Either the text does not show, or the text is not cleared.

Derlin
  • 9,572
  • 2
  • 32
  • 53

1 Answers1

0

Ok, I finally came up with the following (the class extends ConsoleReader):

public boolean showPassword(String pass, int millis) {
    try {
        resetPromptLine("   password>", pass, pass.length());
        Thread.sleep(millis);
        if (setCursorPosition(0) && killLine()) resetPromptLine("   password>", "", 0);

    } catch (InterruptedException | IOException e) {
        e.printStackTrace();
    }
    return false;
}

I use resetLine to show a custom prompt and the password; I also set the cursor to the end of the line. I wait a bit. I set the cursor to the end of the line and "kill the line". For the password to actually disappear, I have to call resetPromptLine once again.

To wait for an input from the user vs a given time, use readCharacter() instead of Thread.sleep().

Derlin
  • 9,572
  • 2
  • 32
  • 53