mrk-cli 0.9.0

Render Markdown beautifully in the terminal: syntax highlighting, tables, themes, and Mermaid diagrams as images.
Documentation
use base64::Engine;
use base64::engine::general_purpose::STANDARD;

use super::crop::Crop;
use super::{ColorDepth, tmux};
use crate::document::Picture;

const CHUNK_BYTES: usize = 4096;
const PLACEMENT_ID: u32 = 1;
/// Under the text and above cell backgrounds, so a title's concealed text stays selectable over its picture.
const UNDER_TEXT: i32 = -1;
/// The cell kitty draws a piece of a virtual placement in; its diacritics say which row, and its colour which image.
const PLACEHOLDER: char = '\u{10eeee}';
/// The combining marks that number placeholder rows and columns, in kitty's order (`rowcolumn-diacritics.txt`).
#[rustfmt::skip]
const DIACRITICS: [char; 297] = [
    '\u{0305}', '\u{030d}', '\u{030e}', '\u{0310}', '\u{0312}', '\u{033d}', '\u{033e}', '\u{033f}', '\u{0346}', '\u{034a}', '\u{034b}',
    '\u{034c}', '\u{0350}', '\u{0351}', '\u{0352}', '\u{0357}', '\u{035b}', '\u{0363}', '\u{0364}', '\u{0365}', '\u{0366}', '\u{0367}',
    '\u{0368}', '\u{0369}', '\u{036a}', '\u{036b}', '\u{036c}', '\u{036d}', '\u{036e}', '\u{036f}', '\u{0483}', '\u{0484}', '\u{0485}',
    '\u{0486}', '\u{0487}', '\u{0592}', '\u{0593}', '\u{0594}', '\u{0595}', '\u{0597}', '\u{0598}', '\u{0599}', '\u{059c}', '\u{059d}',
    '\u{059e}', '\u{059f}', '\u{05a0}', '\u{05a1}', '\u{05a8}', '\u{05a9}', '\u{05ab}', '\u{05ac}', '\u{05af}', '\u{05c4}', '\u{0610}',
    '\u{0611}', '\u{0612}', '\u{0613}', '\u{0614}', '\u{0615}', '\u{0616}', '\u{0617}', '\u{0657}', '\u{0658}', '\u{0659}', '\u{065a}',
    '\u{065b}', '\u{065d}', '\u{065e}', '\u{06d6}', '\u{06d7}', '\u{06d8}', '\u{06d9}', '\u{06da}', '\u{06db}', '\u{06dc}', '\u{06df}',
    '\u{06e0}', '\u{06e1}', '\u{06e2}', '\u{06e4}', '\u{06e7}', '\u{06e8}', '\u{06eb}', '\u{06ec}', '\u{0730}', '\u{0732}', '\u{0733}',
    '\u{0735}', '\u{0736}', '\u{073a}', '\u{073d}', '\u{073f}', '\u{0740}', '\u{0741}', '\u{0743}', '\u{0745}', '\u{0747}', '\u{0749}',
    '\u{074a}', '\u{07eb}', '\u{07ec}', '\u{07ed}', '\u{07ee}', '\u{07ef}', '\u{07f0}', '\u{07f1}', '\u{07f3}', '\u{0816}', '\u{0817}',
    '\u{0818}', '\u{0819}', '\u{081b}', '\u{081c}', '\u{081d}', '\u{081e}', '\u{081f}', '\u{0820}', '\u{0821}', '\u{0822}', '\u{0823}',
    '\u{0825}', '\u{0826}', '\u{0827}', '\u{0829}', '\u{082a}', '\u{082b}', '\u{082c}', '\u{082d}', '\u{0951}', '\u{0953}', '\u{0954}',
    '\u{0f82}', '\u{0f83}', '\u{0f86}', '\u{0f87}', '\u{135d}', '\u{135e}', '\u{135f}', '\u{17dd}', '\u{193a}', '\u{1a17}', '\u{1a75}',
    '\u{1a76}', '\u{1a77}', '\u{1a78}', '\u{1a79}', '\u{1a7a}', '\u{1a7b}', '\u{1a7c}', '\u{1b6b}', '\u{1b6d}', '\u{1b6e}', '\u{1b6f}',
    '\u{1b70}', '\u{1b71}', '\u{1b72}', '\u{1b73}', '\u{1cd0}', '\u{1cd1}', '\u{1cd2}', '\u{1cda}', '\u{1cdb}', '\u{1ce0}', '\u{1dc0}',
    '\u{1dc1}', '\u{1dc3}', '\u{1dc4}', '\u{1dc5}', '\u{1dc6}', '\u{1dc7}', '\u{1dc8}', '\u{1dc9}', '\u{1dcb}', '\u{1dcc}', '\u{1dd1}',
    '\u{1dd2}', '\u{1dd3}', '\u{1dd4}', '\u{1dd5}', '\u{1dd6}', '\u{1dd7}', '\u{1dd8}', '\u{1dd9}', '\u{1dda}', '\u{1ddb}', '\u{1ddc}',
    '\u{1ddd}', '\u{1dde}', '\u{1ddf}', '\u{1de0}', '\u{1de1}', '\u{1de2}', '\u{1de3}', '\u{1de4}', '\u{1de5}', '\u{1de6}', '\u{1dfe}',
    '\u{20d0}', '\u{20d1}', '\u{20d4}', '\u{20d5}', '\u{20d6}', '\u{20d7}', '\u{20db}', '\u{20dc}', '\u{20e1}', '\u{20e7}', '\u{20e9}',
    '\u{20f0}', '\u{2cef}', '\u{2cf0}', '\u{2cf1}', '\u{2de0}', '\u{2de1}', '\u{2de2}', '\u{2de3}', '\u{2de4}', '\u{2de5}', '\u{2de6}',
    '\u{2de7}', '\u{2de8}', '\u{2de9}', '\u{2dea}', '\u{2deb}', '\u{2dec}', '\u{2ded}', '\u{2dee}', '\u{2def}', '\u{2df0}', '\u{2df1}',
    '\u{2df2}', '\u{2df3}', '\u{2df4}', '\u{2df5}', '\u{2df6}', '\u{2df7}', '\u{2df8}', '\u{2df9}', '\u{2dfa}', '\u{2dfb}', '\u{2dfc}',
    '\u{2dfd}', '\u{2dfe}', '\u{2dff}', '\u{a66f}', '\u{a67c}', '\u{a67d}', '\u{a6f0}', '\u{a6f1}', '\u{a8e0}', '\u{a8e1}', '\u{a8e2}',
    '\u{a8e3}', '\u{a8e4}', '\u{a8e5}', '\u{a8e6}', '\u{a8e7}', '\u{a8e8}', '\u{a8e9}', '\u{a8ea}', '\u{a8eb}', '\u{a8ec}', '\u{a8ed}',
    '\u{a8ee}', '\u{a8ef}', '\u{a8f0}', '\u{a8f1}', '\u{aab0}', '\u{aab2}', '\u{aab3}', '\u{aab7}', '\u{aab8}', '\u{aabe}', '\u{aabf}',
    '\u{aac1}', '\u{fe20}', '\u{fe21}', '\u{fe22}', '\u{fe23}', '\u{fe24}', '\u{fe25}', '\u{fe26}', '\u{10a0f}', '\u{10a38}',
    '\u{1d185}', '\u{1d186}', '\u{1d187}', '\u{1d188}', '\u{1d189}', '\u{1d1aa}', '\u{1d1ab}', '\u{1d1ac}', '\u{1d1ad}', '\u{1d242}',
    '\u{1d243}', '\u{1d244}',
];

