use std::io;
#[cfg(feature = "progress-unit-bytes")]
pub use bytesize;
pub use prodash::{
self,
messages::MessageLevel,
progress::{
AtomicStep, Discard, DoOrDiscard, Either, Id, Step, StepShared, Task, ThroughputOnDrop, Value, UNKNOWN,
},
unit, BoxedDynNestedProgress, Count, DynNestedProgress, DynNestedProgressToNestedProgress, NestedProgress,
Progress, Unit,
};
#[cfg(not(feature = "progress-unit-bytes"))]
pub mod bytesize {
pub struct ByteSize(pub u64);
impl std::fmt::Display for ByteSize {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
}
#[cfg(feature = "progress-unit-bytes")]
pub fn bytes() -> Option<Unit> {
Some(unit::dynamic_and_mode(
unit::Bytes,
unit::display::Mode::with_throughput().and_percentage(),
))
}
#[cfg(not(feature = "progress-unit-bytes"))]
pub fn bytes() -> Option<Unit> {
Some(unit::label_and_mode(
"B",
unit::display::Mode::with_throughput().and_percentage(),
))
}
pub fn count(name: &'static str) -> Option<Unit> {
count_with_decimals(name, 1)
}
#[cfg(feature = "progress-unit-human-numbers")]
pub fn count_with_decimals(name: &'static str, decimals: usize) -> Option<Unit> {
Some(unit::dynamic_and_mode(
unit::Human::new(
{
let mut f = unit::human::Formatter::new();
f.with_decimals(decimals);
f
},
name,
),
unit::display::Mode::with_throughput().and_percentage(),
))
}
#[cfg(not(feature = "progress-unit-human-numbers"))]
pub fn count_with_decimals(name: &'static str, _decimals: usize) -> Option<Unit> {
Some(unit::label_and_mode(
name,
unit::display::Mode::with_throughput().and_percentage(),
))
}
pub fn steps() -> Option<Unit> {
Some(unit::dynamic(unit::Range::new("steps")))
}
pub struct Read<T, P> {
pub inner: T,
pub progress: P,
}
impl<T, P> io::Read for Read<T, P>
where
T: io::Read,
P: Progress,
{
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let bytes_read = self.inner.read(buf)?;
self.progress.inc_by(bytes_read);
Ok(bytes_read)
}
}
impl<T, P> io::BufRead for Read<T, P>
where
T: io::BufRead,
P: Progress,
{
fn fill_buf(&mut self) -> io::Result<&[u8]> {
self.inner.fill_buf()
}
fn consume(&mut self, amt: usize) {
self.inner.consume(amt);
}
}
pub struct Write<T, P> {
pub inner: T,
pub progress: P,
}
impl<T, P> io::Write for Write<T, P>
where
T: io::Write,
P: Progress,
{
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let written = self.inner.write(buf)?;
self.progress.inc_by(written);
Ok(written)
}
fn flush(&mut self) -> io::Result<()> {
self.inner.flush()
}
}
impl<T, P> io::Seek for Write<T, P>
where
T: io::Seek,
{
fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
self.inner.seek(pos)
}
}