use retroglyph_core::{Backend, Grid, Rect, Style, Terminal, Tile};
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
use crate::draw::{BL, BR, H, TL, TR, V};
use crate::text::truncate;
use crate::widget::Widget;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Sides {
pub top: u16,
pub right: u16,
pub bottom: u16,
pub left: u16,
}
impl Sides {
pub const ZERO: Self = Self {
top: 0,
right: 0,
bottom: 0,
left: 0,
};
#[must_use]
pub const fn all(n: u16) -> Self {
Self {
top: n,
right: n,
bottom: n,
left: n,
}
}
#[must_use]
pub const fn symmetric(vertical: u16, horizontal: u16) -> Self {
Self {
top: vertical,
right: horizontal,
bottom: vertical,
left: horizontal,
}
}
#[must_use]
pub const fn top(mut self, top: u16) -> Self {
self.top = top;
self
}
#[must_use]
pub const fn right(mut self, right: u16) -> Self {
self.right = right;
self
}
#[must_use]
pub const fn bottom(mut self, bottom: u16) -> Self {
self.bottom = bottom;
self
}
#[must_use]
pub const fn left(mut self, left: u16) -> Self {
self.left = left;
self
}
const fn horizontal(self) -> u16 {
self.left.saturating_add(self.right)
}
const fn vertical(self) -> u16 {
self.top.saturating_add(self.bottom)
}
}
#[derive(Clone, Copy, Debug)]
pub struct BoxStyle {
style: Style,
padding: Sides,
margin: Sides,
border: bool,
width: Option<u16>,
height: Option<u16>,
}
impl BoxStyle {
#[must_use]
pub const fn new(style: Style) -> Self {
Self {
style,
padding: Sides::ZERO,
margin: Sides::ZERO,
border: false,
width: None,
height: None,
}
}
#[must_use]
pub const fn padding(mut self, padding: Sides) -> Self {
self.padding = padding;
self
}
#[must_use]
pub const fn margin(mut self, margin: Sides) -> Self {
self.margin = margin;
self
}
#[must_use]
pub const fn border(mut self, border: bool) -> Self {
self.border = border;
self
}
#[must_use]
pub const fn width(mut self, width: u16) -> Self {
self.width = Some(width);
self
}
#[must_use]
pub const fn height(mut self, height: u16) -> Self {
self.height = Some(height);
self
}
#[must_use]
pub fn render(&self, text: &str) -> Grid {
let lines: Vec<&str> = text.split('\n').collect();
let content_w = self.width.unwrap_or_else(|| {
u16::try_from(lines.iter().map(|l| l.width()).max().unwrap_or(0)).unwrap_or(u16::MAX)
});
let content_h = self
.height
.unwrap_or_else(|| u16::try_from(lines.len()).unwrap_or(u16::MAX));
let (mut grid, content_x, content_y) = self.scaffold(content_w, content_h);
for (row, line) in lines.iter().take(usize::from(content_h)).enumerate() {
let Ok(row) = u16::try_from(row) else { break };
let clipped = truncate(line, usize::from(content_w));
let mut col = 0u16;
for ch in clipped.chars() {
let w = u16::try_from(ch.width().unwrap_or(0)).unwrap_or(u16::MAX);
if col.saturating_add(w) > content_w {
break;
}
grid.put(content_x + col, content_y + row, Tile::new(ch, self.style));
col = col.saturating_add(w);
}
}
grid
}
#[cfg(feature = "egc")]
#[must_use]
pub fn render_wrapped(&self, text: &str) -> Grid {
use retroglyph_core::Headless;
use retroglyph_core::layout::TextLayout;
use retroglyph_core::text::{Line, Span};
let content_w = self.width.unwrap_or_else(|| {
u16::try_from(
text.split('\n')
.map(UnicodeWidthStr::width)
.max()
.unwrap_or(0),
)
.unwrap_or(u16::MAX)
});
let line = Line::from(Span::styled(text, self.style));
let content_h = self.height.unwrap_or_else(|| {
TextLayout::new(&line)
.rect(Rect::new(0, 0, content_w, u16::MAX))
.measure()
.height
});
let (mut grid, content_x, content_y) = self.scaffold(content_w, content_h);
let mut scratch = Terminal::new(Headless::new(content_w.max(1), content_h.max(1)));
TextLayout::new(&line)
.rect(Rect::new(0, 0, content_w, content_h))
.render(&mut scratch);
let content_rect = Rect::new(0, 0, content_w, content_h);
grid.blit(0, scratch.grid(), content_rect, content_x, content_y);
grid
}
fn scaffold(&self, content_w: u16, content_h: u16) -> (Grid, u16, u16) {
let border_wh = u16::from(self.border) * 2;
let inner_w = content_w
.saturating_add(self.padding.horizontal())
.saturating_add(border_wh);
let inner_h = content_h
.saturating_add(self.padding.vertical())
.saturating_add(border_wh);
let outer_w = inner_w.saturating_add(self.margin.horizontal()).max(1);
let outer_h = inner_h.saturating_add(self.margin.vertical()).max(1);
let mut grid = Grid::new(outer_w, outer_h);
let box_x = self.margin.left;
let box_y = self.margin.top;
fill_rect(&mut grid, box_x, box_y, inner_w, inner_h, self.style);
if self.border {
draw_border(&mut grid, box_x, box_y, inner_w, inner_h, self.style);
}
let content_x = box_x
.saturating_add(u16::from(self.border))
.saturating_add(self.padding.left);
let content_y = box_y
.saturating_add(u16::from(self.border))
.saturating_add(self.padding.top);
(grid, content_x, content_y)
}
}
#[derive(Clone, Copy, Debug)]
pub struct Boxed<'a> {
style: BoxStyle,
text: &'a str,
}
impl BoxStyle {
#[must_use]
pub const fn text(self, text: &str) -> Boxed<'_> {
Boxed { style: self, text }
}
}
impl<B: Backend> Widget<B> for Boxed<'_> {
fn render(self, area: Rect, term: &mut Terminal<B>) {
let grid = self.style.render(self.text);
crate::block::blit_into(term, &grid, area.left(), area.top());
}
}
fn fill_rect(grid: &mut Grid, x: u16, y: u16, w: u16, h: u16, style: Style) {
for dy in 0..h {
for dx in 0..w {
grid.put(x + dx, y + dy, Tile::new(' ', style));
}
}
}
fn draw_border(grid: &mut Grid, x: u16, y: u16, w: u16, h: u16, style: Style) {
let right = x + w - 1;
let bottom = y + h - 1;
grid.put(x, y, Tile::new(TL, style));
grid.put(right, y, Tile::new(TR, style));
grid.put(x, bottom, Tile::new(BL, style));
grid.put(right, bottom, Tile::new(BR, style));
for cx in (x + 1)..right {
grid.put(cx, y, Tile::new(H, style));
grid.put(cx, bottom, Tile::new(H, style));
}
for cy in (y + 1)..bottom {
grid.put(x, cy, Tile::new(V, style));
grid.put(right, cy, Tile::new(V, style));
}
}
#[cfg(test)]
mod tests {
use super::*;
fn glyphs(grid: &Grid) -> Vec<String> {
(0..grid.height())
.map(|y| (0..grid.width()).map(|x| grid.get(x, y).glyph()).collect())
.collect()
}
#[test]
fn sides_helpers() {
assert_eq!(
Sides::all(2),
Sides {
top: 2,
right: 2,
bottom: 2,
left: 2
}
);
assert_eq!(
Sides::symmetric(1, 3),
Sides {
top: 1,
right: 3,
bottom: 1,
left: 3
}
);
}
#[test]
fn sizes_to_content_with_no_padding_or_border() {
let grid = BoxStyle::new(Style::default()).render("hi");
assert_eq!((grid.width(), grid.height()), (2, 1));
assert_eq!(grid.get(0, 0).glyph(), 'h');
assert_eq!(grid.get(1, 0).glyph(), 'i');
}
#[test]
fn sizes_to_the_widest_of_multiple_lines() {
let grid = BoxStyle::new(Style::default()).render("a\nbcd\nef");
assert_eq!((grid.width(), grid.height()), (3, 3));
assert_eq!(grid.get(0, 0).glyph(), 'a');
assert_eq!(grid.get(1, 0).glyph(), ' '); assert_eq!(grid.get(0, 1).glyph(), 'b');
assert_eq!(grid.get(2, 1).glyph(), 'd');
}
#[test]
fn explicit_width_clips_longer_lines_and_pads_shorter_ones() {
let grid = BoxStyle::new(Style::default()).width(3).render("hello");
assert_eq!(grid.width(), 3);
let row: String = (0..3).map(|x| grid.get(x, 0).glyph()).collect();
assert_eq!(row, "hel");
}
#[test]
fn explicit_height_drops_extra_lines() {
let grid = BoxStyle::new(Style::default()).height(1).render("a\nb\nc");
assert_eq!(grid.height(), 1);
assert_eq!(grid.get(0, 0).glyph(), 'a');
}
#[test]
fn padding_surrounds_content_with_the_box_style() {
let grid = BoxStyle::new(Style::default())
.padding(Sides::all(1))
.render("x");
assert_eq!((grid.width(), grid.height()), (3, 3));
assert_eq!(grid.get(1, 1).glyph(), 'x');
assert_eq!(grid.get(0, 0).glyph(), ' ');
}
#[test]
fn border_draws_a_box_around_padding_and_content() {
let grid = BoxStyle::new(Style::default()).border(true).render("x");
assert_eq!((grid.width(), grid.height()), (3, 3));
let rows = glyphs(&grid);
assert_eq!(rows[0], "┌─┐");
assert_eq!(rows[1], "│x│");
assert_eq!(rows[2], "└─┘");
}
#[test]
fn margin_is_left_transparent_outside_the_border() {
let grid = BoxStyle::new(Style::default())
.margin(Sides::all(1))
.render("x");
assert_eq!((grid.width(), grid.height()), (3, 3));
assert!(grid.get(0, 0).is_empty());
assert_eq!(grid.get(1, 1).glyph(), 'x');
}
#[test]
fn wide_characters_push_later_columns_over_by_their_width() {
let grid = BoxStyle::new(Style::default()).render("aあb");
assert_eq!(grid.width(), 4);
assert_eq!(grid.get(0, 0).glyph(), 'a');
assert_eq!(grid.get(1, 0).glyph(), 'あ');
assert_eq!(grid.get(3, 0).glyph(), 'b');
}
#[test]
fn border_with_empty_content_is_still_at_least_a_2x2_box() {
let grid = BoxStyle::new(Style::default()).border(true).render("");
assert_eq!((grid.width(), grid.height()), (2, 3));
let rows = glyphs(&grid);
assert_eq!(rows[0], "┌┐");
assert_eq!(rows[2], "└┘");
}
#[test]
#[cfg(feature = "egc")]
fn render_wrapped_word_wraps_to_the_explicit_width() {
let grid = BoxStyle::new(Style::default())
.width(10)
.render_wrapped("the quick brown fox jumps");
assert_eq!(grid.width(), 10);
let rows = glyphs(&grid);
assert_eq!(rows[0].trim_end(), "the quick");
assert_eq!(rows[1].trim_end(), "brown fox");
assert_eq!(rows[2].trim_end(), "jumps");
}
#[test]
#[cfg(feature = "egc")]
fn render_wrapped_without_an_explicit_width_measures_but_does_not_wrap() {
let grid = BoxStyle::new(Style::default()).render_wrapped("hi");
assert_eq!((grid.width(), grid.height()), (2, 1));
assert_eq!(grid.get(0, 0).glyph(), 'h');
assert_eq!(grid.get(1, 0).glyph(), 'i');
}
#[test]
#[cfg(feature = "egc")]
fn render_wrapped_respects_padding_and_border_like_render() {
let grid = BoxStyle::new(Style::default())
.border(true)
.padding(Sides::all(1))
.width(3)
.render_wrapped("hi");
assert_eq!((grid.width(), grid.height()), (7, 5));
assert_eq!(grid.get(2, 2).glyph(), 'h');
assert_eq!(grid.get(3, 2).glyph(), 'i');
}
#[test]
fn boxed_widget_places_the_box_at_the_areas_top_left() {
use retroglyph_core::Headless;
let styled = BoxStyle::new(Style::default()).border(true).text("hi");
let mut term = Terminal::new(Headless::new(10, 6));
styled.render(Rect::new(2, 1, 10, 6), &mut term);
assert_eq!(term.grid().get(2, 1).glyph(), '┌');
assert_eq!(term.grid().get(3, 2).glyph(), 'h');
assert_eq!(term.grid().get(4, 2).glyph(), 'i');
assert_eq!(term.grid().get(5, 3).glyph(), '┘');
}
}