diff --git a/src/archive/tar.rs b/src/archive/tar.rs index 85b41b7..101a0e4 100644 --- a/src/archive/tar.rs +++ b/src/archive/tar.rs @@ -19,11 +19,7 @@ use crate::{ /// Unpacks the archive given by `archive` into the folder given by `into`. /// Assumes that output_folder is empty -pub fn unpack_archive( - reader: Box, - output_folder: &Path, - mut display_handle: impl Write, -) -> crate::Result> { +pub fn unpack_archive(reader: Box, output_folder: &Path, mut out: impl Write) -> crate::Result> { assert!(output_folder.read_dir().expect("dir exists").count() == 0); let mut archive = tar::Archive::new(reader); @@ -40,7 +36,7 @@ pub fn unpack_archive( // and so on info!( - @display_handle, + @out, inaccessible, "{:?} extracted. ({})", utils::strip_cur_dir(&output_folder.join(file.path()?)), Bytes::new(file.size()) @@ -86,7 +82,7 @@ pub fn build_archive_from_paths( input_filenames: &[PathBuf], writer: W, file_visibility_policy: FileVisibilityPolicy, - mut display_handle: D, + mut out: D, ) -> crate::Result where W: Write, @@ -108,7 +104,7 @@ where // little importance for most users, but would generate lots of // spoken text for users using screen readers, braille displays // and so on - info!(@display_handle, inaccessible, "Compressing '{}'.", utils::to_utf(path)); + info!(@out, inaccessible, "Compressing '{}'.", utils::to_utf(path)); if path.is_dir() { builder.append_dir(path, path)?; diff --git a/src/archive/zip.rs b/src/archive/zip.rs index fd0ecfb..ed27b80 100644 --- a/src/archive/zip.rs +++ b/src/archive/zip.rs @@ -27,11 +27,7 @@ use crate::{ /// Unpacks the archive given by `archive` into the folder given by `output_folder`. /// Assumes that output_folder is empty -pub fn unpack_archive( - mut archive: ZipArchive, - output_folder: &Path, - mut display_handle: D, -) -> crate::Result> +pub fn unpack_archive(mut archive: ZipArchive, output_folder: &Path, mut out: D) -> crate::Result> where R: Read + Seek, D: Write, @@ -57,7 +53,7 @@ where // importance for most users, but would generate lots of // spoken text for users using screen readers, braille displays // and so on - info!(@display_handle, inaccessible, "File {} extracted to \"{}\"", idx, file_path.display()); + info!(@out, inaccessible, "File {} extracted to \"{}\"", idx, file_path.display()); fs::create_dir_all(&file_path)?; } _is_file @ false => { @@ -70,7 +66,7 @@ where // same reason is in _is_dir: long, often not needed text info!( - @display_handle, + @out, inaccessible, "{:?} extracted. ({})", file_path.display(), Bytes::new(file.size()) @@ -137,7 +133,7 @@ pub fn build_archive_from_paths( input_filenames: &[PathBuf], writer: W, file_visibility_policy: FileVisibilityPolicy, - mut display_handle: D, + mut out: D, ) -> crate::Result where W: Write + Seek, @@ -177,7 +173,7 @@ where // little importance for most users, but would generate lots of // spoken text for users using screen readers, braille displays // and so on - info!(@display_handle, inaccessible, "Compressing '{}'.", to_utf(path)); + info!(@out, inaccessible, "Compressing '{}'.", to_utf(path)); let metadata = match path.metadata() { Ok(metadata) => metadata, diff --git a/src/commands/compress.rs b/src/commands/compress.rs index ddf24f8..440b9fc 100644 --- a/src/commands/compress.rs +++ b/src/commands/compress.rs @@ -1,11 +1,12 @@ use std::{ - io::{self, BufWriter, Write}, + io::{self, BufWriter, Cursor, Seek, Write}, path::{Path, PathBuf}, }; use fs_err as fs; use crate::{ + accessible::is_running_in_accessible_mode, archive, commands::warn_user_about_loading_zip_in_memory, extension::{ @@ -42,12 +43,6 @@ pub fn compress_files( (total_size + size, and_precise & precise) }); - // NOTE: canonicalize is here to avoid a weird bug: - // > If output_file_path is a nested path and it exists and the user overwrite it - // >> output_file_path.exists() will always return false (somehow) - // - canonicalize seems to fix this - let output_file_path = output_file.path().canonicalize()?; - let file_writer = BufWriter::with_capacity(BUFFER_CAPACITY, output_file); let mut writer: Box = Box::new(file_writer); @@ -80,37 +75,28 @@ pub fn compress_files( match first_format { Gzip | Bzip | Lz4 | Lzma | Snappy | Zstd => { - let _progress = Progress::new_accessible_aware( - total_input_size, - precise, - Some(Box::new(move || { - output_file_path.metadata().expect("file exists").len() - })), - ); - writer = chain_writer_encoder(&first_format, writer)?; let mut reader = fs::File::open(&files[0]).unwrap(); - io::copy(&mut reader, &mut writer)?; + + if is_running_in_accessible_mode() { + io::copy(&mut reader, &mut writer)?; + } else { + io::copy( + &mut Progress::new(total_input_size, precise, true).wrap_read(reader), + &mut writer, + )?; + } } Tar => { - let mut progress = Progress::new_accessible_aware( - total_input_size, - precise, - Some(Box::new(move || { - output_file_path.metadata().expect("file exists").len() - })), - ); - - archive::tar::build_archive_from_paths( - &files, - &mut writer, - file_visibility_policy, - progress - .as_mut() - .map(Progress::display_handle) - .unwrap_or(&mut io::stdout()), - )?; - writer.flush()?; + if is_running_in_accessible_mode() { + archive::tar::build_archive_from_paths(&files, &mut writer, file_visibility_policy, io::stdout())?; + writer.flush()?; + } else { + let mut progress = Progress::new(total_input_size, precise, true); + let mut writer = progress.wrap_write(writer); + archive::tar::build_archive_from_paths(&files, &mut writer, file_visibility_policy, &mut progress)?; + writer.flush()?; + } } Zip => { if !formats.is_empty() { @@ -122,34 +108,18 @@ pub fn compress_files( } } - let mut vec_buffer = io::Cursor::new(vec![]); + let mut vec_buffer = Cursor::new(vec![]); - let current_position_fn = { - let vec_buffer_ptr = { - struct FlyPtr(*const io::Cursor>); - unsafe impl Send for FlyPtr {} - FlyPtr(&vec_buffer as *const _) - }; - Box::new(move || { - let vec_buffer_ptr = &vec_buffer_ptr; - // Safety: ptr is valid and vec_buffer is still alive - unsafe { &*vec_buffer_ptr.0 }.position() - }) - }; - - let mut progress = Progress::new_accessible_aware(total_input_size, precise, Some(current_position_fn)); - - archive::zip::build_archive_from_paths( - &files, - &mut vec_buffer, - file_visibility_policy, - progress - .as_mut() - .map(Progress::display_handle) - .unwrap_or(&mut io::stdout()), - )?; - let vec_buffer = vec_buffer.into_inner(); - io::copy(&mut vec_buffer.as_slice(), &mut writer)?; + if is_running_in_accessible_mode() { + archive::zip::build_archive_from_paths(&files, &mut vec_buffer, file_visibility_policy, io::stdout())?; + vec_buffer.rewind()?; + io::copy(&mut vec_buffer, &mut writer)?; + } else { + let mut progress = Progress::new(total_input_size, precise, true); + archive::zip::build_archive_from_paths(&files, &mut vec_buffer, file_visibility_policy, &mut progress)?; + vec_buffer.rewind()?; + io::copy(&mut progress.wrap_read(vec_buffer), &mut writer)?; + } } } diff --git a/src/commands/decompress.rs b/src/commands/decompress.rs index d918fe9..bddf7eb 100644 --- a/src/commands/decompress.rs +++ b/src/commands/decompress.rs @@ -7,6 +7,7 @@ use std::{ use fs_err as fs; use crate::{ + accessible::is_running_in_accessible_mode, commands::warn_user_about_loading_zip_in_memory, extension::{ split_first_compression_format, @@ -110,13 +111,15 @@ pub fn decompress_file( } let mut writer = writer.unwrap(); - let current_position_fn = Box::new({ - let output_file_path = output_file_path.clone(); - move || output_file_path.clone().metadata().expect("file exists").len() - }); - let _progress = Progress::new_accessible_aware(total_input_size, true, Some(current_position_fn)); + if is_running_in_accessible_mode() { + io::copy(&mut reader, &mut writer)?; + } else { + io::copy( + &mut Progress::new(total_input_size, true, true).wrap_read(reader), + &mut writer, + )?; + } - io::copy(&mut reader, &mut writer)?; vec![output_file_path] } Tar => { @@ -196,13 +199,11 @@ fn smart_unpack( ); // unpack the files - let files = unpack_fn( - temp_dir_path, - Progress::new_accessible_aware(total_input_size, true, None) - .as_mut() - .map(Progress::display_handle) - .unwrap_or(&mut io::stdout()), - )?; + let files = if is_running_in_accessible_mode() { + unpack_fn(temp_dir_path, &mut io::stdout()) + } else { + unpack_fn(temp_dir_path, &mut Progress::new(total_input_size, true, false)) + }?; let root_contains_only_one_element = fs::read_dir(temp_dir_path)?.count() == 1; if root_contains_only_one_element { diff --git a/src/macros.rs b/src/macros.rs index 9068b6d..6150a2b 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -18,7 +18,7 @@ use crate::accessible::is_running_in_accessible_mode; /// ability to skip some lines deemed not important like a seeing person would. /// /// By default `info` outputs to Stdout, if you want to specify the output you can use -/// `@display_handle` modifier +/// `@out` modifier #[macro_export] macro_rules! info { @@ -27,24 +27,24 @@ macro_rules! info { (accessible, $($arg:tt)*) => { info!(@::std::io::stdout(), accessible, $($arg)*); }; - (@$display_handle: expr, accessible, $($arg:tt)*) => { - let display_handle = &mut $display_handle; + (@$out: expr, accessible, $($arg:tt)*) => { + let out = &mut $out; // if in ACCESSIBLE mode, suppress the "[INFO]" and just print the message if !$crate::accessible::is_running_in_accessible_mode() { - $crate::macros::_info_helper(display_handle); + $crate::macros::_info_helper(out); } - writeln!(display_handle, $($arg)*).unwrap(); + writeln!(out, $($arg)*).unwrap(); }; // Inccessible (long/no important) info message. // Print info message if ACCESSIBLE is not turned on (inaccessible, $($arg:tt)*) => { info!(@::std::io::stdout(), inaccessible, $($arg)*); }; - (@$display_handle: expr, inaccessible, $($arg:tt)*) => { + (@$out: expr, inaccessible, $($arg:tt)*) => { if !$crate::accessible::is_running_in_accessible_mode() { - let display_handle = &mut $display_handle; - $crate::macros::_info_helper(display_handle); - writeln!(display_handle, $($arg)*).unwrap(); + let out = &mut $out; + $crate::macros::_info_helper(out); + writeln!(out, $($arg)*).unwrap(); } }; } diff --git a/src/progress.rs b/src/progress.rs index 7ec3683..5c2a530 100644 --- a/src/progress.rs +++ b/src/progress.rs @@ -1,122 +1,68 @@ //! Module that provides functions to display progress bars for compressing and decompressing files. use std::{ - io, - sync::mpsc::{self, Receiver, Sender}, - thread, - time::Duration, + io::{self, Read, Write}, + mem, }; -use indicatif::{ProgressBar, ProgressStyle}; - -use crate::accessible::is_running_in_accessible_mode; +use indicatif::{ProgressBar, ProgressBarIter, ProgressStyle}; /// Draw a ProgressBar using a function that checks periodically for the progress pub struct Progress { - draw_stop: Sender<()>, - clean_done: Receiver<()>, - display_handle: DisplayHandle, + bar: ProgressBar, + buf: Vec, } -/// Writes to this struct will be displayed on the progress bar or stdout depending on the -/// ProgressBarPolicy -struct DisplayHandle { - buf: Vec, - sender: Sender, -} -impl io::Write for DisplayHandle { +impl Write for Progress { fn write(&mut self, buf: &[u8]) -> io::Result { self.buf.extend(buf); - // Newline is the signal to flush - if matches!(buf.last(), Some(&b'\n')) { + + if self.buf.last() == Some(&b'\n') { self.buf.pop(); self.flush()?; } + Ok(buf.len()) } fn flush(&mut self) -> io::Result<()> { - fn io_error(_: X) -> io::Error { - io::Error::new(io::ErrorKind::Other, "failed to flush buffer") - } - self.sender - .send(String::from_utf8(self.buf.drain(..).collect()).map_err(io_error)?) - .map_err(io_error) + self.bar.set_message( + String::from_utf8(mem::take(&mut self.buf)) + .map_err(|_| io::Error::new(io::ErrorKind::Other, "Failed to parse buffer content as utf8"))?, + ); + Ok(()) } } impl Progress { - /// Create a ProgressBar using a function that checks periodically for the progress - /// If precise is true, the total_input_size will be displayed as the total_bytes size - /// If ACCESSIBLE is set, this function returns None - pub fn new_accessible_aware( - total_input_size: u64, - precise: bool, - current_position_fn: Option u64 + Send>>, - ) -> Option { - if is_running_in_accessible_mode() { - return None; - } - Some(Self::new(total_input_size, precise, current_position_fn)) - } - - fn new(total_input_size: u64, precise: bool, current_position_fn: Option u64 + Send>>) -> Self { - let (draw_tx, draw_rx) = mpsc::channel(); - let (clean_tx, clean_rx) = mpsc::channel(); - let (msg_tx, msg_rx) = mpsc::channel(); - - thread::spawn(move || { - let template = { - let mut t = String::new(); - t += "{wide_msg} [{elapsed_precise}] "; - if precise && current_position_fn.is_some() { - t += "[{bar:.cyan/blue}] "; - } else { - t += "{spinner:.green} "; - } - if current_position_fn.is_some() { - t += "{bytes}/ "; - } - if precise { - t += "{total_bytes} "; - } - t += "({bytes_per_sec}, {eta}) {path}"; - t - }; - let bar = ProgressBar::new(total_input_size) - .with_style(ProgressStyle::with_template(&template).unwrap().progress_chars("#>-")); - - while draw_rx.try_recv().is_err() { - if let Some(ref pos_fn) = current_position_fn { - bar.set_position(pos_fn()); - } else { - bar.tick(); - } - if let Ok(msg) = msg_rx.try_recv() { - bar.set_message(msg); - } - thread::sleep(Duration::from_millis(100)); + pub(crate) fn new(total_input_size: u64, precise: bool, position_updates: bool) -> Self { + let template = { + let mut t = String::new(); + t += "{wide_msg} [{elapsed_precise}] "; + if precise && position_updates { + t += "[{bar:.cyan/blue}] "; + } else { + t += "{spinner:.green} "; } - bar.finish(); - let _ = clean_tx.send(()); - }); + if position_updates { + t += "{bytes}/ "; + } + if precise { + t += "{total_bytes} "; + } + t += "({bytes_per_sec}, {eta}) {path}"; + t + }; + let bar = ProgressBar::new(total_input_size) + .with_style(ProgressStyle::with_template(&template).unwrap().progress_chars("#>-")); - Progress { - draw_stop: draw_tx, - clean_done: clean_rx, - display_handle: DisplayHandle { - buf: Vec::new(), - sender: msg_tx, - }, - } + Progress { bar, buf: Vec::new() } } - pub(crate) fn display_handle(&mut self) -> &mut dyn io::Write { - &mut self.display_handle - } -} -impl Drop for Progress { - fn drop(&mut self) { - let _ = self.draw_stop.send(()); - let _ = self.clean_done.recv(); + pub(crate) fn wrap_read(&self, read: R) -> ProgressBarIter { + self.bar.wrap_read(read) + } + + pub(crate) fn wrap_write(&self, write: W) -> ProgressBarIter { + self.bar.wrap_write(write) } }