Screen
The Screen struct provides an off-screen buffer for building terminal displays, with drawing primitives and cell management.
Overview
The screen buffer:
- Maintains a 2D grid of cells
- Provides drawing operations (text, shapes, regions)
- Supports sub-regions for clipping
- Handles wide character positioning
- Enables efficient rendering through diffing
Screen
Main screen buffer structure.
Fields
allocator: std.mem.Allocator- Memory allocatorcells: []Cell- Cell grid in row-major orderwidth: u16- Screen width in cellsheight: u16- Screen height in cellscursor_x: u16- Current cursor X positioncursor_y: u16- Current cursor Y positioncurrent_style: Style- Current drawing style
Methods
init
pub fn init(allocator: std.mem.Allocator, width: u16, height: u16) !ScreenCreates a new screen buffer.
Parameters:
allocator: std.mem.Allocator- Memory allocatorwidth: u16- Screen width in cellsheight: u16- Screen height in cells
Returns: New Screen instance filled with blank cells
deinit
pub fn deinit(self: *Screen) voidFrees the screen buffer memory.
resize
pub fn resize(self: *Screen, new_width: u16, new_height: u16) !voidResizes the screen buffer, preserving existing content where possible.
Parameters:
new_width: u16- New width in cellsnew_height: u16- New height in cells
clear
pub fn clear(self: *Screen) voidClears the entire screen to blank cells and resets cursor to (0,0).
clearWithStyle
pub fn clearWithStyle(self: *Screen, s: Style) voidClears the screen with cells having the specified style.
Parameters:
s: Style- Style for blank cells
getCell
pub fn getCell(self: *const Screen, x: u16, y: u16) ?*const CellGets a read-only pointer to a cell.
Parameters:
x: u16- X coordinatey: u16- Y coordinate
Returns: Pointer to cell or null if out of bounds
getCellMut
pub fn getCellMut(self: *Screen, x: u16, y: u16) ?*CellGets a mutable pointer to a cell.
Parameters:
x: u16- X coordinatey: u16- Y coordinate
Returns: Mutable pointer to cell or null if out of bounds
setCell
pub fn setCell(self: *Screen, x: u16, y: u16, c: Cell) voidSets a cell at the specified position.
Parameters:
x: u16- X coordinatey: u16- Y coordinatec: Cell- Cell to set
Handles wide characters by setting continuation cells.
setStyle
pub fn setStyle(self: *Screen, s: Style) voidSets the current drawing style.
Parameters:
s: Style- New style for drawing operations
moveCursor
pub fn moveCursor(self: *Screen, x: u16, y: u16) voidMoves the cursor to a new position.
Parameters:
x: u16- X coordinatey: u16- Y coordinate
Cursor is clamped to screen bounds.
putChar
pub fn putChar(self: *Screen, char: u21) voidWrites a character at the current cursor position and advances the cursor.
Parameters:
char: u21- Unicode codepoint
putString
pub fn putString(self: *Screen, s: []const u8) voidWrites a UTF-8 string at the current cursor position.
Parameters:
s: []const u8- UTF-8 string
putStringAt
pub fn putStringAt(self: *Screen, x: u16, y: u16, s: []const u8) voidWrites a string at a specific position.
Parameters:
x: u16- X coordinatey: u16- Y coordinates: []const u8- UTF-8 string
hline
pub fn hline(self: *Screen, x: u16, y: u16, len: u16, char: u21) voidDraws a horizontal line.
Parameters:
x: u16- Starting X coordinatey: u16- Y coordinatelen: u16- Line lengthchar: u21- Character to draw
vline
pub fn vline(self: *Screen, x: u16, y: u16, len: u16, char: u21) voidDraws a vertical line.
Parameters:
x: u16- X coordinatey: u16- Starting Y coordinatelen: u16- Line lengthchar: u21- Character to draw
fill
pub fn fill(self: *Screen, x: u16, y: u16, w: u16, h: u16, char: u21) voidFills a rectangular region with a character.
Parameters:
x: u16- Starting X coordinatey: u16- Starting Y coordinatew: u16- Widthh: u16- Heightchar: u21- Fill character
drawBox
pub fn drawBox(self: *Screen, x: u16, y: u16, w: u16, h: u16, border: style.BorderStyle) voidDraws a box border using the specified border style.
Parameters:
x: u16- Starting X coordinatey: u16- Starting Y coordinatew: u16- Box widthh: u16- Box heightborder: style.BorderStyle- Border style
blit
pub fn blit(self: *Screen, src: *const Screen, src_x: u16, src_y: u16, dst_x: u16, dst_y: u16, w: u16, h: u16) voidCopies a region from another screen.
Parameters:
src: *const Screen- Source screensrc_x: u16- Source X coordinatesrc_y: u16- Source Y coordinatedst_x: u16- Destination X coordinatedst_y: u16- Destination Y coordinatew: u16- Width to copyh: u16- Height to copy
getRow
pub fn getRow(self: *const Screen, y: u16) ?[]const CellGets a read-only slice of a row.
Parameters:
y: u16- Row index
Returns: Slice of cells or null if out of bounds
subRegion
pub fn subRegion(self: *Screen, x: u16, y: u16, w: u16, h: u16) SubScreenCreates a sub-region view for clipped drawing.
Parameters:
x: u16- Offset Xy: u16- Offset Yw: u16- Sub-region widthh: u16- Sub-region height
Returns: SubScreen view
SubScreen
A view into a sub-region of a screen for clipped operations.
Fields
parent: *Screen- Parent screenoffset_x: u16- X offset in parentoffset_y: u16- Y offset in parentwidth: u16- Sub-region widthheight: u16- Sub-region heightcursor_x: u16- Local cursor Xcursor_y: u16- Local cursor Ycurrent_style: Style- Current style
Methods
setCell
pub fn setCell(self: *SubScreen, x: u16, y: u16, c: Cell) voidSets a cell in the sub-region.
putChar
pub fn putChar(self: *SubScreen, char: u21) voidWrites a character at the current cursor position.
putString
pub fn putString(self: *SubScreen, s: []const u8) voidWrites a string at the current cursor position.
setStyle
pub fn setStyle(self: *SubScreen, s: Style) voidSets the current style.
moveCursor
pub fn moveCursor(self: *SubScreen, x: u16, y: u16) voidMoves the cursor within the sub-region.
clear
pub fn clear(self: *SubScreen) voidClears the sub-region.
fill
pub fn fill(self: *SubScreen, char: u21) voidFills the sub-region with a character.
subRegion
pub fn subRegion(self: *SubScreen, x: u16, y: u16, w: u16, h: u16) SubScreenCreates a nested sub-region.
Usage Examples
Basic Drawing
var screen = try Screen.init(allocator, 80, 24);
defer screen.deinit();
// Set style and draw text
screen.setStyle(Style.default.bold().foreground(Color.red));
screen.putStringAt(10, 5, "Hello World");
// Draw shapes
screen.hline(0, 0, 80, '─');
screen.vline(0, 0, 24, '│');
screen.drawBox(5, 5, 20, 10, BorderStyle.single);Sub-region Clipping
// Create a clipped area
var sub = screen.subRegion(10, 10, 30, 10);
// Drawing in sub-region is automatically clipped
sub.setStyle(Style.default.foreground(Color.blue));
sub.putString("This text is clipped to the sub-region");Copying Regions
// Copy a 10x5 region from (5,5) to (20,15)
screen.blit(&other_screen, 5, 5, 20, 15, 10, 5);See Also
- Cell API
- Renderer API
- Source:
src/core/screen.zig