Class SwingTerminal

  • All Implemented Interfaces:
    InputProvider, IOSafeTerminal, Terminal, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Closeable, java.io.Serializable, java.lang.AutoCloseable

    public class SwingTerminal
    extends javax.swing.JComponent
    implements IOSafeTerminal
    This class provides an Swing implementation of the Terminal interface that is an embeddable component you can put into a Swing container. The class has static helper methods for opening a new frame with a SwingTerminal as its content, similar to how the SwingTerminal used to work in earlier versions of lanterna. This version supports private mode and non-private mode with a scrollback history. You can customize many of the properties by supplying device configuration, font configuration and color configuration when you construct the object.
    See Also:
    Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from class javax.swing.JComponent

        javax.swing.JComponent.AccessibleJComponent
      • Nested classes/interfaces inherited from class java.awt.Container

        java.awt.Container.AccessibleAWTContainer
      • Nested classes/interfaces inherited from class java.awt.Component

        java.awt.Component.AccessibleAWTComponent, java.awt.Component.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private TerminalInputMethodRequests inputMethodRequests  
      private SwingTerminalImplementation terminalImplementation  
      • Fields inherited from class javax.swing.JComponent

        listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
      • Fields inherited from class java.awt.Component

        accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
      • Fields inherited from interface java.awt.image.ImageObserver

        ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addInput​(KeyStroke keyStroke)
      Takes a KeyStroke and puts it on the input queue of the terminal emulator.
      void addResizeListener​(TerminalResizeListener listener)
      Adds a TerminalResizeListener to be called when the terminal has changed size.
      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, java.util.concurrent.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.
      TerminalEmulatorColorConfiguration getColorConfiguration()
      Returns this terminal emulator's color configuration.
      TerminalPosition getCursorPosition()
      Returns the position of the cursor, as reported by the terminal.
      TerminalEmulatorDeviceConfiguration getDeviceConfiguration()
      Returns this terminal emulator's device configuration.
      SwingTerminalFontConfiguration getFontConfiguration()
      Returns the current font configuration.
      java.awt.im.InputMethodRequests getInputMethodRequests()  
      java.awt.Dimension getPreferredSize()
      Overridden method from Swing's JComponent class that returns the preferred size of the terminal (in pixels)
      TerminalSize getTerminalSize()
      Returns the size of the terminal, expressed as a TerminalSize object.
      TextGraphics newTextGraphics()
      Creates a new TextGraphics object that uses this Terminal directly when outputting.
      protected void paintComponent​(java.awt.Graphics componentGraphics)
      Overridden method from Swing's JComponent class that is called by OS window system when the component needs to be redrawn
      KeyStroke pollInput()
      Returns the next Key off the input queue or null if there is no more input events available.
      protected void processInputMethodEvent​(java.awt.event.InputMethodEvent e)  
      void putCharacter​(char c)
      Prints one character to the terminal at the current cursor location.
      void putString​(java.lang.String string)
      Prints a string 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 removeResizeListener​(TerminalResizeListener listener)
      Removes a TerminalResizeListener from the list of listeners to be notified when the terminal has changed size
      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 setCursorPosition​(int x, int y)
      Moves the text cursor to a new location on the terminal.
      void setCursorPosition​(TerminalPosition position)
      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.
      • Methods inherited from class javax.swing.JComponent

        addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUI, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI
      • Methods inherited from class java.awt.Container

        add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
      • Methods inherited from class java.awt.Component

        action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • SwingTerminal

        public SwingTerminal()
        Creates a new SwingTerminal with all the defaults set and no scroll controller connected.
      • SwingTerminal

        public SwingTerminal​(TerminalScrollController scrollController)
        Creates a new SwingTerminal with a particular scrolling controller that will be notified when the terminals history size grows and will be called when this class needs to figure out the current scrolling position.
        Parameters:
        scrollController - Controller for scrolling the terminal history
      • SwingTerminal

        public SwingTerminal​(TerminalEmulatorDeviceConfiguration deviceConfiguration,
                             SwingTerminalFontConfiguration fontConfiguration,
                             TerminalEmulatorColorConfiguration colorConfiguration)
        Creates a new SwingTerminal component using custom settings and no scroll controller.
        Parameters:
        deviceConfiguration - Device configuration to use for this SwingTerminal
        fontConfiguration - Font configuration to use for this SwingTerminal
        colorConfiguration - Color configuration to use for this SwingTerminal
      • SwingTerminal

        public SwingTerminal​(TerminalSize initialTerminalSize,
                             TerminalEmulatorDeviceConfiguration deviceConfiguration,
                             SwingTerminalFontConfiguration fontConfiguration,
                             TerminalEmulatorColorConfiguration colorConfiguration)
        Creates a new SwingTerminal component using custom settings and no scroll controller.
        Parameters:
        initialTerminalSize - Initial size of the terminal, which will be used when calculating the preferred size of the component. If null, it will default to 80x25. If the AWT layout manager forces the component to a different size, the value of this parameter won't have any meaning
        deviceConfiguration - Device configuration to use for this SwingTerminal
        fontConfiguration - Font configuration to use for this SwingTerminal
        colorConfiguration - Color configuration to use for this SwingTerminal
      • SwingTerminal

        public SwingTerminal​(TerminalEmulatorDeviceConfiguration deviceConfiguration,
                             SwingTerminalFontConfiguration fontConfiguration,
                             TerminalEmulatorColorConfiguration colorConfiguration,
                             TerminalScrollController scrollController)
        Creates a new SwingTerminal component using custom settings and a custom scroll controller. The scrolling controller will be notified when the terminal's history size grows and will be called when this class needs to figure out the current scrolling position.
        Parameters:
        deviceConfiguration - Device configuration to use for this SwingTerminal
        fontConfiguration - Font configuration to use for this SwingTerminal
        colorConfiguration - Color configuration to use for this SwingTerminal
        scrollController - Controller to use for scrolling, the object passed in will be notified whenever the scrollable area has changed
      • SwingTerminal

        public SwingTerminal​(TerminalSize initialTerminalSize,
                             TerminalEmulatorDeviceConfiguration deviceConfiguration,
                             SwingTerminalFontConfiguration fontConfiguration,
                             TerminalEmulatorColorConfiguration colorConfiguration,
                             TerminalScrollController scrollController)
        Creates a new SwingTerminal component using custom settings and a custom scroll controller. The scrolling controller will be notified when the terminal's history size grows and will be called when this class needs to figure out the current scrolling position.
        Parameters:
        initialTerminalSize - Initial size of the terminal, which will be used when calculating the preferred size of the component. If null, it will default to 80x25. If the AWT layout manager forces the component to a different size, the value of this parameter won't have any meaning
        deviceConfiguration - Device configuration to use for this SwingTerminal
        fontConfiguration - Font configuration to use for this SwingTerminal
        colorConfiguration - Color configuration to use for this SwingTerminal
        scrollController - Controller to use for scrolling, the object passed in will be notified whenever the scrollable area has changed
    • Method Detail

      • getFontConfiguration

        public SwingTerminalFontConfiguration getFontConfiguration()
        Returns the current font configuration. Note that it is immutable and cannot be changed.
        Returns:
        This SwingTerminal's current font configuration
      • getColorConfiguration

        public TerminalEmulatorColorConfiguration getColorConfiguration()
        Returns this terminal emulator's color configuration. Note that it is immutable and cannot be changed.
        Returns:
        This SwingTerminal's color configuration
      • getDeviceConfiguration

        public TerminalEmulatorDeviceConfiguration getDeviceConfiguration()
        Returns this terminal emulator's device configuration. Note that it is immutable and cannot be changed.
        Returns:
        This SwingTerminal's device configuration
      • getPreferredSize

        public java.awt.Dimension getPreferredSize()
        Overridden method from Swing's JComponent class that returns the preferred size of the terminal (in pixels)
        Overrides:
        getPreferredSize in class javax.swing.JComponent
        Returns:
        The terminal's preferred size in pixels
      • paintComponent

        protected void paintComponent​(java.awt.Graphics componentGraphics)
        Overridden method from Swing's JComponent class that is called by OS window system when the component needs to be redrawn
        Overrides:
        paintComponent in class javax.swing.JComponent
        Parameters:
        componentGraphics - Graphics object to use when drawing the component
      • addInput

        public void addInput​(KeyStroke keyStroke)
        Takes a KeyStroke and puts it on the input queue of the terminal emulator. This way you can insert synthetic input events to be processed as if they came from the user typing on the keyboard.
        Parameters:
        keyStroke - Key stroke input event to put on the queue
      • getInputMethodRequests

        public java.awt.im.InputMethodRequests getInputMethodRequests()
        Overrides:
        getInputMethodRequests in class java.awt.Component
      • processInputMethodEvent

        protected void processInputMethodEvent​(java.awt.event.InputMethodEvent e)
        Overrides:
        processInputMethodEvent in class java.awt.Component
      • enterPrivateMode

        public void enterPrivateMode()
        Description copied from interface: Terminal
        Calling 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:
        enterPrivateMode in interface IOSafeTerminal
        Specified by:
        enterPrivateMode in interface Terminal
      • exitPrivateMode

        public void exitPrivateMode()
        Description copied from interface: Terminal
        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. 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:
        exitPrivateMode in interface IOSafeTerminal
        Specified by:
        exitPrivateMode in interface Terminal
      • clearScreen

        public void clearScreen()
        Description copied from interface: Terminal
        Removes 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 call moveCursor next. Some terminal implementations doesn't reset color and modifier state so it's also good practise to call resetColorAndSGR() after this.
        Specified by:
        clearScreen in interface IOSafeTerminal
        Specified by:
        clearScreen in interface Terminal
      • setCursorPosition

        public void setCursorPosition​(int x,
                                      int y)
        Description copied from interface: Terminal
        Moves 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:
        setCursorPosition in interface IOSafeTerminal
        Specified by:
        setCursorPosition in interface Terminal
        Parameters:
        x - The 0-indexed column to place the cursor at
        y - The 0-indexed row to place the cursor at
      • getCursorPosition

        public TerminalPosition getCursorPosition()
        Description copied from interface: Terminal
        Returns 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:
        getCursorPosition in interface IOSafeTerminal
        Specified by:
        getCursorPosition in interface Terminal
        Returns:
        Position of the cursor
      • setCursorVisible

        public void setCursorVisible​(boolean visible)
        Description copied from interface: Terminal
        Hides 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:
        setCursorVisible in interface IOSafeTerminal
        Specified by:
        setCursorVisible in interface Terminal
        Parameters:
        visible - Hides the text cursor if false and shows it if true
      • putCharacter

        public void putCharacter​(char c)
        Description copied from interface: Terminal
        Prints 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 to 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).

        Specified by:
        putCharacter in interface IOSafeTerminal
        Specified by:
        putCharacter in interface Terminal
        Parameters:
        c - Character to place on the terminal
      • putString

        public void putString​(java.lang.String string)
        Description copied from interface: Terminal
        Prints 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 to putString 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).

        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:
        putString in interface IOSafeTerminal
        Specified by:
        putString in interface Terminal
        Parameters:
        string - String to place on the terminal
      • enableSGR

        public void enableSGR​(SGR sgr)
        Description copied from interface: Terminal
        Activates an 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.
        Specified by:
        enableSGR in interface IOSafeTerminal
        Specified by:
        enableSGR in interface Terminal
        Parameters:
        sgr - SGR code to apply
        See Also:
        SGR, http://www.vt100.net/docs/vt510-rm/SGR
      • setForegroundColor

        public void setForegroundColor​(TextColor color)
        Description copied from interface: Terminal
        Changes 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:
        setForegroundColor in interface IOSafeTerminal
        Specified by:
        setForegroundColor in interface Terminal
        Parameters:
        color - Color to use for foreground
      • setBackgroundColor

        public void setBackgroundColor​(TextColor color)
        Description copied from interface: Terminal
        Changes 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:
        setBackgroundColor in interface IOSafeTerminal
        Specified by:
        setBackgroundColor in interface Terminal
        Parameters:
        color - Color to use for the background
      • getTerminalSize

        public TerminalSize getTerminalSize()
        Description copied from interface: Terminal
        Returns the size of the terminal, expressed as a 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).
        Specified by:
        getTerminalSize in interface IOSafeTerminal
        Specified by:
        getTerminalSize in interface Terminal
        Returns:
        Size of the terminal
      • enquireTerminal

        public byte[] enquireTerminal​(int timeout,
                                      java.util.concurrent.TimeUnit timeoutUnit)
        Description copied from interface: Terminal
        Retrieves 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:
        enquireTerminal in interface IOSafeTerminal
        Specified by:
        enquireTerminal in interface Terminal
        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 the timeout parameter
        Returns:
        Answer-back message from the terminal or empty if there was nothing
      • bell

        public void bell()
        Description copied from interface: Terminal
        Prints 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:
        bell in interface IOSafeTerminal
        Specified by:
        bell in interface Terminal
      • flush

        public void flush()
        Description copied from interface: Terminal
        Calls 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.
        Specified by:
        flush in interface IOSafeTerminal
        Specified by:
        flush in interface Terminal
      • close

        public void close()
        Description copied from interface: Terminal
        Closes 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:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in interface IOSafeTerminal
        Specified by:
        close in interface Terminal
      • pollInput

        public KeyStroke pollInput()
        Description copied from interface: InputProvider
        Returns the next 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.
        Specified by:
        pollInput in interface InputProvider
        Specified by:
        pollInput in interface IOSafeTerminal
        Returns:
        Key object which represents a keystroke coming in through the input stream
      • readInput

        public KeyStroke readInput()
        Description copied from interface: InputProvider
        Returns the next 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.
        Specified by:
        readInput in interface InputProvider
        Specified by:
        readInput in interface IOSafeTerminal
        Returns:
        Key object which represents a keystroke coming in through the input stream
      • newTextGraphics

        public TextGraphics newTextGraphics()
        Description copied from interface: Terminal
        Creates 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:
        newTextGraphics in interface Terminal
        Returns:
        TextGraphics implementation that draws directly using this Terminal interface
      • addResizeListener

        public void addResizeListener​(TerminalResizeListener listener)
        Description copied from interface: Terminal
        Adds a TerminalResizeListener to be called when the terminal has changed size. There is no guarantee that this listener will really be invoked when the terminal has changed size, at all depends on the terminal emulator implementation. Normally on Unix systems the WINCH signal will be sent to the process and lanterna can intercept this.

        There are no guarantees on what thread the call will be made on, so please be careful with what kind of operation you perform in this callback. You should probably not take too long to return.

        Specified by:
        addResizeListener in interface Terminal
        Parameters:
        listener - Listener object to be called when the terminal has been changed
        See Also:
        TerminalResizeListener