Class Display
- All Implemented Interfaces:
Sized
The Display class provides functionality for managing the display of content on the terminal screen. It handles the complexities of cursor positioning, line wrapping, and efficient screen updates to minimize the amount of data sent to the terminal.
This class supports two main modes of operation:
- Full-screen mode - Takes over the entire terminal screen
- Partial-screen mode - Updates only a portion of the screen, preserving content above
Key features include:
- Efficient screen updates using cursor positioning
- Support for multi-line content with proper wrapping
- Handling of ANSI-styled text (colors, attributes)
- Size-aware rendering that adapts to terminal dimensions
- Cursor positioning relative to the display area
This class is used by various JLine components, such as LineReader, to provide efficient terminal display management for features like command-line editing, completion menus, and status messages.
Thread Safety
This class is NOT thread-safe and must be accessed from a single thread or with external synchronization. The Display class maintains mutable state including cursor position, screen content, and terminal dimensions that can be corrupted by concurrent access.
Components that use Display in multi-threaded environments (such as signal handlers for window resize events) must provide their own synchronization. For example, the LineReader uses a ReentrantLock to coordinate access between the main thread and signal handlers.
Warning: Concurrent access to Display methods may result in:
- ConcurrentModificationException
- Corrupted terminal output
- Inconsistent cursor positioning
- Race conditions in screen updates
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final booleanprotected intprotected intprotected final Map<InfoCmp.Capability, Integer> protected final booleanprotected intprotected booleanprotected booleanprotected final booleanprotected final List<AttributedString> protected booleanprotected intprotected final Terminalprotected final booleanprotected final booleanprotected boolean -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected booleancan(InfoCmp.Capability single, InfoCmp.Capability multi) voidclear()Clears the whole screen.booleanIf cursor is at right margin, don't wrap immediately.protected booleandeleteChars(int nb) protected booleandeleteLines(int nb) Emits terminal control sequences to delete the specified number of lines.intGet the current display width in character cells.intgetRows()The current display height in character rows.protected booleaninsertChars(int nb) protected booleaninsertLines(int nb) protected intmoveVisualCursorTo(int i1) Move the visual cursor to the specified wrapped-line position without allowing movement to a right-margin target.protected voidmoveVisualCursorTo(int targetPos, List<AttributedString> newLines) protected booleanperform(InfoCmp.Capability single, InfoCmp.Capability multi, int nb) Emits a terminal capability to affect a repeated action, using the parameterized (multi) form when available and preferable, otherwise repeating the single-capability.voidreset()Clears the cached model of previously rendered lines.voidresize(int rows, int columns) Deprecated.voidResize the display to the dimensions specified by the given Size.voidsetDelayLineWrap(boolean v) Enable or disable delayed line wrapping when the cursor reaches the right margin.voidupdate(List<AttributedString> newLines, int targetCursorPos) Update the display according to the new lines and flushes the output.voidupdate(List<AttributedString> newLines, int targetCursorPos, boolean flush) Update the display according to the new lines.voidupdateAnsi(List<String> newLines, int targetCursorPos) Update the display with lines containing ANSI escape sequences and flush the output.intCompute the number of terminal columns required to display a string, interpreting ANSI escape sequences.
-
Field Details
-
terminal
-
fullScreen
protected final boolean fullScreen -
oldLines
-
cursorPos
protected int cursorPos -
columns
protected int columns -
columns1
protected int columns1 -
rows
protected int rows -
reset
protected boolean reset -
delayLineWrap
protected boolean delayLineWrap -
cost
-
canScroll
protected final boolean canScroll -
terminalWrapAtEol
protected final boolean terminalWrapAtEol -
terminalDelayedWrapAtEol
protected final boolean terminalDelayedWrapAtEol -
wrapAtEol
protected boolean wrapAtEol -
delayedWrapAtEol
protected boolean delayedWrapAtEol -
cursorDownIsNewLine
protected final boolean cursorDownIsNewLine
-
-
Constructor Details
-
Display
Create a Display bound to the given Terminal and configured for either full-screen or inline (partial-screen) usage.Queries the terminal for capabilities and initializes internal flags that control scrolling, wrap-at-end-of-line behavior, and cursor movement semantics.
- Parameters:
terminal- the target terminal used for renderingfullscreen- true to enable full-screen (application-takes-over) mode, false for partial-screen mode
-
-
Method Details
-
delayLineWrap
public boolean delayLineWrap()If cursor is at right margin, don't wrap immediately. Seeorg.jline.reader.LineReader.Option#DELAY_LINE_WRAP.- Returns:
trueif line wrap is delayed,falseotherwise
-
setDelayLineWrap
public void setDelayLineWrap(boolean v) Enable or disable delayed line wrapping when the cursor reaches the right margin.- Parameters:
v- `true` to delay wrapping at end-of-line, `false` to disable delayed wrapping
-
resize
Resize the display to the dimensions specified by the given Size.- Parameters:
sized- the target display dimensions; its rows and columns are applied to the display
-
resize
Deprecated.Useresize(Sized)instead to avoid parameter order confusion.Resize the display to the specified number of rows and columns. This updates the display geometry, rewraps previously rendered lines to the new width, and adjusts wrap-at-EOL behavior based on the terminal's buffer width. If either dimension is zero the method treats it as a special case (sets rows to 1 and columns to a very large internal value) to avoid a zero-sized display.- Parameters:
rows- the number of display rowscolumns- the number of display columns
-
getColumns
public int getColumns()Get the current display width in character cells.- Specified by:
getColumnsin interfaceSized- Returns:
- the number of columns (display width)
-
getRows
-
reset
public void reset()Clears the cached model of previously rendered lines.The next
update(java.util.List<org.jline.utils.AttributedString>, int)call will treat all content as new and repaint every line via the diff algorithm. This does not issue a terminalclear_screen; to also clear the physical screen, callclear()beforereset(). -
clear
public void clear()Clears the whole screen. Use this method only when using full-screen / application mode. -
updateAnsi
Update the display with lines containing ANSI escape sequences and flush the output.Each string in the list is parsed via
AttributedString.fromAnsi(String)to convert ANSI escape codes into styledAttributedStrings, then delegated toupdate(List, int).- Parameters:
newLines- the new lines to display, with embedded ANSI escape sequencestargetCursorPos- the desired cursor position after the update (0-based character offset from the start of the first line, or -1 to leave the cursor at the end)
-
update
Update the display according to the new lines and flushes the output.- Parameters:
newLines- the lines to displaytargetCursorPos- desired cursor position - see Size.cursorPos.
-
update
Update the display according to the new lines.- Parameters:
newLines- the lines to displaytargetCursorPos- desired cursor position - see Size.cursorPos.flush- whether the output should be flushed or not
-
deleteLines
protected boolean deleteLines(int nb) Emits terminal control sequences to delete the specified number of lines.- Parameters:
nb- the number of lines to delete- Returns:
- `true` if a delete-line capability was available and the operation was issued, `false` otherwise
-
insertLines
protected boolean insertLines(int nb) -
insertChars
protected boolean insertChars(int nb) -
deleteChars
protected boolean deleteChars(int nb) -
can
-
perform
Emits a terminal capability to affect a repeated action, using the parameterized (multi) form when available and preferable, otherwise repeating the single-capability.- Parameters:
single- the single-invocation capability to use repeatedly if a multi-parameter form is unavailable or not preferablemulti- the multi-parameter capability that can perform the action for a specified count in one invocationnb- the number of times the action should be applied- Returns:
trueif a capability sequence was emitted to perform the action,falseif neither capability is available
-
moveVisualCursorTo
-
moveVisualCursorTo
protected int moveVisualCursorTo(int i1) Move the visual cursor to the specified wrapped-line position without allowing movement to a right-margin target. Moves the terminal cursor from the current visual position (stored incursorPos) toi1, handling line and column transitions, and updatingcursorPos. If the current position is at the right margin a carriage return is emitted before further movement. The target position must not lie on a right-margin column (i.e.i1 % columns1 != columns).- Parameters:
i1- the target visual cursor position in wrapped-line coordinates- Returns:
- the updated cursor position (equal to
i1)
-
wcwidth
Compute the number of terminal columns required to display a string, interpreting ANSI escape sequences.- Parameters:
str- the input string, which may contain ANSI escape sequences; if `null` it is treated as empty- Returns:
- the displayed column width of the string (0 if `str` is `null`)
-
resize(Sized)instead to avoid parameter order confusion.