Skip to main content

Class Screen

public class Screen

Low-level terminal screen manager for the jtvision TUI framework.

Screen corresponds to the JT Vision Screen unit: it owns the physical terminal surface, maintains the logical screenBuffer of JtvScreenCell objects, and provides a dirty-cell optimised flush that converts the buffer to ANSI escape sequences.

Lifecycle

Call init() once at startup and shutdown() at exit. init() installs Jansi, detects the terminal size, allocates both the front buffer (screenBuffer) and the shadow buffer (lastBuffer), then switches the terminal to the alternate screen buffer and enables SGR mouse mode. shutdown() reverses all of that.

Double-buffer flush

flushScreen() compares every cell in screenBuffer against the corresponding entry in the private lastBuffer. Only cells whose character or attribute differ are written to the terminal, using compact ESC[row;colH cursor-position sequences and ESC[fg;bgm SGR colour sequences. Cursor position and visibility are deferred and applied at the end of each flush so they are sent exactly once per frame.

Screen mode

screenMode holds a JT Vision screen-mode constant. The default value 0x0003 is smCO80 — 80-column colour mode. Other TV constants include smMono (0x0007), smBW80 (0x0002), and smFont8x8 (0x0100).

Colour mapping

JT Vision uses the CGA/BIOS 4-bit attribute byte: bits 0–3 are the foreground colour index (0–15) and bits 4–6 are the background colour index (0–7). ANSI_FG and ANSI_BG translate these indices to the corresponding ANSI SGR colour codes.

Cursor model

Cursor state is double-buffered. The desired* fields record what the application has requested via setCursorPosition(int, int), setCursorVisible(boolean), and setCursorShape(boolean). The applied* fields track what was last actually sent to the terminal. flushScreen() reconciles them at the end of each frame, emitting only the escape sequences needed to reach the desired state.

Suspend / resume

suspend() temporarily leaves the alternate screen (e.g. before shelling out) and resume() re-enters it. After resuming, invalidate() is called automatically to force a full redraw.

org.viktor44.jtvision.platform.Screen

Summary​

Constructors​

VisibilitySignature
publicScreen()

Fields​

ModifiersField nameType
public staticscreenWidthint
public staticscreenHeightint
public staticscreenModeint
public staticscreenBufferorg.viktor44.jtvision.core.JtvScreenCell[]
public staticcursorLinesint
private staticdesiredCursorXint
private staticdesiredCursorYint
private staticdesiredCursorVisibleboolean
private staticdesiredCursorBlockboolean
private staticappliedCursorXint
private staticappliedCursorYint
private staticappliedCursorVisibleboolean
private staticappliedCursorBlockboolean
private staticlastBufferorg.viktor44.jtvision.core.JtvScreenCell[]
private staticinitializedboolean
private staticoutjava.io.PrintStream
private static finalLAST_BUFFER_INIT_CELLorg.viktor44.jtvision.core.JtvScreenCell
private static finalANSI_FGint[]
private static finalANSI_BGint[]

Methods​

ModifiersMethod signatureReturn type
public staticinit()void
public staticshutdown()void
public staticdetectSize()void
public staticsetVideoMode(int mode)void
public staticflushScreen()void
public staticclearScreen()void
public staticsetCursorPosition(int x, int y)void
public staticsetCursorVisible(boolean visible)void
public staticsetCursorShape(boolean block)void
public staticinvalidate()void
public staticsuspend()void
public staticresume()void
public staticwriteToScreen(int x1, int x2, int y, org.viktor44.jtvision.core.JtvScreenCell[] buf, int bufOffset, boolean inShadow)void

Constructors​

Screen()​

No method description provided

Fields​

int screenWidth​

The current terminal width in columns.

Populated by detectSize() and kept in sync with screenBuffer. The JT Vision default is 80.

int screenHeight​

The current terminal height in rows.

Populated by detectSize() and kept in sync with screenBuffer. The JT Vision default is 25.

int screenMode​

The active screen mode, encoded as a JT Vision screen-mode constant.

The default value 0x0003 corresponds to smCO80 (80-column colour mode). Other JT Vision constants:

  • 0x0002 — smBW80: 80-column black-and-white.
  • 0x0007 — smMono: monochrome.
  • 0x0100 — smFont8x8: 8×8 pixel font (EGA/VGA 43/50-line mode).

Changed by setVideoMode(int).

org.viktor44.jtvision.core.JtvScreenCell[] screenBuffer​

The logical front-buffer: one JtvScreenCell per screen position, stored in row-major order (y * screenWidth + x).

Views write into this array via writeToScreen(int, int, int, JtvScreenCell[], int, boolean). flushScreen() reads it and emits only the cells that differ from the shadow buffer lastBuffer.

int cursorLines​

The cursor shape register, mirroring the JT Vision CursorLines variable. A value of 0 selects the default cursor shape; other values are reserved for hardware-cursor line masks in the original PC BIOS interface (unused in the ANSI terminal back-end).

int desiredCursorX​

The column position that the cursor should be at after the next flushScreen() call (0-based). Set by setCursorPosition(int, int).

int desiredCursorY​

The row position that the cursor should be at after the next flushScreen() call (0-based). Set by setCursorPosition(int, int).

boolean desiredCursorVisible​

Whether the cursor should be visible after the next flushScreen() call. Set by setCursorVisible(boolean).

boolean desiredCursorBlock​

Whether the cursor should use the steady-block shape (true) or the steady-underline shape (false) after the next flushScreen() call. Set by setCursorShape(boolean).

int appliedCursorX​

The column position that was last actually sent to the terminal. Initialised to -1 so the first flush always emits an explicit cursor-position sequence.

int appliedCursorY​

The row position that was last actually sent to the terminal. Initialised to -1 so the first flush always emits an explicit cursor-position sequence.

boolean appliedCursorVisible​

Whether the cursor was last reported as visible to the terminal.

boolean appliedCursorBlock​

Whether the cursor shape last sent to the terminal was the block shape.

org.viktor44.jtvision.core.JtvScreenCell[] lastBuffer​

The shadow (back) buffer used by flushScreen() for dirty-cell detection. Each entry mirrors the on-screen state of the corresponding screenBuffer cell. Cells are initialised with character '\0' and attribute -1 so that every cell is considered dirty on the first flush. Reset to the same sentinel by invalidate().

boolean initialized​

Whether init() has been called without a subsequent shutdown(). Guards against double-initialisation and ensures flushScreen() is a no-op before the screen is set up.

java.io.PrintStream out​

The Jansi-installed output stream used for all terminal output. Obtained from AnsiConsole.out() during init() so that Jansi's Windows-compatibility layer is active.

org.viktor44.jtvision.core.JtvScreenCell LAST_BUFFER_INIT_CELL​

No description provided

int[] ANSI_FG​

ANSI SGR foreground colour codes for the 16 CGA/BIOS colour indices.

The foreground colour index occupies bits 0–3 of a JT Vision attribute byte. Indices 0–7 map to standard ANSI colours (SGR 30–37) and indices 8–15 map to bright/high-intensity colours (SGR 90–97). The ordering follows the CGA palette: <ol start="0">

  • Black (30)
  • Blue (34)
  • Green (32)
  • Cyan (36)
  • Red (31)
  • Magenta (35)
  • Brown/Yellow (33)
  • Light Grey (37)
  • Dark Grey (90)
  • Light Blue (94)
  • Light Green (92)
  • Light Cyan (96)
  • Light Red (91)
  • Light Magenta (95)
  • Yellow (93)
  • White (97)

int[] ANSI_BG​

ANSI SGR background colour codes for the 16 CGA/BIOS colour indices.

The background colour index occupies bits 4–6 of a JT Vision attribute byte (3 bits, so only indices 0–7 are valid for backgrounds). Indices 0–7 map to standard ANSI background codes (SGR 40–47) and indices 8–15 map to high-intensity background codes (SGR 100–107), following the same CGA ordering as ANSI_FG.

Methods​

init()​

Initialises the screen subsystem.

If already initialised this method returns immediately. Otherwise it:

  1. Installs Jansi via AnsiConsole.systemInstall() and captures the Jansi-wrapped output stream.
  2. Calls detectSize() to populate screenWidth and screenHeight.
  3. Allocates screenBuffer and the private shadow buffer lastBuffer. Each shadow cell is seeded with '\0' and attribute -1 so every position is dirty on the first flush.
  4. Sends the following terminal sequences:
  • ESC[?1049h — switch to the alternate screen buffer.
  • ESC[?25l — hide the hardware cursor.
  • ESC[?1002h — enable button-event mouse tracking.
  • ESC[?1006h — enable SGR mouse-reporting mode.

shutdown()​

Tears down the screen subsystem and restores the terminal to its original state.

If not currently initialised this method is a no-op. Otherwise it sends:

  • ESC[?1006l — disable SGR mouse mode.
  • ESC[?1002l — disable button-event mouse tracking.
  • ESC[?25h — restore cursor visibility.
  • ESC[?1049l — return to the normal screen buffer.

Then calls AnsiConsole.systemUninstall() to remove Jansi and sets initialized to false.

detectSize()​

Queries the terminal for its current dimensions and updates screenWidth and screenHeight.

Three strategies are tried in order:

  1. Windows Kernel32 — on Windows, calls Kernel32.GetConsoleScreenBufferInfo on the standard output handle to obtain the window width and height.
  2. POSIX ioctl TIOCGWINSZ — on Unix, calls CLibrary.ioctl with TIOCGWINSZ on stdout, stderr, and stdin in turn until one succeeds.
  3. Environment variables — reads COLUMNS and LINES as a last resort.

If all strategies fail, screenWidth and screenHeight retain their current values (defaulting to 80×25).

setVideoMode(int mode)​

Changes the screen mode and resizes the screen buffers to match.

Calls detectSize() to pick up any terminal resize, then reallocates both screenBuffer and the private shadow buffer to the new screenWidth * screenHeight size. The shadow buffer is seeded with dirty sentinels so the next flushScreen() performs a full repaint.

The mode parameter is stored in screenMode by the caller; this method does not record it itself.

Parameters

  • mode - the JT Vision screen-mode constant (smCO80, smMono, smBW80, or smFont8x8)

flushScreen()​

Flushes the logical screen buffer to the terminal, emitting ANSI escape sequences only for cells that have changed since the last flush.

The algorithm:

  1. Iterates over every cell in screenBuffer row by row.
  2. Compares each cell against lastBuffer. If the character or attribute differs, a cursor-position sequence (ESC[row;colH) is emitted before the first dirty cell in each run; subsequent adjacent dirty cells need no position sequence. An SGR colour sequence (ESC[fg;bgm) is emitted whenever the attribute changes.
  3. After writing a dirty cell the shadow buffer is updated so the same cell is not re-sent on the next flush.
  4. If any cell was written, the cursor position and visibility are considered undefined; the applied* tracking fields are reset.
  5. After the cell pass, cursor state is reconciled: position, shape (ESC[1 q block / ESC[3 q underline), and visibility (ESC[?25h / ESC[?25l) are emitted as needed to reach the desired state.
  6. The completed escape-sequence string is written to out in a single PrintStream.print(String) call.

This method is a no-op if initialized is false.

clearScreen()​

Fills every cell in screenBuffer with a space character (' ') and the default attribute 0x07 (light-grey foreground on black background), matching the JT Vision ClearScreen behaviour.

This does not immediately update the terminal; call flushScreen() afterwards.

setCursorPosition(int x, int y)​

Queues a cursor position to be applied on the next flushScreen() call.

Coordinates are 0-based. The cursor is only moved if visibility is also enabled via setCursorVisible(boolean).

Parameters

  • x - the column index (0-based, left edge = 0)
  • y - the row index (0-based, top edge = 0)

setCursorVisible(boolean visible)​

Queues cursor visibility to be applied on the next flushScreen() call.

When true, the cursor is shown at the position set by setCursorPosition(int, int) using the shape set by setCursorShape(boolean). When false, the cursor is hidden (ESC[?25l).

Parameters

  • visible - true to show the cursor, false to hide it

setCursorShape(boolean block)​

Queues a cursor shape to be applied on the next flushScreen() call.

Two shapes are supported, both steady (non-blinking) to match the JT Vision insert/overwrite convention:

  • true — steady block cursor (ESC[1 q), used in overwrite mode.
  • false — steady underline cursor (ESC[3 q), used in insert mode.

Parameters

  • block - true for a block cursor, false for an underline cursor

invalidate()​

Marks all cells in the shadow buffer as dirty, forcing a full repaint on the next flushScreen() call.

Every cell in the private lastBuffer is reset to character '\0' and attribute -1. Because these values cannot appear in normal screen content, every position will be considered changed by the next flush.

Called automatically by resume() after returning from a suspend() and should also be called after any external process has written to the terminal.

suspend()​

Temporarily suspends screen output and restores the normal terminal state, for example before spawning a child shell.

Sends the same teardown sequences as shutdown() (disable mouse, show cursor, leave alternate screen) but does not uninstall Jansi or clear initialized, so resume() can restore the TUI without a full reinitialisation.

resume()​

Resumes screen output after a suspend() call.

Re-enters the alternate screen buffer and re-enables mouse reporting, then calls invalidate() to force a full repaint so that the TUI is completely redrawn after whatever the child process may have written to the terminal.

writeToScreen(int x1, int x2, int y, org.viktor44.jtvision.core.JtvScreenCell[] buf, int bufOffset, boolean inShadow)​

Copies a horizontal run of JtvScreenCell objects from a source buffer into screenBuffer, optionally applying a shadow attribute transform.

The destination range is the columns [x1, x2) on row y. The source cells are read starting at buf[bufOffset].

When inShadow is true each cell's attribute is passed through JtvView.applyShadow(int) before being stored, darkening the colour to produce the drop-shadow effect used by overlapping windows and menus. When false the cell is copied verbatim via JtvScreenCell.assign(JtvScreenCell).

Out-of-range indices (negative idx or bi, or values beyond the buffer lengths) are silently skipped.

Parameters

  • x1 - the first destination column (inclusive, 0-based)
  • x2 - the last destination column (exclusive)
  • y - the destination row (0-based)
  • buf - the source cell array
  • bufOffset - the index in buf corresponding to column x1
  • inShadow - true to apply the shadow colour transform, false to copy cells verbatim