Class: TUITD::HtmlRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/tui_td/html_renderer.rb

Overview

Renders terminal state as a self-contained HTML document. Faithfully reproduces what a TUI application shows — colors, styles, cursor position — so an LLM or human can “see” the terminal.

Constant Summary collapse

ANSI_RGB =
{
  "black"         => [0x00, 0x00, 0x00],
  "red"           => [0xAA, 0x00, 0x00],
  "green"         => [0x00, 0xAA, 0x00],
  "yellow"        => [0xAA, 0x55, 0x00],
  "blue"          => [0x00, 0x00, 0xAA],
  "magenta"       => [0xAA, 0x00, 0xAA],
  "cyan"          => [0x00, 0xAA, 0xAA],
  "white"         => [0xAA, 0xAA, 0xAA],
  "bright_black"  => [0x55, 0x55, 0x55],
  "bright_red"    => [0xFF, 0x55, 0x55],
  "bright_green"  => [0x55, 0xFF, 0x55],
  "bright_yellow" => [0xFF, 0xFF, 0x55],
  "bright_blue"   => [0x55, 0x55, 0xFF],
  "bright_magenta"=> [0xFF, 0x55, 0xFF],
  "bright_cyan"   => [0x55, 0xFF, 0xFF],
  "bright_white"  => [0xFF, 0xFF, 0xFF],
}.freeze
CUBE =
[0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF].freeze
ANSI_INDEX =
%w[
  black red green yellow blue magenta cyan white
  bright_black bright_red bright_green bright_yellow
  bright_blue bright_magenta bright_cyan bright_white
].freeze
DEFAULT_FG =
[0xC0, 0xC0, 0xC0].freeze
DEFAULT_BG =
[0x00, 0x00, 0x00].freeze

Instance Method Summary collapse

Constructor Details

#initialize(state) ⇒ HtmlRenderer

Returns a new instance of HtmlRenderer.



38
39
40
41
42
43
44
# File 'lib/tui_td/html_renderer.rb', line 38

def initialize(state)
  @state = state
  @rows = _dig(state, :size, :rows) || 40
  @cols = _dig(state, :size, :cols) || 120
  @grid = state[:rows] || state["rows"] || []
  @cursor = state[:cursor] || state["cursor"] || { row: 0, col: 0 }
end

Instance Method Details

#render(output_path) ⇒ Object

Write HTML to a file



65
66
67
68
# File 'lib/tui_td/html_renderer.rb', line 65

def render(output_path)
  File.write(output_path, to_html)
  output_path
end

#to_htmlObject

Return HTML string



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/tui_td/html_renderer.rb', line 47

def to_html
  css = render_css
  body = render_body
  <<~HTML
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>TUI Terminal</title>
    <style>#{css}</style>
    </head>
    <body>#{body}</body>
    </html>
  HTML
end