1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-29 16:30:56 -05:00

fix(bundle): better size output (#5997)

This commit is contained in:
Ali Hasani 2020-06-02 03:12:12 +04:30 committed by GitHub
parent a4567e0e01
commit 30785ed592
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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>,