Skip to content

Commit

Permalink
Add format_bulleted_list_multiline (#26)
Browse files Browse the repository at this point in the history
For #21
  • Loading branch information
9999years authored Oct 15, 2024
1 parent 1a890e1 commit e5b9209
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/format_bulleted_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,82 @@ pub fn format_bulleted_list(items: impl IntoIterator<Item = impl Display>) -> St
format!("• {}", items.join("\n• "))
}
}

/// Like [`format_bulleted_list`], except the second and subsequent lines of multi-line items are
/// indented as well.
pub fn format_bulleted_list_multiline(items: impl IntoIterator<Item = impl Display>) -> String {
format_bulleted_list(items.into_iter().map(|item| {
let item = item.to_string();
let mut lines = item.lines().peekable();
match lines.next() {
None => {
// ???
String::new()
}
Some(first) => {
if lines.peek().is_none() {
// One line.
item
} else {
// Two or more lines.
format!("{first}\n {}", lines.join("\n "))
}
}
}
}))
}

#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
use pretty_assertions::assert_eq;

#[test]
fn test_format_bulleted_list() {
assert_eq!(format_bulleted_list(Vec::<String>::new()), "");

assert_eq!(format_bulleted_list(["puppy"]), "• puppy");
assert_eq!(
format_bulleted_list(["puppy", "doggy"]),
indoc!(
"
• puppy
• doggy"
)
);
}

#[test]
fn test_format_bulleted_list_multiline() {
assert_eq!(format_bulleted_list_multiline(Vec::<String>::new()), "");

assert_eq!(format_bulleted_list_multiline(["puppy"]), "• puppy");
assert_eq!(
format_bulleted_list_multiline(["puppy", "doggy"]),
indoc!(
"
• puppy
• doggy"
)
);

assert_eq!(
format_bulleted_list_multiline([
"puppy\ndoggy",
"sammy\ngoldie",
&format_bulleted_list_multiline(["ears", "tail", "fetch!"])
]),
indoc!(
"
• puppy
doggy
• sammy
goldie
• • ears
• tail
• fetch!"
)
);
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub use app::App;
pub use app_git::AppGit;
pub use config::Config;
pub use format_bulleted_list::format_bulleted_list;
pub use format_bulleted_list::format_bulleted_list_multiline;
pub use git::repository_url_destination;
pub use git::AddWorktreeOpts;
pub use git::BranchRef;
Expand Down

0 comments on commit e5b9209

Please sign in to comment.