use std::collections::HashMap;
use std::iter::zip;
use std::sync::LazyLock;
use ratatui_core::buffer::Buffer;
use ratatui_core::layout::Rect;
use ratatui_core::widgets::Widget;
pub struct BoxChar(char);
impl BoxChar {
pub const fn new(c: char) -> Self {
Self(c)
}
}
impl Widget for &BoxChar {
fn render(self, area: Rect, buf: &mut Buffer) {
let c = self
.0
.to_uppercase() .next()
.and_then(|c| CHARS.get(&c))
.unwrap_or(&" ");
let lines = c.lines().collect::<Vec<_>>();
for (line, row) in zip(lines, area.rows()) {
for (char, cell) in zip(line.chars(), row.columns()) {
buf[cell.as_position()].set_symbol(&char.to_string());
}
}
}
}
macro_rules! char_table {
( $($char:expr => $repr:expr),* $(,)? ) => {
{
let mut table = ::std::collections::HashMap::new();
$(
table.insert($char, ::indoc::indoc! {$repr});
)*
table
}
};
}
static CHARS: LazyLock<HashMap<char, &str>> = LazyLock::new(|| {
char_table!(
' ' => " ",
'!' => "│
╵
╵",
'"' => "╭╭",
'#' => "┼─┼
┼─┼",
'$' => "╭┼╴
└┼┐
╶┼╯",
'%' => "o╱
╱o",
'&' => "╭─╮
╭╲╯
╰─╲",
'\'' => "╭",
'(' => "╭
│
╰",
')' => "╮
│
╯",
'*' => "
*
",
'+' => "
╷
╶┼╴
╵",
',' => "
╯",
'-' => "
──
",
'.' => "
.
",
'/' => "
╱
╱
",
'0' => "╭─╮
│╱│
╰─╯",
'1' => "
╶┐
│
─┴─",
'2' => "╶─╮
┌─┘
└─╴",
'3' => "╶─╮
╶─┤
╶─╯",
'4' => "╷ ╷
╰─┤
╵",
'5' => "┌─╴
└─╮
╰─╯",
'6' => "╭─╴
├─╮
╰─╯",
'7' => "╶─┐
╱
╵ ",
'8' => "╭─╮
├─┤
╰─╯",
'9' => "╭─╮
╰─┤
╶─╯",
':' => "╷
╵
│
",
';' => "╷
╵
╯",
'<' => "
╱
╲
",
'=' => "
──
──",
'>' => "
╲
╱
",
'?' => "
╶─╮
╭╯
╷",
'@' => "╭─╮
╭╮│
╰┴╯",
'A' => "╭─╮
├─┤
╵ ╵",
'B' => "┌╮
├┴╮
╰─╯",
'C' => "╭─╮
│
╰─╯",
'D' => "┌─╮
│ │
└─╯",
'E' => "┌─╴
├─
└─╴",
'F' => "┌─╴
├─
╵ ",
'G' => "╭─╮
│─╮
╰─╯",
'H' => "╷ ╷
├─┤
╵ ╵",
'I' => "╶┬╴
│
╶┴╴",
'J' => " ╶┐
│
╰─╯",
'K' => "╷╭
├┴╮
╵ ╵",
'L' => "╷
│
└──",
'M' => "╭┬╮
│││
╵╵╵",
'N' => "╭─╮
│ │
╵ ╵",
'O' => "╭─╮
│ │
╰─╯",
'P' => "┌─╮
├─╯
╵ ",
'Q' => "╭─╮
│ │
╰─╳",
'R' => "┌─╮
├┬╯
╵╰ ",
'S' => "╭─╮
╰─╮
╰─╯",
'T' => "
╶┬╴
│
╵",
'U' => "╷ ╷
│ │
╰─╯",
'V' => "╷ ╷
│ │
└─╯",
'W' => "╷╷╷
│││
╰┴╯",
'X' => "╮ ╭
╰─╮
╯ ╰",
'Y' => "╮ ╭
╰┬╯
╵",
'Z' => "╶─╮
╱
╰─╴",
'[' => "┌─
│
└─",
'\\' => "
╲
╲
",
']' => "─┐
│
─┘",
'^' => "╱╲",
'_' => "
──",
'`' => "╮",
'{' => "
╭
┤
╰",
'|' => "│
│
│",
'}' => "╮
├
╯",
'~' => "
╭╮
╰╯",
)
});