pub use super::*;
pub fn chop( text: &str, width: usize) -> Vec<String> {
let mut lines = Vec::new();
let mut line = String::new();
let mut line_width = 0;
for word in text.split_whitespace() {
let word_width = word.chars().count();
if line_width + word_width + 1 > width {
if !line.is_empty() {
lines.push(line);
line = String::new();
line_width = 0;
}
if word_width > width {
for (i, c) in word.chars().enumerate() {
if i > 0 && i % width == 0 {
lines.push(line);
line = String::new();
}
line.push(c);
}
lines.push(line);
line = String::new();
line_width = 0;
} else {
line = word.to_string();
line_width = word_width;
}
} else {
if !line.is_empty() {
line.push(' ');
line_width += 1;
}
line.push_str(word);
line_width += word_width;
}
}
if !line.is_empty() {
lines.push(line);
}
lines
}
pub fn wrap(text: &str, width: usize) -> String {
let mut lines = chop(text, width);
lines.join("\n")
}
pub fn fill(text: &str, width: usize, filler: char) -> String {
let mut line = text.to_string();
while line.len() < width {
line.push(filler);
}
line
}
pub fn center(text: &str, width: usize, filler: char) -> String {
let mut line = text.to_string();
let spaces = width - line.len();
let left_spaces = spaces / 2;
let right_spaces = spaces - left_spaces;
line.insert(0, filler);
line
}
pub fn random_char() -> char {
random::random_char()
}
pub fn random_word(length:usize) -> String {
random::random_ascii_string(length)
}
pub fn random_sentence(length:usize) -> String {
let mut sentence = String::new();
for _ in 0..length {
sentence.push(random_char());
}
sentence
}