返回 CodeWhale
pet_widget_legacy.rs
根目录 / crates / tui / src / tui / ambient_life / pet_widget_legacy.rs
1 //! Frozen current packed-raster painter, acceptance only.
2 use ratatui::{
3 buffer::Buffer,
4 layout::{Alignment, Rect},
5 style::Style,
6 widgets::{Paragraph, Widget, Wrap},
7 };
8 use unicode_width::UnicodeWidthStr;
9
10 pub(super) fn render_grid(area: Rect, buf: &mut Buffer, grid: &[u8], label: &str, style: Style) {
11 if area.width == 0 || area.height == 0 {
12 return;
13 }
14 if label.width() > usize::from(area.width) || area.height < 4 {
15 Paragraph::new(label)
16 .style(style)
17 .alignment(Alignment::Center)
18 .wrap(Wrap { trim: false })
19 .render(area, buf);
20 return;
21 }
22 for y in 0..area.height - 1 {
23 for x in 0..area.width {
24 let bits = grid
25 .get(usize::from(y) * usize::from(area.width) + usize::from(x))
26 .copied()
27 .unwrap_or(0);
28 if bits != 0
29 && let Some(cell) = buf.cell_mut((area.x + x, area.y + y))
30 {
31 let glyph = char::from_u32(0x2800 + u32::from(bits)).expect("braille");
32 cell.set_symbol(&glyph.to_string()).set_style(style);
33 }
34 }
35 }
36 let x = area.x + (area.width - label.width() as u16) / 2;
37 buf.set_stringn(x, area.bottom() - 1, label, usize::from(area.width), style);
38 }
39
39 lines RUST