131 lines
3.3 KiB
Rust
131 lines
3.3 KiB
Rust
use std::fs::read_dir;
|
|
use std::path::{Path, PathBuf};
|
|
use std::io::{Result, ErrorKind};
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct Directory {
|
|
name: PathBuf,
|
|
size: u64,
|
|
children: Vec<Directory>,
|
|
}
|
|
impl Directory {
|
|
#[inline(always)]
|
|
pub const fn size(&self) -> u64 {
|
|
self.size
|
|
}
|
|
|
|
#[inline(always)]
|
|
pub fn path(&self) -> &Path {
|
|
self.name.as_ref()
|
|
}
|
|
|
|
pub fn new<P>(path: P) -> Result<Self>
|
|
where
|
|
P: AsRef<Path>
|
|
{
|
|
let path = path.as_ref();
|
|
let name = path.canonicalize()?.file_name().unwrap().into();
|
|
|
|
let dir = match read_dir(&path) {
|
|
Ok(dir) => dir,
|
|
Err(io_error) => match io_error.kind() {
|
|
ErrorKind::NotADirectory => return Ok(Self {
|
|
name,
|
|
size: path.metadata()?.len(),
|
|
children: Vec::new()
|
|
}),
|
|
_ => return Err(io_error),
|
|
}
|
|
};
|
|
|
|
let mut size = 0;
|
|
let mut children = Vec::new();
|
|
for entry in dir {
|
|
let child = Directory::new(entry?.path())?;
|
|
size += child.size;
|
|
children.push(child);
|
|
}
|
|
|
|
Ok(Self{
|
|
name,
|
|
size,
|
|
children,
|
|
})
|
|
}
|
|
|
|
pub fn display(self) -> String {
|
|
// since self.size is definitionally the greatest value, the tab length
|
|
// is just the length of self.len, plus two for a tab width
|
|
let tab_size = self.size.to_string().len() + 2;
|
|
self.vectorise().iter().map(|e| e.to_string(tab_size) + "\n").collect()
|
|
}
|
|
|
|
/// TODO: make not recursive, take &self if possible,
|
|
/// and maybe write directly to stdout to not use so much mem
|
|
fn vectorise(self) -> Vec<TreeEntry> {
|
|
let mut result = Vec::new();
|
|
|
|
result.push(TreeEntry(Vec::new(), self.name.display().to_string(), self.size));
|
|
|
|
let mut new_entry_part = TreePart::First;
|
|
let mut continue_part = TreePart::Wait;
|
|
|
|
let len = self.children.len();
|
|
|
|
for (idx, child) in self.children.into_iter().enumerate() {
|
|
if idx+1 == len {
|
|
new_entry_part = TreePart::Last;
|
|
continue_part = TreePart::Blank;
|
|
}
|
|
|
|
let subtree = child.vectorise();
|
|
|
|
for mut item in subtree {
|
|
if item.0.len() == 0 {
|
|
item.0.push(new_entry_part);
|
|
} else {
|
|
item.0.push(continue_part);
|
|
}
|
|
result.push(item);
|
|
}
|
|
}
|
|
|
|
result
|
|
}
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
struct TreeEntry(Vec<TreePart>, String, u64);
|
|
impl TreeEntry {
|
|
fn to_string(&self, tab_size: usize) -> String {
|
|
let mut result = format!("{:<tab_size$}", self.2);
|
|
|
|
for part in self.0.iter().rev() {
|
|
result += &part.display().to_owned();
|
|
}
|
|
// dont add the space to empty entries
|
|
result += " ";
|
|
result += &self.1;
|
|
|
|
result
|
|
}
|
|
}
|
|
|
|
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
|
|
enum TreePart {
|
|
First,
|
|
Wait,
|
|
Last,
|
|
Blank
|
|
}
|
|
impl TreePart {
|
|
/// convert to ascii art
|
|
pub const fn display(&self) -> &str {
|
|
match self {
|
|
Self::First => "├──",
|
|
Self::Wait => "│ ",
|
|
Self::Last => "└──",
|
|
Self::Blank => " ",
|
|
}
|
|
}
|
|
} |