use console::measure_text_width;
#[inline]
pub fn pad_to_width(text: &str, width: usize) -> String {
let current = measure_text_width(text);
if current >= width {
return text.to_string();
}
let padding = width - current;
let total_capacity = text.len() + padding;
let mut result = String::with_capacity(total_capacity);
result.push_str(text);
result.push_str(&" ".repeat(padding));
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_pad_to_width_short_text() {
let result = pad_to_width("hello", 10);
assert_eq!(result, "hello ");
assert_eq!(result.len(), 10);
}
#[test]
fn test_pad_to_width_exact() {
let result = pad_to_width("exactly10!", 10);
assert_eq!(result, "exactly10!");
}
#[test]
fn test_pad_to_width_already_wide() {
let long = "this is longer than ten";
let result = pad_to_width(long, 10);
assert_eq!(result, long);
}
#[test]
fn test_pad_to_width_empty() {
let result = pad_to_width("", 5);
assert_eq!(result, " ");
}
#[test]
fn test_pad_preserves_capacity() {
let result = pad_to_width("test", 10);
assert!(result.capacity() >= 10);
}
}