Skip to main content

Cell

Struct Cell 

Source
pub struct Cell<'a> { /* private fields */ }
Expand description

A Cell contains the widget to be displayed in a Row of a Table.

Unlike the Cell in ratatui’s built-in table, which can only hold Text, this Cell can hold any widget that can be rendered by reference (i.e. &W implements Widget). This includes all of the built-in ratatui widgets such as Paragraph, Block, Gauge, List, Sparkline, Chart, and even another Table.

For widgets that can only be rendered by value, or for custom drawing logic, use Cell::from_fn.

Note: unlike the built-in table, Cell::new takes a widget, so to create a cell from a string use Cell::from (e.g. Cell::from("text")) or wrap it in Text (Cell::new(Text::from( "text"))). Anything convertible into Text can also be passed directly to Row::new.

You can apply a Style to the Cell using Cell::style. This will set the style for the entire area of the cell before the widget is rendered on top of it.

§Examples

use ratatui::style::Style;
use ratatui::widgets::{Block, Gauge, Paragraph, Wrap};
use ratatui_widgettable::Cell;

// Anything convertible into `Text` still works, just like the built-in table
Cell::from("simple string");

// Any widget can be used as the content of a cell
Cell::new(Paragraph::new("some long text that wraps").wrap(Wrap { trim: true }));
Cell::new(Gauge::default().percent(42));
Cell::new(Block::bordered().title("boxed"));

// Or use a closure for custom rendering
Cell::from_fn(|area, buf| {
    buf.set_string(area.x, area.y, "custom", Style::new());
});

Cell implements Styled which means you can use style shorthands from the Stylize trait to set the style of the cell concisely.

use ratatui::style::Stylize;
use ratatui_widgettable::Cell;

Cell::from("Cell 1").red().italic();

Implementations§

Source§

impl<'a> Cell<'a>

Source

pub fn new<W>(widget: W) -> Self
where W: 'a, for<'b> &'b W: Widget,

Creates a new Cell containing the given widget.

The widget parameter accepts any widget which implements Widget for a reference to itself (&W: Widget), which all of the built-in ratatui widgets do.

To create a cell from a string or other Text, use Cell::from.

§Examples
use ratatui::text::Text;
use ratatui::widgets::{Block, Paragraph};
use ratatui_widgettable::Cell;

Cell::new(Paragraph::new("hello").block(Block::bordered()));
Cell::new(Text::from("a text"));
Source

pub fn from_fn<F>(render: F) -> Self
where F: Fn(Rect, &mut Buffer) + 'a,

Creates a new Cell which renders its content using the given closure.

The closure is called with the area of the cell and the buffer to render into. This is useful for widgets that only implement Widget by value, stateful widgets, or custom drawing logic.

§Examples
use ratatui::widgets::{Paragraph, Widget};
use ratatui_widgettable::Cell;

let cell = Cell::from_fn(|area, buf| {
    Paragraph::new("rendered from a closure").render(area, buf);
});
Source

pub fn content<W>(self, widget: W) -> Self
where W: 'a, for<'b> &'b W: Widget,

Set the content of the Cell to the given widget.

This is a fluent setter method which must be chained or used as it consumes self

§Examples
use ratatui::widgets::Paragraph;
use ratatui_widgettable::Cell;

Cell::default().content(Paragraph::new("hello"));
Source

pub const fn column_span(self, column_span: u16) -> Self

Set the column_span of this cell

This is a fluent setter method which must be chained or used as it consumes self

§Example
use ratatui_widgettable::{Cell, Row};
let rows = vec![
    Row::new(vec![Cell::from("12345").column_span(2)]),
    Row::new(vec![Cell::from("xx"), Cell::from("yy")]),
];
// "12345",
// "xx yy",
Source

pub fn style<S: Into<Style>>(self, style: S) -> Self

Set the Style of this cell

style accepts any type that is convertible to Style (e.g. Style, Color, or your own type that implements Into<Style>).

This Style will override the Style of the Row and can be overridden by any styles applied by the widget content.

This is a fluent setter method which must be chained or used as it consumes self

§Examples
use ratatui::style::{Style, Stylize};
use ratatui_widgettable::Cell;

Cell::from("Cell 1").style(Style::new().red().italic());

Trait Implementations§

Source§

impl Debug for Cell<'_>

Source§

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

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

impl Default for Cell<'_>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'a, T> From<T> for Cell<'a>
where T: Into<Text<'a>>,

Source§

fn from(content: T) -> Self

Converts to this type from the input type.
Source§

impl Styled for Cell<'_>

Source§

