Package org.jline.utils
Class ScreenTerminal
java.lang.Object
org.jline.utils.ScreenTerminal
- All Implemented Interfaces:
Sized
A virtual terminal screen implementation.
This class provides a virtual terminal screen that can process ANSI escape sequences and maintain the state of a terminal display. It supports features including:
- Cursor positioning and movement
- Text attributes (bold, underline, etc.)
- Color support (including 256 colors)
- Screen clearing and scrolling
- Alternate screen buffer
- Screen resizing
This implementation is based on the Apache Karaf WebConsole Gogo plugin, with adaptations to support alternate screen, resizing, and 256 colors. It follows the ECMA-48 standard for terminal control sequences.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates a ScreenTerminal using the default size of 80 columns and 24 rows.ScreenTerminal(int columns, int rows) Creates a ScreenTerminal with the specified number of columns and rows.ScreenTerminal(int columns, int rows, boolean eatNewlineGlitch) Creates a ScreenTerminal with the specified size and wrap behavior. -
Method Summary
Modifier and TypeMethodDescriptionstatic longcellAttr(long cell) Extracts the raw 32-bit attribute word from a cell value.static intcellBackground(long cell) Extracts the background color as a 12-bit value (4 bits per r/g/b channel).static booleancellBold(long cell) Returnstrueif the cell has the bold attribute set.static intcellCodePoint(long cell) Extracts the Unicode code point from a cell value.static intcellColorRgb(int color12) Expands a 12-bit color (as returned bycellForeground(long)orcellBackground(long)) to a 24-bit RGB value.static booleancellConceal(long cell) Returnstrueif the cell has the conceal (hidden) attribute set.static booleancellDim(long cell) Returnstrueif the cell has the dim/faint attribute set.static intcellForeground(long cell) Extracts the foreground color as a 12-bit value (4 bits per r/g/b channel).static booleancellHasBackground(long cell) Returnstrueif the cell has an explicit background color set.static booleancellHasForeground(long cell) Returnstrueif the cell has an explicit foreground color set.static booleancellInverse(long cell) Returnstrueif the cell has the inverse (negative) attribute set.static booleancellItalic(long cell) Returnstrueif the cell has the italic attribute set.static booleancellUnderline(long cell) Returnstrueif the cell has the underline attribute set.dump()Dumps the terminal content as HTML with inline RGB color styles.voiddump(long[] fullscreen, int[] cursor) Dumps the raw screen content into a flat array.voiddump(long[] fullscreen, int ftop, int fleft, int fheight, int fwidth, int[] cursor) dump(long timeout, boolean forceDump) Dumps the terminal content as HTML with inline RGB color styles.booleandump(long timeout, boolean forceDump, long[] fullscreen, int[] cursor) Waits for the screen to be dirty, then dumps the raw screen content.booleandump(long timeout, boolean forceDump, long[] fullscreen, int ftop, int fleft, int fheight, int fwidth, int[] cursor) Waits for the screen to be dirty, then dumps the raw screen content into a subregion.intProvide the current number of columns in the terminal.intDeprecated.List<long[]> Returns a snapshot of the scrollback history: the lines that have scrolled off the top of the active screen, oldest first.intReturns the number of lines currently held in the scrollback history.intgetRows()Gets the number of rows in the terminal.intgetWidth()Deprecated.UsegetColumns()instead.booleanisDirty()read()protected voidsetDirty()booleansetSize(int columns, int rows) Deprecated.UsesetSize(Sized)instead.booleanResize the terminal to the specified columns and rows.toString()voidbooleanwaitDirty(long timeout) Waits for the screen to become dirty, up to the given timeout.boolean
-
Field Details
-
MIN_SIZE
public static final int MIN_SIZE- See Also:
-
MAX_SIZE
public static final int MAX_SIZE- See Also:
-
-
Constructor Details
-
ScreenTerminal
public ScreenTerminal()Creates a ScreenTerminal using the default size of 80 columns and 24 rows. -
ScreenTerminal
public ScreenTerminal(int columns, int rows) Creates a ScreenTerminal with the specified number of columns and rows.- Parameters:
columns- the number of character columns for the terminalrows- the number of character rows for the terminal
-
ScreenTerminal
public ScreenTerminal(int columns, int rows, boolean eatNewlineGlitch) Creates a ScreenTerminal with the specified size and wrap behavior.- Parameters:
columns- the number of character columns for the terminalrows- the number of character rows for the terminaleatNewlineGlitch- whentrue, the cursor stays at the last column after writing a character there (delayed wrap / xenl behavior, like xterm). Whenfalse, writing at the last column immediately wraps the cursor to column 0 of the next line (like windows-vtp).
-
-
Method Details
-
isDirty
public boolean isDirty() -
waitDirty
- Throws:
InterruptedException
-
waitDirty
Waits for the screen to become dirty, up to the given timeout. Uses a while loop to guard against spurious wakeups.- Parameters:
timeout- maximum time to wait in milliseconds; if<= 0, returns immediately- Returns:
- true if the screen is dirty
- Throws:
InterruptedException- if interrupted while waiting
-
setDirty
protected void setDirty() -
getColumns
public int getColumns()Provide the current number of columns in the terminal.- Specified by:
getColumnsin interfaceSized- Returns:
- the current number of columns
-
getRows
public int getRows()Gets the number of rows in the terminal. -
getWidth
Deprecated.UsegetColumns()instead.Gets the terminal width in characters.- Returns:
- the width in characters
-
getHeight
Deprecated.UsegetRows()instead.Gets the terminal height in characters.- Returns:
- the height in characters
-
setSize
Resize the terminal to the specified columns and rows.- Parameters:
sized- the new Size whose columns and rows will be applied- Returns:
- true if the size was set successfully, false otherwise
-
setSize
Deprecated.UsesetSize(Sized)instead.Resize the terminal to the given number of columns and rows.- Parameters:
columns- the target number of columns (2?4096)rows- the target number of rows (2?4096)- Returns:
- `true` if the size was changed; `false` if the requested dimensions are out of range
-
read
-
pipe
-
write
-
dump
public void dump(long[] fullscreen, int ftop, int fleft, int fheight, int fwidth, int[] cursor) -
dump
public void dump(long[] fullscreen, int[] cursor) Dumps the raw screen content into a flat array. The array must be at leastcolumns * rowselements long.- Parameters:
fullscreen- destination arraycursor- 2-element array to receive cursor [x, y], or null
-
getHistory
Returns a snapshot of the scrollback history: the lines that have scrolled off the top of the active screen, oldest first. Each row is encoded in the same cell format asdump(long[], int[])(the low 32 bits hold the code point, the high 32 bits hold the attributes).The returned
Listis a copy, so it is safe to iterate while the terminal keeps processing output. Thelong[]rows it references are shared with the terminal and must be treated as read-only.- Returns:
- a snapshot of the scrollback history rows, oldest first (never
null)
-
getHistorySize
public int getHistorySize()Returns the number of lines currently held in the scrollback history.Equivalent to
getHistory().size()but without copying the history.- Returns:
- the number of scrollback history lines
-
cellCodePoint
public static int cellCodePoint(long cell) Extracts the Unicode code point from a cell value. A return value of 0 indicates a continuation cell for a wide character.- Parameters:
cell- the 64-bit cell value fromdump(long[], int[])orgetHistory()- Returns:
- the Unicode code point stored in the cell
-
cellAttr
public static long cellAttr(long cell) Extracts the raw 32-bit attribute word from a cell value. Prefer the typed accessors (cellBold(long),cellForeground(long), etc.) over inspecting this value directly.- Parameters:
cell- the 64-bit cell value- Returns:
- the upper 32 bits containing style and color information
-
cellBold
public static boolean cellBold(long cell) Returnstrueif the cell has the bold attribute set.- Parameters:
cell- the 64-bit cell value- Returns:
- whether bold is enabled
-
cellDim
public static boolean cellDim(long cell) Returnstrueif the cell has the dim/faint attribute set.- Parameters:
cell- the 64-bit cell value- Returns:
- whether dim is enabled
-
cellItalic
public static boolean cellItalic(long cell) Returnstrueif the cell has the italic attribute set.- Parameters:
cell- the 64-bit cell value- Returns:
- whether italic is enabled
-
cellUnderline
public static boolean cellUnderline(long cell) Returnstrueif the cell has the underline attribute set.- Parameters:
cell- the 64-bit cell value- Returns:
- whether underline is enabled
-
cellInverse
public static boolean cellInverse(long cell) Returnstrueif the cell has the inverse (negative) attribute set.- Parameters:
cell- the 64-bit cell value- Returns:
- whether inverse is enabled
-
cellConceal
public static boolean cellConceal(long cell) Returnstrueif the cell has the conceal (hidden) attribute set.- Parameters:
cell- the 64-bit cell value- Returns:
- whether conceal is enabled
-
cellHasForeground
public static boolean cellHasForeground(long cell) Returnstrueif the cell has an explicit foreground color set. Whenfalse, the terminal's default foreground should be used.- Parameters:
cell- the 64-bit cell value- Returns:
- whether a foreground color is set
-
cellHasBackground
public static boolean cellHasBackground(long cell) Returnstrueif the cell has an explicit background color set. Whenfalse, the terminal's default background should be used.- Parameters:
cell- the 64-bit cell value- Returns:
- whether a background color is set
-
cellForeground
public static int cellForeground(long cell) Extracts the foreground color as a 12-bit value (4 bits per r/g/b channel). Only meaningful whencellHasForeground(long)returnstrue. UsecellColorRgb(int)to expand to a 24-bit RGB value.- Parameters:
cell- the 64-bit cell value- Returns:
- the 12-bit foreground color (0x000?0xFFF)
-
cellBackground
public static int cellBackground(long cell) Extracts the background color as a 12-bit value (4 bits per r/g/b channel). Only meaningful whencellHasBackground(long)returnstrue. UsecellColorRgb(int)to expand to a 24-bit RGB value.- Parameters:
cell- the 64-bit cell value- Returns:
- the 12-bit background color (0x000?0xFFF)
-
cellColorRgb
public static int cellColorRgb(int color12) Expands a 12-bit color (as returned bycellForeground(long)orcellBackground(long)) to a 24-bit RGB value. Each 4-bit channel is expanded to 8 bits by replicating the nibble (e.g.0xA ? 0xAA).- Parameters:
color12- the 12-bit color value (0x000?0xFFF)- Returns:
- the 24-bit RGB color (0x000000?0xFFFFFF)
-
dump
public boolean dump(long timeout, boolean forceDump, long[] fullscreen, int ftop, int fleft, int fheight, int fwidth, int[] cursor) throws InterruptedException Waits for the screen to be dirty, then dumps the raw screen content into a subregion.- Parameters:
timeout- maximum time to wait in millisecondsforceDump- if true, dump even if the screen is not dirtyfullscreen- destination array (must be at least fwidth * fheight)ftop- top row offsetfleft- left column offsetfheight- number of rows to dumpfwidth- number of columns to dumpcursor- 2-element array to receive cursor [x, y], or null- Returns:
- true if the screen was dumped
- Throws:
InterruptedException- if interrupted
-
dump
public boolean dump(long timeout, boolean forceDump, long[] fullscreen, int[] cursor) throws InterruptedException Waits for the screen to be dirty, then dumps the raw screen content.- Parameters:
timeout- maximum time to wait in millisecondsforceDump- if true, dump even if the screen is not dirtyfullscreen- destination array (must be at least columns * rows)cursor- 2-element array to receive cursor [x, y], or null- Returns:
- true if the screen was dumped
- Throws:
InterruptedException- if interrupted
-
dump
Dumps the terminal content as HTML with inline RGB color styles.- Parameters:
timeout- maximum time to wait for changes in millisecondsforceDump- whether to force a dump even if screen is not dirty- Returns:
- the terminal content as HTML, or null if no update
- Throws:
InterruptedException- if interrupted
-
dump
Dumps the terminal content as HTML with inline RGB color styles.- Returns:
- the terminal content as HTML.
-
toString
-
getRows()instead.