fn command(index: usize, chunk: &str, is_last: bool, first_keys: &str) -> String {
    let more = u8::from(!is_last);
    let keys = if index == 0 { format!("{first_keys},m={more}") } else { format!("q=2,m={more}") };
    format!("\x1b_G{keys};{chunk}\x1b\\")
}

/// The PNG in base64, which is ASCII, so cutting it every `CHUNK_BYTES` bytes always lands between characters.
fn commands(png: &[u8], first_keys: &str) -> Vec<String> {
    let encoded = STANDARD.encode(png);
    let starts = (0..encoded.len()).step_by(CHUNK_BYTES);
    let last = starts.len().saturating_sub(1);
    starts
        .enumerate()
        .map(|(index, start)| command(index, &encoded[start..encoded.len().min(start + CHUNK_BYTES)], index == last, first_keys))
        .collect()
}

fn chunked(png: &[u8], first_keys: &str) -> String {
    commands(png, first_keys).concat()
}

/// The layer the picture is drawn on: under its concealed text when it has some, over the text otherwise.
pub fn z_index(picture: &Picture) -> i32 {
    if picture.concealed_text.is_some() { UNDER_TEXT } else { 0 }
}

/// Draws the `crop` of the picture, all of it when `None`, at the cursor into its columns and `rows` rows with `C=1`,
/// cursor unmoved: where kitty, Ghostty and WezTerm would otherwise leave it differs.
pub fn draw(picture: &Picture, rows: u16, crop: Option<Crop>) -> String {
    let source = crop.map(|Crop { x, y, width, height }| format!(",x={x},y={y},w={width},h={height}")).unwrap_or_default();
    chunked(&picture.png, &format!("a=T,f=100,q=2,C=1{source},c={},r={rows},z={}", picture.cols, z_index(picture)))
}

