minor refactoring
parent
60f0e550fa
commit
a65e78b915
|
@ -1,7 +1,10 @@
|
||||||
use std::fs::read_dir;
|
use std::fs::read_dir;
|
||||||
|
use std::hint::unreachable_unchecked;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::io::{Result, ErrorKind};
|
use std::io::{Result, ErrorKind};
|
||||||
|
|
||||||
|
use crate::unit::Unit;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Directory {
|
pub struct Directory {
|
||||||
name: PathBuf,
|
name: PathBuf,
|
||||||
|
@ -9,12 +12,17 @@ pub struct Directory {
|
||||||
children: Vec<Directory>,
|
children: Vec<Directory>,
|
||||||
}
|
}
|
||||||
impl Directory {
|
impl Directory {
|
||||||
#[inline(always)]
|
#[inline]
|
||||||
pub const fn size(&self) -> u64 {
|
pub const fn size(&self) -> u64 {
|
||||||
self.size
|
self.size
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline]
|
||||||
|
pub fn scale(&self, unit: Unit) -> String {
|
||||||
|
unit.convert(self.size)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn path(&self) -> &Path {
|
pub fn path(&self) -> &Path {
|
||||||
self.name.as_ref()
|
self.name.as_ref()
|
||||||
}
|
}
|
||||||
|
@ -24,9 +32,15 @@ impl Directory {
|
||||||
P: AsRef<Path>
|
P: AsRef<Path>
|
||||||
{
|
{
|
||||||
let path = path.as_ref();
|
let path = path.as_ref();
|
||||||
let name = path.canonicalize()?.file_name().unwrap().into();
|
let name = path.canonicalize()?
|
||||||
|
.file_name()
|
||||||
|
// file_name() returns None if and only if the path ends in "..".
|
||||||
|
// due to the call to canonicalize(), this can never be the case,
|
||||||
|
// so this can be safely unwrapped
|
||||||
|
.unwrap_or_else(|| unsafe { unreachable_unchecked() })
|
||||||
|
.into();
|
||||||
|
|
||||||
let dir = match read_dir(&path) {
|
let dir = match read_dir(path) {
|
||||||
Ok(dir) => dir,
|
Ok(dir) => dir,
|
||||||
Err(io_error) => match io_error.kind() {
|
Err(io_error) => match io_error.kind() {
|
||||||
ErrorKind::NotADirectory => return Ok(Self {
|
ErrorKind::NotADirectory => return Ok(Self {
|
||||||
|
@ -37,11 +51,11 @@ impl Directory {
|
||||||
_ => return Err(io_error),
|
_ => return Err(io_error),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut size = 0;
|
let mut size = 0;
|
||||||
let mut children = Vec::new();
|
let mut children = Vec::new();
|
||||||
for entry in dir {
|
for entry in dir {
|
||||||
let child = Directory::new(entry?.path())?;
|
let child = Self::new(entry?.path())?;
|
||||||
size += child.size;
|
size += child.size;
|
||||||
children.push(child);
|
children.push(child);
|
||||||
}
|
}
|
||||||
|
@ -53,19 +67,28 @@ impl Directory {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn display(self) -> String {
|
pub fn display(self, unit: Unit) -> String {
|
||||||
// since self.size is definitionally the greatest value, the tab length
|
// since self.size is definitionally the greatest value, the tab length
|
||||||
// is just the length of self.len, plus two for a tab width
|
// is just the length of self.len, plus two for a tab width
|
||||||
let tab_size = self.size.to_string().len() + 2;
|
let tab_size = self.size.to_string().len() + 2;
|
||||||
self.vectorise().iter().map(|e| e.to_string(tab_size) + "\n").collect()
|
let mut result = self.vectorise(unit).iter().map(|e| e.stringify_tabbed(tab_size) + "\n").collect::<String>();
|
||||||
|
|
||||||
|
if ! result.is_empty() {
|
||||||
|
let final_newline_char_range = result.len()-2 .. result.len();
|
||||||
|
result.drain(final_newline_char_range);
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
/// TODO: make not recursive, take &self if possible,
|
/// TODO: make not recursive, take &self if possible,
|
||||||
/// and maybe write directly to stdout to not use so much mem
|
/// and maybe write directly to stdout to not use so much mem
|
||||||
fn vectorise(self) -> Vec<TreeEntry> {
|
fn vectorise(self, unit: Unit) -> Vec<TreeEntry> {
|
||||||
let mut result = Vec::new();
|
let mut result = Vec::new();
|
||||||
|
|
||||||
result.push(TreeEntry(Vec::new(), self.name.display().to_string(), self.size));
|
result.push(TreeEntry::new(
|
||||||
|
Vec::new(), self.name.display().to_string(), self.size, unit
|
||||||
|
));
|
||||||
|
|
||||||
let mut new_entry_part = TreePart::First;
|
let mut new_entry_part = TreePart::First;
|
||||||
let mut continue_part = TreePart::Wait;
|
let mut continue_part = TreePart::Wait;
|
||||||
|
@ -78,13 +101,13 @@ impl Directory {
|
||||||
continue_part = TreePart::Blank;
|
continue_part = TreePart::Blank;
|
||||||
}
|
}
|
||||||
|
|
||||||
let subtree = child.vectorise();
|
let subtree = child.vectorise(unit);
|
||||||
|
|
||||||
for mut item in subtree {
|
for mut item in subtree {
|
||||||
if item.0.len() == 0 {
|
if item.parts.is_empty() {
|
||||||
item.0.push(new_entry_part);
|
item.parts.push(new_entry_part);
|
||||||
} else {
|
} else {
|
||||||
item.0.push(continue_part);
|
item.parts.push(continue_part);
|
||||||
}
|
}
|
||||||
result.push(item);
|
result.push(item);
|
||||||
}
|
}
|
||||||
|
@ -95,18 +118,29 @@ impl Directory {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct TreeEntry(Vec<TreePart>, String, u64);
|
struct TreeEntry {
|
||||||
|
parts: Vec<TreePart>,
|
||||||
|
path: String,
|
||||||
|
size: u64,
|
||||||
|
unit: Unit
|
||||||
|
}
|
||||||
impl TreeEntry {
|
impl TreeEntry {
|
||||||
fn to_string(&self, tab_size: usize) -> String {
|
fn new(parts: Vec<TreePart>, path: String, size: u64, unit: Unit) -> Self {
|
||||||
let mut result = format!("{:<tab_size$}", self.2);
|
Self {
|
||||||
|
parts, path, size, unit
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn stringify_tabbed(&self, tab_size: usize) -> String {
|
||||||
|
let mut result = format!("{:<tab_size$}", self.unit.convert(self.size));
|
||||||
|
|
||||||
for part in self.0.iter().rev() {
|
for part in self.parts.iter().rev() {
|
||||||
result += &part.display().to_owned();
|
result += part.display();
|
||||||
}
|
}
|
||||||
// dont add the space to empty entries
|
// dont add the space to empty entries
|
||||||
result += " ";
|
result += " ";
|
||||||
result += &self.1;
|
result += &self.path;
|
||||||
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue