Skip to content

Commit

Permalink
Merge pull request #210 from sigmaSd/progress
Browse files Browse the repository at this point in the history
Add progress to compressing/decompressing
  • Loading branch information
marcospb19 committed Nov 25, 2021
2 parents b31f407 + 0976970 commit bafc2d3
Show file tree
Hide file tree
Showing 10 changed files with 291 additions and 41 deletions.
48 changes: 48 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ xz2 = "0.1.6"
zip = { version = "0.5.13", default-features = false }
zstd = { version = "0.9.0", default-features = false }
tempfile = "3.2.0"
indicatif = "0.16.2"

[build-dependencies]
clap = "=3.0.0-beta.5"
Expand Down
17 changes: 7 additions & 10 deletions src/archive/tar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,30 @@ use crate::{
info,
list::FileInArchive,
utils::{self, Bytes},
QuestionPolicy,
};

/// Unpacks the archive given by `archive` into the folder given by `into`.
/// Assumes that output_folder is empty
pub fn unpack_archive(
reader: Box<dyn Read>,
output_folder: &Path,
question_policy: QuestionPolicy,
mut display_handle: impl Write,
) -> crate::Result<Vec<PathBuf>> {
assert!(output_folder.read_dir().expect("dir exists").count() == 0);
let mut archive = tar::Archive::new(reader);

let mut files_unpacked = vec![];
for file in archive.entries()? {
let mut file = file?;

let file_path = output_folder.join(file.path()?);
if !utils::clear_path(&file_path, question_policy)? {
// User doesn't want to overwrite
continue;
}

file.unpack_in(output_folder)?;

// This is printed for every file in the archive and has little
// importance for most users, but would generate lots of
// spoken text for users using screen readers, braille displays
// and so on
info!(inaccessible, "{:?} extracted. ({})", output_folder.join(file.path()?), Bytes::new(file.size()));
info!(@display_handle, inaccessible, "{:?} extracted. ({})", output_folder.join(file.path()?), Bytes::new(file.size()));

files_unpacked.push(file_path);
}
Expand All @@ -68,9 +64,10 @@ pub fn list_archive(reader: Box<dyn Read>) -> crate::Result<Vec<FileInArchive>>
}

/// Compresses the archives given by `input_filenames` into the file given previously to `writer`.
pub fn build_archive_from_paths<W>(input_filenames: &[PathBuf], writer: W) -> crate::Result<W>
pub fn build_archive_from_paths<W, D>(input_filenames: &[PathBuf], writer: W, mut display_handle: D) -> crate::Result<W>
where
W: Write,
D: Write,
{
let mut builder = tar::Builder::new(writer);

Expand All @@ -88,7 +85,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!(inaccessible, "Compressing '{}'.", utils::to_utf(path));
info!(@display_handle, inaccessible, "Compressing '{}'.", utils::to_utf(path));

if path.is_dir() {
builder.append_dir(path, path)?;
Expand Down
31 changes: 15 additions & 16 deletions src/archive/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,23 @@ use crate::{
info,
list::FileInArchive,
utils::{
cd_into_same_dir_as, clear_path, concatenate_os_str_list, dir_is_empty, get_invalid_utf8_paths, strip_cur_dir,
to_utf, Bytes,
cd_into_same_dir_as, concatenate_os_str_list, dir_is_empty, get_invalid_utf8_paths, strip_cur_dir, to_utf,
Bytes,
},
QuestionPolicy,
};

/// Unpacks the archive given by `archive` into the folder given by `into`.
pub fn unpack_archive<R>(
/// Unpacks the archive given by `archive` into the folder given by `output_folder`.
/// Assumes that output_folder is empty
pub fn unpack_archive<R, D>(
mut archive: ZipArchive<R>,
into: &Path,
question_policy: QuestionPolicy,
output_folder: &Path,
mut display_handle: D,
) -> crate::Result<Vec<PathBuf>>
where
R: Read + Seek,
D: Write,
{
assert!(output_folder.read_dir().expect("dir exists").count() == 0);
let mut unpacked_files = vec![];
for idx in 0..archive.len() {
let mut file = archive.by_index(idx)?;
Expand All @@ -38,11 +40,7 @@ where
None => continue,
};

let file_path = into.join(file_path);
if !clear_path(&file_path, question_policy)? {
// User doesn't want to overwrite
continue;
}
let file_path = output_folder.join(file_path);

check_for_comments(&file);

Expand All @@ -52,7 +50,7 @@ where
// importance for most users, but would generate lots of
// spoken text for users using screen readers, braille displays
// and so on
info!(inaccessible, "File {} extracted to \"{}\"", idx, file_path.display());
info!(@display_handle, inaccessible, "File {} extracted to \"{}\"", idx, file_path.display());
fs::create_dir_all(&file_path)?;
}
_is_file @ false => {
Expand All @@ -64,7 +62,7 @@ where
let file_path = strip_cur_dir(file_path.as_path());

// same reason is in _is_dir: long, often not needed text
info!(inaccessible, "{:?} extracted. ({})", file_path.display(), Bytes::new(file.size()));
info!(@display_handle, inaccessible, "{:?} extracted. ({})", file_path.display(), Bytes::new(file.size()));

let mut output_file = fs::File::create(&file_path)?;
io::copy(&mut file, &mut output_file)?;
Expand Down Expand Up @@ -102,9 +100,10 @@ where
}

/// Compresses the archives given by `input_filenames` into the file given previously to `writer`.
pub fn build_archive_from_paths<W>(input_filenames: &[PathBuf], writer: W) -> crate::Result<W>
pub fn build_archive_from_paths<W, D>(input_filenames: &[PathBuf], writer: W, mut display_handle: D) -> crate::Result<W>
where
W: Write + Seek,
D: Write,
{
let mut writer = zip::ZipWriter::new(writer);
let options = zip::write::FileOptions::default();
Expand Down Expand Up @@ -134,7 +133,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!(inaccessible, "Compressing '{}'.", to_utf(path));
info!(@display_handle, inaccessible, "Compressing '{}'.", to_utf(path));

if path.is_dir() {
if dir_is_empty(path) {
Expand Down
3 changes: 2 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use once_cell::sync::OnceCell;
use crate::{Opts, QuestionPolicy, Subcommand};

/// Whether to enable accessible output (removes info output and reduces other
/// output, removes visual markers like '[' and ']')
/// output, removes visual markers like '[' and ']').
/// Removes th progress bar as well
pub static ACCESSIBLE: OnceCell<bool> = OnceCell::new();

impl Opts {
Expand Down
Loading

0 comments on commit bafc2d3

Please sign in to comment.