mirror of
https://github.com/bootandy/dust.git
synced 2026-06-08 11:29:05 +03:00
Refactor: Reduce complexity of progress bar code
This commit is contained in:
@@ -20,7 +20,6 @@ _dust() {
|
|||||||
case "${cmd}" in
|
case "${cmd}" in
|
||||||
dust)
|
dust)
|
||||||
opts="-h -V -d -n -p -X -L -x -s -r -c -b -z -f -i -v -e -t -w -H -P -D --help --version --depth --number-of-lines --full-paths --ignore-directory --dereference-links --limit-filesystem --apparent-size --reverse --no-colors --no-percent-bars --min-size --skip-total --filecount --ignore_hidden --invert-filter --filter --file_types --terminal_width --si --no-progress --only-dir <inputs>..."
|
opts="-h -V -d -n -p -X -L -x -s -r -c -b -z -f -i -v -e -t -w -H -P -D --help --version --depth --number-of-lines --full-paths --ignore-directory --dereference-links --limit-filesystem --apparent-size --reverse --no-colors --no-percent-bars --min-size --skip-total --filecount --ignore_hidden --invert-filter --filter --file_types --terminal_width --si --no-progress --only-dir <inputs>..."
|
||||||
|
|
||||||
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
|
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
|
||||||
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
+16
-64
@@ -43,12 +43,11 @@ pub fn walk_it(dirs: HashSet<PathBuf>, walk_data: WalkData) -> (Vec<Node>, bool)
|
|||||||
let top_level_nodes: Vec<_> = dirs
|
let top_level_nodes: Vec<_> = dirs
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|d| {
|
.filter_map(|d| {
|
||||||
clean_inodes(
|
let node = walk(d, &permissions_flag, &walk_data, 0)?;
|
||||||
walk(d, &permissions_flag, &walk_data, 0)?,
|
if let Some(data) = walk_data.progress_data {
|
||||||
&mut inodes,
|
data.state.set(progress::Operation::PREPARING);
|
||||||
walk_data.progress_data,
|
}
|
||||||
walk_data.use_apparent_size,
|
clean_inodes(node, &mut inodes, walk_data.use_apparent_size)
|
||||||
)
|
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
(top_level_nodes, permissions_flag.into_inner())
|
(top_level_nodes, permissions_flag.into_inner())
|
||||||
@@ -58,13 +57,8 @@ pub fn walk_it(dirs: HashSet<PathBuf>, walk_data: WalkData) -> (Vec<Node>, bool)
|
|||||||
fn clean_inodes(
|
fn clean_inodes(
|
||||||
x: Node,
|
x: Node,
|
||||||
inodes: &mut HashSet<(u64, u64)>,
|
inodes: &mut HashSet<(u64, u64)>,
|
||||||
info_data: Option<&Arc<PAtomicInfo>>,
|
|
||||||
use_apparent_size: bool,
|
use_apparent_size: bool,
|
||||||
) -> Option<Node> {
|
) -> Option<Node> {
|
||||||
if let Some(data) = info_data {
|
|
||||||
data.state.set(progress::Operation::PREPARING);
|
|
||||||
}
|
|
||||||
|
|
||||||
if !use_apparent_size {
|
if !use_apparent_size {
|
||||||
if let Some(id) = x.inode_device {
|
if let Some(id) = x.inode_device {
|
||||||
if !inodes.insert(id) {
|
if !inodes.insert(id) {
|
||||||
@@ -78,7 +72,7 @@ fn clean_inodes(
|
|||||||
tmp.sort_by(sort_by_inode);
|
tmp.sort_by(sort_by_inode);
|
||||||
let new_children: Vec<_> = tmp
|
let new_children: Vec<_> = tmp
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|c| clean_inodes(c, inodes, info_data, use_apparent_size))
|
.filter_map(|c| clean_inodes(c, inodes, use_apparent_size))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
Some(Node {
|
Some(Node {
|
||||||
@@ -143,7 +137,6 @@ fn walk(
|
|||||||
depth: usize,
|
depth: usize,
|
||||||
) -> Option<Node> {
|
) -> Option<Node> {
|
||||||
let info_data = &walk_data.progress_data;
|
let info_data = &walk_data.progress_data;
|
||||||
let info_conf = &walk_data.progress_config;
|
|
||||||
|
|
||||||
if let Some(data) = info_data {
|
if let Some(data) = info_data {
|
||||||
data.state.set(progress::Operation::INDEXING);
|
data.state.set(progress::Operation::INDEXING);
|
||||||
@@ -151,8 +144,6 @@ fn walk(
|
|||||||
data.current_path.set(dir.to_string_lossy().to_string());
|
data.current_path.set(dir.to_string_lossy().to_string());
|
||||||
|
|
||||||
// reset the value between each target dirs
|
// reset the value between each target dirs
|
||||||
data.files_skipped.set(0);
|
|
||||||
data.directories_skipped.set(0);
|
|
||||||
data.total_file_size.set(0);
|
data.total_file_size.set(0);
|
||||||
data.file_number.set(0);
|
data.file_number.set(0);
|
||||||
}
|
}
|
||||||
@@ -178,7 +169,7 @@ fn walk(
|
|||||||
return walk(entry.path(), permissions_flag, walk_data, depth + 1);
|
return walk(entry.path(), permissions_flag, walk_data, depth + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
let n = build_node(
|
let node = build_node(
|
||||||
entry.path(),
|
entry.path(),
|
||||||
vec![],
|
vec![],
|
||||||
walk_data.filter_regex,
|
walk_data.filter_regex,
|
||||||
@@ -190,57 +181,27 @@ fn walk(
|
|||||||
depth,
|
depth,
|
||||||
);
|
);
|
||||||
|
|
||||||
if !ignore_file(entry, walk_data) {
|
|
||||||
if let Some(ref node) = n {
|
|
||||||
if let Some(data) = info_data {
|
if let Some(data) = info_data {
|
||||||
data.file_number.add(1);
|
data.file_number.add(1);
|
||||||
|
if let Some(ref file) = node {
|
||||||
|
data.total_file_size.add(file.size);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use `is_some_and` when stabilized
|
return node;
|
||||||
if let Some(conf) = info_conf {
|
|
||||||
if !conf.file_count_only {
|
|
||||||
if let Some(data) = info_data {
|
|
||||||
data.total_file_size.add(node.size);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if let Some(data) = info_data {
|
|
||||||
data.files_skipped.add(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
n
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if let Some(data) = info_data {
|
|
||||||
data.files_skipped.add(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
permissions_flag.store(true, atomic::Ordering::Relaxed);
|
permissions_flag.store(true, atomic::Ordering::Relaxed);
|
||||||
|
|
||||||
if let Some(data) = info_data {
|
|
||||||
data.directories_skipped.add(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
None
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
} else {
|
} else {
|
||||||
// Handle edge case where dust is called with a file instead of a directory
|
// Handle edge case where dust is called with a file instead of a directory
|
||||||
if !dir.exists() {
|
if !dir.exists() {
|
||||||
|
// TODO: Migrate permissions_flag to the new progress_data object
|
||||||
permissions_flag.store(true, atomic::Ordering::Relaxed);
|
permissions_flag.store(true, atomic::Ordering::Relaxed);
|
||||||
|
|
||||||
if let Some(data) = info_data {
|
|
||||||
data.files_skipped.add(1);
|
|
||||||
}
|
|
||||||
} else if let Some(data) = info_data {
|
|
||||||
data.directories_skipped.add(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
build_node(
|
build_node(
|
||||||
@@ -278,13 +239,10 @@ mod tests {
|
|||||||
let n = create_node();
|
let n = create_node();
|
||||||
|
|
||||||
// First time we insert the node
|
// First time we insert the node
|
||||||
assert_eq!(
|
assert_eq!(clean_inodes(n.clone(), &mut inodes, false), Some(n.clone()));
|
||||||
clean_inodes(n.clone(), &mut inodes, None, false),
|
|
||||||
Some(n.clone())
|
|
||||||
);
|
|
||||||
|
|
||||||
// Second time is a duplicate - we ignore it
|
// Second time is a duplicate - we ignore it
|
||||||
assert_eq!(clean_inodes(n.clone(), &mut inodes, None, false), None);
|
assert_eq!(clean_inodes(n.clone(), &mut inodes, false), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -294,13 +252,7 @@ mod tests {
|
|||||||
let n = create_node();
|
let n = create_node();
|
||||||
|
|
||||||
// If using apparent size we include Nodes, even if duplicate inodes
|
// If using apparent size we include Nodes, even if duplicate inodes
|
||||||
assert_eq!(
|
assert_eq!(clean_inodes(n.clone(), &mut inodes, true), Some(n.clone()));
|
||||||
clean_inodes(n.clone(), &mut inodes, None, true),
|
assert_eq!(clean_inodes(n.clone(), &mut inodes, true), Some(n.clone()));
|
||||||
Some(n.clone())
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
clean_inodes(n.clone(), &mut inodes, None, true),
|
|
||||||
Some(n.clone())
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user