- java.lang.Object
-
- com.googlecode.lanterna.terminal.AbstractTerminal
-
- com.googlecode.lanterna.terminal.ansi.StreamBasedTerminal
-
- com.googlecode.lanterna.terminal.ansi.ANSITerminal
-
- com.googlecode.lanterna.terminal.ansi.UnixLikeTerminal
-
- All Implemented Interfaces:
Scrollable,InputProvider,ExtendedTerminal,Terminal,java.io.Closeable,java.lang.AutoCloseable
- Direct Known Subclasses:
UnixLikeTTYTerminal
public abstract class UnixLikeTerminal extends ANSITerminal
Base class for all terminals that generally behave like Unix terminals. This class defined a number of abstract methods that needs to be implemented which are all used to setup the terminal environment (turning off echo, canonical mode, etc) and also a control variable for how to react to CTRL+c keystroke.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classUnixLikeTerminal.CtrlCBehaviourThis enum lets you control how Lanterna will handle a ctrl+c keystroke from the user.
-
Field Summary
Fields Modifier and Type Field Description private booleanacquiredprivate booleancatchSpecialCharactersprivate java.lang.ThreadshutdownHookprivate UnixLikeTerminal.CtrlCBehaviourterminalCtrlCBehaviour
-
Constructor Summary
Constructors Modifier Constructor Description protectedUnixLikeTerminal(java.io.InputStream terminalInput, java.io.OutputStream terminalOutput, java.nio.charset.Charset terminalCharset, UnixLikeTerminal.CtrlCBehaviour terminalCtrlCBehaviour)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voidacquire()Effectively taking over the terminal and enabling it for Lanterna to use, by turning off echo and canonical mode, adding resize listeners and optionally trap unix signals.protected abstract voidcanonicalMode(boolean enabled)In canonical mode, data are accumulated in a line editing buffer, and do not become "available for reading" until line editing has been terminated by the user sending a line delimiter character.voidclose()Closes the terminal, if applicable.private voidexitPrivateModeAndRestoreState()protected UnixLikeTerminal.CtrlCBehaviourgetTerminalCtrlCBehaviour()private voidisCtrlC(KeyStroke key)protected abstract voidkeyEchoEnabled(boolean enabled)Enables or disable key echo mode, which means when the user press a key, the terminal will immediately print that key to the terminal.protected abstract voidkeyStrokeSignalsEnabled(boolean enabled)This method causes certain keystrokes (at the moment only ctrl+c) to be passed in to the program as a regularKeyStrokeinstead of as a signal to the JVM process.KeyStrokepollInput()Returns the nextKeyoff the input queue or null if there is no more input events available.KeyStrokereadInput()Returns the nextKeyoff the input queue or blocks until one is available.protected abstract voidregisterTerminalResizeListener(java.lang.Runnable onResize)protected abstract voidrestoreTerminalSettings()Restores the terminal settings from last timesaveTerminalSettings()was calledprivate voidrestoreTerminalSettingsAndKeyStrokeSignals()protected abstract voidsaveTerminalSettings()Stores the current terminal device settings (the ones that are modified through this interface) so that they can be restored later usingrestoreTerminalSettings()-
Methods inherited from class com.googlecode.lanterna.terminal.ansi.ANSITerminal
clearScreen, deiconify, disableSGR, enableSGR, enterPrivateMode, exitPrivateMode, findTerminalSize, getCursorPosition, getDefaultKeyDecodingProfile, getTerminalSize, iconify, isInPrivateMode, maximize, popTitle, pushTitle, reportPosition, resetColorAndSGR, restoreCursorPosition, saveCursorPosition, scrollLines, setBackgroundColor, setCursorPosition, setCursorPosition, setCursorVisible, setForegroundColor, setMouseCaptureMode, setTerminalSize, setTitle, unmaximize
-
Methods inherited from class com.googlecode.lanterna.terminal.ansi.StreamBasedTerminal
bell, enquireTerminal, flush, getCharset, getInputDecoder, putCharacter, putString, resetMemorizedCursorPosition, translateCharacter, waitForCursorPositionReport, writeToTerminal
-
Methods inherited from class com.googlecode.lanterna.terminal.AbstractTerminal
addResizeListener, newTextGraphics, onResized, onResized, removeResizeListener
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.googlecode.lanterna.terminal.Terminal
addResizeListener, bell, enquireTerminal, flush, newTextGraphics, putCharacter, putString, removeResizeListener
-
-
-
-
Field Detail
-
terminalCtrlCBehaviour
private final UnixLikeTerminal.CtrlCBehaviour terminalCtrlCBehaviour
-
catchSpecialCharacters
private final boolean catchSpecialCharacters
-
shutdownHook
private final java.lang.Thread shutdownHook
-
acquired
private boolean acquired
-
-
Constructor Detail
-
UnixLikeTerminal
protected UnixLikeTerminal(java.io.InputStream terminalInput, java.io.OutputStream terminalOutput, java.nio.charset.Charset terminalCharset, UnixLikeTerminal.CtrlCBehaviour terminalCtrlCBehaviour) throws java.io.IOException- Throws:
java.io.IOException
-
-
Method Detail
-
acquire
protected void acquire() throws java.io.IOExceptionEffectively taking over the terminal and enabling it for Lanterna to use, by turning off echo and canonical mode, adding resize listeners and optionally trap unix signals. This should be called automatically by the constructor of any end-user class extending fromUnixLikeTerminal- Throws:
java.io.IOException- If there was an I/O error
-
close
public void close() throws java.io.IOExceptionDescription copied from interface:TerminalCloses the terminal, if applicable. If the implementation doesn't support closing the terminal, this will do nothing. The Swing/AWT emulator implementations will translate this into a dispose() call on the UI resources, the telnet implementation will hang out the connection.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein interfaceTerminal- Overrides:
closein classANSITerminal- Throws:
java.io.IOException- If there was an underlying I/O error
-
pollInput
public KeyStroke pollInput() throws java.io.IOException
Description copied from interface:InputProviderReturns the nextKeyoff the input queue or null if there is no more input events available. Note, this method call is not blocking, it returns null immediately if there is nothing on the input stream.- Specified by:
pollInputin interfaceInputProvider- Overrides:
pollInputin classANSITerminal- Returns:
- Key object which represents a keystroke coming in through the input stream
- Throws:
java.io.IOException- Propagated error if the underlying stream gave errors
-
readInput
public KeyStroke readInput() throws java.io.IOException
Description copied from interface:InputProviderReturns the nextKeyoff the input queue or blocks until one is available. NOTE: In previous versions of Lanterna, this method was not blocking. From lanterna 3, it is blocking and you can callpollInput()for the non-blocking version.- Specified by:
readInputin interfaceInputProvider- Overrides:
readInputin classANSITerminal- Returns:
- Key object which represents a keystroke coming in through the input stream
- Throws:
java.io.IOException- Propagated error if the underlying stream gave errors
-
getTerminalCtrlCBehaviour
protected UnixLikeTerminal.CtrlCBehaviour getTerminalCtrlCBehaviour()
-
registerTerminalResizeListener
protected abstract void registerTerminalResizeListener(java.lang.Runnable onResize) throws java.io.IOException- Throws:
java.io.IOException
-
saveTerminalSettings
protected abstract void saveTerminalSettings() throws java.io.IOExceptionStores the current terminal device settings (the ones that are modified through this interface) so that they can be restored later usingrestoreTerminalSettings()- Throws:
java.io.IOException- If there was an I/O error when altering the terminal environment
-
restoreTerminalSettings
protected abstract void restoreTerminalSettings() throws java.io.IOExceptionRestores the terminal settings from last timesaveTerminalSettings()was called- Throws:
java.io.IOException- If there was an I/O error when altering the terminal environment
-
restoreTerminalSettingsAndKeyStrokeSignals
private void restoreTerminalSettingsAndKeyStrokeSignals() throws java.io.IOException- Throws:
java.io.IOException
-
keyEchoEnabled
protected abstract void keyEchoEnabled(boolean enabled) throws java.io.IOExceptionEnables or disable key echo mode, which means when the user press a key, the terminal will immediately print that key to the terminal. Normally for Lanterna, this should be turned off so the software can take the key as an input event, put it on the input queue and then depending on the code decide what to do with it.- Parameters:
enabled-trueif key echo should be enabled,falseotherwise- Throws:
java.io.IOException- If there was an I/O error when altering the terminal environment
-
canonicalMode
protected abstract void canonicalMode(boolean enabled) throws java.io.IOExceptionIn canonical mode, data are accumulated in a line editing buffer, and do not become "available for reading" until line editing has been terminated by the user sending a line delimiter character. This is usually the default mode for a terminal. Lanterna wants to read each character as they are typed, without waiting for the final newline, so it will attempt to turn canonical mode off on initialization.- Parameters:
enabled-trueif canonical input mode should be enabled,falseotherwise- Throws:
java.io.IOException- If there was an I/O error when altering the terminal environment
-
keyStrokeSignalsEnabled
protected abstract void keyStrokeSignalsEnabled(boolean enabled) throws java.io.IOExceptionThis method causes certain keystrokes (at the moment only ctrl+c) to be passed in to the program as a regularKeyStrokeinstead of as a signal to the JVM process. For example, ctrl+c will normally send an interrupt that causes the JVM to shut down, but this method will make it pass in ctrl+c as a regularKeyStrokeinstead. You can of course still make ctrl+c kill the application through your own input handling if you like.Please note that this method is called automatically by lanterna to disable signals unless you define a system property "com.googlecode.lanterna.terminal.UnixTerminal.catchSpecialCharacters" and set it to the string "false".
- Parameters:
enabled- Pass intrueif you want keystrokes to generate system signals (like process interrupt),falseif you want lanterna to catch and interpret these keystrokes are regular keystrokes- Throws:
java.io.IOException- If there was an I/O error when attempting to disable special characters- See Also:
UnixLikeTerminal.CtrlCBehaviour
-
isCtrlC
private void isCtrlC(KeyStroke key) throws java.io.IOException
- Throws:
java.io.IOException
-
exitPrivateModeAndRestoreState
private void exitPrivateModeAndRestoreState()
-
-