/// An image id a 256-colour placeholder can carry, the low byte as its colour and the high byte as a third diacritic,
/// so it survives a tmux that downgrades true colour; `pager_id`'s never have their low 24 bits under 256. Taken
/// from the PNG: the same diagram shares one image, and pictures left in the scrollback keep theirs.
pub fn placeholder_id(png: &[u8]) -> u32 {
    let hash = png.iter().fold(0x811c_9dc5_u32, |hash, &byte| (hash ^ u32::from(byte)).wrapping_mul(0x0100_0193));
    let low = hash % 255 + 1;
    let high = (hash >> 8) & 0xff;
    (high << 24) | low
}

/// Set in the low 24 bits of every true-colour pager id, which stay below 256 in a printed picture's (`placeholder_id`).
const PAGER_MARK: u32 = 1 << 23;

/// The id through tmux of the picture at `index` in the pager of process `pid`, `None` past the 256th: the index as the
/// high byte, and every tmux pane shares the terminal outside, so the low bits follow the process. With true colour
/// they are the process id under `PAGER_MARK`: process ids stay below 2^22 (Linux's limit; macOS and the BSDs stop
/// under 100 000), so two live pagers never share an id, and the mark keeps them clear of printed pictures. A tmux
/// that downgrades true colour only carries a 256-colour index, so without it the low byte is the process id modulo
/// 255, plus one: two pagers, or a pager and a printed picture, may then share an id.
pub fn pager_id(pid: u32, index: usize, color: ColorDepth) -> Option<u32> {
    let number = u32::from(u8::try_from(index).ok()?) << 24;
    let low = if color == ColorDepth::TrueColor { PAGER_MARK | (pid & (PAGER_MARK - 1)) } else { pid % 255 + 1 };
    Some(number | low)
}

/// Sends the picture through tmux under `id` with a virtual placement of its cell box, for placeholder cells to show.
pub fn store_through_tmux(picture: &Picture, id: u32) -> String {
    let keys = format!("a=T,U=1,f=100,i={id},c={},r={},q=2", picture.cols, picture.rows.max(1));
    commands(&picture.png, &keys).iter().map(|command| tmux::passthrough(command)).collect()
}

/// The foreground that carries the id's low 24 bits: a 256-colour index when they fit one, a true colour otherwise.
fn id_color(id: u32) -> String {
    match id.to_be_bytes() {
        [_, 0, 0, index] => format!("5;{index}"),
        [_, red, green, blue] => format!("2;{red};{green};{blue}"),
    }
}

/// Row `row` of the picture `id` as `cols` placeholder cells: the first names the row, column 0 and the id's high
/// byte, the others follow on. `None` past the last row the diacritics can number.
pub fn placeholder_row(id: u32, row: usize, cols: u16) -> Option<String> {
    let row_mark = DIACRITICS.get(row)?;
    let [high, ..] = id.to_be_bytes();
    let high_mark = DIACRITICS[usize::from(high)];
    let rest = PLACEHOLDER.to_string().repeat(usize::from(cols.saturating_sub(1)));
    Some(format!("\x1b[38;{}m{PLACEHOLDER}{row_mark}{}{high_mark}{rest}\x1b[39m", id_color(id), DIACRITICS[0]))
}

