pub struct TextTreeElements<'a> {
pub prefix: TextTreeSymbols<'a>,
pub branch: TextTreeSymbols<'a>,
}
Fields§
§prefix: TextTreeSymbols<'a>
symbols for padding and previous branches
branch: TextTreeSymbols<'a>
symbols for padding and branch
Implementations§
Source§impl<'a> TextTreeElements<'a>
impl<'a> TextTreeElements<'a>
Sourcepub fn new(prefix: [&'a str; 4], branch: [&'a str; 4]) -> Self
pub fn new(prefix: [&'a str; 4], branch: [&'a str; 4]) -> Self
Initialize prefix and branch structures with user given symbols
let branch = [root, first, middle, last];
(default: ["", "", "├─ ", "└─ "]
)
let prefix = [root, first, middle, last];
(default: ["", "", "│ ", " "]
)
Examples found in repository?
examples/example_1.rs (line 72)
56fn main() {
57 let path = PathBuf::from(".");
58 let path_str = path.as_os_str().to_str().unwrap();
59 let prefixes = "";
60 let ignore = vec![".git", "target"];
61
62 // print root dir
63 println!("\n\n{}", path_str);
64 // default symbols
65 let tree = TextTreeElements::default();
66 // 0 - root, 0 - first item of 1 - max items
67 list_files(&path, Some(&ignore), &tree, prefixes, 0, 0, 1);
68
69 // print root dir
70 println!("\n\n{}", path_str);
71 // custom symbols
72 let tree = TextTreeElements::new(["", "", "| ", " "], ["", "", "|- ", "'- "]);
73 // 0 - root, 0 - first item of 1 - max items
74 list_files(&path, Some(&ignore), &tree, prefixes, 0, 0, 1);
75
76 // print root dir
77 println!("\n\n{}", path_str);
78 // custom symbols
79 let tree = TextTreeElements::new(["", "", " ", " "], ["", "", " ", " "]);
80 // 0 - root, 0 - first item of 1 - max items
81 list_files(&path, Some(&ignore), &tree, prefixes, 0, 0, 1);
82}
Sourcepub fn get_prefix(&self, level: usize, index: usize, size: usize) -> &'a str
pub fn get_prefix(&self, level: usize, index: usize, size: usize) -> &'a str
Function returns the current item prefix, depending on the position in the children list, and depth level
prefixes
- all prefixes from previous levellevel
- current depth level of the tree (0
- for the root item)index
- current item index (0
- for the first item)size
- items num (items number on the current level), will be used for detecting of the last item
Sourcepub fn get_branch(&self, level: usize, index: usize, size: usize) -> &'a str
pub fn get_branch(&self, level: usize, index: usize, size: usize) -> &'a str
Function returns the current tree item branch symbol, depending on the position in the children list, and depth level
level
- current depth level of the tree (0
- for the root item)index
- current item index (0
- for the first item)size
- items num (items number on the current level), will be used for detecting of the last item
Sourcepub fn get_prefix_branch(
&self,
level: usize,
index: usize,
size: usize,
) -> (&'a str, &'a str)
pub fn get_prefix_branch( &self, level: usize, index: usize, size: usize, ) -> (&'a str, &'a str)
Function returns a tuple containing both a prefix and a branch, depending on the position in the children list, and depth level
level
- current depth level of the tree (0
- for the root item)index
- current item index (0
- for the first item)size
- items num (items number on the current level), will be used for detecting of the last item
Examples found in repository?
examples/example_1.rs (line 24)
15fn list_files(
16 path: &Path,
17 ignore: Option<&Vec<&str>>,
18 tree: &TextTreeElements,
19 prefixes: &str,
20 level: usize,
21 index: usize,
22 size: usize,
23) {
24 let (prefix, branch) = tree.get_prefix_branch(level, index, size);
25
26 // skip "." path, doesn't have file name
27 if let Some(file_name) = path.file_name() {
28 let file_name = file_name.to_str().unwrap();
29
30 // skip if in ignore list
31 if let Some(v) = ignore {
32 if v.contains(&file_name) {
33 return;
34 }
35 }
36
37 println!("{}{}{}", prefixes, branch, file_name);
38 }
39
40 // if dir contains files?
41 if let Ok(files) = fs::read_dir(path) {
42 let files = files.collect::<Result<Vec<_>, _>>().unwrap();
43
44 let prefixes = format!("{}{}", prefixes, prefix);
45 let size = files.len();
46
47 // for all children files
48 for (i, file) in files.iter().enumerate() {
49 let dir = file.path();
50 // recursive call
51 list_files(&dir, ignore, tree, &prefixes, level + 1, i, size)
52 }
53 }
54}
Trait Implementations§
Source§impl<'a> Debug for TextTreeElements<'a>
impl<'a> Debug for TextTreeElements<'a>
Auto Trait Implementations§
impl<'a> Freeze for TextTreeElements<'a>
impl<'a> RefUnwindSafe for TextTreeElements<'a>
impl<'a> Send for TextTreeElements<'a>
impl<'a> Sync for TextTreeElements<'a>
impl<'a> Unpin for TextTreeElements<'a>
impl<'a> UnwindSafe for TextTreeElements<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more