summaryrefslogtreecommitdiff
path: root/cli/main.rs
diff options
context:
space:
mode:
authorAli Hasani <a.hassssani@gmail.com>2020-06-02 03:12:12 +0430
committerGitHub <noreply@github.com>2020-06-01 18:42:12 -0400
commit30785ed592d97717922ab65cbffb2353ed466350 (patch)
tree49318f6d55950be9f3d73464ebcc3b3d32c83905 /cli/main.rs
parenta4567e0e0162349f141ce2795ce793cfccfa1f9b (diff)
fix(bundle): better size output (#5997)
Diffstat (limited to 'cli/main.rs')
-rw-r--r--cli/main.rs37
1 files changed, 34 insertions, 3 deletions
diff --git a/cli/main.rs b/cli/main.rs
index d947088d2..f5f1e2661 100644
--- a/cli/main.rs
+++ b/cli/main.rs
@@ -445,15 +445,46 @@ async fn bundle_command(
let output_bytes = output_string.as_bytes();
let output_len = output_bytes.len();
deno_fs::write_file(out_file_, output_bytes, 0o666)?;
- // TODO(bartlomieju): add "humanFileSize" method
- info!("{} bytes emitted.", output_len);
+ info!("{} emitted.", human_size(output_len as f64));
} else {
println!("{}", output_string);
}
-
Ok(())
}
+fn human_size(bytse: f64) -> String {
+ let negative = if bytse.is_sign_positive() { "" } else { "-" };
+ let bytse = bytse.abs();
+ let units = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
+ if bytse < 1_f64 {
+ return format!("{}{} {}", negative, bytse, "Bytes");
+ }
+ let delimiter = 1024_f64;
+ let exponent = std::cmp::min(
+ (bytse.ln() / delimiter.ln()).floor() as i32,
+ (units.len() - 1) as i32,
+ );
+ let pretty_bytes = format!("{:.2}", bytse / delimiter.powi(exponent))
+ .parse::<f64>()
+ .unwrap()
+ * 1_f64;
+ let unit = units[exponent as usize];
+ format!("{}{} {}", negative, pretty_bytes, unit)
+}
+
+#[test]
+fn human_size_test() {
+ assert_eq!(human_size(16_f64), "16 Bytes");
+ assert_eq!(human_size((16 * 1024) as f64), "16 KB");
+ assert_eq!(human_size((16 * 1024 * 1024) as f64), "16 MB");
+ assert_eq!(human_size(16_f64 * 1024_f64.powf(3.0)), "16 GB");
+ assert_eq!(human_size(16_f64 * 1024_f64.powf(4.0)), "16 TB");
+ assert_eq!(human_size(16_f64 * 1024_f64.powf(5.0)), "16 PB");
+ assert_eq!(human_size(16_f64 * 1024_f64.powf(6.0)), "16 EB");
+ assert_eq!(human_size(16_f64 * 1024_f64.powf(7.0)), "16 ZB");
+ assert_eq!(human_size(16_f64 * 1024_f64.powf(8.0)), "16 YB");
+}
+
async fn doc_command(
flags: Flags,
source_file: Option<String>,