/// Draws the picture inside tmux: sent once through it, then shown by rows of placeholder cells after `indent`, which
/// tmux keeps, scrolls and redraws like any text.
pub fn draw_through_tmux(picture: &Picture, indent: &str) -> Option<String> {
    let id = placeholder_id(&picture.png);
    let rows = (0..usize::from(picture.rows.max(1))).map(|row| Some(format!("{indent}{}\n", placeholder_row(id, row, picture.cols)?)));
    Some(store_through_tmux(picture, id) + &rows.collect::<Option<String>>()?)
}

/// A stored picture drawn at the cursor: the `crop` of its PNG scaled into `cols`×`rows` cells, on layer `z`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Placement {
    pub id: u32,
    pub crop: Crop,
    pub cols: u16,
    pub rows: u16,
    pub z: i32,
}

/// Sends the PNG once under `id` without drawing it, so frames can place it again and again.
pub fn store(png: &[u8], id: u32) -> String {
    chunked(png, &format!("a=t,f=100,i={id},q=2"))
}

/// Draws a stored picture at the cursor, leaving the cursor where it was. Placing the same image and placement id
/// again replaces the previous placement.
pub fn place(placement: &Placement) -> String {
    let Placement { id, crop: Crop { x, y, width, height }, cols, rows, z } = *placement;
    format!("\x1b_Ga=p,i={id},p={PLACEMENT_ID},x={x},y={y},w={width},h={height},c={cols},r={rows},z={z},C=1,q=2\x1b\\")
}

/// Keeps the picture's data, so it can be placed again without resending it.
pub fn hide(id: u32) -> String {
    format!("\x1b_Ga=d,d=i,i={id},q=2\x1b\\")
}

pub fn free(id: u32) -> String {
    format!("\x1b_Ga=d,d=I,i={id},q=2\x1b\\")
}

#[cfg(test)]
mod tests {
    use super::*;

    fn sample(png: Vec<u8>) -> Picture {
        Picture { png, cols: 40, rows: 3, alt: "flow".to_owned(), indent: crate::document::Line::blank(), concealed_text: None }
    }

    #[test]
    fn a_small_picture_is_one_final_chunk() {
        assert_eq!(draw(&sample(vec![1, 2, 3]), 3, None), "\x1b_Ga=T,f=100,q=2,C=1,c=40,r=3,z=0,m=0;AQID\x1b\\");
    }

    #[test]
    fn a_picture_with_concealed_text_is_drawn_under_the_text() {
        let title = Picture { concealed_text: Some("Title".to_owned()), ..sample(vec![1, 2, 3]) };

        assert_eq!(draw(&title, 3, None), "\x1b_Ga=T,f=100,q=2,C=1,c=40,r=3,z=-1,m=0;AQID\x1b\\");
    }

    #[test]
    fn a_large_picture_is_split_in_4096_byte_chunks() {
        let out = draw(&sample(vec![0; 3 * 4096]), 3, None);
        let commands: Vec<&str> = out.split("\x1b_G").skip(1).collect();

        assert_eq!(commands.len(), 4);
        assert!(commands[0].starts_with("a=T,f=100,q=2,C=1,c=40,r=3,z=0,m=1;"));
        assert!(commands[1].starts_with("q=2,m=1;"));
        assert!(commands[3].starts_with("q=2,m=0;"));
        let payload = |command: &str| command.split([';', '\x1b']).nth(1).map_or(0, str::len);
        assert_eq!(commands.iter().map(|command| payload(command)).collect::<Vec<_>>(), [4096, 4096, 4096, 4096]);
    }

    fn readable(sequence: &str) -> String {
        sequence.replace('\x1b', "␛")
    }

    #[test]
    fn the_pager_commands_follow_the_kitty_protocol() {
        let placement = Placement { id: 7, crop: Crop { x: 0, y: 40, width: 400, height: 60 }, cols: 40, rows: 3, z: -1 };

        let commands = [store(&[1, 2, 3], 7), place(&placement), hide(7), free(7)].map(|command| readable(&command));

        insta::assert_snapshot!(commands.join("\n"));
    }

    #[test]
    fn a_placeholder_row_names_its_row_column_and_id_on_the_first_cell_only() {
        let id = 0x0700_002a;

        assert_eq!(placeholder_row(id, 2, 3), Some("\x1b[38;5;42m\u{10eeee}\u{30e}\u{305}\u{33f}\u{10eeee}\u{10eeee}\x1b[39m".to_owned()));
        assert_eq!(placeholder_row(id, 297, 3), None);
    }

