Interface Terminal.SignalHandler
- All Known Implementing Classes:
NativeSignalHandler
- Enclosing interface:
Terminal
The SignalHandler interface defines the contract for objects that can respond to
terminal signals. When a signal is raised, the corresponding handler's handle(Signal)
method is called with the signal that was raised.
JLine provides two predefined signal handlers:
SIG_DFL- Default signal handler that uses the JVM's default behaviorSIG_IGN- Ignores the signal and performs no special processing
Example usage with a custom handler:
Terminal terminal = TerminalBuilder.terminal();
// Create a custom signal handler
SignalHandler handler = signal -> {
if (signal == Signal.INT) {
terminal.writer().println("\nInterrupted!");
terminal.flush();
}
};
// Register the handler for the INT signal
terminal.handle(Signal.INT, handler);
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Terminal.SignalHandlerDefault signal handler that uses the JVM's default behavior for the signal.static final Terminal.SignalHandlerSignal handler that ignores the signal and performs no special processing. -
Method Summary
Modifier and TypeMethodDescriptionvoidhandle(Terminal.Signal signal) Handles the specified signal.
-
Field Details
-
SIG_DFL
Default signal handler that uses the JVM's default behavior for the signal.When this handler is registered for a signal, the terminal will use the JVM's default behavior to handle the signal. For example, the default behavior for the INT signal (Ctrl+C) is to terminate the JVM.
Example usage:
// Restore default behavior for INT signal terminal.handle(Signal.INT, SignalHandler.SIG_DFL);
-
SIG_IGN
Signal handler that ignores the signal and performs no special processing.When this handler is registered for a signal, the terminal will completely ignore the signal and continue normal operation. This is useful for preventing signals like INT (Ctrl+C) from terminating the application.
Example usage:
// Ignore INT signal (Ctrl+C will not terminate the application) terminal.handle(Signal.INT, SignalHandler.SIG_IGN);
-
-
Method Details
-
handle
Handles the specified signal.This method is called when a signal is raised and this handler is registered for that signal. Implementations should perform any necessary actions in response to the signal.
Note that signal handlers should generally be short-lived and avoid blocking operations, as they may be called in contexts where blocking could cause deadlocks or other issues.
- Parameters:
signal- the signal that was raised
-