Class DefaultVirtualTerminal
- All Implemented Interfaces:
InputProvider, IOSafeTerminal, Terminal, VirtualTerminal, Closeable, AutoCloseable
-
Nested Class Summary
Nested classes/interfaces inherited from interface VirtualTerminal
VirtualTerminal.BufferLine, VirtualTerminal.BufferWalker -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate TextColorprivate TextColorprivate intprivate TextBufferprivate TerminalPositionprivate booleanprivate final TreeSet<TerminalPosition> private final BlockingQueue<KeyStroke> private final List<VirtualTerminalListener> private final TextBufferprivate final TextBufferprivate TerminalPositionprivate TerminalSizeprivate boolean -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new virtual terminal with an initial size setDefaultVirtualTerminal(TerminalSize initialTerminalSize) Creates a new virtual terminal with an initial size set -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds aKeyStroketo the input queue of this virtual terminal.void/////////////////////////////////////////////////////////////////////////////////////////////////////////////////voidbell()Prints 0x7 to the terminal, which will make the terminal (emulator) ring a bell (or more likely beep).voidRemoves all the characters, colors and graphics from the screen and leaves you with a big empty space.voidclose()Closes the terminal, if applicable.private voidvoiddisableSGR(SGR sgr) Deactivates anSGR(Selected Graphic Rendition) code which has previously been activated throughenableSGR(..).voidActivates anSGR(Selected Graphic Rendition) code.byte[]enquireTerminal(int timeout, TimeUnit timeoutUnit) Retrieves optional information from the terminal by printing the ENQ (\u005) character.voidCalling this method will, where supported, give your terminal a private area to use, separate from what was there before.voidIf you have previously entered private mode, this method will exit this and, depending on implementation, maybe restore what the terminal looked like before private mode was entered.voidflush()Callsflush()on the underlyingOutputStreamobject, or whatever other implementation this terminal is built around.voidforEachLine(int startRow, int endRow, VirtualTerminal.BufferWalker bufferWalker) Iterates over a range of lines in the text buffergetBufferCharacter(int column, int row) Returns a character from this virtual terminal, relative to the top-left position of the text buffer including any backlog.getBufferCharacter(TerminalPosition position) Returns a character from this virtual terminal, relative to the top-left position of the text buffer including any backlog.intReturns the number of lines in the entire text buffer, including any backloggetCharacter(int column, int row) Returns a character from the viewport at the specified coordinates.getCharacter(TerminalPosition position) Returns a character from the viewport at the specified coordinates.Returns the position of the terminal cursor where the row index is counted from the top of the text buffer, including all backlog.Returns the position of the cursor, as reported by the terminal./////////////////////////////////////////////////////////////////////////////////////////////////////////////////booleanChecks if the terminal cursor is visible or notbooleanprivate voidMoves the text cursor to the first column of the next line and trims the backlog of necessaryCreates a new TextGraphics object that uses this Terminal directly when outputting.Returns the nextKeyoff the input queue or null if there is no more input events available.voidputCharacter(char c) Prints one character to the terminal at the current cursor location.(package private) voidputCharacter(TextCharacter terminalCharacter) voidPrints a string to the terminal at the current cursor location.Returns the nextKeyoff the input queue or blocks until one is available.voidRemoves a listener from this virtual terminal so it will no longer receive events.voidRemoves all currently active SGR codes and sets foreground and background colors back to default.voidsetBackgroundColor(TextColor color) Changes the background color for all the following characters put to the terminal.voidsetBacklogSize(int backlogSize) Sets the number of rows to allow in the non-private buffer above the viewport.voidsetCursorPosition(int x, int y) Moves the text cursor to a new location on the terminal.voidsetCursorPosition(TerminalPosition cursorPosition) Same as callingsetCursorPosition(position.getColumn(), position.getRow())voidsetCursorVisible(boolean visible) Hides or shows the text cursor, but not all terminal (-emulators) supports this.voidsetForegroundColor(TextColor color) Changes the foreground color for all the following characters put to the terminal.voidsetTerminalSize(TerminalSize newSize) Changes the "visible size" of the virtual terminal.private voidMarks the whole buffer as dirty so every cell is considered in need to repainting.toString()private voidMethods inherited from class AbstractTerminal
addResizeListener, onResized, onResized, removeResizeListenerMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Terminal
addResizeListener, removeResizeListener
-
Field Details
-
regularTextBuffer
-
privateModeTextBuffer
-
dirtyTerminalCells
-
listeners
-
currentTextBuffer
-
wholeBufferDirty
private boolean wholeBufferDirty -
terminalSize
-
cursorVisible
private boolean cursorVisible -
backlogSize
private int backlogSize -
inputQueue
-
activeModifiers
-
activeForegroundColor
-
activeBackgroundColor
-
cursorPosition
-
savedCursorPosition
-
-
Constructor Details
-
DefaultVirtualTerminal
public DefaultVirtualTerminal()Creates a new virtual terminal with an initial size set -
DefaultVirtualTerminal
Creates a new virtual terminal with an initial size set- Parameters:
initialTerminalSize- Starting size of the virtual terminal
-
-
Method Details
-
getTerminalSize
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////- Specified by:
getTerminalSizein interfaceIOSafeTerminal- Specified by:
getTerminalSizein interfaceTerminal- Returns:
- Size of the terminal
-
setTerminalSize
Description copied from interface:VirtualTerminalChanges the "visible size" of the virtual terminal. This is the area at the bottom of the text buffer that is considered the workable area since the cursor is restricted to this space. If you call this method with a size that is different from the current size of the virtual terminal, the resize event will be fired on all listeners.- Specified by:
setTerminalSizein interfaceVirtualTerminal- Parameters:
newSize- New size of the virtual terminal
-
enterPrivateMode
public void enterPrivateMode()Description copied from interface:TerminalCalling this method will, where supported, give your terminal a private area to use, separate from what was there before. Some terminal emulators will preserve the terminal history and restore it when you exit private mode. Some terminals will just clear the screen and put the cursor in the top-left corner. Typically, if you terminal supports scrolling, going into private mode will disable the scrolling and leave you with a fixed screen, which can be useful if you don't want to deal with what the terminal buffer will look like if the user scrolls up.- Specified by:
enterPrivateModein interfaceIOSafeTerminal- Specified by:
enterPrivateModein interfaceTerminal
-
exitPrivateMode
public void exitPrivateMode()Description copied from interface:TerminalIf you have previously entered private mode, this method will exit this and, depending on implementation, maybe restore what the terminal looked like before private mode was entered. If the terminal doesn't support a secondary buffer for private mode, it will probably make a new line below the private mode and place the cursor there.- Specified by:
exitPrivateModein interfaceIOSafeTerminal- Specified by:
exitPrivateModein interfaceTerminal
-
clearScreen
public void clearScreen()Description copied from interface:TerminalRemoves all the characters, colors and graphics from the screen and leaves you with a big empty space. Text cursor position is undefined after this call (depends on platform and terminal) so you should always callmoveCursornext. Some terminal implementations doesn't reset color and modifier state so it's also good practise to callresetColorAndSGR()after this.- Specified by:
clearScreenin interfaceIOSafeTerminal- Specified by:
clearScreenin interfaceTerminal
-
setCursorPosition
public void setCursorPosition(int x, int y) Description copied from interface:TerminalMoves the text cursor to a new location on the terminal. The top-left corner has coordinates 0 x 0 and the bottom- right corner has coordinates terminal_width-1 x terminal_height-1. You can retrieve the size of the terminal by calling getTerminalSize().- Specified by:
setCursorPositionin interfaceIOSafeTerminal- Specified by:
setCursorPositionin interfaceTerminal- Parameters:
x- The 0-indexed column to place the cursor aty- The 0-indexed row to place the cursor at
-
setCursorPosition
Description copied from interface:TerminalSame as callingsetCursorPosition(position.getColumn(), position.getRow())- Specified by:
setCursorPositionin interfaceIOSafeTerminal- Specified by:
setCursorPositionin interfaceTerminal- Parameters:
cursorPosition- Position to place the cursor at
-
getCursorPosition
Description copied from interface:TerminalReturns the position of the cursor, as reported by the terminal. The top-left corner has coordinates 0 x 0 and the bottom-right corner has coordinates terminal_width-1 x terminal_height-1.- Specified by:
getCursorPositionin interfaceIOSafeTerminal- Specified by:
getCursorPositionin interfaceTerminal- Returns:
- Position of the cursor
-
getCursorBufferPosition
Description copied from interface:VirtualTerminalReturns the position of the terminal cursor where the row index is counted from the top of the text buffer, including all backlog. This means, if there is 500 lines of backlog but the cursor position is set to 0x0, this method will return 0x500. If you want to get the cursor's position in the viewport, please useIOSafeTerminal.getCursorPosition()instead.- Specified by:
getCursorBufferPositionin interfaceVirtualTerminal- Returns:
- Cursor position as an offset from the top-left position of the text buffer including any backlog
-
setCursorVisible
public void setCursorVisible(boolean visible) Description copied from interface:TerminalHides or shows the text cursor, but not all terminal (-emulators) supports this. The text cursor is normally a text block or an underscore, sometimes blinking, which shows the user where keyboard-entered text is supposed to show up.- Specified by:
setCursorVisiblein interfaceIOSafeTerminal- Specified by:
setCursorVisiblein interfaceTerminal- Parameters:
visible- Hides the text cursor iffalseand shows it iftrue
-
putCharacter
public void putCharacter(char c) Description copied from interface:TerminalPrints one character to the terminal at the current cursor location. Please note that the cursor will then move one column to the right, so multiple calls toputCharacterwill print out a text string without the need to reposition the text cursor. If you reach the end of the line while putting characters using this method, you can expect the text cursor to move to the beginning of the next line.You can output CJK (Chinese, Japanese, Korean) characters (as well as other regional scripts) but remember that the terminal that the user is using might not have the required font to render it. Also worth noticing is that CJK (and some others) characters tend to take up 2 columns per character, simply because they are a square in their construction as opposed to the somewhat rectangular shape we fit latin characters in. As it's very difficult to create a monospace font for CJK with a 2:1 height-width proportion, it seems like the implementers back in the days simply gave up and made each character take 2 column. It causes issues for the random terminal programmer because you can't really trust 1 character = 1 column, but I suppose it's "しょうがない". If you try to print non-printable control characters, the terminal is likely to ignore them (all
Terminalimplementations bundled with Lanterna will).- Specified by:
putCharacterin interfaceIOSafeTerminal- Specified by:
putCharacterin interfaceTerminal- Parameters:
c- Character to place on the terminal
-
putString
Description copied from interface:TerminalPrints a string to the terminal at the current cursor location. Please note that the cursor will then move one column to the right, so multiple calls toputStringwill print out a text string without the need to reposition the text cursor. If you reach the end of the line while putting characters using this method, you can expect the text cursor to move to the beginning of the next line.You can output CJK (Chinese, Japanese, Korean) characters (as well as other regional scripts) but remember that the terminal that the user is using might not have the required font to render it. Also worth noticing is that CJK (and some others) characters tend to take up 2 columns per character, simply because they are a square in their construction as opposed to the somewhat rectangular shape we fit latin characters in. As it's very difficult to create a monospace font for CJK with a 2:1 height-width proportion, it seems like the implementers back in the days simply gave up and made each character take 2 column. It causes issues for the random terminal programmer because you can't really trust 1 character = 1 column, but I suppose it's "しょうがない".
If you try to print non-printable control characters, the terminal is likely to ignore them (all
Terminalimplementations bundled with Lanterna will).You can use this method to place emoji characters on the terminal, since they take up more than one char with Java's built-in UTF16 encoding.
- Specified by:
putStringin interfaceIOSafeTerminal- Specified by:
putStringin interfaceTerminal- Parameters:
string- String to place on the terminal
-
enableSGR
Description copied from interface:TerminalActivates anSGR(Selected Graphic Rendition) code. This code modifies a state inside the terminal that will apply to all characters written afterwards, such as bold, italic, blinking code and so on.- Specified by:
enableSGRin interfaceIOSafeTerminal- Specified by:
enableSGRin interfaceTerminal- Parameters:
sgr- SGR code to apply- See Also:
-
disableSGR
Description copied from interface:TerminalDeactivates anSGR(Selected Graphic Rendition) code which has previously been activated throughenableSGR(..).- Specified by:
disableSGRin interfaceIOSafeTerminal- Specified by:
disableSGRin interfaceTerminal- Parameters:
sgr- SGR code to apply- See Also:
-
resetColorAndSGR
public void resetColorAndSGR()Description copied from interface:TerminalRemoves all currently active SGR codes and sets foreground and background colors back to default.- Specified by:
resetColorAndSGRin interfaceIOSafeTerminal- Specified by:
resetColorAndSGRin interfaceTerminal- See Also:
-
setForegroundColor
Description copied from interface:TerminalChanges the foreground color for all the following characters put to the terminal. The foreground color is what color to draw the text in, as opposed to the background color which is the color surrounding the characters.This overload is using the TextColor class to define a color, which is a layer of abstraction above the three different color formats supported (ANSI, indexed and RGB). The other setForegroundColor(..) overloads gives you direct access to set one of those three.
Note to implementers of this interface, just make this method call color.applyAsForeground(this);
- Specified by:
setForegroundColorin interfaceIOSafeTerminal- Specified by:
setForegroundColorin interfaceTerminal- Parameters:
color- Color to use for foreground
-
setBackgroundColor
Description copied from interface:TerminalChanges the background color for all the following characters put to the terminal. The background color is the color surrounding the text being printed.This overload is using the TextColor class to define a color, which is a layer of abstraction above the three different color formats supported (ANSI, indexed and RGB). The other setBackgroundColor(..) overloads gives you direct access to set one of those three.
Note to implementers of this interface, just make this method call color.applyAsBackground(this);
- Specified by:
setBackgroundColorin interfaceIOSafeTerminal- Specified by:
setBackgroundColorin interfaceTerminal- Parameters:
color- Color to use for the background
-
enquireTerminal
Description copied from interface:TerminalRetrieves optional information from the terminal by printing the ENQ (\u005) character. Terminals and terminal emulators may or may not respond to this command, sometimes it's configurable.- Specified by:
enquireTerminalin interfaceIOSafeTerminal- Specified by:
enquireTerminalin interfaceTerminal- Parameters:
timeout- How long to wait for the talk-back message, if there's nothing immediately available on the input stream, you should probably set this to a somewhat small value to prevent unnecessary blockage on the input stream but large enough to accommodate a round-trip to the user's terminal (~300 ms if you are connection across the globe).timeoutUnit- What unit to use when interpreting thetimeoutparameter- Returns:
- Answer-back message from the terminal or empty if there was nothing
-
bell
public void bell()Description copied from interface:TerminalPrints 0x7 to the terminal, which will make the terminal (emulator) ring a bell (or more likely beep). Not all terminals implements this. Wikipedia has more details.- Specified by:
bellin interfaceIOSafeTerminal- Specified by:
bellin interfaceTerminal
-
flush
public void flush()Description copied from interface:TerminalCallsflush()on the underlyingOutputStreamobject, or whatever other implementation this terminal is built around. Some implementing classes of this interface (like SwingTerminal) doesn't do anything as it doesn't really apply to them.- Specified by:
flushin interfaceIOSafeTerminal- Specified by:
flushin interfaceTerminal
-
close
public void close()Description 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 interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceIOSafeTerminal- Specified by:
closein interfaceTerminal
-
pollInput
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- Specified by:
pollInputin interfaceIOSafeTerminal- Returns:
- Key object which represents a keystroke coming in through the input stream
-
readInput
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- Specified by:
readInputin interfaceIOSafeTerminal- Returns:
- Key object which represents a keystroke coming in through the input stream
-
newTextGraphics
Description copied from interface:TerminalCreates a new TextGraphics object that uses this Terminal directly when outputting. Keep in mind that you are probably better off to switch to a Screen to make advanced text graphics more efficient. Also, this TextGraphics implementation will not call.flush()after any operation, so you'll need to do that on your own.- Specified by:
newTextGraphicsin interfaceTerminal- Overrides:
newTextGraphicsin classAbstractTerminal- Returns:
- TextGraphics implementation that draws directly using this Terminal interface
-
addVirtualTerminalListener
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////- Specified by:
addVirtualTerminalListenerin interfaceVirtualTerminal- Parameters:
listener- Listener to receive events from this virtual terminal
-
removeVirtualTerminalListener
Description copied from interface:VirtualTerminalRemoves a listener from this virtual terminal so it will no longer receive events. Notice that this is not the same as the list ofTerminalResizeListener.- Specified by:
removeVirtualTerminalListenerin interfaceVirtualTerminal- Parameters:
listener- Listener to remove from this virtual terminal
-
setBacklogSize
public void setBacklogSize(int backlogSize) Description copied from interface:VirtualTerminalSets the number of rows to allow in the non-private buffer above the viewport. The total size of the text buffer will bebacklogSize + terminalSize.getRows(). If set to 0, there is no scrollback. Please note that private mode is unaffected by this and will always have no backlog.- Specified by:
setBacklogSizein interfaceVirtualTerminal- Parameters:
backlogSize- Number of rows of backlog
-
isCursorVisible
public boolean isCursorVisible()Description copied from interface:VirtualTerminalChecks if the terminal cursor is visible or not- Specified by:
isCursorVisiblein interfaceVirtualTerminal- Returns:
trueif the terminal cursor is visible,falseotherwise
-
addInput
Description copied from interface:VirtualTerminalAdds aKeyStroketo the input queue of this virtual terminal. This even will be read the next time eitherIOSafeTerminal.pollInput()orIOSafeTerminal.readInput()is called, assuming there are no other events before it in the queue.- Specified by:
addInputin interfaceVirtualTerminal- Parameters:
keyStroke-KeyStroketo add to the input queue of this virtual terminal
-
getDirtyCells
-
getAndResetDirtyCells
-
isWholeBufferDirtyThenReset
public boolean isWholeBufferDirtyThenReset() -
getCharacter
Description copied from interface:VirtualTerminalReturns a character from the viewport at the specified coordinates. This method cannot access the backlog, if you want to fetch a character potentially from the backlog, please useVirtualTerminal.getBufferCharacter(TerminalPosition)instead.- Specified by:
getCharacterin interfaceVirtualTerminal- Parameters:
position- Position of the character to return- Returns:
- Text character at the specific position in the viewport
-
getCharacter
Description copied from interface:VirtualTerminalReturns a character from the viewport at the specified coordinates. This method cannot access the backlog, if you want to fetch a character potentially from the backlog, please useVirtualTerminal.getBufferCharacter(int,int)instead.- Specified by:
getCharacterin interfaceVirtualTerminal- Parameters:
column- Column in the viewport to get the character fromrow- Row in the viewport to get the character form- Returns:
- Text character at the specific position in the viewport
-
getBufferCharacter
Description copied from interface:VirtualTerminalReturns a character from this virtual terminal, relative to the top-left position of the text buffer including any backlog. If you want to get a character from the bottom viewport, please useVirtualTerminal.getCharacter(int, int)instead.- Specified by:
getBufferCharacterin interfaceVirtualTerminal- Parameters:
column- Column to get the character fromrow- Row, counting from the first line in the backlog, to get the character from- Returns:
- Text character at the specific position in the text buffer
-
getBufferCharacter
Description copied from interface:VirtualTerminalReturns a character from this virtual terminal, relative to the top-left position of the text buffer including any backlog. If you want to get a character from the bottom viewport, please useVirtualTerminal.getCharacter(TerminalPosition)instead.- Specified by:
getBufferCharacterin interfaceVirtualTerminal- Parameters:
position- Position to get the character from- Returns:
- Text character at the specific position in the text buffer
-
getBufferLineCount
public int getBufferLineCount()Description copied from interface:VirtualTerminalReturns the number of lines in the entire text buffer, including any backlog- Specified by:
getBufferLineCountin interfaceVirtualTerminal- Returns:
- Number of lines in the buffer
-
forEachLine
Description copied from interface:VirtualTerminalIterates over a range of lines in the text buffer- Specified by:
forEachLinein interfaceVirtualTerminal- Parameters:
startRow- Index of the first row of the iteration, counting 0 as the first row in the backlogendRow- Index of the last row of the iteration (inclusive), counting 0 as the first row in the backlogbufferWalker- Callback to invoke on each row in the iteration
-
putCharacter
-
moveCursorToNextLine
private void moveCursorToNextLine()Moves the text cursor to the first column of the next line and trims the backlog of necessary -
setWholeBufferDirty
private void setWholeBufferDirty()Marks the whole buffer as dirty so every cell is considered in need to repainting. This is used by methods such as clear and bell that will affect all content at once. -
trimBufferBacklog
private void trimBufferBacklog() -
correctCursor
private void correctCursor() -
toString
-