Skip to main content

Renderer

Struct Renderer 

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

A Renderer is responsible for rendering the UI to the terminal. It takes a Container and displays its components on the screen.

§Usage

A Renderer is used to render a Container to the terminal. It manages drawing operations and handles the rendering process efficiently.

§Derives

Clone, Debug, PartialEq, Eq

§Example

// Create a Renderer with a width of 40 and a height of 20
let mut renderer = Renderer::new(40, 20);

// Clear the buffer before rendering
renderer.clear();

// Render the container (assuming `container` is created elsewhere)
renderer.render(&container);

// Draw the final output to the terminal
renderer.draw();

Implementations§

Source§

impl Renderer

Source

pub fn new(width: u16, height: u16) -> Renderer

Constructs a new Renderer with the specified width and height.

§Parameters
  • width: A u16 representing the width in characters.
  • height: A u16 representing the height in characters.
§Returns

A Renderer instance.

§Example
// Create a Renderer with a width of 40 and a height of 20 characters.
let renderer = Renderer::new(40, 20);
Source

pub fn fullscreen() -> FtuiResult<Renderer>

Constructs a new fullscreen Renderer (Does not resize).

§Returns

Ok(Renderer): A Renderer instance. Err(FtuiError): Returns an error.

§Example
// Create a fullscreen Renderer.
let renderer = Renderer::fullscreen()?;
Source

pub fn render(&mut self, container: &mut Container) -> FtuiResult<()>

Renders a Container into the Renderer buffer without drawing to the terminal.

§Parameters
  • container: A mutable reference to the Container to be rendered.
§Note
  • This method only updates the internal buffer.
  • To display the rendered content, call the draw method.
  • You should use the clear method to clear the buffer first.
§Returns
  • Ok(()): Returns nothing.
  • Err(FtuiError): Returns an error.
§Example
// Create a `Renderer` with a width of 40 and a height of 20 characters.
let mut renderer = Renderer::new(40, 20);

// Render the container into the renderer buffer
// (assuming `container` is created elsewhere)
renderer.render(&mut container)?;
Source

pub fn render_list(&mut self, list: &mut List) -> FtuiResult<()>

Renders a List into the Renderer buffer without drawing to the terminal.

§Parameters
  • list: A mutable reference to the List to be rendered.
§Note
  • This method only updates the internal buffer.
  • To display the rendered content, call the draw method.
  • You should use the clear method to clear the buffer first.
§Returns
  • Ok(()): Returns nothing.
  • Err(FtuiError): Returns an error.
§Example
// Create a `Renderer` with a width of 40 and a height of 20 characters.
let mut renderer = Renderer::new(40, 20);

// Render a list into the renderer buffer
// (assuming `list` is created elsewhere)
renderer.render_list(&mut list)?;
Source

pub fn draw(&mut self) -> FtuiResult<()>

Draws the Renderer buffer to the terminal.

§Note

The render method must be called at least once before draw, as draw only displays the content stored in the Renderer buffer.

§Example
// Create a `Renderer` with a width of 40 and a height of 20 characters.
let mut renderer = Renderer::new(40, 20);

// Render the container into the renderer buffer
// (assuming `container` is created elsewhere)
renderer.render(&mut container)?;

// Draw the rendered content to the terminal
renderer.draw();

// The draw method can be called again without re-rendering,
// but changes won't be reflected unless `render` is called.
renderer.draw();
Source

pub fn clear(&mut self)

Clears the Renderer buffer. This method should be called before rendering.

§Note

Calling this method before rendering prevents visual artifacts.

§Example
// Create a `Renderer` with a width of 40 and a height of 20 characters.
let mut renderer = Renderer::new(40, 20);

// Rendering loop
loop {
    // Clear the `Renderer` buffer to remove previous frame content
    renderer.clear();

    // Render the container into the renderer buffer
    // (assuming `container` is created elsewhere)
    renderer.render(&mut container)?;

    // Draw the rendered content to the terminal
    renderer.draw();
}
Source

pub fn simple_draw(&mut self, container: &mut Container) -> FtuiResult<()>

Executes a full rendering cycle in a single method call. This method automatically calls clear, render, and draw in sequence.

§Parameters
  • container: A mutable reference to the Container to be drawn.
§Returns
  • Ok(()): Returns nothing.
  • Err(FtuiError): Returns an error.
§Example
// Create a `Renderer` with a width of 40 and a height of 20 characters.
let mut renderer = Renderer::new(40, 20);

// Standard rendering loop
loop {
    renderer.clear();
    // Render content (assuming `container` is created elsewhere)
    renderer.render(&mut container)?;
    renderer.draw();
}

// Simplified rendering loop using `simple_draw`
loop {
    // Render and draw in a single step
    renderer.simple_draw(&mut container)?;
}

Trait Implementations§

Source§

impl Clone for Renderer

Source§

fn clone(&self) -> Renderer

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Renderer

Source§

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

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

impl Eq for Renderer

Source§

impl PartialEq for Renderer

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Renderer

Auto Trait Implementations§

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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.