use crate::theme::Theme;
pub fn cursor_glyph(theme: &Theme) -> &'static str {
if theme.ascii {
">"
} else {
"❯"
}
}
pub fn wordmark(theme: &Theme) -> &'static str {
if theme.ascii {
"#"
} else {
"▌"
}
}
pub fn dot(theme: &Theme) -> &'static str {
if theme.ascii {
"-"
} else {
"·"
}
}
pub fn panel(theme: &Theme, label: &str, body: &[String], width: usize) -> Vec<String> {
let bc = theme.box_chars();
let content_w = width.saturating_sub(2);
let body_w = content_w.saturating_sub(2);
let corner = format!("ADOS {} {label}", dot(theme));
let mut out = Vec::with_capacity(body.len() + 2);
out.push(theme.accent(&titled_border(bc.tl, bc.tr, bc.h, &corner, content_w)));
let v = theme.accent(bc.v);
for line in body {
out.push(format!("{v} {} {v}", fit_to(line, body_w)));
}
out.push(theme.accent(&plain_border(bc.bl, bc.br, bc.h, content_w)));
out
}
pub fn panel_body_width(width: usize) -> usize {
width.saturating_sub(4)
}
fn titled_border(left: &str, right: &str, h: &str, title: &str, content_w: usize) -> String {
let lead = format!("{} {} ", h, truncate(title, content_w.saturating_sub(4)));
let dashes = content_w.saturating_sub(lead.chars().count());
format!("{left}{lead}{}{right}", h.repeat(dashes))
}
fn plain_border(left: &str, right: &str, h: &str, content_w: usize) -> String {
format!("{left}{}{right}", h.repeat(content_w))
}
pub fn truncate(s: &str, max: usize) -> String {
if s.chars().count() <= max {
return s.to_string();
}
if max == 0 {
return String::new();
}
let mut out: String = s.chars().take(max.saturating_sub(1)).collect();
out.push('…');
out
}
pub fn fit_to(s: &str, w: usize) -> String {
let vis = visible_width(s);
if vis <= w {
format!("{s}{}", " ".repeat(w - vis))
} else {
let plain = truncate(&strip_ansi(s), w);
let pad = w.saturating_sub(plain.chars().count());
format!("{plain}{}", " ".repeat(pad))
}
}
pub fn center(s: &str, w: usize) -> String {
let vis = visible_width(s);
if vis >= w {
return fit_to(s, w);
}
let total = w - vis;
let left = total / 2;
let right = total - left;
format!("{}{s}{}", " ".repeat(left), " ".repeat(right))
}
pub fn visible_width(s: &str) -> usize {
let mut width = 0;
let mut chars = s.chars();
while let Some(c) = chars.next() {
if c == '\u{1b}' {
for nc in chars.by_ref() {
if nc == 'm' {
break;
}
}
} else {
width += 1;
}
}
width
}
pub fn strip_ansi(s: &str) -> String {
let mut out = String::with_capacity(s.len());
let mut chars = s.chars();
while let Some(c) = chars.next() {
if c == '\u{1b}' {
for nc in chars.by_ref() {
if nc == 'm' {
break;
}
}
} else {
out.push(c);
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
use crate::theme::ColorTier;
fn theme() -> Theme {
Theme {
ascii: false,
tier: ColorTier::None,
}
}
#[test]
fn panel_rows_are_exactly_the_width_with_color_off() {
let t = theme();
let body = vec!["hello".to_string(), "world".to_string()];
let width = 50;
for line in panel(&t, "setup", &body, width) {
assert_eq!(
line.chars().count(),
width,
"panel line not exactly {width} cols: {line:?}"
);
}
}
#[test]
fn panel_carries_the_corner_label() {
let t = theme();
let lines = panel(&t, "setup", &["body".to_string()], 50);
assert!(lines.first().unwrap().contains("ADOS"));
assert!(lines.first().unwrap().contains("setup"));
assert!(!lines.last().unwrap().contains("Enter"));
}
#[test]
fn panel_body_width_is_outer_minus_four() {
assert_eq!(panel_body_width(50), 46);
assert_eq!(panel_body_width(3), 0);
}
#[test]
fn center_pads_both_sides_by_visible_width() {
assert_eq!(center("ab", 6), " ab ");
let colored = "\x1b[36m x\x1b[39m"; assert_eq!(visible_width(¢er(colored, 8)), 8);
}
#[test]
fn fit_to_pads_visible_width_ignoring_ansi() {
let colored = "\x1b[36m x\x1b[39m"; let out = fit_to(colored, 6);
assert_eq!(visible_width(&out), 6);
}
#[test]
fn truncate_adds_ellipsis() {
assert_eq!(truncate("hello", 10), "hello");
assert_eq!(truncate("hello world", 5), "hell…");
}
#[test]
fn visible_width_and_strip_ignore_escapes() {
let colored = "\x1b[36m❯\x1b[39m";
assert_eq!(visible_width(colored), 1);
assert_eq!(strip_ansi(colored), "❯");
}
}