Use scanner if no console is found
This commit is contained in:
parent
9e7f2c8f75
commit
52541b0ca8
@ -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 -->
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user