list: add comments

This commit is contained in:
Anton Hermann 2021-11-01 12:15:56 +01:00
parent e8f24b7178
commit cabb4f03bf

View File

@ -33,6 +33,8 @@ pub fn list_files(archive: &Path, files: Vec<FileInArchive>, list_options: ListO
} }
} }
/// Print an entry and highlight directories, either by coloring them
/// if that's supported or by adding a trailing /
fn print_entry(name: impl std::fmt::Display, is_dir: bool) { fn print_entry(name: impl std::fmt::Display, is_dir: bool) {
use crate::utils::colors::*; use crate::utils::colors::*;
@ -49,6 +51,10 @@ fn print_entry(name: impl std::fmt::Display, is_dir: bool) {
} }
} }
/// Since archives store files as a list of entries -> without direct
/// directory structure (the directories are however part of the name),
/// we have to construct the tree structure ourselves to be able to
/// display them as a tree
mod tree { mod tree {
use super::FileInArchive; use super::FileInArchive;
use linked_hash_map::LinkedHashMap; use linked_hash_map::LinkedHashMap;
@ -56,11 +62,7 @@ mod tree {
use std::iter::FromIterator; use std::iter::FromIterator;
use std::path; use std::path;
const TREE_PREFIX_EMPTY: &str = " "; /// Directory tree
const TREE_PREFIX_LINE: &str = "";
const TREE_FINAL_BRANCH: &str = "├── ";
const TREE_FINAL_LAST: &str = "└── ";
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Tree { pub struct Tree {
file: Option<FileInArchive>, file: Option<FileInArchive>,
@ -114,8 +116,8 @@ mod tree {
// If there are no further elements in the parent directory, add // If there are no further elements in the parent directory, add
// "└── " to the prefix, otherwise add "├── " // "└── " to the prefix, otherwise add "├── "
let final_part = match last { let final_part = match last {
true => TREE_FINAL_LAST, true => draw::FINAL_LAST,
false => TREE_FINAL_BRANCH, false => draw::FINAL_BRANCH,
}; };
print!("{}{}", prefix, final_part); print!("{}{}", prefix, final_part);
@ -123,13 +125,13 @@ mod tree {
Some(FileInArchive { is_dir, .. }) => is_dir, Some(FileInArchive { is_dir, .. }) => is_dir,
None => true, None => true,
}; };
super::print_file(name, is_dir); super::print_entry(name, is_dir);
// Construct prefix for children, adding either a line if this isn't // Construct prefix for children, adding either a line if this isn't
// the last entry in the parent dir or empty space if it is. // the last entry in the parent dir or empty space if it is.
prefix.push_str(match last { prefix.push_str(match last {
true => TREE_PREFIX_EMPTY, true => draw::PREFIX_EMPTY,
false => TREE_PREFIX_LINE, false => draw::PREFIX_LINE,
}); });
// Recursively print all children // Recursively print all children
for (i, (name, subtree)) in self.children.iter().enumerate() { for (i, (name, subtree)) in self.children.iter().enumerate() {
@ -147,4 +149,26 @@ mod tree {
tree tree
} }
} }
/// Constants containing the visual parts of which the displayed tree
/// is constructed.
///
/// They fall into 2 categories: the `PREFIX_*` parts form the first
/// `depth - 1` parts while the `FINAL_*` parts form the last part,
/// right before the entry itself
///
/// `PREFIX_EMPTY`: the corresponding dir is the last entry in its parent dir
/// `PREFIX_LINE`: there are other entries after the corresponding dir
/// `FINAL_LAST`: this entry is the last entry in its parent dir
/// `FINAL_BRANCH`: there are other entries after this entry
mod draw {
/// the corresponding dir is the last entry in its parent dir
pub const PREFIX_EMPTY: &str = " ";
/// there are other entries after the corresponding dir
pub const PREFIX_LINE: &str = "";
/// this entry is the last entry in its parent dir
pub const FINAL_LAST: &str = "└── ";
/// there are other entries after this entry
pub const FINAL_BRANCH: &str = "├── ";
}
} }