type Item = Cell<'_>

Source§

fn style(&self) -> Style

Returns the style of the object.
Source§

fn set_style<S: Into<Style>>(self, style: S) -> Self::Item

Sets the style of the object. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Cell<'a>

§

impl<'a> !Send for Cell<'a>

§

impl<'a> !Sync for Cell<'a>

§

impl<'a> !UnwindSafe for Cell<'a>

§

impl<'a> Freeze for Cell<'a>

§

impl<'a> Unpin for Cell<'a>

§

impl<'a> UnsafeUnpin for Cell<'a>

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> 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> 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, U> Stylize<'_, T> for U
where U: Styled<Item = T>,

Source§

fn bg<C>(self, color: C) -> T
where C: Into<Color>,

Source§

fn fg<C>(self, color: C) -> T
where C: Into<Color>,

Source§

fn add_modifier(self, modifier: Modifier) -> T

Source§

fn remove_modifier(self, modifier: Modifier) -> T

Source§

fn reset(self) -> T

Source§

fn black(self) -> T

Sets the foreground color to black.
Source§

fn on_black(self) -> T

Sets the background color to black.
Source§

fn red(self) -> T

Sets the foreground color to red.
Source§

fn on_red(self) -> T

Sets the background color to red.
Source§

fn green(self) -> T

Sets the foreground color to green.
Source§

fn on_green(self) -> T

Sets the background color to green.
Source§

fn yellow(self) -> T

Sets the foreground color to yellow.
Source§

fn on_yellow(self) -> T

Sets the background color to yellow.
Source§

fn blue(self) -> T

Sets the foreground color to blue.
Source§

fn on_blue(self) -> T

Sets the background color to blue.
Source§

fn magenta(self) -> T

Sets the foreground color to magenta.
Source§

fn on_magenta(self) -> T

Sets the background color to magenta.
Source§

fn cyan(self) -> T

Sets the foreground color to cyan.
Source§

fn on_cyan(self) -> T

Sets the background color to cyan.
Source§

fn gray(self) -> T

Sets the foreground color to gray.
Source§

fn on_gray(self) -> T

Sets the background color to gray.
Source§

fn dark_gray(self) -> T

Sets the foreground color to dark_gray.
Source§

fn on_dark_gray(self) -> T

Sets the background color to dark_gray.
Source§

fn light_red(self) -> T

Sets the foreground color to light_red.
Source§

fn on_light_red(self) -> T

Sets the background color to light_red.
Source§

fn light_green(self) -> T

Sets the foreground color to light_green.
Source§

fn on_light_green(self) -> T

Sets the background color to light_green.
Source§

fn light_yellow(self) -> T

Sets the foreground color to light_yellow.
Source§

fn on_light_yellow(self) -> T

Sets the background color to light_yellow.
Source§

fn light_blue(self) -> T

Sets the foreground color to light_blue.
Source§

fn on_light_blue(self) -> T

Sets the background color to light_blue.
Source§

fn light_magenta(self) -> T

Sets the foreground color to light_magenta.
Source§

fn on_light_magenta(self) -> T

Sets the background color to light_magenta.
Source§

fn light_cyan(self) -> T

Sets the foreground color to light_cyan.
Source§

fn on_light_cyan(self) -> T

Sets the background color to light_cyan.
Source§

fn white(self) -> T

Sets the foreground color to white.
Source§

fn on_white(self) -> T

Sets the background color to white.
Source§

fn bold(self) -> T

Adds the bold modifier.
Source§

fn not_bold(self) -> T

Removes the bold modifier.
Source§

fn dim(self) -> T

Adds the dim modifier.
Source§

fn not_dim(self) -> T

Removes the dim modifier.
Source§

fn italic(self) -> T

Adds the italic modifier.
Source§

fn not_italic(self) -> T

Removes the italic modifier.
Source§

fn underlined(self) -> T

Adds the underlined modifier.
Source§

fn not_underlined(self) -> T

Removes the underlined modifier.
Adds the slow_blink modifier.
Removes the slow_blink modifier.
Adds the rapid_blink modifier.
Removes the rapid_blink modifier.
Source§

fn reversed(self) -> T

Adds the reversed modifier.
Source§

fn not_reversed(self) -> T

Removes the reversed modifier.
Source§

fn hidden(self) -> T

Adds the hidden modifier.
Source§

fn not_hidden(self) -> T

Removes the hidden modifier.
Source§

fn crossed_out(self) -> T

Adds the crossed_out modifier.
Source§

fn not_crossed_out(self) -> T

Removes the crossed_out modifier.
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.