relay-cli 0.1.2

CLI interface for Relay Agents.
const BORDER: usize = 2;

pub fn print(data: impl AsRef<[u8]>) {
    let code = qrcode::QrCode::new(data.as_ref()).unwrap();
    let w = code.width();
    let h = w;
    let colors = code.to_colors();

    let total_w = w + BORDER * 2;
    let total_h = h + BORDER * 2;

    for y_pair in (0..total_h).step_by(2) {
        for x in 0..total_w {
            let top_y = y_pair;
            let bot_y = y_pair + 1;

            let top = cell_color(&colors, w, h, x, top_y);
            let bot = cell_color(&colors, w, h, x, bot_y);

            let ch = match (top, bot) {
                (qrcode::Color::Dark, qrcode::Color::Dark) => '',
                (qrcode::Color::Dark, qrcode::Color::Light) => '',
                (qrcode::Color::Light, qrcode::Color::Dark) => '',
                (qrcode::Color::Light, qrcode::Color::Light) => ' ',
            };
            print!("{ch}");
        }
        println!();
    }
}

fn cell_color(colors: &[qrcode::Color], w: usize, h: usize, x: usize, y: usize) -> qrcode::Color {
    if x < BORDER || y < BORDER || x >= w + BORDER || y >= h + BORDER {
        qrcode::Color::Light
    } else {
        colors[(y - BORDER) * w + (x - BORDER)]
    }
}