clearScreen
method of ConsoleReader
! Any other changes don't have an effect. Is this then a bug in JLine2?
JLine2:
Why, when I run this, do I get two console prompts directly following each other (----> ---->
)?
Is it because two consoles are being created? I do not understand how.
What am I failing to see here?
import java.io.IOException;
import jline.console.ConsoleReader;
class TextUi implements Ui {
private static final String prompt1 = "----> ";
public void homeScreen() {
try {
ConsoleReader con = new ConsoleReader();
con.setPrompt(prompt1);
con.clearScreen();
System.out.println("Press any key to continue...");
con.readCharacter();
con.clearScreen();
System.out.println("Here is a prompt. Do something and press enter to continue...");
String line = con.readLine();
con.clearScreen();
System.out.println("You typed: ");
System.out.println(line);
System.out.println("Press any key to exit. ");
con.readCharacter();
con.clearScreen();
} catch (IOException e) {
e.printStackTrace();
}
}
public void exitSplash() {
System.out.println("Thank You. Goodbye.");
System.out.println("");
}
public void creditsScreen() {
}
public static void main (String argv[]) {
TextUi ui = new TextUi();
ui.homeScreen();
ui.exitSplash();
}
}
This is not a bug, you just need to call con.flush()
after each time you call con.clearScreen()
.
The clearScreen
method doesn't calls flush()
automatically (it might work in some cases without flushing) but the readLine
method does, so the screen is actually clearing only when you call con.readLine()
. This is causing to the last System.out.println
(before the readLine
) to be cleared even it was called after con.clearScreen()
.
Your code inside the try
block should be changed to:
ConsoleReader con = new ConsoleReader();
con.setPrompt(prompt1);
con.clearScreen();
con.flush();
System.out.println("Press any key to continue...");
con.readCharacter();
con.clearScreen();
con.flush();
System.out.println("Here is a prompt. Do something and press enter to continue...");
String line = con.readLine();
con.clearScreen();
con.flush();
System.out.println("You typed: ");
System.out.println(line);
System.out.println("Press any key to exit. ");
con.readCharacter();
con.clearScreen();
con.flush();