Properly fix prompt printing in Java example.

GitOrigin-RevId: 70500ce141b333c186c79ff777df7bf742159df5
This commit is contained in:
levlam 2018-03-20 18:41:47 +03:00
parent 4acf428c9b
commit 42d5248446
2 changed files with 13 additions and 15 deletions

View File

@ -34,6 +34,6 @@ java '-Djava.library.path=.' org/drinkless/tdlib/example/Example
If you get "Could NOT find JNI ..." error from CMake, you need to specify to CMake path to the installed JDK, for example, "-DJAVA_HOME=/usr/lib/jvm/java-8-oracle/". If you get "Could NOT find JNI ..." error from CMake, you need to specify to CMake path to the installed JDK, for example, "-DJAVA_HOME=/usr/lib/jvm/java-8-oracle/".
If you get java.lang.UnsatisfiedLinkError with "Can't find dependent libraries", you may also need to copy some dependent shared libraries to bin/. If you get java.lang.UnsatisfiedLinkError with "Can't find dependent libraries", you may also need to copy some dependent shared libraries to `bin/`.
In case you compiled the example as 32-bit version, you may need to give -d32 parameter to Java. In case you compiled the example as 32-bit version, you may need to give -d32 parameter to Java.

View File

@ -52,17 +52,16 @@ public final class Example {
private static final String newLine = System.getProperty("line.separator"); private static final String newLine = System.getProperty("line.separator");
private static final String commandsLine = "Enter command (gcs - GetChats, gc <chatId> - GetChat, me - GetMe, sm <chatId> <message> - SendMessage, lo - LogOut, q - Quit): "; private static final String commandsLine = "Enter command (gcs - GetChats, gc <chatId> - GetChat, me - GetMe, sm <chatId> <message> - SendMessage, lo - LogOut, q - Quit): ";
private static volatile String currentPrompt = null;
static { static {
System.loadLibrary("tdjni"); System.loadLibrary("tdjni");
} }
private static void print(String str) { private static void print(String str) {
System.out.println();
System.out.println(str); System.out.println(str);
if (haveAuthorization) if (currentPrompt != null) {
{ System.out.print(currentPrompt);
System.out.print(commandsLine);
} }
} }
@ -106,17 +105,17 @@ public final class Example {
client.send(new TdApi.CheckDatabaseEncryptionKey(), new AuthorizationRequestHandler()); client.send(new TdApi.CheckDatabaseEncryptionKey(), new AuthorizationRequestHandler());
break; break;
case TdApi.AuthorizationStateWaitPhoneNumber.CONSTRUCTOR: { case TdApi.AuthorizationStateWaitPhoneNumber.CONSTRUCTOR: {
String phoneNumber = promptString("Please enter phone number: ", false); String phoneNumber = promptString("Please enter phone number: ");
client.send(new TdApi.SetAuthenticationPhoneNumber(phoneNumber, false, false), new AuthorizationRequestHandler()); client.send(new TdApi.SetAuthenticationPhoneNumber(phoneNumber, false, false), new AuthorizationRequestHandler());
break; break;
} }
case TdApi.AuthorizationStateWaitCode.CONSTRUCTOR: { case TdApi.AuthorizationStateWaitCode.CONSTRUCTOR: {
String code = promptString("Please enter authentication code: ", false); String code = promptString("Please enter authentication code: ");
client.send(new TdApi.CheckAuthenticationCode(code, "", ""), new AuthorizationRequestHandler()); client.send(new TdApi.CheckAuthenticationCode(code, "", ""), new AuthorizationRequestHandler());
break; break;
} }
case TdApi.AuthorizationStateWaitPassword.CONSTRUCTOR: { case TdApi.AuthorizationStateWaitPassword.CONSTRUCTOR: {
String password = promptString("Please enter password: ", false); String password = promptString("Please enter password: ");
client.send(new TdApi.CheckAuthenticationPassword(password), new AuthorizationRequestHandler()); client.send(new TdApi.CheckAuthenticationPassword(password), new AuthorizationRequestHandler());
break; break;
} }
@ -165,12 +164,10 @@ public final class Example {
} }
return chatId; return chatId;
} }
private static String promptString(String prompt, boolean printNewline) { private static String promptString(String prompt) {
System.out.print(prompt); System.out.print(prompt);
if (printNewline) { currentPrompt = prompt;
System.out.print(newLine);
}
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String str = ""; String str = "";
try { try {
@ -178,11 +175,12 @@ public final class Example {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
currentPrompt = null;
return str; return str;
} }
private static void getCommand() { private static void getCommand() {
String command = promptString(commandsLine, true); String command = promptString(commandsLine);
String[] commands = command.split(" ", 2); String[] commands = command.split(" ", 2);
try { try {
switch (commands[0]) { switch (commands[0]) {
@ -269,6 +267,7 @@ public final class Example {
System.out.println(chatId + ": " + chat.title); System.out.println(chatId + ": " + chat.title);
} }
} }
print("");
} }
} }
@ -282,7 +281,6 @@ public final class Example {
} }
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) throws InterruptedException {
// disable TDLib log // disable TDLib log
Log.setVerbosityLevel(0); Log.setVerbosityLevel(0);
if (!Log.setFilePath("tdlib.log")) { if (!Log.setFilePath("tdlib.log")) {