Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Progress indicator #256

Merged
merged 3 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
311 changes: 204 additions & 107 deletions Cargo.lock

Large diffs are not rendered by default.

19 changes: 9 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,24 @@ wildcard_imports = "allow"
obfuscated_if_else = "allow"

[dependencies]
ahash = "0.8.6"
ahash = "0.8.11"
ansi_term = "0.12.1"
anyhow = "1.0.75"
chrono = { version = "0.4.24", default-features = false, features = ["clock", "std"] }
clap = { version = "4.4.10", features = ["derive"] }
clap = { version = "4.5.4", features = ["derive"] }
clap_complete = "4.1.1"
config = { version = "0.14.0", default-features = false, features = ["toml"] }
crossterm = "0.26.1"
ctrlc = "3.4.0"
crossterm = "0.27.0"
ctrlc = "3.4.4"
dirs = "5.0"
errno = "0.3.1"
filesize = "0.2.0"
ignore = "0.4.2"
ignore = "0.4.22"
indextree = "4.6.0"
log = { version = "0.4.20", features = ["std"] }
lscolors = { version = "0.13.0", features = ["ansi_term"] }
once_cell = "1.17.0"
regex = "1.7.3"
terminal_size = "0.2.6"
lscolors = { version = "0.17.0", features = ["ansi_term"] }
once_cell = "1.19.0"
regex = "1.10.4"
terminal_size = "0.3.0"
thiserror = "1.0.40"
toml = "0.8.8"

Expand Down
1 change: 0 additions & 1 deletion rustfmt.toml

This file was deleted.

22 changes: 6 additions & 16 deletions src/disk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,8 @@ impl Usage {
let word_count =
std::fs::read_to_string(data.path()).map(|data| data.split_whitespace().count())?;

let word_count = u64::try_from(word_count).map_or_else(
|e| {
log::warn!("Usage::init_word_count {e}");
Self::WordCount(word_count as u64)
},
Self::WordCount,
);
let word_count = u64::try_from(word_count)
.map_or_else(|_| Self::WordCount(word_count as u64), Self::WordCount);

Ok(word_count)
}
Expand All @@ -143,13 +138,8 @@ impl Usage {

let line_count = fs::read_to_string(data.path()).map(|data| data.lines().count())?;

let line_count = u64::try_from(line_count).map_or_else(
|e| {
log::warn!("Usage::init_line_count {e}");
Self::WordCount(line_count as u64)
},
Self::LineCount,
);
let line_count = u64::try_from(line_count)
.map_or_else(|_| Self::WordCount(line_count as u64), Self::LineCount);

Ok(line_count)
}
Expand Down Expand Up @@ -192,7 +182,7 @@ impl Display for Usage {
let bytes = ($v as f64) / prefix.base_value();
write!(f, "{bytes:.FLOAT_PRECISION$} {prefix}B")
}
},
}
BytePresentation::Si => {
let prefix = prefix::Si::from($v);

Expand All @@ -202,7 +192,7 @@ impl Display for Usage {
let bytes = ($v as f64) / prefix.base_value();
write!(f, "{bytes:.1} {prefix}B")
}
},
}
}
};
}
Expand Down
54 changes: 53 additions & 1 deletion src/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ pub struct File {
unix_attrs: unix::Attrs,
}

// For keeping track of the count of file-types while loading from disk.
#[derive(Default)]
pub struct Accumulator {
num_file: usize,
num_dir: usize,
num_link: usize,
}

/// [`Display`] implementation concerned with human-readable presentation of the file-name.
pub struct DisplayName<'a> {
file: &'a File,
Expand Down Expand Up @@ -246,6 +254,24 @@ impl Deref for File {
}
}

impl Accumulator {
pub fn total(&self) -> usize {
self.num_dir + self.num_file + self.num_link
}

pub fn increment(&mut self, ft: Option<fs::FileType>) {
let Some(file_type) = ft else { return };

if file_type.is_file() {
self.num_file += 1;
} else if file_type.is_dir() {
self.num_dir += 1;
} else if file_type.is_symlink() {
self.num_link += 1;
}
}
}

impl Display for DisplayName<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let file_name = self.file.file_name().to_string_lossy();
Expand All @@ -266,7 +292,7 @@ impl Display for DisplayPath<'_> {
let path = self.file.path();
path.strip_prefix(prefix)
.map_or_else(|_| path.display(), |p| p.display())
},
}
None => self.file.path().display(),
};

Expand All @@ -279,3 +305,29 @@ impl Display for DisplayPath<'_> {
}
}
}

impl Display for Accumulator {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut displayed_count = Vec::new();

match self.num_dir {
n if n > 1 => displayed_count.push(format!("{} directories", n)),
1 => displayed_count.push("1 directory".to_string()),
_ => (),
}

match self.num_file {
n if n > 1 => displayed_count.push(format!("{} files", n)),
1 => displayed_count.push("1 file".to_string()),
_ => (),
}

match self.num_link {
n if n > 1 => displayed_count.push(format!("{} links", n)),
1 => displayed_count.push("1 link".to_string()),
_ => (),
}

writeln!(f, "{}", displayed_count.join(", "))
}
}
10 changes: 5 additions & 5 deletions src/file/tree/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl Tree {
match ft {
FileType::Dir if matches!(layout, Layout::Tree | Layout::InvertedTree) => {
filters.push(Box::new(|f| f.is_dir()))
},
}
FileType::Dir => filters.push(Box::new(|f| f.is_dir())),
FileType::File => filters.push(Box::new(|f| f.is_file())),
FileType::Symlink => filters.push(Box::new(|f| f.is_symlink())),
Expand Down Expand Up @@ -152,7 +152,7 @@ impl Tree {
to_remove
.into_iter()
.for_each(|n| n.remove(&mut self.arena));
},
}
_ => {
let to_remove = self
.root_id
Expand All @@ -167,7 +167,7 @@ impl Tree {
to_remove
.into_iter()
.for_each(|n| n.remove_subtree(&mut self.arena));
},
}
};

Ok(())
Expand Down Expand Up @@ -230,7 +230,7 @@ impl Tree {
to_remove
.into_iter()
.for_each(|n| n.remove(&mut self.arena));
},
}
_ => {
let to_remove = self
.root_id
Expand All @@ -251,7 +251,7 @@ impl Tree {
to_remove
.into_iter()
.for_each(|n| n.remove_subtree(&mut self.arena));
},
}
}

Ok(())
Expand Down
Loading
Loading