Skip to content

Commit

Permalink
progress indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
solidiquis committed May 19, 2024
1 parent 56fbf61 commit 37bdf6b
Show file tree
Hide file tree
Showing 15 changed files with 341 additions and 198 deletions.
247 changes: 166 additions & 81 deletions Cargo.lock

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +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.5.4", features = ["derive"] }
clap_complete = "4.1.1"
config = { version = "0.14.0", default-features = false, features = ["toml"] }
crossterm = "0.27.0"
ctrlc = "3.4.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"
lscolors = { version = "0.13.0", features = ["ansi_term"] }
lscolors = { version = "0.17.0", features = ["ansi_term"] }
once_cell = "1.19.0"
regex = "1.7.3"
terminal_size = "0.2.6"
regex = "1.10.4"
terminal_size = "0.3.0"
thiserror = "1.0.40"
toml = "0.8.8"

Expand Down
16 changes: 6 additions & 10 deletions src/disk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +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(
|_| 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 @@ -140,10 +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(
|_| 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 @@ -186,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 @@ -196,7 +192,7 @@ impl Display for Usage {
let bytes = ($v as f64) / prefix.base_value();
write!(f, "{bytes:.1} {prefix}B")
}
},
}
}
};
}
Expand Down
32 changes: 28 additions & 4 deletions src/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,7 @@ impl Accumulator {
}

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

if file_type.is_file() {
self.num_file += 1;
Expand Down Expand Up @@ -294,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 @@ -307,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
44 changes: 26 additions & 18 deletions src/file/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ pub enum TreeError {
impl Tree {
/// Like [`Tree::init`] but leverages parallelism for disk-reads and [`File`] initialization.
pub fn init(ctx: &Context) -> Result<(Self, super::Accumulator, column::Metadata)> {
let (TransitionState {
mut arena,
mut branches,
mut column_metadata,
root_id,
}, accumulator)= Self::load(ctx)?;
let (
TransitionState {
mut arena,
mut branches,
mut column_metadata,
root_id,
},
accumulator,
) = Self::load(ctx)?;

let mut dir_stack = vec![root_id];
let mut inode_set = HashSet::default();
Expand Down Expand Up @@ -130,7 +133,7 @@ impl Tree {
all_dirents
.into_iter()
.for_each(|n| root_id.append(n, &mut arena));
},
}
_ => {
for (dir_id, dirsize) in dirsize_map.into_iter() {
let dir = arena[dir_id].get_mut();
Expand All @@ -148,7 +151,7 @@ impl Tree {
}
}
}
},
}
},
None => {
for (dir_id, dirsize) in dirsize_map.into_iter() {
Expand All @@ -161,7 +164,7 @@ impl Tree {
}
}
}
},
}
}

column_metadata.update_size_width(arena[root_id].get(), ctx);
Expand All @@ -171,13 +174,18 @@ impl Tree {
Ok((tree, accumulator, column_metadata))
}

pub fn init_without_disk_usage(ctx: &Context) -> Result<(Self, super::Accumulator, column::Metadata)> {
let (TransitionState {
mut arena,
mut branches,
mut column_metadata,
root_id,
}, accumulator)= Self::load(ctx)?;
pub fn init_without_disk_usage(
ctx: &Context,
) -> Result<(Self, super::Accumulator, column::Metadata)> {
let (
TransitionState {
mut arena,
mut branches,
mut column_metadata,
root_id,
},
accumulator,
) = Self::load(ctx)?;

#[cfg(unix)]
macro_rules! update_metadata {
Expand Down Expand Up @@ -211,7 +219,7 @@ impl Tree {
#[cfg(unix)]
update_metadata!(dirent_id);
}
},
}
_ => {
let dirs = arena
.iter()
Expand Down Expand Up @@ -244,7 +252,7 @@ impl Tree {
}
}
});
},
}
}

let tree = Self { root_id, arena };
Expand Down
6 changes: 3 additions & 3 deletions src/file/tree/traverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ impl ParallelVisitor for Visitor<'_> {
Err(e) => {
let _ = self.send(TraversalState::Error(e));
return WalkState::Continue;
},
}
};

match File::init(entry, self.ctx).into_report(ErrorCategory::Warning) {
Ok(file) => {
let _ = self.send(TraversalState::Ongoing(file));
},
}
Err(e) => {
let _ = self.send(TraversalState::Error(e));
},
}
}

WalkState::Continue
Expand Down
21 changes: 7 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#![cfg_attr(windows, feature(windows_by_handle))]
use clap::CommandFactory;
use std::{
io::stdout,
process::ExitCode,
};
use std::{io::stdout, process::ExitCode};

/// Concerned with disk usage calculation and presentation.
mod disk;
Expand All @@ -25,7 +22,6 @@ mod progress;
mod user;
use user::Context;


/// For basic performance measurements when compiling for debug.
#[cfg(debug_assertions)]
#[macro_use]
Expand Down Expand Up @@ -65,15 +61,8 @@ fn run() -> Result<()> {
return Ok(());
}

// TODO: Use accumulator
let (mut file_tree, _accumulator, column_metadata) = if ctx.no_progress {
if ctx.suppress_size {
file::Tree::init_without_disk_usage(&ctx)
} else {
file::Tree::init(&ctx)
}
} else {
progress::Indicator::init().show_progress(|| {
let (mut file_tree, accumulator, column_metadata) = {
progress::init_indicator(&ctx)?.show_progress(|| {
if ctx.suppress_size {
file::Tree::init_without_disk_usage(&ctx)
} else {
Expand All @@ -88,6 +77,10 @@ fn run() -> Result<()> {

Renderer::new(&ctx, &file_tree).render()?;

if !ctx.no_report {
println!("{accumulator}");
}

#[cfg(debug_assertions)]
{
perf::finish_recording("crate::main");
Expand Down
21 changes: 8 additions & 13 deletions src/perf.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use ahash::HashMap;
use once_cell::sync::Lazy;
use std::{
io,
fmt,
fmt, io,
sync::{
mpsc::{self, Sender},
OnceLock,
},
time::{Duration, Instant},
thread::{self, JoinHandle},
time::{Duration, Instant},
};

static mut GLOBAL_PERF_METRICS: OnceLock<PerfMetrics> = OnceLock::new();
Expand Down Expand Up @@ -76,23 +75,19 @@ pub fn finish_recording(name: &str) {
OUTPUT_NOTIFIER
.get()
.cloned()
.and_then(|tx| {
tx.send(Message::Recording(recording)).ok()
});
.and_then(|tx| tx.send(Message::Recording(recording)).ok());
}

pub fn output<T: io::Write>(mut w: T) {
OUTPUT_NOTIFIER.get().cloned().and_then(|tx| {
tx.send(Message::Terminate).ok()
});
OUTPUT_NOTIFIER
.get()
.cloned()
.and_then(|tx| tx.send(Message::Terminate).ok());

let mut recordings = unsafe {
GLOBAL_PERF_METRICS
.get_mut()
.and_then(|PerfMetrics(join)| {
join.take()
.and_then(|h| h.join().ok())
})
.and_then(|PerfMetrics(join)| join.take().and_then(|h| h.join().ok()))
.unwrap()
};

Expand Down
Loading

0 comments on commit 37bdf6b

Please sign in to comment.