Lines
99.32 %
Functions
100 %
Branches
use super::*;
use image::{ImageBuffer, Rgba};
use ratatui::Terminal;
use ratatui::backend::TestBackend;
use ratatui::style::Color;
const CELL: (u16, u16) = (10, 20);
const BACKGROUND: Color = Color::Rgb(1, 2, 3);
fn striped_png(width: u32, height: u32) -> Vec<u8> {
let image = ImageBuffer::from_fn(width, height, |_, y| {
if y / 10 % 2 == 0 {
Rgba([255u8, 0, 0, 255])
} else {
Rgba([0u8, 0, 255, 255])
}
});
let mut bytes = Vec::new();
image
.write_to(&mut std::io::Cursor::new(&mut bytes), ImageFormat::Png)
.expect("png encodes");
bytes
fn draw(chart: &Image, area: Rect) -> Terminal<TestBackend> {
let mut terminal = Terminal::new(TestBackend::new(30, 12)).expect("test backend");
terminal
.draw(|frame| chart.render(area, frame.buffer_mut(), BACKGROUND))
.expect("draws");
#[test]
fn halfblocks_fill_every_cell_of_the_pane() {
let size = Size::new(8, 4);
let chart =
Image::new(&striped_png(80, 80), ImageBackend::HalfBlocks, size, CELL).expect("chart");
let terminal = draw(&chart, Rect::new(2, 1, 8, 4));
let buffer = terminal.backend().buffer();
for y in 1..5 {
for x in 2..10 {
let cell = &buffer[(x, y)];
assert_eq!(cell.symbol(), "▀", "({x}, {y})");
let (Color::Rgb(fr, _, fb), Color::Rgb(br, _, bb)) = (cell.fg, cell.bg) else {
panic!("true colour expected at ({x}, {y})");
};
assert!(fr > fb && bb > br, "red over blue at ({x}, {y})");
assert_eq!(buffer[(1, 1)].symbol(), " ");
fn kitty_writes_the_transmit_sequence_and_placeholders() {
let chart = Image::new(
&striped_png(80, 80),
ImageBackend::Kitty { id: 7 },
size,
CELL,
)
.expect("chart");
let first = buffer[(2, 1)].symbol();
assert!(first.contains("\x1b_G"), "{first:?}");
assert!(first.contains("o=z"), "{first:?}");
assert!(first.contains('\u{10EEEE}'), "{first:?}");
assert!(buffer[(9, 4)].symbol().contains('\u{10EEEE}'));
fn kitty_placeholders_survive_a_second_frame() {
let mut terminal = draw(&chart, Rect::new(2, 1, 8, 4));
.draw(|frame| chart.render(Rect::new(2, 1, 8, 4), frame.buffer_mut(), BACKGROUND))
.expect("redraws");
for (x, y) in [(2, 1), (9, 1), (2, 4), (9, 4)] {
assert!(buffer[(x, y)].symbol().contains('\u{10EEEE}'), "({x}, {y})");
fn a_pane_without_cells_is_an_error() {
let result = Image::new(
&striped_png(10, 10),
ImageBackend::HalfBlocks,
Size::new(0, 3),
);
assert!(matches!(result, Err(ImageError::NoRoom)));
fn bytes_that_are_not_a_png_are_an_error() {
b"nope",
ImageBackend::Kitty { id: 1 },
Size::new(4, 2),
let err = result.err().expect("error");
assert!(matches!(err, ImageError::NotPng(_)), "{err}");
/// An 80x80 PNG: `top` over the upper 40 rows, `bottom` over the lower 40.
fn two_tone_png(top: Rgba<u8>, bottom: Rgba<u8>) -> Vec<u8> {
let image = ImageBuffer::from_fn(80, 80, |_, y| if y < 40 { top } else { bottom });
const RED: Rgba<u8> = Rgba([255, 0, 0, 255]);
const BLUE: Rgba<u8> = Rgba([0, 0, 255, 255]);
const CLEAR: Rgba<u8> = Rgba([9, 9, 9, 0]);
/// The cells of the top-left 8x4 pane after drawing `png` as halfblocks: (glyph, fg, bg).
fn halfblock_cells(png: &[u8]) -> Vec<(String, Color, Color)> {
let chart = Image::new(png, ImageBackend::HalfBlocks, Size::new(8, 4), CELL).expect("chart");
let terminal = draw(&chart, Rect::new(0, 0, 8, 4));
(0..4)
.flat_map(|y| (0..8).map(move |x| (x, y)))
.map(|(x, y)| {
(cell.symbol().to_string(), cell.fg, cell.bg)
})
.collect()
fn a_transparent_png_shows_the_background_in_every_cell() {
for cell in halfblock_cells(&two_tone_png(CLEAR, CLEAR)) {
assert_eq!(cell, (" ".to_string(), BACKGROUND, BACKGROUND));
fn an_opaque_png_keeps_its_colours() {
let cells = halfblock_cells(&two_tone_png(RED, RED));
let red = Color::Rgb(255, 0, 0);
for cell in cells {
assert_eq!(cell, ("▀".to_string(), red, red));
fn a_transparent_bottom_half_leaves_the_background_under_the_top() {
// Cell rows 2 and 3 are the transparent lower half.
let cells = halfblock_cells(&two_tone_png(RED, CLEAR));
assert_eq!(cells[0], ("▀".to_string(), red, red));
assert_eq!(cells[16], (" ".to_string(), BACKGROUND, BACKGROUND));
fn a_transparent_top_half_draws_the_lower_block() {
// Only the first half row is clear, so the top cell splits mid-cell.
let image = ImageBuffer::from_fn(80, 80, |_, y| if y < 10 { CLEAR } else { BLUE });
let mut png = Vec::new();
.write_to(&mut std::io::Cursor::new(&mut png), ImageFormat::Png)
let cells = halfblock_cells(&png);
assert_eq!(
cells[0],
("▄".to_string(), Color::Rgb(0, 0, 255), BACKGROUND)
assert_eq!(cells[8].0, "▀");
fn translucent_colours_are_rounded_not_truncated() {
let image = ImageBuffer::from_pixel(80, 80, Rgba([3u8, 3, 3, 128]));
let grey = Color::Rgb(4, 4, 4);
for cell in halfblock_cells(&png) {
assert_eq!(cell, ("▀".to_string(), grey, grey));
fn kitty_delete_names_the_image_and_frees_it() {
assert_eq!(kitty_delete(77), b"\x1b_Ga=d,d=I,i=77,q=2\x1b\\");