使用 JLine2 仅显示几秒钟的字符串
Display a string just for a few seconds with JLine2
我正在使用 JLine 2 在 Java 中编写控制台应用程序。我需要让密码在控制台上显示 10 秒,然后将其删除(在 GNU 终端上)。
我尝试了不同的东西,其中:
putString(pass);
Thread.sleep(10*1000);
resetLine();
但运气不好。要么文字不显示,要么文字没有清除。
好的,我终于想到了以下内容(class 扩展了 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;
}
我使用resetLine
显示自定义提示和密码;我还将光标设置到行尾。我稍等一下。我将光标设置到行尾 "kill the line"。为了使密码真正消失,我必须再次调用 resetPromptLine
。
要在给定时间内等待用户输入,请使用 readCharacter()
而不是 Thread.sleep()
。
我正在使用 JLine 2 在 Java 中编写控制台应用程序。我需要让密码在控制台上显示 10 秒,然后将其删除(在 GNU 终端上)。
我尝试了不同的东西,其中:
putString(pass);
Thread.sleep(10*1000);
resetLine();
但运气不好。要么文字不显示,要么文字没有清除。
好的,我终于想到了以下内容(class 扩展了 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;
}
我使用resetLine
显示自定义提示和密码;我还将光标设置到行尾。我稍等一下。我将光标设置到行尾 "kill the line"。为了使密码真正消失,我必须再次调用 resetPromptLine
。
要在给定时间内等待用户输入,请使用 readCharacter()
而不是 Thread.sleep()
。