Raxol.Core.Renderer (Raxol v2.3.0)

View Source

Rendering utilities for Raxol.Core.Buffer.

Provides functions to render buffers to strings and calculate minimal differences between buffers for efficient terminal updates.

Example

buffer = Raxol.Core.Buffer.create_blank_buffer(80, 24)
buffer = Raxol.Core.Buffer.write_at(buffer, 0, 0, "Hello, World!")
output = Raxol.Core.Renderer.render_to_string(buffer)

Summary

Functions

Apply diff operations to produce ANSI output string.

Calculate minimal updates between two buffers.

Render a buffer to a string with ANSI escape codes for styling.

Render a buffer to a plain string without ANSI codes.

Functions

apply_diff(operations)

@spec apply_diff(list()) :: String.t()

Apply diff operations to produce ANSI output string.

Takes the diff operations from render_diff/2 and produces a string with ANSI cursor movement and styling codes.

Example

diff = [{:move, 0, 0}, {:write, "Hello", %{fg_color: :blue}}]
Raxol.Core.Renderer.apply_diff(diff)
# => "Hello"

render_diff(old_buffer, new_buffer)

@spec render_diff(Raxol.Core.Buffer.t(), Raxol.Core.Buffer.t()) :: list()

Calculate minimal updates between two buffers.

Returns a list of update operations that can be applied to transform the old buffer into the new buffer with minimal terminal output.

Return Format

Returns a list of update tuples:

  • {:move, x, y} - Move cursor to position
  • {:write, text, style} - Write text with style
  • {:clear_line, y} - Clear line at y

Example

old_buffer = Raxol.Core.Buffer.create_blank_buffer(20, 5)
new_buffer = Raxol.Core.Buffer.write_at(old_buffer, 0, 0, "Hello")
diff = Raxol.Core.Renderer.render_diff(old_buffer, new_buffer)
# => [{:move, 0, 0}, {:write, "Hello", %{}}]

render_to_ansi(buffer)

@spec render_to_ansi(Raxol.Core.Buffer.t()) :: String.t()

Render a buffer to a string with ANSI escape codes for styling.

Example

buffer = Raxol.Core.Buffer.create_blank_buffer(20, 5)
Raxol.Core.Renderer.render_to_ansi(buffer)
# => String with ANSI codes for colors and styles

render_to_string(buffer)

@spec render_to_string(Raxol.Core.Buffer.t()) :: String.t()

Render a buffer to a plain string without ANSI codes.

Example

buffer = Raxol.Core.Buffer.create_blank_buffer(20, 5)
Raxol.Core.Renderer.render_to_string(buffer)
# => "                    \n..."