use alloc::boxed::Box;
use core::fmt;
use ratatui_core::buffer::Buffer;
use ratatui_core::layout::Rect;
use ratatui_core::style::{Style, Styled};
use ratatui_core::text::Text;
use ratatui_core::widgets::Widget;
pub struct Cell<'a> {
content: Box<dyn CellContent + 'a>,
style: Style,
pub(crate) column_span: u16,
}
trait CellContent {
fn render_ref(&self, area: Rect, buf: &mut Buffer);
}
impl<W> CellContent for W
where
for<'b> &'b W: Widget,
{
fn render_ref(&self, area: Rect, buf: &mut Buffer) {
Widget::render(self, area, buf);
}
}
struct FnContent<F>(F);
impl<F> CellContent for FnContent<F>
where
F: Fn(Rect, &mut Buffer),
{
fn render_ref(&self, area: Rect, buf: &mut Buffer) {
(self.0)(area, buf);
}
}
impl<'a> Cell<'a> {
pub fn new<W>(widget: W) -> Self
where
W: 'a,
for<'b> &'b W: Widget,
{
Self {
content: Box::new(widget),
style: Style::default(),
column_span: 1,
}
}
pub fn from_fn<F>(render: F) -> Self
where
F: Fn(Rect, &mut Buffer) + 'a,
{
Self {
content: Box::new(FnContent(render)),
style: Style::default(),
column_span: 1,
}
}
#[must_use = "method moves the value of self and returns the modified value"]
pub fn content<W>(mut self, widget: W) -> Self
where
W: 'a,
for<'b> &'b W: Widget,
{
self.content = Box::new(widget);
self
}
#[must_use = "method moves the value of self and returns the modified value"]
pub const fn column_span(mut self, column_span: u16) -> Self {
self.column_span = column_span;
self
}
#[must_use = "method moves the value of self and returns the modified value"]
pub fn style<S: Into<Style>>(mut self, style: S) -> Self {
self.style = style.into();
self
}
}
impl Cell<'_> {
pub(crate) fn render(&self, area: Rect, buf: &mut Buffer) {
if area.is_empty() {
return;
}
buf.set_style(area, self.style);
self.content.render_ref(area, buf);
}
}
impl Default for Cell<'_> {
fn default() -> Self {
Self::new(Text::default())
}
}
impl fmt::Debug for Cell<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Cell")
.field("content", &"<widget>")
.field("style", &self.style)
.field("column_span", &self.column_span)
.finish()
}
}
impl<'a, T> From<T> for Cell<'a>
where
T: Into<Text<'a>>,
{
fn from(content: T) -> Self {
Self::new(content.into())
}
}
impl Styled for Cell<'_> {
type Item = Self;
fn style(&self) -> Style {
self.style
}
fn set_style<S: Into<Style>>(self, style: S) -> Self::Item {
self.style(style)
}
}
#[cfg(test)]
mod tests {
use ratatui_core::style::{Color, Modifier, Stylize};
use ratatui_widgets::paragraph::Paragraph;
use super::*;
#[test]
fn style() {
let style = Style::default().red().italic();
let cell = Cell::default().style(style);
assert_eq!(cell.style, style);
}
#[test]
fn stylize() {
assert_eq!(
Cell::from("").black().on_white().bold().not_dim().style,
Style::default()
.fg(Color::Black)
.bg(Color::White)
.add_modifier(Modifier::BOLD)
.remove_modifier(Modifier::DIM)
);
}
#[test]
fn render_text() {
let mut buf = Buffer::empty(Rect::new(0, 0, 5, 1));
Cell::from("abc").render(buf.area, &mut buf);
assert_eq!(buf, Buffer::with_lines(["abc "]));
}
#[test]
fn render_widget() {
let mut buf = Buffer::empty(Rect::new(0, 0, 5, 1));
Cell::new(Paragraph::new("abc").right_aligned()).render(buf.area, &mut buf);
assert_eq!(buf, Buffer::with_lines([" abc"]));
}
#[test]
fn render_fn() {
let mut buf = Buffer::empty(Rect::new(0, 0, 5, 1));
Cell::from_fn(|area, buf| {
buf.set_string(area.x + 1, area.y, "x", Style::new());
})
.render(buf.area, &mut buf);
assert_eq!(buf, Buffer::with_lines([" x "]));
}
}