boseiju/utils/
tree_formatter.rs1pub struct TreeFormatter<'out> {
2 output: &'out mut dyn std::io::Write,
3 padding: String,
4}
5
6impl<'out> TreeFormatter<'out> {
7 pub fn new(output: &'out mut dyn std::io::Write, padding_capacity: usize, prefix: &str) -> TreeFormatter<'out> {
8 let mut padding = String::with_capacity(padding_capacity);
9 padding.push_str(prefix);
10 TreeFormatter { output, padding }
11 }
12
13 pub fn push_inter_branch(&mut self) -> std::io::Result<()> {
14 writeln!(self.output, "")?;
15 write!(self.output, "{}├─", self.padding)?;
16 self.padding.push_str("│ ");
17 Ok(())
18 }
19
20 pub fn next_inter_branch(&mut self) -> std::io::Result<()> {
21 self.pop_branch();
22 self.push_inter_branch()
23 }
24
25 pub fn push_final_branch(&mut self) -> std::io::Result<()> {
26 writeln!(self.output, "")?;
27 write!(self.output, "{}╰─", self.padding)?;
28 self.padding.push_str(" ");
29 Ok(())
30 }
31
32 pub fn next_final_branch(&mut self) -> std::io::Result<()> {
33 self.pop_branch();
34 self.push_final_branch()
35 }
36
37 pub fn new_line(&mut self) -> std::io::Result<()> {
38 writeln!(self.output, "")?;
39 write!(self.output, "{}", self.padding)?;
40 Ok(())
41 }
42
43 pub fn pop_branch(&mut self) {
44 self.padding.pop();
45 self.padding.pop();
46 }
47}
48
49impl<'out> std::io::Write for TreeFormatter<'out> {
50 fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
51 self.output.write(buf)
52 }
53
54 fn flush(&mut self) -> std::io::Result<()> {
55 self.output.flush()
56 }
57}