pwd/
pwd.rs

1use log_tree::LogTree;
2use std::{env, fs::read_dir, path::Path};
3
4struct DirTree {
5    path: String,
6    is_dir: bool,
7}
8
9impl LogTree for DirTree {
10    fn add_node(&self, lvl: u8) -> Option<(String, log_tree::Childs<Self>)> {
11        let key = Path::new(&self.path)
12            .file_name()?
13            .to_string_lossy()
14            .into_owned();
15
16        let mut childs = Vec::new();
17
18        if let Some(Ok(dir)) = (lvl < 2).then(|| read_dir(&self.path)) {
19            childs = dir
20                .filter_map(|entry| entry.ok())
21                .map(|entry| DirTree {
22                    path: entry.path().to_str().unwrap().to_string(),
23                    is_dir: entry.file_type().unwrap().is_dir(),
24                })
25                .collect();
26        }
27        Some((key, Box::new(childs)))
28    }
29
30    fn decorators(&self, _lvl: u8) -> [&str; 4] {
31        match self.is_dir {
32            true => ["├─╼ ", "│  ", "└─╼ ", "   "],
33            false => ["├─> ", "│  ", "╰─> ", "   "],
34        }
35    }
36}
37
38fn main() {
39    let path = env::current_dir().unwrap().to_string_lossy().to_string();
40    let tree = DirTree { path, is_dir: true };
41    println!("{}", tree.fmt_tree_node(true));
42}