use owo_colors::OwoColorize;
pub enum CellColor {
Green,
Red,
Yellow,
Magenta,
Blue,
}
impl CellColor {
fn paint(&self, glyph: &str) -> String {
match self {
CellColor::Green => glyph.green().to_string(),
CellColor::Red => glyph.red().to_string(),
CellColor::Yellow => glyph.yellow().to_string(),
CellColor::Magenta => glyph.magenta().to_string(),
CellColor::Blue => glyph.blue().to_string(),
}
}
}
pub fn render(
title: &str,
subtitle: &str,
start: u32,
cell_bytes: u32,
n_cells: usize,
color: impl Fn(usize) -> CellColor,
) {
const COLS: usize = 64;
if crate::render::is_quiet() {
return;
}
let row_bytes = COLS as u32 * cell_bytes;
let n_rows = n_cells.div_ceil(COLS);
println!();
println!("{} {}", bold(title), dim(subtitle));
print!(" ");
for col in 0..COLS {
if col % 8 == 0 {
print!("{}", dim(&format!("{col:<8}")));
}
}
println!();
for row in 0..n_rows {
let row_addr = start + (row as u32) * row_bytes;
print!(
"{} {} ",
dim(&format!("0x{row_addr:08X}")),
bright_black("│")
);
for col in 0..COLS {
let cell = row * COLS + col;
if cell >= n_cells {
print!(" ");
continue;
}
print!("{}", color(cell).paint("█"));
}
println!();
}
}
pub fn legend(entries: &[(CellColor, &str)]) {
if crate::render::is_quiet() {
return;
}
print!(" Legend: ");
for (i, (c, label)) in entries.iter().enumerate() {
if i > 0 {
print!(" ");
}
print!("{} {}", c.paint("█"), label);
}
println!();
}
fn bold(s: &str) -> String {
s.bold().to_string()
}
fn dim(s: &str) -> String {
s.dimmed().to_string()
}
fn bright_black(s: &str) -> String {
s.bright_black().to_string()
}