rustimal 0.4.0

A minimal, GPU-rendered terminal for Windows that runs Git Bash, with animated split panes and a glass look
//! Embeds the app icon into the Windows executable.

#[path = "src/icon.rs"]
mod icon;

use std::io::Write;
use std::path::PathBuf;

fn main() {
    println!("cargo:rerun-if-changed=src/icon.rs");
    println!("cargo:rerun-if-changed=build.rs");
    if std::env::var("CARGO_CFG_TARGET_OS").as_deref() != Ok("windows") {
        return;
    }

    let out = PathBuf::from(std::env::var("OUT_DIR").unwrap());
    let ico = out.join("rustimal.ico");
    write_ico(&ico, &[16, 20, 24, 32, 40, 48, 64, 256]).expect("write icon");

    let rc = out.join("rustimal.rc");
    let ico_path = ico.to_string_lossy().replace('\\', "\\\\");
    std::fs::write(&rc, format!("1 ICON \"{ico_path}\"\n")).expect("write rc");
    embed_resource::compile(&rc, embed_resource::NONE).manifest_optional().expect("compile icon resource");
}

/// Write an .ico with uncompressed 32-bit BMP entries.
fn write_ico(path: &PathBuf, sizes: &[u32]) -> std::io::Result<()> {
    let images: Vec<Vec<u8>> = sizes.iter().map(|&s| bmp_entry(s)).collect();
    let mut f = std::fs::File::create(path)?;
    f.write_all(&[0, 0, 1, 0])?;
    f.write_all(&(sizes.len() as u16).to_le_bytes())?;
    let mut offset = 6 + 16 * sizes.len() as u32;
    for (s, img) in sizes.iter().zip(&images) {
        let dim = if *s >= 256 { 0 } else { *s as u8 };
        f.write_all(&[dim, dim, 0, 0])?;
        f.write_all(&1u16.to_le_bytes())?; // planes
        f.write_all(&32u16.to_le_bytes())?; // bpp
        f.write_all(&(img.len() as u32).to_le_bytes())?;
        f.write_all(&offset.to_le_bytes())?;
        offset += img.len() as u32;
    }
    for img in &images {
        f.write_all(img)?;
    }
    Ok(())
}

fn bmp_entry(size: u32) -> Vec<u8> {
    let rgba = icon::render(size);
    let mut v = Vec::new();
    // BITMAPINFOHEADER; height is doubled to account for the (empty) AND mask.
    v.extend_from_slice(&40u32.to_le_bytes());
    v.extend_from_slice(&(size as i32).to_le_bytes());
    v.extend_from_slice(&((size * 2) as i32).to_le_bytes());
    v.extend_from_slice(&1u16.to_le_bytes());
    v.extend_from_slice(&32u16.to_le_bytes());
    v.extend_from_slice(&[0u8; 24]);
    // Pixels bottom-up, BGRA.
    for y in (0..size).rev() {
        for x in 0..size {
            let i = ((y * size + x) * 4) as usize;
            v.extend_from_slice(&[rgba[i + 2], rgba[i + 1], rgba[i], rgba[i + 3]]);
        }
    }
    // AND mask: 1 bit per pixel, rows padded to 32 bits; all zero (alpha is used).
    let row = size.div_ceil(32) * 4;
    v.extend(std::iter::repeat_n(0u8, (row * size) as usize));
    v
}