    #[test]
    fn an_id_past_a_256_colour_index_is_carried_in_true_colour() {
        assert_eq!(placeholder_row(0x0381_1497, 0, 1), Some("\x1b[38;2;129;20;151m\u{10eeee}\u{305}\u{305}\u{310}\x1b[39m".to_owned()));
    }

    #[test]
    fn placeholder_ids_fit_a_256_colour_cell_and_follow_the_png() {
        let ids = [placeholder_id(&[1, 2, 3]), placeholder_id(&[1, 2, 4]), placeholder_id(&[])];

        assert!(ids.iter().all(|id| (1..=255).contains(&(id & 0xff)) && id & 0x00ff_ff00 == 0), "{ids:x?}");
        assert_eq!(placeholder_id(&[1, 2, 3]), ids[0]);
        assert_ne!(ids[0], ids[1]);
    }

    #[test]
    fn a_pager_id_is_the_picture_number_over_the_marked_process_id() {
        assert_eq!(pager_id(70_807, 0, ColorDepth::TrueColor), Some(0x0081_1497));
        assert_eq!(pager_id(70_807, 3, ColorDepth::TrueColor), Some(0x0381_1497));
        assert_eq!(pager_id((1 << 22) - 1, 255, ColorDepth::TrueColor), Some(0xffbf_ffff));
        assert_eq!(pager_id(70_807, 256, ColorDepth::TrueColor), None);
    }

    #[test]
    fn pager_ids_never_meet_between_processes_pictures_or_printed_pictures() {
        let pids = [1, 2, 255, 256, 23_668, 70_807, 99_998, (1 << 22) - 1];
        let ids: Vec<u32> =
            pids.iter().flat_map(|&pid| (0..=255).filter_map(move |index| pager_id(pid, index, ColorDepth::TrueColor))).collect();
        let distinct: std::collections::BTreeSet<u32> = ids.iter().copied().collect();

        assert_eq!(distinct.len(), pids.len() * 256);
        assert!(ids.iter().all(|id| id & 0x00ff_ffff > 0xff), "a printed id keeps its low 24 bits under 256");
        assert!(placeholder_id(&[1, 2, 3]) & 0x00ff_ffff <= 0xff);
    }

    #[test]
    fn without_true_colour_a_pager_id_fits_a_256_colour_cell() {
        let ids: Vec<u32> = (0..3).filter_map(|index| pager_id(70_807, index, ColorDepth::Ansi256)).collect();

        assert_eq!(ids, [0x0000_00ad, 0x0100_00ad, 0x0200_00ad]);
        assert_eq!(pager_id(254, 0, ColorDepth::Ansi256), Some(0xff));
        assert_eq!(pager_id(255, 0, ColorDepth::Ansi256), Some(1));
    }

    #[test]
    fn inside_tmux_every_chunk_passes_through_and_each_row_is_placeholders() {
        let drawn = draw_through_tmux(&sample(vec![0; 4096]), "  ").unwrap_or_default();
        let id = placeholder_id(&[0; 4096]);

        assert_eq!(drawn.matches("\x1bPtmux;\x1b\x1b_G").count(), 2, "{drawn:?}");
        assert!(drawn.contains(&format!("\x1bPtmux;\x1b\x1b_Ga=T,U=1,f=100,i={id},c=40,r=3,q=2,m=1;")));
        assert_eq!(drawn.matches('\n').count(), 3);
        assert!(drawn.ends_with(&format!("  {}\n", placeholder_row(id, 2, 40).unwrap_or_default())));
    }

    #[test]
    fn a_stored_picture_is_chunked_with_its_keys_on_the_first_chunk_only() {
        let out = store(&vec![0; 3 * 4096], 7);
        let commands: Vec<&str> = out.split("\x1b_G").skip(1).collect();

        assert_eq!(commands.len(), 4);
        assert!(commands[0].starts_with("a=t,f=100,i=7,q=2,m=1;"));
        assert!(commands[1].starts_with("q=2,m=1;"));
        assert!(commands[3].starts_with("q=2,m=0;"));
    }
}