mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-07 20:15:27 +00:00
rewrite progress module
This commit is contained in:
parent
ee082e1367
commit
8b0f4f3ee4
@ -19,11 +19,7 @@ use crate::{
|
|||||||
|
|
||||||
/// Unpacks the archive given by `archive` into the folder given by `into`.
|
/// Unpacks the archive given by `archive` into the folder given by `into`.
|
||||||
/// Assumes that output_folder is empty
|
/// Assumes that output_folder is empty
|
||||||
pub fn unpack_archive(
|
pub fn unpack_archive(reader: Box<dyn Read>, output_folder: &Path, mut out: impl Write) -> crate::Result<Vec<PathBuf>> {
|
||||||
reader: Box<dyn Read>,
|
|
||||||
output_folder: &Path,
|
|
||||||
mut display_handle: impl Write,
|
|
||||||
) -> crate::Result<Vec<PathBuf>> {
|
|
||||||
assert!(output_folder.read_dir().expect("dir exists").count() == 0);
|
assert!(output_folder.read_dir().expect("dir exists").count() == 0);
|
||||||
let mut archive = tar::Archive::new(reader);
|
let mut archive = tar::Archive::new(reader);
|
||||||
|
|
||||||
@ -40,7 +36,7 @@ pub fn unpack_archive(
|
|||||||
// and so on
|
// and so on
|
||||||
|
|
||||||
info!(
|
info!(
|
||||||
@display_handle,
|
@out,
|
||||||
inaccessible,
|
inaccessible,
|
||||||
"{:?} extracted. ({})",
|
"{:?} extracted. ({})",
|
||||||
utils::strip_cur_dir(&output_folder.join(file.path()?)), Bytes::new(file.size())
|
utils::strip_cur_dir(&output_folder.join(file.path()?)), Bytes::new(file.size())
|
||||||
@ -86,7 +82,7 @@ pub fn build_archive_from_paths<W, D>(
|
|||||||
input_filenames: &[PathBuf],
|
input_filenames: &[PathBuf],
|
||||||
writer: W,
|
writer: W,
|
||||||
file_visibility_policy: FileVisibilityPolicy,
|
file_visibility_policy: FileVisibilityPolicy,
|
||||||
mut display_handle: D,
|
mut out: D,
|
||||||
) -> crate::Result<W>
|
) -> crate::Result<W>
|
||||||
where
|
where
|
||||||
W: Write,
|
W: Write,
|
||||||
@ -108,7 +104,7 @@ where
|
|||||||
// little importance for most users, but would generate lots of
|
// little importance for most users, but would generate lots of
|
||||||
// spoken text for users using screen readers, braille displays
|
// spoken text for users using screen readers, braille displays
|
||||||
// and so on
|
// and so on
|
||||||
info!(@display_handle, inaccessible, "Compressing '{}'.", utils::to_utf(path));
|
info!(@out, inaccessible, "Compressing '{}'.", utils::to_utf(path));
|
||||||
|
|
||||||
if path.is_dir() {
|
if path.is_dir() {
|
||||||
builder.append_dir(path, path)?;
|
builder.append_dir(path, path)?;
|
||||||
|
@ -27,11 +27,7 @@ use crate::{
|
|||||||
|
|
||||||
/// Unpacks the archive given by `archive` into the folder given by `output_folder`.
|
/// Unpacks the archive given by `archive` into the folder given by `output_folder`.
|
||||||
/// Assumes that output_folder is empty
|
/// Assumes that output_folder is empty
|
||||||
pub fn unpack_archive<R, D>(
|
pub fn unpack_archive<R, D>(mut archive: ZipArchive<R>, output_folder: &Path, mut out: D) -> crate::Result<Vec<PathBuf>>
|
||||||
mut archive: ZipArchive<R>,
|
|
||||||
output_folder: &Path,
|
|
||||||
mut display_handle: D,
|
|
||||||
) -> crate::Result<Vec<PathBuf>>
|
|
||||||
where
|
where
|
||||||
R: Read + Seek,
|
R: Read + Seek,
|
||||||
D: Write,
|
D: Write,
|
||||||
@ -57,7 +53,7 @@ where
|
|||||||
// importance for most users, but would generate lots of
|
// importance for most users, but would generate lots of
|
||||||
// spoken text for users using screen readers, braille displays
|
// spoken text for users using screen readers, braille displays
|
||||||
// and so on
|
// 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)?;
|
fs::create_dir_all(&file_path)?;
|
||||||
}
|
}
|
||||||
_is_file @ false => {
|
_is_file @ false => {
|
||||||
@ -70,7 +66,7 @@ where
|
|||||||
|
|
||||||
// same reason is in _is_dir: long, often not needed text
|
// same reason is in _is_dir: long, often not needed text
|
||||||
info!(
|
info!(
|
||||||
@display_handle,
|
@out,
|
||||||
inaccessible,
|
inaccessible,
|
||||||
"{:?} extracted. ({})",
|
"{:?} extracted. ({})",
|
||||||
file_path.display(), Bytes::new(file.size())
|
file_path.display(), Bytes::new(file.size())
|
||||||
@ -137,7 +133,7 @@ pub fn build_archive_from_paths<W, D>(
|
|||||||
input_filenames: &[PathBuf],
|
input_filenames: &[PathBuf],
|
||||||
writer: W,
|
writer: W,
|
||||||
file_visibility_policy: FileVisibilityPolicy,
|
file_visibility_policy: FileVisibilityPolicy,
|
||||||
mut display_handle: D,
|
mut out: D,
|
||||||
) -> crate::Result<W>
|
) -> crate::Result<W>
|
||||||
where
|
where
|
||||||
W: Write + Seek,
|
W: Write + Seek,
|
||||||
@ -177,7 +173,7 @@ where
|
|||||||
// little importance for most users, but would generate lots of
|
// little importance for most users, but would generate lots of
|
||||||
// spoken text for users using screen readers, braille displays
|
// spoken text for users using screen readers, braille displays
|
||||||
// and so on
|
// and so on
|
||||||
info!(@display_handle, inaccessible, "Compressing '{}'.", to_utf(path));
|
info!(@out, inaccessible, "Compressing '{}'.", to_utf(path));
|
||||||
|
|
||||||
let metadata = match path.metadata() {
|
let metadata = match path.metadata() {
|
||||||
Ok(metadata) => metadata,
|
Ok(metadata) => metadata,
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
use std::{
|
use std::{
|
||||||
io::{self, BufWriter, Write},
|
io::{self, BufWriter, Cursor, Seek, Write},
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
use fs_err as fs;
|
use fs_err as fs;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
accessible::is_running_in_accessible_mode,
|
||||||
archive,
|
archive,
|
||||||
commands::warn_user_about_loading_zip_in_memory,
|
commands::warn_user_about_loading_zip_in_memory,
|
||||||
extension::{
|
extension::{
|
||||||
@ -42,12 +43,6 @@ pub fn compress_files(
|
|||||||
(total_size + size, and_precise & precise)
|
(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 file_writer = BufWriter::with_capacity(BUFFER_CAPACITY, output_file);
|
||||||
|
|
||||||
let mut writer: Box<dyn Write> = Box::new(file_writer);
|
let mut writer: Box<dyn Write> = Box::new(file_writer);
|
||||||
@ -80,37 +75,28 @@ pub fn compress_files(
|
|||||||
|
|
||||||
match first_format {
|
match first_format {
|
||||||
Gzip | Bzip | Lz4 | Lzma | Snappy | Zstd => {
|
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)?;
|
writer = chain_writer_encoder(&first_format, writer)?;
|
||||||
let mut reader = fs::File::open(&files[0]).unwrap();
|
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 => {
|
Tar => {
|
||||||
let mut progress = Progress::new_accessible_aware(
|
if is_running_in_accessible_mode() {
|
||||||
total_input_size,
|
archive::tar::build_archive_from_paths(&files, &mut writer, file_visibility_policy, io::stdout())?;
|
||||||
precise,
|
writer.flush()?;
|
||||||
Some(Box::new(move || {
|
} else {
|
||||||
output_file_path.metadata().expect("file exists").len()
|
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()?;
|
||||||
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()?;
|
|
||||||
}
|
}
|
||||||
Zip => {
|
Zip => {
|
||||||
if !formats.is_empty() {
|
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 = {
|
if is_running_in_accessible_mode() {
|
||||||
let vec_buffer_ptr = {
|
archive::zip::build_archive_from_paths(&files, &mut vec_buffer, file_visibility_policy, io::stdout())?;
|
||||||
struct FlyPtr(*const io::Cursor<Vec<u8>>);
|
vec_buffer.rewind()?;
|
||||||
unsafe impl Send for FlyPtr {}
|
io::copy(&mut vec_buffer, &mut writer)?;
|
||||||
FlyPtr(&vec_buffer as *const _)
|
} else {
|
||||||
};
|
let mut progress = Progress::new(total_input_size, precise, true);
|
||||||
Box::new(move || {
|
archive::zip::build_archive_from_paths(&files, &mut vec_buffer, file_visibility_policy, &mut progress)?;
|
||||||
let vec_buffer_ptr = &vec_buffer_ptr;
|
vec_buffer.rewind()?;
|
||||||
// Safety: ptr is valid and vec_buffer is still alive
|
io::copy(&mut progress.wrap_read(vec_buffer), &mut writer)?;
|
||||||
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)?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ use std::{
|
|||||||
use fs_err as fs;
|
use fs_err as fs;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
accessible::is_running_in_accessible_mode,
|
||||||
commands::warn_user_about_loading_zip_in_memory,
|
commands::warn_user_about_loading_zip_in_memory,
|
||||||
extension::{
|
extension::{
|
||||||
split_first_compression_format,
|
split_first_compression_format,
|
||||||
@ -110,13 +111,15 @@ pub fn decompress_file(
|
|||||||
}
|
}
|
||||||
let mut writer = writer.unwrap();
|
let mut writer = writer.unwrap();
|
||||||
|
|
||||||
let current_position_fn = Box::new({
|
if is_running_in_accessible_mode() {
|
||||||
let output_file_path = output_file_path.clone();
|
io::copy(&mut reader, &mut writer)?;
|
||||||
move || output_file_path.clone().metadata().expect("file exists").len()
|
} else {
|
||||||
});
|
io::copy(
|
||||||
let _progress = Progress::new_accessible_aware(total_input_size, true, Some(current_position_fn));
|
&mut Progress::new(total_input_size, true, true).wrap_read(reader),
|
||||||
|
&mut writer,
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
|
||||||
io::copy(&mut reader, &mut writer)?;
|
|
||||||
vec![output_file_path]
|
vec![output_file_path]
|
||||||
}
|
}
|
||||||
Tar => {
|
Tar => {
|
||||||
@ -196,13 +199,11 @@ fn smart_unpack(
|
|||||||
);
|
);
|
||||||
|
|
||||||
// unpack the files
|
// unpack the files
|
||||||
let files = unpack_fn(
|
let files = if is_running_in_accessible_mode() {
|
||||||
temp_dir_path,
|
unpack_fn(temp_dir_path, &mut io::stdout())
|
||||||
Progress::new_accessible_aware(total_input_size, true, None)
|
} else {
|
||||||
.as_mut()
|
unpack_fn(temp_dir_path, &mut Progress::new(total_input_size, true, false))
|
||||||
.map(Progress::display_handle)
|
}?;
|
||||||
.unwrap_or(&mut io::stdout()),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let root_contains_only_one_element = fs::read_dir(temp_dir_path)?.count() == 1;
|
let root_contains_only_one_element = fs::read_dir(temp_dir_path)?.count() == 1;
|
||||||
if root_contains_only_one_element {
|
if root_contains_only_one_element {
|
||||||
|
@ -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.
|
/// 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
|
/// By default `info` outputs to Stdout, if you want to specify the output you can use
|
||||||
/// `@display_handle` modifier
|
/// `@out` modifier
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! info {
|
macro_rules! info {
|
||||||
@ -27,24 +27,24 @@ macro_rules! info {
|
|||||||
(accessible, $($arg:tt)*) => {
|
(accessible, $($arg:tt)*) => {
|
||||||
info!(@::std::io::stdout(), accessible, $($arg)*);
|
info!(@::std::io::stdout(), accessible, $($arg)*);
|
||||||
};
|
};
|
||||||
(@$display_handle: expr, accessible, $($arg:tt)*) => {
|
(@$out: expr, accessible, $($arg:tt)*) => {
|
||||||
let display_handle = &mut $display_handle;
|
let out = &mut $out;
|
||||||
// if in ACCESSIBLE mode, suppress the "[INFO]" and just print the message
|
// if in ACCESSIBLE mode, suppress the "[INFO]" and just print the message
|
||||||
if !$crate::accessible::is_running_in_accessible_mode() {
|
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.
|
// Inccessible (long/no important) info message.
|
||||||
// Print info message if ACCESSIBLE is not turned on
|
// Print info message if ACCESSIBLE is not turned on
|
||||||
(inaccessible, $($arg:tt)*) => {
|
(inaccessible, $($arg:tt)*) => {
|
||||||
info!(@::std::io::stdout(), inaccessible, $($arg)*);
|
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() {
|
if !$crate::accessible::is_running_in_accessible_mode() {
|
||||||
let display_handle = &mut $display_handle;
|
let out = &mut $out;
|
||||||
$crate::macros::_info_helper(display_handle);
|
$crate::macros::_info_helper(out);
|
||||||
writeln!(display_handle, $($arg)*).unwrap();
|
writeln!(out, $($arg)*).unwrap();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
134
src/progress.rs
134
src/progress.rs
@ -1,122 +1,68 @@
|
|||||||
//! Module that provides functions to display progress bars for compressing and decompressing files.
|
//! Module that provides functions to display progress bars for compressing and decompressing files.
|
||||||
use std::{
|
use std::{
|
||||||
io,
|
io::{self, Read, Write},
|
||||||
sync::mpsc::{self, Receiver, Sender},
|
mem,
|
||||||
thread,
|
|
||||||
time::Duration,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use indicatif::{ProgressBar, ProgressStyle};
|
use indicatif::{ProgressBar, ProgressBarIter, ProgressStyle};
|
||||||
|
|
||||||
use crate::accessible::is_running_in_accessible_mode;
|
|
||||||
|
|
||||||
/// Draw a ProgressBar using a function that checks periodically for the progress
|
/// Draw a ProgressBar using a function that checks periodically for the progress
|
||||||
pub struct Progress {
|
pub struct Progress {
|
||||||
draw_stop: Sender<()>,
|
bar: ProgressBar,
|
||||||
clean_done: Receiver<()>,
|
buf: Vec<u8>,
|
||||||
display_handle: DisplayHandle,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Writes to this struct will be displayed on the progress bar or stdout depending on the
|
impl Write for Progress {
|
||||||
/// ProgressBarPolicy
|
|
||||||
struct DisplayHandle {
|
|
||||||
buf: Vec<u8>,
|
|
||||||
sender: Sender<String>,
|
|
||||||
}
|
|
||||||
impl io::Write for DisplayHandle {
|
|
||||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||||
self.buf.extend(buf);
|
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.buf.pop();
|
||||||
self.flush()?;
|
self.flush()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(buf.len())
|
Ok(buf.len())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flush(&mut self) -> io::Result<()> {
|
fn flush(&mut self) -> io::Result<()> {
|
||||||
fn io_error<X>(_: X) -> io::Error {
|
self.bar.set_message(
|
||||||
io::Error::new(io::ErrorKind::Other, "failed to flush buffer")
|
String::from_utf8(mem::take(&mut self.buf))
|
||||||
}
|
.map_err(|_| io::Error::new(io::ErrorKind::Other, "Failed to parse buffer content as utf8"))?,
|
||||||
self.sender
|
);
|
||||||
.send(String::from_utf8(self.buf.drain(..).collect()).map_err(io_error)?)
|
Ok(())
|
||||||
.map_err(io_error)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Progress {
|
impl Progress {
|
||||||
/// Create a ProgressBar using a function that checks periodically for the progress
|
pub(crate) fn new(total_input_size: u64, precise: bool, position_updates: bool) -> Self {
|
||||||
/// If precise is true, the total_input_size will be displayed as the total_bytes size
|
let template = {
|
||||||
/// If ACCESSIBLE is set, this function returns None
|
let mut t = String::new();
|
||||||
pub fn new_accessible_aware(
|
t += "{wide_msg} [{elapsed_precise}] ";
|
||||||
total_input_size: u64,
|
if precise && position_updates {
|
||||||
precise: bool,
|
t += "[{bar:.cyan/blue}] ";
|
||||||
current_position_fn: Option<Box<dyn Fn() -> u64 + Send>>,
|
} else {
|
||||||
) -> Option<Self> {
|
t += "{spinner:.green} ";
|
||||||
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<Box<dyn Fn() -> 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));
|
|
||||||
}
|
}
|
||||||
bar.finish();
|
if position_updates {
|
||||||
let _ = clean_tx.send(());
|
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 {
|
Progress { bar, buf: Vec::new() }
|
||||||
draw_stop: draw_tx,
|
|
||||||
clean_done: clean_rx,
|
|
||||||
display_handle: DisplayHandle {
|
|
||||||
buf: Vec::new(),
|
|
||||||
sender: msg_tx,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn display_handle(&mut self) -> &mut dyn io::Write {
|
pub(crate) fn wrap_read<R: Read>(&self, read: R) -> ProgressBarIter<R> {
|
||||||
&mut self.display_handle
|
self.bar.wrap_read(read)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
impl Drop for Progress {
|
pub(crate) fn wrap_write<W: Write>(&self, write: W) -> ProgressBarIter<W> {
|
||||||
fn drop(&mut self) {
|
self.bar.wrap_write(write)
|
||||||
let _ = self.draw_stop.send(());
|
|
||||||
let _ = self.clean_done.recv();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user