1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-28 16:20:57 -05:00

refactor: add cli/display.rs module (#13879)

This commit is contained in:
Bartek Iwańczuk 2022-03-09 00:19:02 +01:00 committed by GitHub
parent a3d6be025c
commit 22dbbf75f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 83 additions and 59 deletions

67
cli/display.rs Normal file
View file

@ -0,0 +1,67 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
/// A function that converts a float to a string the represents a human
/// readable version of that number.
pub fn human_size(size: f64) -> String {
let negative = if size.is_sign_positive() { "" } else { "-" };
let size = size.abs();
let units = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
if size < 1_f64 {
return format!("{}{}{}", negative, size, "B");
}
let delimiter = 1024_f64;
let exponent = std::cmp::min(
(size.ln() / delimiter.ln()).floor() as i32,
(units.len() - 1) as i32,
);
let pretty_bytes = format!("{:.2}", size / delimiter.powi(exponent))
.parse::<f64>()
.unwrap()
* 1_f64;
let unit = units[exponent as usize];
format!("{}{}{}", negative, pretty_bytes, unit)
}
/// A function that converts a milisecond elapsed time to a string that
/// represents a human readable version of that time.
pub fn human_elapsed(elapsed: u128) -> String {
if elapsed < 1_000 {
return format!("{}ms", elapsed);
}
if elapsed < 1_000 * 60 {
return format!("{}s", elapsed / 1000);
}
let seconds = elapsed / 1_000;
let minutes = seconds / 60;
let seconds_remainder = seconds % 60;
format!("{}m{}s", minutes, seconds_remainder)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_human_size() {
assert_eq!(human_size(1_f64), "1B");
assert_eq!(human_size((12 * 1024) as f64), "12KB");
assert_eq!(human_size((24_i64 * 1024 * 1024) as f64), "24MB");
assert_eq!(human_size((24_i64 * 1024 * 1024 * 1024) as f64), "24GB");
assert_eq!(
human_size((24_i64 * 1024 * 1024 * 1024 * 1024) as f64),
"24TB"
);
}
#[test]
fn test_human_elapsed() {
assert_eq!(human_elapsed(1), "1ms");
assert_eq!(human_elapsed(256), "256ms");
assert_eq!(human_elapsed(1000), "1s");
assert_eq!(human_elapsed(1001), "1s");
assert_eq!(human_elapsed(1020), "1s");
assert_eq!(human_elapsed(70 * 1000), "1m10s");
assert_eq!(human_elapsed(86 * 1000 + 100), "1m26s");
}
}

View file

@ -10,6 +10,7 @@ mod deno_dir;
mod diagnostics; mod diagnostics;
mod diff; mod diff;
mod disk_cache; mod disk_cache;
mod display;
mod emit; mod emit;
mod errors; mod errors;
mod file_fetcher; mod file_fetcher;
@ -755,28 +756,6 @@ fn bundle_module_graph(
) )
} }
/// A function that converts a float to a string the represents a human
/// readable version of that number.
fn human_size(size: f64) -> String {
let negative = if size.is_sign_positive() { "" } else { "-" };
let size = size.abs();
let units = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
if size < 1_f64 {
return format!("{}{}{}", negative, size, "B");
}
let delimiter = 1024_f64;
let exponent = std::cmp::min(
(size.ln() / delimiter.ln()).floor() as i32,
(units.len() - 1) as i32,
);
let pretty_bytes = format!("{:.2}", size / delimiter.powi(exponent))
.parse::<f64>()
.unwrap()
* 1_f64;
let unit = units[exponent as usize];
format!("{}{}{}", negative, pretty_bytes, unit)
}
async fn bundle_command( async fn bundle_command(
flags: Flags, flags: Flags,
bundle_flags: BundleFlags, bundle_flags: BundleFlags,
@ -843,7 +822,7 @@ async fn bundle_command(
"{} {:?} ({})", "{} {:?} ({})",
colors::green("Emit"), colors::green("Emit"),
out_file, out_file,
colors::gray(human_size(output_len as f64)) colors::gray(display::human_size(output_len as f64))
); );
if let Some(bundle_map) = maybe_bundle_map { if let Some(bundle_map) = maybe_bundle_map {
let map_bytes = bundle_map.as_bytes(); let map_bytes = bundle_map.as_bytes();
@ -859,7 +838,7 @@ async fn bundle_command(
"{} {:?} ({})", "{} {:?} ({})",
colors::green("Emit"), colors::green("Emit"),
map_out_file, map_out_file,
colors::gray(human_size(map_len as f64)) colors::gray(display::human_size(map_len as f64))
); );
} }
} else { } else {

View file

@ -5,6 +5,7 @@ use crate::cache::CacherLoader;
use crate::colors; use crate::colors;
use crate::compat; use crate::compat;
use crate::create_main_worker; use crate::create_main_worker;
use crate::display;
use crate::emit; use crate::emit;
use crate::file_fetcher::File; use crate::file_fetcher::File;
use crate::file_watcher; use crate::file_watcher;
@ -269,7 +270,11 @@ impl PrettyTestReporter {
print!("{}", " ".repeat(description.level)); print!("{}", " ".repeat(description.level));
} }
println!("{} {}", status, colors::gray(human_elapsed(elapsed.into()))); println!(
"{} {}",
status,
colors::gray(format!("({})", display::human_elapsed(elapsed.into())))
);
if let Some(error_text) = result.error() { if let Some(error_text) = result.error() {
for line in error_text.lines() { for line in error_text.lines() {
@ -279,22 +284,6 @@ impl PrettyTestReporter {
} }
} }
/// A function that converts a milisecond elapsed time to a string that
/// represents a human readable version of that time.
fn human_elapsed(elapsed: u128) -> String {
if elapsed < 1_000 {
return format!("({}ms)", elapsed);
}
if elapsed < 1_000 * 60 {
return format!("({}s)", elapsed / 1000);
}
let seconds = elapsed / 1_000;
let minutes = seconds / 60;
let seconds_remainder = seconds % 60;
format!("({}m{}s)", minutes, seconds_remainder)
}
impl TestReporter for PrettyTestReporter { impl TestReporter for PrettyTestReporter {
fn report_plan(&mut self, plan: &TestPlan) { fn report_plan(&mut self, plan: &TestPlan) {
let inflection = if plan.total == 1 { "test" } else { "tests" }; let inflection = if plan.total == 1 { "test" } else { "tests" };
@ -355,7 +344,11 @@ impl TestReporter for PrettyTestReporter {
print!(" "); print!(" ");
} }
println!("{} {}", status, colors::gray(human_elapsed(elapsed.into()))); println!(
"{} {}",
status,
colors::gray(format!("({})", display::human_elapsed(elapsed.into())))
);
} }
fn report_step_wait(&mut self, description: &TestStepDescription) { fn report_step_wait(&mut self, description: &TestStepDescription) {
@ -432,7 +425,8 @@ impl TestReporter for PrettyTestReporter {
get_steps_text(summary.ignored_steps), get_steps_text(summary.ignored_steps),
summary.measured, summary.measured,
summary.filtered_out, summary.filtered_out,
colors::gray(human_elapsed(elapsed.as_millis())), colors::gray(
format!("({})", display::human_elapsed(elapsed.as_millis()))),
); );
} }
} }
@ -1303,19 +1297,3 @@ pub async fn run_tests_with_watch(
Ok(()) Ok(())
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_human_elapsed() {
assert_eq!(human_elapsed(1), "(1ms)");
assert_eq!(human_elapsed(256), "(256ms)");
assert_eq!(human_elapsed(1000), "(1s)");
assert_eq!(human_elapsed(1001), "(1s)");
assert_eq!(human_elapsed(1020), "(1s)");
assert_eq!(human_elapsed(70 * 1000), "(1m10s)");
assert_eq!(human_elapsed(86 * 1000 + 100), "(1m26s)");
}
}