Renderer
The renderer handles outputting the screen buffer to the terminal with efficient diff-based rendering and ANSI escape sequence management.
Overview
The renderer provides:
- Diff-based rendering to minimize terminal updates
- ANSI escape sequence generation for styling
- Cursor position optimization
- Cross-platform terminal output
Types
RenderStats
Statistics from a render operation.
Fields
cells_drawn: usize- Number of cells that were updatedcells_skipped: usize- Number of cells that remained unchangedefficiency: f32- Rendering efficiency (0.0 to 1.0)
Renderer
Main diff-based renderer for optimal performance.
Fields
allocator: std.mem.Allocator- Memory allocatorprev_buffer: ?Screen- Previous frame for diffingoutput_buffer: std.ArrayListUnmanaged(u8)- Buffered outputcurrent_style: Style- Current terminal style statelast_x: u16- Last cursor X positionlast_y: u16- Last cursor Y positionstdout: std.fs.File- Standard output handlecells_drawn: usize- Statistics: cells drawn this framecells_skipped: usize- Statistics: cells skipped this frame
Methods
init
pub fn init(allocator: std.mem.Allocator) RendererCreates a new renderer instance.
Parameters:
allocator: std.mem.Allocator- Memory allocator
Returns: New Renderer instance
deinit
pub fn deinit(self: *Renderer) voidCleans up renderer resources.
render
pub fn render(self: *Renderer, current: *const Screen) !voidRenders a screen buffer to the terminal.
Parameters:
current: *const Screen- Screen buffer to render
Performs diff-based rendering on subsequent calls, full rendering on first call or size changes.
invalidate
pub fn invalidate(self: *Renderer) voidForces a full redraw on the next render call.
Clears the previous buffer to ensure all cells are re-rendered.
getStats
pub fn getStats(self: *Renderer) RenderStatsGets rendering statistics from the last frame.
Returns: RenderStats with performance metrics
ImmediateRenderer
Simple immediate-mode renderer without diffing.
Fields
stdout: std.fs.File- Standard output handlecurrent_style: Style- Current style state
Methods
init
pub fn init() ImmediateRendererCreates a new immediate renderer.
Returns: New ImmediateRenderer instance
render
pub fn render(self: *ImmediateRenderer, screen_buf: *const Screen) !voidRenders the entire screen buffer immediately.
Parameters:
screen_buf: *const Screen- Screen buffer to render
Always performs a full redraw of the entire screen.
Usage Examples
Basic Rendering
const allocator = std.heap.page_allocator;
var renderer = Renderer.init(allocator);
defer renderer.deinit();
var screen = try Screen.init(allocator, 80, 24);
defer screen.deinit();
// ... populate screen ...
try renderer.render(&screen);
// Get performance stats
const stats = renderer.getStats();
std.debug.print("Efficiency: {d:.2}%\n", .{stats.efficiency * 100});Immediate Rendering
var renderer = ImmediateRenderer.init();
// For simple cases or debugging
try renderer.render(&screen);Forcing Redraw
// When screen content changes significantly
renderer.invalidate();
try renderer.render(&screen); // Will do full redrawPerformance Considerations
- Diff Rendering: The main
Rendereronly updates changed cells - Buffering: Output is buffered to minimize syscalls
- Cursor Optimization: Cursor movement is optimized for common patterns
- Style Diffing: Only changed style attributes are sent to terminal
See Also
- Screen API
- Style API
- Source:
src/core/renderer.zig