use crate::compiler::COUNT_SLOTS;
use crate::corpus::{CHAPTERS, CORPUS};
pub const TEX_COMPAT_VERSION: &str = "3.141592653";
pub const TEX_ENGINE: &str = "texrs";
pub fn platform() -> String {
format!("{}-{}", std::env::consts::ARCH, std::env::consts::OS)
}
pub fn version_banner() -> String {
format!(
"texrs {} (TeX {} mouth+expander) [{}]",
env!("CARGO_PKG_VERSION"),
TEX_COMPAT_VERSION,
platform()
)
}
pub fn visible_width(s: &str) -> usize {
let bytes = s.as_bytes();
let (mut i, mut w) = (0usize, 0usize);
while i < bytes.len() {
if bytes[i] == 0x1B && i + 1 < bytes.len() && bytes[i + 1] == b'[' {
i += 2;
while i < bytes.len() && !(0x40..=0x7E).contains(&bytes[i]) {
i += 1;
}
i += 1;
} else {
let step = std::str::from_utf8(&bytes[i..])
.ok()
.and_then(|s| s.chars().next())
.map(|c| c.len_utf8())
.unwrap_or(1);
w += 1;
i += step;
}
}
w
}
pub fn render_banner(colored: bool) -> String {
let version = env!("CARGO_PKG_VERSION");
let cores = std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1);
let os = std::env::consts::OS;
let arch = std::env::consts::ARCH;
let pid = std::process::id();
let primitives = CORPUS.len();
let chapters = CHAPTERS.len();
let registers = COUNT_SLOTS;
let cached = crate::script_cache::default_cache_path();
let cached = match crate::script_cache::cache_enabled() {
true => format!("{}", cached.display()),
false => "off (TEXRS_CACHE)".to_string(),
};
let (c, m, r, y, g, n) = match colored {
true => (
"\x1b[36m", "\x1b[35m", "\x1b[31m", "\x1b[33m", "\x1b[32m", "\x1b[0m",
),
false => ("", "", "", "", "", ""),
};
const INNER: usize = 64;
let mut out = String::with_capacity(2048);
let row = |out: &mut String, body: &str| {
let pad = INNER.saturating_sub(visible_width(body));
out.push_str(&format!("{c} │{n}{body}{:pad$}{c}│{n}\n", "", pad = pad));
};
out.push_str(&format!(
"{c} ████████╗███████╗██╗ ██╗██████╗ ███████╗{n}\n"
));
out.push_str(&format!(
"{c} ╚══██╔══╝██╔════╝╚██╗██╔╝██╔══██╗██╔════╝{n}\n"
));
out.push_str(&format!(
"{m} ██║ █████╗ ╚███╔╝ ██████╔╝███████╗{n}\n"
));
out.push_str(&format!(
"{m} ██║ ██╔══╝ ██╔██╗ ██╔══██╗╚════██║{n}\n"
));
out.push_str(&format!(
"{r} ██║ ███████╗██╔╝ ██╗██║ ██║███████║{n}\n"
));
out.push_str(&format!(
"{r} ╚═╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝{n}\n"
));
out.push_str(&format!(
"{c} ┌────────────────────────────────────────────────────────────────┐{n}\n"
));
row(
&mut out,
&format!(
" {y}SYSTEM{n} status:{g} ONLINE {c}//{n} {y}os:{n} {os} {y}arch:{n} {arch} {y}pid:{n} {pid}"
),
);
row(
&mut out,
&format!(" {y}CORES{n} {cores} {y}ENGINE{n} TeX {TEX_COMPAT_VERSION} mouth+expander"),
);
out.push_str(&format!(
"{c} ├────────────────────────────────────────────────────────────────┤{n}\n"
));
row(
&mut out,
&format!(
" {y}%p{n} primitives {primitives:<5} {y}%c{n} chapters {chapters:<5} {y}%r{n} registers {registers}"
),
);
row(&mut out, &format!(" {y}CACHE{n} {cached}"));
out.push_str(&format!(
"{c} └────────────────────────────────────────────────────────────────┘{n}\n"
));
out.push_str(&format!(
"{m} >> TEX'S MOUTH AND EXPANDER // COMPILED TO BYTECODE v{version} <<{n}\n"
));
out
}
pub fn print_banner(colored: bool) {
print!("{}", render_banner(colored));
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_box_lines_up_in_both_renderings() {
for colored in [false, true] {
let banner = render_banner(colored);
let widths: Vec<usize> = banner
.lines()
.filter(|l| l.contains('│'))
.map(visible_width)
.collect();
assert!(!widths.is_empty(), "no box rows rendered");
assert!(
widths.iter().all(|w| *w == widths[0]),
"ragged box (colored={colored}): {widths:?}"
);
}
}
#[test]
fn the_counts_are_the_real_ones() {
let plain = render_banner(false);
assert!(plain.contains(&CORPUS.len().to_string()));
assert!(plain.contains(&COUNT_SLOTS.to_string()));
assert!(plain.contains(env!("CARGO_PKG_VERSION")));
}
#[test]
fn colour_adds_escapes_and_nothing_else() {
let plain = render_banner(false);
let colored = render_banner(true);
assert!(colored.contains("\x1b["), "no escapes in the coloured form");
let stripped: String = {
let mut out = String::new();
let bytes = colored.as_bytes();
let mut i = 0;
while i < bytes.len() {
if bytes[i] == 0x1B && i + 1 < bytes.len() && bytes[i + 1] == b'[' {
i += 2;
while i < bytes.len() && !(0x40..=0x7E).contains(&bytes[i]) {
i += 1;
}
i += 1;
} else {
out.push(bytes[i] as char);
i += 1;
}
}
out
};
assert_eq!(stripped.lines().count(), plain.lines().count());
}
}