refactor: update Progress bar: progress.rs

Remove none-needed macro
This commit is contained in:
andy.boot
2023-01-13 00:42:25 +00:00
parent 400ff513f4
commit 29957c1f2c
2 changed files with 43 additions and 48 deletions

View File

@@ -183,7 +183,9 @@ fn main() {
use_iso: iso, use_iso: iso,
ignore_hidden, // can we rm this? ignore_hidden, // can we rm this?
}; };
Some(PIndicator::spawn(conf)) let mut indicator = PIndicator::build_me(conf);
indicator.spawn();
Some(indicator)
}; };
// Must be a cleaner way to do this // Must be a cleaner way to do this
@@ -201,6 +203,7 @@ fn main() {
by_filecount, by_filecount,
ignore_hidden, ignore_hidden,
follow_links, follow_links,
// Maybe just arc::clone the whole PIndicator and send that down here:
progress_config: tmp_config, progress_config: tmp_config,
progress_data: tmp_data, progress_data: tmp_data,
}; };

View File

@@ -15,6 +15,11 @@ use crate::display;
pub const ATOMIC_ORDERING: Ordering = Ordering::Relaxed; pub const ATOMIC_ORDERING: Ordering = Ordering::Relaxed;
const SHOW_WALKING_AFTER: u64 = 0;
const PROGRESS_CHARS_DELTA: u64 = 100;
const PROGRESS_CHARS: [char; 4] = ['-', '\\', '|', '/'];
const PROGRESS_CHARS_LEN: usize = PROGRESS_CHARS.len();
// small wrappers for atomic number to reduce overhead // small wrappers for atomic number to reduce overhead
pub trait ThreadSyncTrait<T> { pub trait ThreadSyncTrait<T> {
fn set(&self, val: T); fn set(&self, val: T);
@@ -141,6 +146,14 @@ impl ThreadSyncMathTrait<u64> for TotalSize {
} }
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
fn format(data: &PAtomicInfo, progress_char_i: usize, s: &str) -> String {
format!(
"\r{} \"{}\"... {}",
s,
data.current_path.get(),
PROGRESS_CHARS[progress_char_i],
)
}
#[derive(Default)] #[derive(Default)]
pub struct PConfig { pub struct PConfig {
@@ -151,61 +164,44 @@ pub struct PConfig {
pub struct PIndicator { pub struct PIndicator {
thread_run: Arc<AtomicBool>, thread_run: Arc<AtomicBool>,
thread: JoinHandle<()>, pub thread: Option<JoinHandle<()>>,
pub data: Arc<PAtomicInfo>, pub data: Arc<PAtomicInfo>,
pub config: Arc<PConfig>, pub config: Arc<PConfig>,
} }
impl PIndicator { impl PIndicator {
pub fn spawn(config: PConfig) -> Self { pub fn build_me(c: PConfig) -> Self {
macro_rules! init_shared_data { Self {
(let $ident: ident, $ident2: ident = $value: expr) => { thread_run: Arc::new(AtomicBool::new(true)),
let $ident = Arc::new($value); thread: None,
let $ident2 = $ident.clone(); data: Arc::new(PAtomicInfo::new(&c)),
}; config: Arc::new(c),
} }
}
init_shared_data!(let instant, instant2 = Instant::now()); pub fn spawn(&mut self) {
init_shared_data!(let time_thread_run, time_thread_run2 = AtomicBool::new(true)); let instant = Instant::now();
init_shared_data!(let config, config2 = config); let data_thread = self.data.clone();
init_shared_data!(let data, data2 = PAtomicInfo::new(&config)); let is_building_data_const = self.thread_run.clone();
let c = self.config.clone();
let time_info_thread = std::thread::spawn(move || { let time_info_thread = std::thread::spawn(move || {
const SHOW_WALKING_AFTER: u64 = 0;
const PROGRESS_CHARS_DELTA: u64 = 100;
const PROGRESS_CHARS: [char; 4] = ['-', '\\', '|', '/'];
const PROGRESS_CHARS_LEN: usize = PROGRESS_CHARS.len();
let mut progress_char_i: usize = 0; let mut progress_char_i: usize = 0;
let mut stdout = std::io::stdout(); let mut stdout = std::io::stdout();
let mut last_msg_len = 0; let mut last_msg_len = 0;
while time_thread_run2.load(ATOMIC_ORDERING) { while is_building_data_const.load(ATOMIC_ORDERING) {
if instant2.elapsed() > Duration::from_secs(SHOW_WALKING_AFTER) { if instant.elapsed() > Duration::from_secs(SHOW_WALKING_AFTER) {
// print!("{:?}", *state2.read().unwrap());
// clear the line // clear the line
print!("\r{:width$}", " ", width = last_msg_len); print!("\r{:width$}", " ", width = last_msg_len);
macro_rules! format_base { let msg = match data_thread.state.get() {
($state: expr) => {
format!(
"\r{} \"{}\"... {}",
$state,
data2.current_path.get(),
PROGRESS_CHARS[progress_char_i],
)
};
}
let msg = match data2.state.get() {
Operation::INDEXING => { Operation::INDEXING => {
const PROPS_SEPARATOR: &str = ", "; const PROPS_SEPARATOR: &str = ", ";
let base = format_base!("Indexing"); let base = format(&data_thread, progress_char_i, "Indexing");
// why all the macros ?
macro_rules! format_property { macro_rules! format_property {
($value: ident, $singular: expr, $plural: expr) => { ($value: ident, $singular: expr, $plural: expr) => {
format!( format!(
@@ -218,11 +214,11 @@ impl PIndicator {
let mut main_props = Vec::new(); let mut main_props = Vec::new();
let fn_ = data2.file_number.get(); let fn_ = data_thread.file_number.get();
if config2.file_count_only { if c.file_count_only {
main_props.push(format_property!(fn_, "file", "files")); main_props.push(format_property!(fn_, "file", "files"));
} else { } else {
main_props.push(format!("{}", data2.total_file_size)); main_props.push(format!("{}", data_thread.total_file_size));
main_props.push(format_property!(fn_, "file", "files")); main_props.push(format_property!(fn_, "file", "files"));
}; };
@@ -230,7 +226,7 @@ impl PIndicator {
format!("{} - {}", base, main_props_str) format!("{} - {}", base, main_props_str)
} }
Operation::PREPARING => { Operation::PREPARING => {
format_base!("Preparing") format(&data_thread, progress_char_i, "Preparing")
} }
_ => panic!("Unknown State"), _ => panic!("Unknown State"),
}; };
@@ -256,17 +252,13 @@ impl PIndicator {
print!("\r"); print!("\r");
stdout.flush().unwrap(); stdout.flush().unwrap();
}); });
self.thread = Some(time_info_thread)
Self {
thread_run: time_thread_run,
thread: time_info_thread,
data,
config,
}
} }
pub fn stop(self) { pub fn stop(self) {
self.thread_run.store(false, ATOMIC_ORDERING); self.thread_run.store(false, ATOMIC_ORDERING);
self.thread.join().unwrap(); if let Some(t) = self.thread {
t.join().unwrap();
}
} }
} }