pub const LOGO_ROWS: [&str; 6] = [
"███████╗██╗ ██╗███████╗██████╗ ██████╗ ███████╗",
"██╔════╝██║ ██║██╔════╝██╔══██╗██╔══██╗██╔════╝",
"█████╗ ██║ ██║███████╗██████╔╝██████╔╝███████╗",
"██╔══╝ ██║ ██║╚════██║██╔═══╝ ██╔══██╗╚════██║",
"███████╗███████╗██║███████║██║ ██║ ██║███████║",
"╚══════╝╚══════╝╚═╝╚══════╝╚═╝ ╚═╝ ╚═╝╚══════╝",
];
const LOGO_COLORS: [&str; 6] = [
"\x1b[36m", "\x1b[36m", "\x1b[35m", "\x1b[35m", "\x1b[31m", "\x1b[31m",
];
pub fn logo_colored(colored: bool) -> String {
let mut out = String::with_capacity(512);
for (i, row) in LOGO_ROWS.iter().enumerate() {
if colored {
out.push_str(&format!("{} {row}\x1b[0m\n", LOGO_COLORS[i]));
} else {
out.push_str(&format!(" {row}\n"));
}
}
out
}
pub fn visible_width(s: &str) -> usize {
let bytes = s.as_bytes();
let mut i = 0;
let mut w = 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 n_special = crate::lsp::SPECIAL_FORMS.len();
let n_subrs = crate::lsp::SUBRS.len();
let n_builtins = crate::with_host(|h| h.builtin_count());
let (mem_total_gib, mem_avail_gib) = {
use sysinfo::System;
let mut sys = System::new();
sys.refresh_memory();
let total = sys.total_memory() as f64 / 1024.0 / 1024.0 / 1024.0;
let avail = sys.available_memory() as f64 / 1024.0 / 1024.0 / 1024.0;
(total, avail)
};
let os = std::env::consts::OS;
let arch = std::env::consts::ARCH;
let pid = std::process::id();
let (c, m, r, y, g, n) = if colored {
(
"\x1b[36m", "\x1b[35m", "\x1b[31m", "\x1b[33m", "\x1b[32m", "\x1b[0m",
)
} else {
("", "", "", "", "", "")
};
let _ = r;
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(&logo_colored(colored));
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}MEM{n} {mem_avail_gib:.1} {c}/{n} {mem_total_gib:.1} GiB available"
),
);
out.push_str(&format!(
"{c} ├────────────────────────────────────────────────────────────────┤{n}\n"
));
row(
&mut out,
&format!(
" {y}special-forms{n} {n_special:<4} {y}subrs{n} {n_subrs:<4} {y}builtins{n} {n_builtins:<4}"
),
);
out.push_str(&format!(
"{c} └────────────────────────────────────────────────────────────────┘{n}\n"
));
out.push_str(&format!(
"{m} >> EMACS LISP ON FUSEVM // FULL SPECTRUM // v{version} <<{n}\n"
));
out
}
pub fn print_banner(colored: bool) {
print!("{}", render_banner(colored));
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn visible_width_ignores_csi_sequences() {
assert_eq!(visible_width("\x1b[31mabc\x1b[0m"), 3);
assert_eq!(visible_width("\x1b[1;38;5;202mok"), 2);
}
#[test]
fn visible_width_counts_each_char_once_for_multibyte() {
assert_eq!(visible_width("─├┤"), 3);
assert_eq!(visible_width("aé你"), 3);
}
#[test]
fn visible_width_handles_empty_and_lone_escape() {
assert_eq!(visible_width(""), 0);
assert_eq!(visible_width("\x1bz"), 2);
}
#[test]
fn render_banner_plain_has_no_ansi_escapes() {
let s = render_banner(false);
assert!(!s.contains('\x1b'), "plain banner must not contain ESC");
assert!(s.contains("EMACS LISP ON FUSEVM"));
assert!(s.contains(env!("CARGO_PKG_VERSION")));
}
#[test]
fn render_banner_colored_contains_ansi_escapes() {
let s = render_banner(true);
assert!(s.contains("\x1b["));
assert!(s.contains("\x1b[0m"));
}
#[test]
fn logo_rows_are_all_the_same_visible_width() {
let want = visible_width(LOGO_ROWS[0]);
for r in LOGO_ROWS {
assert_eq!(visible_width(r), want, "logo row width drift: {r}");
}
}
#[test]
fn render_banner_rows_all_match_inner_width_after_strip() {
let s = render_banner(false);
let top = s
.lines()
.find(|l| l.starts_with(" ┌"))
.expect("top border present");
let want = visible_width(top);
let mut box_rows = 0;
for line in s.lines() {
if line.starts_with(" │") && line.ends_with('│') {
box_rows += 1;
assert_eq!(
visible_width(line),
want,
"box row width drift on line: {line}"
);
}
}
assert!(box_rows >= 3, "expected several rendered box rows");
}
}