Skip to content

Commit

Permalink
Add zero width support to wrap_text
Browse files Browse the repository at this point in the history
  • Loading branch information
akiomik committed Dec 30, 2023
1 parent 95e61cb commit e14e929
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/text.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use unicode_width::UnicodeWidthStr;

pub fn wrap_text(s: &str, width: usize) -> String {
if width == 0 {
return String::from("");
}

s.chars().fold(String::from(""), |acc: String, c: char| {
let last_line = acc.lines().last().unwrap_or(&acc);
if last_line.width() + c.to_string().width() > width {
Expand Down Expand Up @@ -76,6 +80,13 @@ mod tests {
assert_eq!(actual, expected);
}

#[test]
fn test_wrap_text_zero_width() {
let actual = wrap_text("hello, world!", 0);
let expected = "";
assert_eq!(actual, expected);
}

#[test]
fn test_truncate_text_no_truncate() {
let actual = truncate_text("foo\nbar\nbaz", 3);
Expand Down

0 comments on commit e14e929

Please sign in to comment.