From bfd278abbe379e9c027b7ef4b6c9c0e268c29551 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Mon, 3 Jul 2023 12:11:41 +0200 Subject: [PATCH] feat: show backup sizes in list command --- src/backup/mod.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/backup/mod.rs b/src/backup/mod.rs index e1d6bfb..f569697 100644 --- a/src/backup/mod.rs +++ b/src/backup/mod.rs @@ -20,6 +20,8 @@ use std::fs::File; use std::io; use std::path::{Path, PathBuf}; +const BYTE_SUFFIXES: [&str; 5] = ["B", "KiB", "MiB", "GiB", "TiB"]; + #[derive(Debug, PartialEq, Serialize, Deserialize)] pub enum BackupType { Full, @@ -230,11 +232,24 @@ impl fmt::Display for Backup { BackupType::Incremental => 'I', }; + // Pretty-print size + // If your backup is a petabyte or larger, this will crash and you need to re-evaluate your + // life choices + let mut index = 0; + let mut size = self.size as f64; + + while size >= 1024.0 { + index += 1; + size /= 1024.0; + } + write!( f, - "{} ({}, {})", + "{} ({}, {:.2}{}, {})", self.start_time.format(Backup::FILENAME_FORMAT), letter, + size, + BYTE_SUFFIXES[index], self.delta ) }