Use scanner if no console is found

This commit is contained in:
Andrea Cavalli 2021-10-24 00:12:01 +02:00
parent 9e7f2c8f75
commit 52541b0ca8
2 changed files with 21 additions and 2 deletions

View File

@ -26,7 +26,7 @@
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-java</artifactId>
<version>2.7.8.33</version>
<version>2.7.8.35</version>
</dependency>
<!-- TDLight natives -->

View File

@ -1,8 +1,27 @@
package it.tdlight.common.utils;
import java.io.Console;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicReference;
public final class ScannerUtils {
private static final Object lock = new Object();
private static Scanner SCANNER = null;
public static String askParameter(String displayName, String question) {
return System.console().readLine("[%s] %s", displayName, question);
Console console = System.console();
if (console != null) {
return console.readLine("[%s] %s", displayName, question);
} else {
synchronized (lock) {
if (SCANNER == null) {
SCANNER = new Scanner(System.in);
Runtime.getRuntime().addShutdownHook(new Thread(() -> SCANNER.close()));
}
}
System.out.printf("[%s] %s: ", displayName, question);
return SCANNER.nextLine();
}
}
}