use std::{error::Error, io::Write};
use crate::style::FILL_EOL;
pub const MAX_LINE_WIDTH: usize = 120;
pub fn wrap_line(line: &str, first_avail: usize, rest_avail: usize) -> Vec<String> {
if line.is_empty() {
return vec![String::new()];
}
if line.chars().count() <= first_avail {
return vec![line.to_string()];
}
let mut rows = Vec::new();
let mut remaining = line;
let mut avail = first_avail;
loop {
if remaining.chars().count() <= avail {
rows.push(remaining.to_string());
break;
}
let chunk_end = remaining.floor_char_boundary(avail);
let chunk = &remaining[..chunk_end];
match chunk.rfind(' ') {
Some(pos) if pos > 0 => {
rows.push(chunk[..pos].to_string());
remaining = remaining[pos..].trim_start();
}
_ => {
rows.push(chunk.to_string());
remaining = remaining[chunk_end..].trim_start();
}
}
avail = rest_avail;
}
rows
}
pub fn write_wrapped_lines<W: Write>(
stdout: &mut W,
content: &str,
base_indent: &str,
cont_indent: &str,
max_width: usize,
fill_bg: bool,
) -> Result<(), Box<dyn Error>> {
let suffix = if fill_bg { FILL_EOL } else { "" };
let first_avail = max_width.saturating_sub(base_indent.chars().count());
let rest_avail = max_width.saturating_sub(cont_indent.chars().count());
for line in content.lines() {
for (i, row) in wrap_line(line, first_avail, rest_avail)
.into_iter()
.enumerate()
{
let indent = if i == 0 { base_indent } else { cont_indent };
writeln!(stdout, "{indent}{row}{suffix}")?;
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
fn rows(line: &str, avail: usize) -> Vec<String> {
wrap_line(line, avail, avail)
}
#[test]
fn short_line_is_one_row() {
assert_eq!(rows("hello", 10), vec!["hello"]);
}
#[test]
fn empty_line_yields_one_empty_row() {
assert_eq!(rows("", 10), vec![String::new()]);
}
#[test]
fn wraps_at_word_boundaries() {
assert_eq!(rows("alpha beta gamma", 12), vec!["alpha beta", "gamma"]);
}
#[test]
fn hard_breaks_when_a_word_exceeds_the_budget() {
let out = rows(&"x".repeat(25), 10);
assert_eq!(out.len(), 3);
assert_eq!(out[0], "x".repeat(10));
assert_eq!(out.concat(), "x".repeat(25));
}
#[test]
fn continuation_budget_is_honoured() {
let line = "aaa bbb ccc ddd eee";
let out = wrap_line(line, 7, 12);
assert!(out[0].len() <= 7);
for rest in &out[1..] {
assert!(rest.len() <= 12);
}
assert_eq!(out.join(" ").replace(' ', ""), line.replace(' ', ""));
}
#[test]
fn multibyte_input_never_panics_and_loses_nothing() {
let line = "🎉 ".repeat(10);
let out = wrap_line(&line, 6, 6);
assert_eq!(out.join(" ").replace(' ', ""), line.replace(' ', ""));
}
#[test]
fn writer_pads_every_row_when_requested() {
let mut buf = Vec::new();
write_wrapped_lines(&mut buf, "one two three", " ", " ", 9, true).unwrap();
let out = String::from_utf8(buf).unwrap();
assert_eq!(out.matches(FILL_EOL).count(), out.lines().count());
assert!(out.lines().all(|l| l.len() <= 12), "{out:?}");
}
#[test]
fn writer_keeps_blank_lines_blank() {
let mut buf = Vec::new();
write_wrapped_lines(&mut buf, "a\n\nb", "", "", 80, false).unwrap();
assert_eq!(String::from_utf8(buf).unwrap(), "a\n\nb\n");
}
}