-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from solidiquis/disk-usage
Disk usage
- Loading branch information
Showing
10 changed files
with
192 additions
and
92 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,22 +7,25 @@ use std::{ | |
convert::From, | ||
error::Error as StdError, | ||
fmt::{self, Display, Formatter}, | ||
fs, | ||
io, | ||
fs, io, | ||
path::{Path, PathBuf}, | ||
usize, | ||
}; | ||
|
||
/// Defines the CLI. | ||
#[derive(Parser, Debug)] | ||
#[command(name = "Erdtree")] | ||
#[command(name = "erdtree")] | ||
#[command(author = "Benjamin Nguyen. <[email protected]>")] | ||
#[command(version = "1.2")] | ||
#[command(about = "erdtree (et) is a multi-threaded filetree visualizer and disk usage analyzer.", long_about = None)] | ||
pub struct Clargs { | ||
/// Root directory to traverse; defaults to current working directory | ||
dir: Option<PathBuf>, | ||
|
||
/// Print physical or logical file size | ||
#[arg(short, long, value_enum, default_value_t = DiskUsage::Logical)] | ||
disk_usage: DiskUsage, | ||
|
||
/// Include or exclude files using glob patterns | ||
#[arg(short, long)] | ||
glob: Vec<String>, | ||
|
@@ -78,32 +81,50 @@ pub enum Order { | |
/// Sort entries by file name | ||
Name, | ||
|
||
/// Sort entries by size in descending order | ||
/// Sort entries by size smallest to largest, top to bottom | ||
Size, | ||
|
||
/// Sort entries by size largest to smallest, bottom to top | ||
SizeRev, | ||
|
||
/// No sorting | ||
None, | ||
} | ||
|
||
/// Display disk usage output as either logical size or physical size. | ||
#[derive(Copy, Clone, Debug, ValueEnum)] | ||
pub enum DiskUsage { | ||
/// How many bytes does a file contain | ||
Logical, | ||
|
||
/// How much actual space on disk based on blocks allocated, taking into account sparse files | ||
/// and compression. | ||
Physical, | ||
} | ||
|
||
impl Clargs { | ||
/// Returns reference to the path of the root directory to be traversed. | ||
pub fn dir(&self) -> &Path { | ||
if let Some(ref path) = self.dir { | ||
path.as_path() | ||
} else { | ||
Path::new(".") | ||
} | ||
self.dir | ||
.as_ref() | ||
.map_or_else(|| Path::new("."), |pb| pb.as_path()) | ||
} | ||
|
||
/// The sort-order used for printing. | ||
pub fn sort(&self) -> Order { | ||
self.sort | ||
} | ||
|
||
/// Getter for `dirs_first` field. | ||
pub fn dirs_first(&self) -> bool { | ||
self.dirs_first | ||
} | ||
|
||
/// Getter for `disk_usage` field. | ||
pub fn disk_usage(&self) -> &DiskUsage { | ||
&self.disk_usage | ||
} | ||
|
||
/// The max depth to print. Note that all directories are fully traversed to compute file | ||
/// sizes; this just determines how much to print. | ||
pub fn level(&self) -> Option<usize> { | ||
|
@@ -146,8 +167,7 @@ impl TryFrom<&Clargs> for WalkParallel { | |
fn try_from(clargs: &Clargs) -> Result<Self, Self::Error> { | ||
let root = fs::canonicalize(clargs.dir())?; | ||
|
||
fs::metadata(&root) | ||
.map_err(|e| Error::DirNotFound(format!("{}: {e}", root.display())))?; | ||
fs::metadata(&root).map_err(|e| Error::DirNotFound(format!("{}: {e}", root.display())))?; | ||
|
||
Ok(WalkBuilder::new(root) | ||
.follow_links(clargs.follow_links) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use crate::cli; | ||
use std::convert::From; | ||
|
||
/// Determines between logical or physical size for display | ||
#[derive(Debug)] | ||
pub enum DiskUsage { | ||
/// How many bytes does a file contain | ||
Logical, | ||
|
||
/// How much actual space on disk, taking into account sparse files and compression. | ||
Physical, | ||
} | ||
|
||
impl From<&cli::DiskUsage> for DiskUsage { | ||
fn from(du: &cli::DiskUsage) -> Self { | ||
match du { | ||
cli::DiskUsage::Logical => Self::Logical, | ||
cli::DiskUsage::Physical => Self::Physical, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.