Skip to main content

Frame

Struct Frame 

Source
pub struct Frame { /* private fields */ }
Expand description

Logical output from a Component::render call: a vector of Lines plus a Cursor position.

A Frame is the only render artifact of a component. Composition between parents and children happens by transforming and stacking frames — not by reaching back into raw Vec<Line>. Raw line vectors remain a useful internal convenience inside a single component, but they are not the public composition primitive.

The Renderer consumes frames, diffs them against the previous render, and emits only the changed ANSI sequences.

§Construction

use tui::{Frame, Line};
use tui::Cursor;

let frame = Frame::new(vec![
    Line::new("Hello, world!"),
    Line::new("Press q to quit"),
]);

// Optionally place a visible cursor
let frame = frame.with_cursor(Cursor::visible(0, 5));

§Composition

All composition operations live on Frame itself and own the cursor remapping for the transform they perform.

  • fit(width, options) — Wrap or truncate each row to a target width, optionally extending each row to that width with the row’s background color. See FitOptions and Overflow.
  • indent(cols) — Shift visual content cols columns to the right and shift the cursor.
  • vstack(frames) — Concatenate frames vertically. The first visible cursor wins; its row is offset by the cumulative line count of preceding frames.
  • hstack(parts) — Compose FramePart slots horizontally into fixed-width columns. Heights are balanced by padding shorter slots with blank rows of the slot’s width. The first visible cursor wins; its column is offset by the cumulative width of preceding slots.

These primitives are intentionally small. Containers should compose with them rather than reimplementing wrap, pad, or cursor math by hand.

§Other methods

  • lines() — Borrow the rendered lines.
  • cursor() — The current cursor state.
  • with_cursor(cursor) — Replace the cursor (builder pattern, moves self).
  • clamp_cursor() — Clamp the cursor row to the last line index, preventing out-of-bounds positions.
  • into_lines() — Consume the frame and return the lines.
  • into_parts() — Consume and return (Vec<Line>, Cursor).
  • empty() — Construct an empty frame with a hidden cursor.

§See also

  • Cursor — Logical cursor with row, col, and visibility.
  • Line — A single line of styled terminal output.
  • ViewContext — The allocated render region passed into Component::render.
  • Renderer — Consumes frames and renders them to the terminal.
  • Frame::vstack — Composes multiple frames vertically.

Implementations§

Source§

impl Frame

Source

pub fn new(lines: Vec<Line>) -> Self

Source

pub fn empty() -> Self

An empty frame with a hidden cursor.

Source

pub fn lines(&self) -> &[Line]

Source

pub fn cursor(&self) -> Cursor

Source

pub fn with_cursor(self, cursor: Cursor) -> Self

Replace the cursor without cloning lines.

Source

pub fn into_lines(self) -> Vec<Line>

Source

pub fn into_parts(self) -> (Vec<Line>, Cursor)

Source

pub fn clamp_cursor(self) -> Self

Source

pub fn fit(self, width: u16, options: FitOptions) -> Self

Fit the frame to a target width.

  • Overflow::Wrap: each line is soft-wrapped to width. The cursor row is remapped to the wrapped visual row, and the cursor column is reduced modulo width, with the row advanced for any overflow. This matches the wrap math used by VisualFrame.
  • Overflow::Truncate: each line is truncated to width. Row count is unchanged. The cursor column is clamped to width.saturating_sub(1).
  • fill_x: every resulting row is marked with row-fill metadata using any background color present on the row. The fill is not materialized into trailing spaces here — that happens later, in Frame::hstack (per slot width) or VisualFrame (per terminal width). Deferring materialization is what prevents trailing-space rows from producing phantom rows when wrapped again at a smaller width.

As a special case, width == 0 returns the frame unchanged with a hidden cursor (matching the zero-width behavior of VisualFrame).

Source

pub fn indent(self, cols: u16) -> Self

Shift visual content cols columns to the right by prepending spaces to each line. The cursor column is shifted by cols. The row is unchanged.

The prepended prefix inherits any background color from the line, so row-fill highlights extend through the indent.

Source

pub fn vstack(frames: impl IntoIterator<Item = Frame>) -> Self

Concatenate frames vertically.

The first frame in the iterator that has a visible cursor wins. Its row is offset by the cumulative line count of all preceding frames.

Source

pub fn hstack(parts: impl IntoIterator<Item = FramePart>) -> Self

Compose frames horizontally into fixed-width slots.

Each part’s frame is assumed to already fit its width (callers should fit(width, FitOptions::wrap()) or similar first). Any row-fill metadata on a part’s rows is materialized to the part’s slot width before merging, so trailing fill never bleeds into a neighboring slot. Heights are balanced by padding shorter frames with blank rows of the slot’s width. The first part with a visible cursor wins; its column is offset by the cumulative width of preceding slots.

Source

pub fn map_lines<T: FnMut(Line) -> Line>(self, f: T) -> Self

Apply f to each line in turn, preserving cursor and overall row count. The function may not split or merge rows; doing so will leave the cursor pointing at the wrong row.

Source

pub fn prefix(self, head: &Line, tail: &Line) -> Self

Prepend a fixed-width gutter to each row. The first row gets head, subsequent rows get tail. Use the same value for both for a uniform gutter, or different values for first/continuation patterns (e.g., line numbers on the first row of a wrapped block, blanks on the rest).

head and tail must have equal display width — debug-asserted. The cursor column is shifted by that width. Any row-fill metadata on the original row is preserved on the prefixed row.

Source

pub fn pad_height(self, target: u16, width: u16) -> Self

Pad with blank rows of width columns until at least target rows total. No-op if already at or above target. Cursor preserved.

Source

pub fn truncate_height(self, target: u16) -> Self

Truncate to at most target rows. If the visible cursor falls beyond the truncation, it is hidden.

Source

pub fn fit_height(self, target: u16, width: u16) -> Self

Force the frame to exactly target rows: truncate if taller, pad with blank rows of width columns if shorter. Convenience for layouts that emit a fixed-height region regardless of child content.

Source

pub fn wrap_each(self, inner_width: u16, left: &Line, right: &Line) -> Self

Wrap each row in side chrome: materialize fill to inner_width, then prepend left and append right to every row. The cursor column is shifted by left.display_width().

Used for borders/box chrome where the row’s interior should fill its allocated width before the right edge is appended.

Source

pub fn splice(self, after_row: usize, other: Frame) -> Self

Insert the lines of other into this frame immediately after after_row.

Rows 0..=after_row remain in place. The lines of other appear between row after_row and what was previously row after_row + 1. Rows that followed the insertion point shift down by other.lines().len().

If after_row >= self.lines.len(), the lines are appended at the end.

Cursor rule (consistent with vstack / hstack): the host frame’s visible cursor takes priority — shifted down when it sits after the insertion point. Otherwise, other’s visible cursor is adopted with its row offset to the insertion position.

Source

pub fn scroll(self, offset: usize, height: usize) -> Self

Drop the first offset rows and keep at most height rows.

The cursor row is shifted up by offset. If the cursor falls outside the visible window it is hidden.

Trait Implementations§

Source§

impl Clone for Frame

Source§

fn clone(&self) -> Frame

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Frame

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Frame

Source§

fn eq(&self, other: &Frame) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Frame

Source§

impl StructuralPartialEq for Frame

Auto Trait Implementations§

§

impl Freeze for Frame

§

impl RefUnwindSafe for Frame

§

impl Send for Frame

§

impl Sync for Frame

§

impl Unpin for Frame

§

impl UnsafeUnpin for Frame

§

impl UnwindSafe for Frame

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more