From 678e8aafb30fbed371e5243dc4b7da9805ebeebb Mon Sep 17 00:00:00 2001 From: Chris Butler Date: Sat, 28 Oct 2023 22:50:59 -0600 Subject: [PATCH] Add minimal examples demonstrating Formatters This change adds some doc tests to show off what the formatter looks like. This change adds it for just the byte variants: - `HumanBytes`, which seems to forward to `BinaryBytes`? - `DecimalBytes`, 1 MB == 1 * 10**6 bytes - `BinaryBytes`, 1 MiB == 1 * (1 << 20) bytes --- src/format.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/format.rs b/src/format.rs index 7475a251..3325368c 100644 --- a/src/format.rs +++ b/src/format.rs @@ -19,14 +19,47 @@ pub struct FormattedDuration(pub Duration); pub struct HumanDuration(pub Duration); /// Formats bytes for human readability +/// +/// # Examples +/// ```rust +/// # use indicatif::HumanBytes; +/// assert_eq!("15 B", format!("{}", HumanBytes(15))); +/// assert_eq!("1.46 KiB", format!("{}", HumanBytes(1_500))); +/// assert_eq!("1.43 MiB", format!("{}", HumanBytes(1_500_000))); +/// assert_eq!("1.40 GiB", format!("{}", HumanBytes(1_500_000_000))); +/// assert_eq!("1.36 TiB", format!("{}", HumanBytes(1_500_000_000_000))); +/// assert_eq!("1.33 PiB", format!("{}", HumanBytes(1_500_000_000_000_000))); +/// ``` #[derive(Debug)] pub struct HumanBytes(pub u64); /// Formats bytes for human readability using SI prefixes +/// +/// # Examples +/// ```rust +/// # use indicatif::DecimalBytes; +/// assert_eq!("15 B", format!("{}", DecimalBytes(15))); +/// assert_eq!("1.50 kB", format!("{}", DecimalBytes(1_500))); +/// assert_eq!("1.50 MB", format!("{}", DecimalBytes(1_500_000))); +/// assert_eq!("1.50 GB", format!("{}", DecimalBytes(1_500_000_000))); +/// assert_eq!("1.50 TB", format!("{}", DecimalBytes(1_500_000_000_000))); +/// assert_eq!("1.50 PB", format!("{}", DecimalBytes(1_500_000_000_000_000))); +/// ``` #[derive(Debug)] pub struct DecimalBytes(pub u64); /// Formats bytes for human readability using ISO/IEC prefixes +/// +/// # Examples +/// ```rust +/// # use indicatif::BinaryBytes; +/// assert_eq!("15 B", format!("{}", BinaryBytes(15))); +/// assert_eq!("1.46 KiB", format!("{}", BinaryBytes(1_500))); +/// assert_eq!("1.43 MiB", format!("{}", BinaryBytes(1_500_000))); +/// assert_eq!("1.40 GiB", format!("{}", BinaryBytes(1_500_000_000))); +/// assert_eq!("1.36 TiB", format!("{}", BinaryBytes(1_500_000_000_000))); +/// assert_eq!("1.33 PiB", format!("{}", BinaryBytes(1_500_000_000_000_000))); +/// ``` #[derive(Debug)] pub struct BinaryBytes(pub u64);