use ratatui_core::buffer::Buffer;
use ratatui_core::layout::Rect;
use ratatui_core::style::Color as RatColor;
use ratatui_core::widgets::Widget;
use crate::plot::{Frame, Plot};
use crate::render::{Charset, Color, ColorMode};
use crate::theme::Theme;
impl Plot<'_> {
pub fn widget(&self) -> PlotWidget<'_> {
PlotWidget {
plot: self,
charset: Charset::Quadrants,
theme: Theme::DARK,
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct PlotWidget<'a> {
plot: &'a Plot<'a>,
charset: Charset,
theme: Theme,
}
impl PlotWidget<'_> {
#[must_use]
pub fn charset(mut self, charset: Charset) -> Self {
self.charset = charset;
self
}
#[must_use]
pub fn theme(mut self, theme: Theme) -> Self {
self.theme = theme;
self
}
}
impl Widget for PlotWidget<'_> {
fn render(self, area: Rect, buffer: &mut Buffer) {
(&self).render(area, buffer);
}
}
impl Widget for &PlotWidget<'_> {
fn render(self, area: Rect, buffer: &mut Buffer) {
let frame = Frame {
width: area.width as usize,
height: area.height as usize,
charset: self.charset,
color: ColorMode::TrueColor,
theme: self.theme,
};
let surface = self.plot.rasterize(&frame);
for (column, row, glyph, foreground, background) in surface.cells() {
let x = area.x + column as u16;
let y = area.y + row as u16;
if x >= area.right() || y >= area.bottom() {
continue;
}
let cell = &mut buffer[(x, y)];
let mut symbol = [0u8; 4];
cell.set_symbol(glyph.encode_utf8(&mut symbol));
if let Some(fg) = convert(foreground) {
cell.set_fg(fg);
}
if let Some(bg) = convert(background) {
cell.set_bg(bg);
}
}
}
}
fn convert(color: Color) -> Option<RatColor> {
Some(match color {
Color::Default => return None,
Color::Black => RatColor::Black,
Color::Red => RatColor::Red,
Color::Green => RatColor::Green,
Color::Yellow => RatColor::Yellow,
Color::Blue => RatColor::Blue,
Color::Magenta => RatColor::Magenta,
Color::Cyan => RatColor::Cyan,
Color::White => RatColor::White,
Color::BrightBlack => RatColor::DarkGray,
Color::BrightRed => RatColor::LightRed,
Color::BrightGreen => RatColor::LightGreen,
Color::BrightYellow => RatColor::LightYellow,
Color::BrightBlue => RatColor::LightBlue,
Color::BrightMagenta => RatColor::LightMagenta,
Color::BrightCyan => RatColor::LightCyan,
Color::BrightWhite => RatColor::White,
Color::Ansi256(index) => RatColor::Indexed(index),
Color::Rgb(r, g, b) => RatColor::Rgb(r, g, b),
})
}
#[cfg(test)]
#[path = "adapter_tests.rs"]
mod tests;