Package org.dellroad.jct.core.util
Class ConsoleUtil
java.lang.Object
org.dellroad.jct.core.util.ConsoleUtil
Utility routines.
-
Method Summary
Modifier and TypeMethodDescriptionstatic int
Determine the current JDK version.static org.jline.terminal.Terminal.SignalHandler
interrruptHandler
(Supplier<? extends ConsoleSession<?, ?>> sessionGetter, org.jline.terminal.Terminal.SignalHandler fallthroughHandler) Build aTerminal
signal handler thatinterrupt()
's aConsoleSession
when aTerminal.Signal.INT
signal is received.static PrintStream
unclosable
(PrintStream stream) Wrap anPrintStream
in a new one that ignoresclose()
.
-
Method Details
-
getJavaVersion
public static int getJavaVersion()Determine the current JDK version.- Returns:
- current JDK version as an integer
-
interrruptHandler
public static org.jline.terminal.Terminal.SignalHandler interrruptHandler(Supplier<? extends ConsoleSession<?, ?>> sessionGetter, org.jline.terminal.Terminal.SignalHandler fallthroughHandler) Build aTerminal
signal handler thatinterrupt()
's aConsoleSession
when aTerminal.Signal.INT
signal is received.This is needed when building a
Terminal
to make Control-C work in a shell session: pass the returned handler toTerminalBuilder.signalHandler()
.For example:
Terminal terminal = TerminalBuilder.builder() .signalHandler(ConsoleUtil.interruptHandler(this::getSession, Terminal.SignalHandler.SIG_DFL)) // other terminal config... .build();
- Parameters:
sessionGetter
- finds the session that should be interrupted; this may return null to demurefallthroughHandler
- handler for signals other thanTerminal.Signal.INT
, or whensessionGetter
returns null; this may be null to do nothing- Returns:
- new signal handler
- Throws:
IllegalArgumentException
- ifsessionGetter
is null
-
unclosable
Wrap anPrintStream
in a new one that ignoresclose()
.- Parameters:
stream
- originalPrintStream
- Returns:
PrintStream
that can't be closed- Throws:
IllegalArgumentException
- ifstream
is null
-