pub fn dedent(text: impl AsRef<str>) -> String {
textwrap::dedent(text.as_ref())
}
pub fn indent(text: impl AsRef<str>, space: usize) -> String {
textwrap::indent(text.as_ref(), &" ".repeat(space))
}
pub fn indent_with(text: impl AsRef<str>, prefix: &str) -> String {
textwrap::indent(text.as_ref(), prefix)
}
pub fn dedent_less_than(text: impl AsRef<str>, max: usize) -> String {
text.as_ref()
.lines()
.map(|line| {
let mut max = max;
line.chars()
.skip_while(|c| {
if max == 0 {
false
}
else {
max -= 1;
c.is_whitespace()
}
})
.collect::<String>()
})
.collect::<Vec<_>>()
.join("\n")
}
pub fn indent_count(text: impl AsRef<str>) -> usize {
let mut spaces = 0;
for c in text.as_ref().chars() {
match c {
' ' => spaces += 1,
_ => break,
}
}
return spaces;
}