use std::io::{self, Write};
use crate::style::*;
pub const INDENT: &str = " ";
pub const GUTTER: &str = "│";
pub const GAP: &str = "┆";
pub const FOOT: &str = "╰";
pub const GUTTER_WIDTH: usize = 4;
pub fn truncate_ellipsis(s: &str, max: usize) -> String {
if s.char_indices().nth(max).is_none() {
return s.to_string();
}
let kept: String = s.chars().take(max.saturating_sub(1)).collect();
kept + "…"
}
fn band_line_on<W: Write>(w: &mut W, bg: &str, styled: &str) -> io::Result<()> {
writeln!(w, "{bg}{INDENT}{styled}{bg}{FILL_EOL}{RESET}")
}
pub fn write_band_on<W: Write>(w: &mut W, bg: &str, segments: &[(&str, &str)]) -> io::Result<()> {
let mut styled = String::new();
for (text, style) in segments {
styled.push_str(&format!("{style}{text}{RESET}{bg}"));
}
band_line_on(w, bg, &styled)
}
pub fn write_band<W: Write>(w: &mut W, segments: &[(&str, &str)]) -> io::Result<()> {
write_band_on(w, BG_BAND, segments)
}
pub fn gutter_line<W: Write>(w: &mut W, glyph: &str, styled: &str) -> io::Result<()> {
writeln!(w, "{INDENT}{FG_FAINT}{glyph}{RESET} {styled}{RESET}")
}
pub fn status_line<W: Write>(w: &mut W, glyph: &str, color: &str, text: &str) -> io::Result<()> {
writeln!(w, "{INDENT}{color}{glyph}{RESET} {FG_MUTED}{text}{RESET}")
}
pub fn title_case(name: &str) -> String {
let sentence = name
.split(['_', '-'])
.filter(|w| !w.is_empty())
.map(str::to_lowercase)
.collect::<Vec<_>>()
.join(" ");
let mut chars = sentence.chars();
match chars.next() {
Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
None => String::new(),
}
}
#[cfg(test)]
mod tests {
use super::*;
fn strip_ansi(s: &str) -> String {
let re = regex::Regex::new(r"\x1b\[[0-9;]*[mK]").unwrap();
re.replace_all(s, "").to_string()
}
fn render_gutter(glyph: &str, text: &str) -> String {
let mut buf = Vec::new();
gutter_line(&mut buf, glyph, &format!("{FG_FAINT}{text}{RESET}")).unwrap();
String::from_utf8(buf).unwrap()
}
#[test]
fn gutter_row_is_indented_and_reset() {
let out = render_gutter(GUTTER, "hello");
assert_eq!(strip_ansi(&out), " │ hello\n");
assert!(out.ends_with(&format!("{RESET}\n")));
}
#[test]
fn gutter_width_matches_the_rendered_prefix() {
let out = render_gutter(GUTTER, "");
let prefix = strip_ansi(&out);
assert_eq!(prefix.chars().count(), GUTTER_WIDTH + 1);
}
#[test]
fn band_fills_to_end_of_line() {
let mut buf = Vec::new();
write_band(&mut buf, &[("title", FG_BAND)]).unwrap();
let out = String::from_utf8(buf).unwrap();
assert_eq!(strip_ansi(&out), " title\n");
assert!(out.contains(BG_BAND));
assert!(out.contains(FILL_EOL));
}
#[test]
fn segmented_band_keeps_the_background_continuous() {
let mut buf = Vec::new();
write_band(&mut buf, &[("●", FG_OK), (" run", FG_MUTED)]).unwrap();
let out = String::from_utf8(buf).unwrap();
assert_eq!(strip_ansi(&out), " ● run\n");
let chunks: Vec<&str> = out.split("\x1b[0m").collect();
for chunk in &chunks[1..chunks.len() - 1] {
assert!(
chunk.starts_with(BG_BAND) || chunk.starts_with(FILL_EOL),
"background must reopen after each segment, got {chunk:?}"
);
}
}
#[test]
fn title_case_humanizes_identifiers() {
assert_eq!(title_case("deploy_to_staging"), "Deploy to staging");
assert_eq!(title_case("run"), "Run");
assert_eq!(title_case("my-tool"), "My tool");
assert_eq!(title_case("__"), "");
}
#[test]
fn truncate_ellipsis_is_char_boundary_safe() {
let s = "żółw".repeat(50);
let t = truncate_ellipsis(&s, 10);
assert!(t.ends_with('…'));
assert_eq!(t.chars().count(), 10);
}
#[test]
fn truncate_ellipsis_leaves_short_text_alone() {
assert_eq!(truncate_ellipsis("short", 10), "short");
assert_eq!(truncate_ellipsis("exact", 5), "exact");
}
}