text_utils/utils/
text_align.rs1pub fn dedent(text: impl AsRef<str>) -> String {
3 textwrap::dedent(text.as_ref())
4}
5
6pub fn indent(text: impl AsRef<str>, space: usize) -> String {
8 textwrap::indent(text.as_ref(), &" ".repeat(space))
9}
10
11pub fn indent_with(text: impl AsRef<str>, prefix: &str) -> String {
13 textwrap::indent(text.as_ref(), prefix)
14}
15
16pub fn dedent_less_than(text: impl AsRef<str>, max: usize) -> String {
18 text.as_ref()
20 .lines()
21 .map(|line| {
22 let mut max = max;
23 line.chars()
24 .skip_while(|c| {
26 if max == 0 {
27 false
28 }
29 else {
30 max -= 1;
31 c.is_whitespace()
32 }
33 })
34 .collect::<String>()
35 })
36 .collect::<Vec<_>>()
37 .join("\n")
38}
39
40pub fn indent_count(text: impl AsRef<str>) -> usize {
42 let mut spaces = 0;
43 for c in text.as_ref().chars() {
44 match c {
45 ' ' => spaces += 1,
46 _ => break,
47 }
48 }
49 return spaces;
50}