Class RatatuiTerminal
- Namespace
- RatatuiUnity
High-level C# API for the ratatui native library. Wraps lifecycle, layout, style, and widget commands. Must be disposed when no longer needed.
public sealed class RatatuiTerminal : IDisposable
- Inheritance
-
RatatuiTerminal
- Implements
- Inherited Members
Constructors
RatatuiTerminal(int, int, float)
public RatatuiTerminal(int cols, int rows, float fontSize = 16)
Parameters
colsintTerminal width in character columns.
rowsintTerminal height in character rows.
fontSizefloatFont size in pixels.
Fields
BytesPerPixel
Bytes per pixel in the native pixel buffer (RGB24).
public const int BytesPerPixel = 3
Field Value
Properties
CellHeight
Height of a single character cell in pixels (PixelHeight / Rows).
public int CellHeight { get; }
Property Value
CellWidth
Width of a single character cell in pixels (PixelWidth / Cols).
public int CellWidth { get; }
Property Value
Cols
Terminal width in character columns.
public int Cols { get; }
Property Value
PixelHeight
Height of the rendered texture in pixels.
public int PixelHeight { get; }
Property Value
PixelWidth
Width of the rendered texture in pixels.
public int PixelWidth { get; }
Property Value
RootArea
Root area ID (always 0).
public uint RootArea { get; }
Property Value
Rows
Terminal height in character rows.
public int Rows { get; }
Property Value
Version
Returns the native library version string.
public static string Version { get; }
Property Value
Methods
BarChart(uint, string, ushort, ushort)
Render a bar chart. data is a newline-separated list of
"label\tvalue" pairs (e.g. "Jan\t42\nFeb\t37").
public void BarChart(uint areaId, string data, ushort barWidth = 3, ushort barGap = 1)
Parameters
BeginCanvas(uint, double, double, double, double, Marker)
Begin a canvas builder. Finalize with Render().
public CanvasBuilder BeginCanvas(uint areaId, double xMin, double xMax, double yMin, double yMax, Marker marker = Marker.Dot)
Parameters
Returns
BeginChart(uint)
Begin a chart builder. Finalize with Render().
public ChartBuilder BeginChart(uint areaId)
Parameters
areaIduint
Returns
BeginFrame()
Start a new frame. Clears all queued widget commands. Call before adding widgets and before EndFrame().
public void BeginFrame()
BeginStyledParagraph(uint, Alignment, bool)
Begin a styled paragraph builder. Finalize with Render().
public StyledText BeginStyledParagraph(uint areaId, Alignment alignment = Alignment.Left, bool wrap = false)
Parameters
Returns
Block(uint, string, Borders)
Render a bordered block with an optional title into areaId.
public void Block(uint areaId, string title = "", Borders borders = Borders.All)
Parameters
Calendar(uint, int, int, int)
Render a monthly calendar for the given date.
public void Calendar(uint areaId, int year, int month, int day)
Parameters
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
EndFrame()
Execute all queued widget commands and copy the RGB24 pixel data into
a newly allocated byte[]. Prefer EndFrameRaw() to
avoid a ~1-2 MB GC allocation every frame.
public byte[] EndFrame()
Returns
- byte[]
EndFrameRaw()
Execute all queued widget commands and return a direct pointer to the
native RGB24 pixel buffer. The pointer is valid until the next
BeginFrame() call. Byte count is PixelWidth * PixelHeight * 3.
Prefer this over EndFrame() to avoid a managed byte[] allocation
every frame — pass the pointer directly to
Texture2D.LoadRawTextureData(IntPtr, int).
public nint EndFrameRaw()
Returns
EndFrameRawIfDirty()
Like EndFrameRaw(), but uses a hash-based dirty check on the cell buffer. Returns Zero when the buffer content is unchanged from the previous frame, allowing the caller to skip the GPU texture upload. The previous frame's pixel data remains valid and the texture does not need updating.
public nint EndFrameRawIfDirty()
Returns
~RatatuiTerminal()
protected ~RatatuiTerminal()
Gauge(uint, float, string)
Render a progress gauge. ratio is clamped to [0, 1].
public void Gauge(uint areaId, float ratio, string label = "")
Parameters
HitTest(int, int)
Returns the most specific area ID at the given terminal cell coordinates. Uses the area_map from the previous frame's layout. Returns 0 (root area) if no specific widget area contains the cell.
public uint HitTest(int col, int row)
Parameters
Returns
Inner(uint, ushort, ushort)
Returns a new area ID that represents the inside of areaId
shrunk by the given margin on each side.
Typical usage: call after Block(uint, string, Borders) to get the area inside the border:
term.Block(area, "Title", Borders.All);
uint inner = term.Inner(area); // 1 char inside borders
term.Paragraph(inner, "Hello");
Use horizontal = 2 for one extra character of text padding on
left/right in addition to the border.
public uint Inner(uint areaId, ushort horizontal = 1, ushort vertical = 1)
Parameters
Returns
LineGauge(uint, float, string)
Render a horizontal line gauge. ratio is clamped to [0, 1].
public void LineGauge(uint areaId, float ratio, string label = "")
Parameters
List(uint, string, int)
Render a list widget. items is a newline-separated string.
Pass selected = -1 for no selection.
public void List(uint areaId, string items, int selected = -1)
Parameters
Paragraph(uint, string, Alignment, bool)
Render a paragraph of text into areaId.
public void Paragraph(uint areaId, string text, Alignment alignment = Alignment.Left, bool wrap = true)
Parameters
Scrollbar(uint, long, long, long, ScrollbarOrientation)
Render a scrollbar using "scroll offset" semantics. The thumb reaches the end of the
track exactly when position equals
contentLength - viewportLength (i.e. the content is scrolled to its last page).
When viewportLength is at least as large as
contentLength, the thumb fills the entire track to indicate that
everything is already visible and no scrolling is possible.
public void Scrollbar(uint areaId, long contentLength, long position, long viewportLength, ScrollbarOrientation orientation = ScrollbarOrientation.VerticalRight)
Parameters
areaIduintTarget area.
contentLengthlongTotal number of scrollable items / rows.
positionlongTop visible item index (clamped to
0 .. contentLength - viewportLength).viewportLengthlongNumber of items visible at once. Must be >= 1.
orientationScrollbarOrientationScrollbar placement / direction.
Remarks
Ratatui's underlying scrollbar formula is
thumb_end / track = (position + viewport) / (contentLength - 1 + viewport),
which only saturates when position == contentLength - 1. That corresponds to
"last item at the top of the viewport" — past the visual end of the list — so a
real scroll offset (max contentLength - viewportLength) never makes the thumb
reach the bottom. Substituting contentLength' = contentLength - viewportLength + 1
reduces the formula to (position + viewport) / contentLength so the thumb
saturates at the actual end of the scroll range.
Parameters are declared as long so callers passing either int or uint counts compile without explicit casts.
Scrollbar(uint, long, long, long, ScrollbarOrientation, bool)
Same as Scrollbar(uint, long, long, long, ScrollbarOrientation), but
skips rendering when autoHide is true and the content fits
the viewport (contentLength <= viewportLength) — i.e. nothing to scroll.
public void Scrollbar(uint areaId, long contentLength, long position, long viewportLength, ScrollbarOrientation orientation, bool autoHide)
Parameters
areaIduintcontentLengthlongpositionlongviewportLengthlongorientationScrollbarOrientationautoHidebool
SetBackgroundColor(byte, byte, byte)
Set the terminal background color. This is the color used for cells that have no explicit background set. Must be called before BeginFrame() for the change to take effect in that frame.
public void SetBackgroundColor(byte r, byte g, byte b)
Parameters
SetBackgroundColor(Color)
Convenience overload: Set the background color using a Unity UnityEngine.Color. Alpha channel is ignored — the terminal background is always opaque.
public void SetBackgroundColor(Color color)
Parameters
colorColor
SetCustomFont(byte[])
Replace the embedded JetBrains Mono font with a custom TTF font.
public bool SetCustomFont(byte[] ttfBytes)
Parameters
ttfBytesbyte[]
Returns
- bool
True if the font was loaded successfully.
SetStyle(Color, Color, Modifier)
Convenience overload: SetStyle with Modifier flags enum.
public void SetStyle(Color fg, Color bg, Modifier modifiers)
Parameters
fgColorbgColormodifiersModifier
SetStyle(Color, Color, byte)
Set a style that will be applied to the next widget command.
Use Color.clear to keep the terminal default for fg or bg.
public void SetStyle(Color fg, Color bg, byte modifiers = 0)
Parameters
fgColorbgColormodifiersbyteBitmask: 0x01=Bold, 0x02=Italic, 0x04=Underlined, 0x08=Dim
Sparkline(uint, ulong[])
Render a sparkline from an array of data values.
public void Sparkline(uint areaId, ulong[] data)
Parameters
Split(uint, Direction, params Constraint[])
Split an area into children according to the provided constraints. Returns the array of child area IDs.
public uint[] Split(uint areaId, Direction direction, params Constraint[] constraints)
Parameters
areaIduintdirectionDirectionconstraintsConstraint[]
Returns
- uint[]
Table(uint, string)
Render a table. data format: first line = tab-separated headers;
subsequent lines = tab-separated row cells.
public void Table(uint areaId, string data)
Parameters
TableEx(uint, string, Constraint[], int)
Render a table with per-column width constraints and optional row selection.
Same data format as Table(uint, string); pass null columnWidths
for equal-width columns.
public void TableEx(uint areaId, string data, Constraint[] columnWidths = null, int selectedRow = -1)
Parameters
areaIduintdatastringcolumnWidthsConstraint[]selectedRowint
Tabs(uint, string, uint)
Render a tab bar. titles is a newline-separated string.
public void Tabs(uint areaId, string titles, uint selected = 0)
Parameters
TryGetAreaRect(uint, out int, out int, out int, out int)
Returns the cell-space rectangle of the given area ID. Useful for positioning tooltips, calculating scroll bounds, etc. Returns false if the area ID is not found.
public bool TryGetAreaRect(uint areaId, out int x, out int y, out int width, out int height)