Struct TextTreeElements

Source
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>

Source

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}
Source

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 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
Source

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
Source

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>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Default for TextTreeElements<'a>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.