# retroglyph-terminal - Complete API Documentation > Shared ANSI/SGR cell-diff renderer for retroglyph's terminal-family backends **Version:** 0.0.0 **Authors:** Matan Lurey **License:** MIT **Repository:** https://github.com/crates-lurey-io/retroglyph **Keywords:** roguelike, terminal, grid, gamedev Generated: 2026-08-05 20:21:10 UTC Created by: [cargo-llms-txt](https://github.com/masinc/cargo-llms-txt) ## Table of Contents ### src/lib.rs - pub enum ColorSupport - impl ColorSupport - pub struct TerminalRenderer - impl TerminalRenderer - impl TerminalRenderer --- ## README.md ### retroglyph-terminal Shared ANSI/SGR cell-diff renderer for retroglyph's terminal-family backends Part of the [retroglyph](https://github.com/crates-lurey-io/retroglyph) workspace. --- ## src/lib.rs ### ColorSupport ```rust #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)] pub enum ColorSupport { Truecolor, Indexed256, Ansi16, None, } ``` How aggressively [`TerminalRenderer`] quantizes [`Color`] before emitting an SGR sequence. See the crate-level "RGB color fallback on 256-color terminals" doc section for the full contract, and [`TerminalRenderer::with_color_support`]/[`TerminalRenderer::set_color_support`] to configure it. ### impl ColorSupport ```rust impl ColorSupport { } ``` ### TerminalRenderer ```rust #[derive(Debug)] pub struct TerminalRenderer { } ``` A generic ANSI/SGR cell-diff renderer. Converts [`Tile`] content into standard ANSI/CSI escape sequences and writes them to a caller-supplied [`std::io::Write`] sink `W`. Tracks cursor position and the last-emitted foreground/background/attribute state across calls to [`draw`](Self::draw) so it only emits the escape codes needed to move to changed cells and change state. This type has no knowledge of *how* its output bytes reach a display (stdout, a `String` buffer for JS, a test harness) or *how* input arrives: it is a pure `Tile` stream -> ANSI bytes transform, reused by every terminal-family [`Backend`](retroglyph_core::backend::Backend) implementor. #### Examples Driving the renderer over a `Vec` sink and asserting on the emitted ANSI bytes: no real terminal is needed, since `W` here is just an in-memory buffer. ``` use retroglyph_core::backend::DrawCell; use retroglyph_core::color::{AnsiColor, Color}; use retroglyph_core::grid::Pos; use retroglyph_core::color::Style; use retroglyph_core::tile::Tile; use retroglyph_terminal::TerminalRenderer; let mut renderer = TerminalRenderer::new(Vec::new()); let tile = Tile::new('X', Style::new().fg(Color::Ansi(AnsiColor::Red))); renderer.draw(core::iter::once(DrawCell::new(Pos { x: 0, y: 0 }, &tile)))?; renderer.flush()?; let out = String::from_utf8(renderer.into_writer()).expect("renderer only writes ASCII/UTF-8"); // `\x1b[1;1H` moves the cursor to row 1, col 1 (1-indexed); `\x1b[31;49m` sets red // foreground with the default background. assert_eq!(out, "\x1b[1;1H\x1b[31;49mX"); #### Ok::<(), std::io::Error>(()) ``` ### impl TerminalRenderer ```rust impl TerminalRenderer { pub fn new(writer: W) -> Self; pub fn with_plain_mode(writer: W, plain: bool) -> Self; pub fn plain_mode(&self) -> bool; pub fn color_support(&self) -> ColorSupport; pub fn set_color_support(&mut self, color_support: ColorSupport); pub fn with_color_support(self, color_support: ColorSupport) -> Self; pub fn set_plain_mode(&mut self, plain: bool); pub fn writer(&self) -> &W; pub fn writer_mut(&mut self) -> &mut W; pub fn into_writer(self) -> W; pub fn reset_state(&mut self); pub fn begin_synchronized_update(&mut self) -> io::Result<()>; pub fn end_synchronized_update(&mut self) -> io::Result<()>; pub fn draw<'a, I>(&mut self, content: I) -> io::Result<()> where I: Iterator; pub fn flush(&mut self) -> io::Result<()>; pub fn draw_frame<'a, I>(&mut self, content: I) -> io::Result<()> where I: Iterator; pub fn end_frame(&mut self) -> io::Result<()>; pub fn clear_screen(&mut self) -> io::Result<()>; pub fn move_cursor_to(&mut self, position: Pos) -> io::Result<()>; pub fn set_cursor_visible(&mut self, visible: bool) -> io::Result<()>; pub fn set_cursor_style(&mut self, style: CursorStyle) -> io::Result<()>; } ``` ### impl TerminalRenderer ```rust impl TerminalRenderer { pub fn auto(writer: W) -> Self; } ```