refactor: progress bar

Pull out display code into different functions

Experimented with printing the actual directory currently being parsed
but this resulted into too many flickering hard to follow prints. It's
cleaner to just print the target directory rather than the directory
currently being examined by the dir_walker.
This commit is contained in:
andy.boot
2023-01-21 16:40:43 +00:00
parent 9ff28b3456
commit af9f0b5125
2 changed files with 18 additions and 21 deletions

View File

@@ -40,6 +40,7 @@ pub fn walk_it(dirs: HashSet<PathBuf>, walk_data: WalkData) -> Vec<Node> {
let node = walk(d, &walk_data, 0)?; let node = walk(d, &walk_data, 0)?;
prog_data.state.store(Operation::PREPARING, ORDERING); prog_data.state.store(Operation::PREPARING, ORDERING);
clean_inodes(node, &mut inodes, walk_data.use_apparent_size) clean_inodes(node, &mut inodes, walk_data.use_apparent_size)
}) })
.collect(); .collect();
@@ -125,7 +126,6 @@ fn ignore_file(entry: &DirEntry, walk_data: &WalkData) -> bool {
fn walk(dir: PathBuf, walk_data: &WalkData, depth: usize) -> Option<Node> { fn walk(dir: PathBuf, walk_data: &WalkData, depth: usize) -> Option<Node> {
let prog_data = &walk_data.progress_data; let prog_data = &walk_data.progress_data;
let mut children = vec![]; let mut children = vec![];
if let Ok(entries) = fs::read_dir(&dir) { if let Ok(entries) = fs::read_dir(&dir) {

View File

@@ -69,13 +69,19 @@ impl PAtomicInfo {
} }
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
fn format_indicator_str(data: &PAtomicInfo, progress_char_i: usize, status: &str) -> String {
format!( fn format_preparing_str(prog_char: char, data: &PAtomicInfo, is_iso: bool) -> String {
"\r{} \"{}\"... {}", let path_in = data.current_path.get();
status, let size = human_readable_number(data.total_file_size.load(ORDERING), is_iso);
data.current_path.get(), format!("Preparing: {} {} ... {}", path_in, size, prog_char)
PROGRESS_CHARS[progress_char_i], }
)
fn format_indexing_str(prog_char: char, data: &PAtomicInfo, is_iso: bool) -> String {
let path_in = data.current_path.get();
let file_count = data.num_files.load(ORDERING);
let size = human_readable_number(data.total_file_size.load(ORDERING), is_iso);
let file_str = format!("{} files, {}", file_count, size);
format!("Indexing: {} {} ... {}", path_in, file_str, prog_char)
} }
pub struct PIndicator { pub struct PIndicator {
@@ -109,24 +115,15 @@ impl PIndicator {
{ {
// Clear the text written by 'write!'& Return at the start of line // Clear the text written by 'write!'& Return at the start of line
print!("\r{:width$}", " ", width = msg.len()); print!("\r{:width$}", " ", width = msg.len());
let prog_char = PROGRESS_CHARS[progress_char_i];
msg = match data.state.load(ORDERING) { msg = match data.state.load(ORDERING) {
Operation::INDEXING => { Operation::INDEXING => format_indexing_str(prog_char, &data, is_iso),
let base = format_indicator_str(&data, progress_char_i, "Indexing"); Operation::PREPARING => format_preparing_str(prog_char, &data, is_iso),
let file_count = data.num_files.load(ORDERING);
let size =
human_readable_number(data.total_file_size.load(ORDERING), is_iso);
let file_str = format!("{} {} files", file_count, size);
format!("{} - {}", base, file_str)
}
Operation::PREPARING => {
format_indicator_str(&data, progress_char_i, "Preparing")
}
_ => panic!("Unknown State"), _ => panic!("Unknown State"),
}; };
write!(stdout, "{}", msg).unwrap(); write!(stdout, "\r{}", msg).unwrap();
stdout.flush().unwrap(); stdout.flush().unwrap();
progress_char_i += 1; progress_char_i += 1;