InputProvider
, IOSafeTerminal
, Terminal
, VirtualTerminal
, Closeable
, AutoCloseable
public class DefaultVirtualTerminal extends AbstractTerminal implements VirtualTerminal
VirtualTerminal.BufferLine, VirtualTerminal.BufferWalker
Constructor | Description |
---|---|
DefaultVirtualTerminal() |
Creates a new virtual terminal with an initial size set
|
DefaultVirtualTerminal(TerminalSize initialTerminalSize) |
Creates a new virtual terminal with an initial size set
|
Modifier and Type | Method | Description |
---|---|---|
void |
addInput(KeyStroke keyStroke) |
Adds a
KeyStroke to the input queue of this virtual terminal. |
void |
addVirtualTerminalListener(VirtualTerminalListener listener) |
Adds a listener to receive notifications when certain events happens on the virtual terminal.
|
void |
bell() |
Prints 0x7 to the terminal, which will make the terminal (emulator) ring a bell (or more likely beep).
|
void |
clearScreen() |
Removes all the characters, colors and graphics from the screen and leaves you with a big empty space.
|
void |
close() |
Closes the terminal, if applicable.
|
void |
disableSGR(SGR sgr) |
Deactivates an
SGR (Selected Graphic Rendition) code which has previously been activated through
enableSGR(..) . |
void |
enableSGR(SGR sgr) |
Activates an
SGR (Selected Graphic Rendition) code. |
byte[] |
enquireTerminal(int timeout,
TimeUnit timeoutUnit) |
Retrieves optional information from the terminal by printing the ENQ (\u005) character.
|
void |
enterPrivateMode() |
Calling this method will, where supported, give your terminal a private area to use, separate from what was there
before.
|
void |
exitPrivateMode() |
If 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.
|
void |
flush() |
Calls
flush() on the underlying OutputStream object, or whatever other implementation this
terminal is built around. |
void |
forEachLine(int startRow,
int endRow,
VirtualTerminal.BufferWalker bufferWalker) |
Iterates over a range of lines in the text buffer
|
TreeSet<TerminalPosition> |
getAndResetDirtyCells() |
|
TextCharacter |
getBufferCharacter(int column,
int row) |
Returns a character from this virtual terminal, relative to the top-left position of the text buffer including
any backlog.
|
TextCharacter |
getBufferCharacter(TerminalPosition position) |
Returns a character from this virtual terminal, relative to the top-left position of the text buffer including
any backlog.
|
int |
getBufferLineCount() |
Returns the number of lines in the entire text buffer, including any backlog
|
TextCharacter |
getCharacter(int column,
int row) |
Returns a character from the viewport at the specified coordinates.
|
TextCharacter |
getCharacter(TerminalPosition position) |
Returns a character from the viewport at the specified coordinates.
|
TerminalPosition |
getCursorBufferPosition() |
Returns the position of the terminal cursor where the row index is counted from the top of the text buffer,
including all backlog.
|
TerminalPosition |
getCursorPosition() |
Returns the position of the cursor, as reported by the terminal.
|
TreeSet<TerminalPosition> |
getDirtyCells() |
|
TerminalSize |
getTerminalSize() |
Returns the size of the terminal, expressed as a
TerminalSize object. |
boolean |
isCursorVisible() |
Checks if the terminal cursor is visible or not
|
boolean |
isWholeBufferDirtyThenReset() |
|
TextGraphics |
newTextGraphics() |
Creates a new TextGraphics object that uses this Terminal directly when outputting.
|
KeyStroke |
pollInput() |
Returns the next
Key off the input queue or null if there is no more input events available. |
void |
putCharacter(char c) |
Prints one character to the terminal at the current cursor location.
|
KeyStroke |
readInput() |
Returns the next
Key off the input queue or blocks until one is available. |
void |
removeVirtualTerminalListener(VirtualTerminalListener listener) |
Removes a listener from this virtual terminal so it will no longer receive events.
|
void |
resetColorAndSGR() |
Removes all currently active SGR codes and sets foreground and background colors back to default.
|
void |
setBackgroundColor(TextColor color) |
Changes the background color for all the following characters put to the terminal.
|
void |
setBacklogSize(int backlogSize) |
Sets the number of rows to allow in the non-private buffer above the viewport.
|
void |
setCursorPosition(int x,
int y) |
Moves the text cursor to a new location on the terminal.
|
void |
setCursorPosition(TerminalPosition cursorPosition) |
Same as calling
setCursorPosition(position.getColumn(), position.getRow()) |
void |
setCursorVisible(boolean visible) |
Hides or shows the text cursor, but not all terminal (-emulators) supports this.
|
void |
setForegroundColor(TextColor color) |
Changes the foreground color for all the following characters put to the terminal.
|
void |
setTerminalSize(TerminalSize newSize) |
Changes the "visible size" of the virtual terminal.
|
addResizeListener, onResized, onResized, removeResizeListener
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
addResizeListener, removeResizeListener
public DefaultVirtualTerminal()
public DefaultVirtualTerminal(TerminalSize initialTerminalSize)
initialTerminalSize
- Starting size of the virtual terminalpublic TerminalSize getTerminalSize()
Terminal
TerminalSize
object. Please bear in mind that depending
on the Terminal
implementation, this may or may not be accurate. See the implementing classes for more
information. Most commonly, calling getTerminalSize() will involve some kind of hack to retrieve the size of the
terminal, like moving the cursor to position 5000x5000 and then read back the location, unless the terminal
implementation has a more smooth way of getting this data. Keep this in mind and see if you can avoid calling
this method too often. There is a helper class, SimpleTerminalResizeListener, that you can use to cache the size
and update it only when resize events are received (which depends on if a resize is detectable, which they are not
on all platforms).getTerminalSize
in interface IOSafeTerminal
getTerminalSize
in interface Terminal
public void setTerminalSize(TerminalSize newSize)
VirtualTerminal
setTerminalSize
in interface VirtualTerminal
newSize
- New size of the virtual terminalpublic void enterPrivateMode()
Terminal
enterPrivateMode
in interface IOSafeTerminal
enterPrivateMode
in interface Terminal
public void exitPrivateMode()
Terminal
exitPrivateMode
in interface IOSafeTerminal
exitPrivateMode
in interface Terminal
public void clearScreen()
Terminal
moveCursor
next. Some terminal implementations doesn't reset color and modifier state so it's also good
practise to call resetColorAndSGR()
after this.clearScreen
in interface IOSafeTerminal
clearScreen
in interface Terminal
public void setCursorPosition(int x, int y)
Terminal
setCursorPosition
in interface IOSafeTerminal
setCursorPosition
in interface Terminal
x
- The 0-indexed column to place the cursor aty
- The 0-indexed row to place the cursor atpublic void setCursorPosition(TerminalPosition cursorPosition)
Terminal
setCursorPosition(position.getColumn(), position.getRow())
setCursorPosition
in interface IOSafeTerminal
setCursorPosition
in interface Terminal
cursorPosition
- Position to place the cursor atpublic TerminalPosition getCursorPosition()
Terminal
getCursorPosition
in interface IOSafeTerminal
getCursorPosition
in interface Terminal
public TerminalPosition getCursorBufferPosition()
VirtualTerminal
IOSafeTerminal.getCursorPosition()
instead.getCursorBufferPosition
in interface VirtualTerminal
public void setCursorVisible(boolean visible)
Terminal
setCursorVisible
in interface IOSafeTerminal
setCursorVisible
in interface Terminal
visible
- Hides the text cursor if false
and shows it if true
public void putCharacter(char c)
Terminal
putCharacter
will 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 Terminal
implementations bundled with Lanterna will).
putCharacter
in interface IOSafeTerminal
putCharacter
in interface Terminal
c
- Character to place on the terminalpublic void enableSGR(SGR sgr)
Terminal
SGR
(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.enableSGR
in interface IOSafeTerminal
enableSGR
in interface Terminal
sgr
- SGR code to applySGR
,
http://www.vt100.net/docs/vt510-rm/SGRpublic void disableSGR(SGR sgr)
Terminal
SGR
(Selected Graphic Rendition) code which has previously been activated through
enableSGR(..)
.disableSGR
in interface IOSafeTerminal
disableSGR
in interface Terminal
sgr
- SGR code to applySGR
,
http://www.vt100.net/docs/vt510-rm/SGRpublic void resetColorAndSGR()
Terminal
resetColorAndSGR
in interface IOSafeTerminal
resetColorAndSGR
in interface Terminal
SGR
,
http://www.vt100.net/docs/vt510-rm/SGRpublic void setForegroundColor(TextColor color)
Terminal
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);
setForegroundColor
in interface IOSafeTerminal
setForegroundColor
in interface Terminal
color
- Color to use for foregroundpublic void setBackgroundColor(TextColor color)
Terminal
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);
setBackgroundColor
in interface IOSafeTerminal
setBackgroundColor
in interface Terminal
color
- Color to use for the backgroundpublic byte[] enquireTerminal(int timeout, TimeUnit timeoutUnit)
Terminal
enquireTerminal
in interface IOSafeTerminal
enquireTerminal
in interface Terminal
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 the timeout
parameterpublic void bell()
Terminal
bell
in interface IOSafeTerminal
bell
in interface Terminal
public void flush()
Terminal
flush()
on the underlying OutputStream
object, 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.flush
in interface IOSafeTerminal
flush
in interface Terminal
public void close()
Terminal
close
in interface AutoCloseable
close
in interface Closeable
close
in interface IOSafeTerminal
close
in interface Terminal
public KeyStroke pollInput()
InputProvider
Key
off 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.pollInput
in interface InputProvider
pollInput
in interface IOSafeTerminal
public KeyStroke readInput()
InputProvider
Key
off 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 call
pollInput()
for the non-blocking version.readInput
in interface InputProvider
readInput
in interface IOSafeTerminal
public TextGraphics newTextGraphics()
Terminal
.flush()
after any operation, so you'll need to do that on your own.newTextGraphics
in interface Terminal
newTextGraphics
in class AbstractTerminal
public void addVirtualTerminalListener(VirtualTerminalListener listener)
VirtualTerminal
TerminalResizeListener
, but as the
VirtualTerminalListener
also allows you to listen on size changes, it can be used for the same purpose.addVirtualTerminalListener
in interface VirtualTerminal
listener
- Listener to receive events from this virtual terminalpublic void removeVirtualTerminalListener(VirtualTerminalListener listener)
VirtualTerminal
TerminalResizeListener
.removeVirtualTerminalListener
in interface VirtualTerminal
listener
- Listener to remove from this virtual terminalpublic void setBacklogSize(int backlogSize)
VirtualTerminal
backlogSize + 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.setBacklogSize
in interface VirtualTerminal
backlogSize
- Number of rows of backlogpublic boolean isCursorVisible()
VirtualTerminal
isCursorVisible
in interface VirtualTerminal
true
if the terminal cursor is visible, false
otherwisepublic void addInput(KeyStroke keyStroke)
VirtualTerminal
KeyStroke
to the input queue of this virtual terminal. This even will be read the next time either
IOSafeTerminal.pollInput()
or IOSafeTerminal.readInput()
is called, assuming there are no other events before it in the queue.addInput
in interface VirtualTerminal
keyStroke
- KeyStroke
to add to the input queue of this virtual terminalpublic TreeSet<TerminalPosition> getDirtyCells()
public TreeSet<TerminalPosition> getAndResetDirtyCells()
public boolean isWholeBufferDirtyThenReset()
public TextCharacter getCharacter(TerminalPosition position)
VirtualTerminal
VirtualTerminal.getBufferCharacter(TerminalPosition)
instead.getCharacter
in interface VirtualTerminal
position
- Position of the character to returnpublic TextCharacter getCharacter(int column, int row)
VirtualTerminal
VirtualTerminal.getBufferCharacter(int,int)
instead.getCharacter
in interface VirtualTerminal
column
- Column in the viewport to get the character fromrow
- Row in the viewport to get the character formpublic TextCharacter getBufferCharacter(int column, int row)
VirtualTerminal
VirtualTerminal.getCharacter(int, int)
instead.getBufferCharacter
in interface VirtualTerminal
column
- Column to get the character fromrow
- Row, counting from the first line in the backlog, to get the character frompublic TextCharacter getBufferCharacter(TerminalPosition position)
VirtualTerminal
VirtualTerminal.getCharacter(TerminalPosition)
instead.getBufferCharacter
in interface VirtualTerminal
position
- Position to get the character frompublic int getBufferLineCount()
VirtualTerminal
getBufferLineCount
in interface VirtualTerminal
public void forEachLine(int startRow, int endRow, VirtualTerminal.BufferWalker bufferWalker)
VirtualTerminal
forEachLine
in interface VirtualTerminal
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 iterationCopyright © 2020. All rights reserved.