1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-12-05 03:13:14 +00:00

fix(cmd_duration): Make render_time format more consistent (#5825)

* fix(cmd_duration): Make render_time format consistent

* Cleanup render_time comments

* Fix AWS test for new render_time format
This commit is contained in:
Leon Strauss 2024-07-29 22:22:55 +02:00 committed by GitHub
parent a62012e328
commit 4abea6b601
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 46 additions and 32 deletions

View File

@ -702,7 +702,7 @@ credential_process = /opt/bin/awscreds-retriever
.collect(); .collect();
let possible_values = [ let possible_values = [
"30m2s", "30m1s", "30m", "29m59s", "29m58s", "29m57s", "29m56s", "29m55s", "30m2s", "30m1s", "30m0s", "29m59s", "29m58s", "29m57s", "29m56s", "29m55s",
]; ];
let possible_values = possible_values.map(|duration| { let possible_values = possible_values.map(|duration| {
let segment_colored = format!("☁️ astronauts (ap-northeast-2) [{duration}] "); let segment_colored = format!("☁️ astronauts (ap-northeast-2) [{duration}] ");
@ -756,7 +756,7 @@ aws_secret_access_key=dummy
// In principle, "30m" should be correct. However, bad luck in scheduling // In principle, "30m" should be correct. However, bad luck in scheduling
// on shared runners may delay it. // on shared runners may delay it.
let possible_values = [ let possible_values = [
"30m2s", "30m1s", "30m", "29m59s", "29m58s", "29m57s", "29m56s", "29m55s", "30m2s", "30m1s", "30m0s", "29m59s", "29m58s", "29m57s", "29m56s", "29m55s",
]; ];
let possible_values = possible_values.map(|duration| { let possible_values = possible_values.map(|duration| {
let segment_colored = format!("☁️ astronauts (ap-northeast-2) [{duration}] "); let segment_colored = format!("☁️ astronauts (ap-northeast-2) [{duration}] ");

View File

@ -622,9 +622,11 @@ pub fn exec_timeout(cmd: &mut Command, time_limit: Duration) -> Option<CommandOu
// Render the time into a nice human-readable string // Render the time into a nice human-readable string
pub fn render_time(raw_millis: u128, show_millis: bool) -> String { pub fn render_time(raw_millis: u128, show_millis: bool) -> String {
// Make sure it renders something if the time equals zero instead of an empty string // Fast returns for zero cases to render something
if raw_millis == 0 { match (raw_millis, show_millis) {
return "0ms".into(); (0, true) => return "0ms".into(),
(0..=999, false) => return "0s".into(),
_ => (),
} }
// Calculate a simple breakdown into days/hours/minutes/seconds/milliseconds // Calculate a simple breakdown into days/hours/minutes/seconds/milliseconds
@ -633,25 +635,29 @@ pub fn render_time(raw_millis: u128, show_millis: bool) -> String {
let (minutes, raw_hours) = (raw_minutes % 60, raw_minutes / 60); let (minutes, raw_hours) = (raw_minutes % 60, raw_minutes / 60);
let (hours, days) = (raw_hours % 24, raw_hours / 24); let (hours, days) = (raw_hours % 24, raw_hours / 24);
let components = [days, hours, minutes, seconds]; // Calculate how long the string will be to allocate once in most cases
let suffixes = ["d", "h", "m", "s"]; let result_capacity = match raw_millis {
1..=59 => 3,
60..=3599 => 6,
3600..=86399 => 9,
_ => 12,
} + if show_millis { 5 } else { 0 };
let mut rendered_components: Vec<String> = components let components = [(days, "d"), (hours, "h"), (minutes, "m"), (seconds, "s")];
.iter()
.zip(&suffixes)
.map(render_time_component)
.collect();
if show_millis || raw_millis < 1000 {
rendered_components.push(render_time_component((&millis, &"ms")));
}
rendered_components.join("")
}
/// Render a single component of the time string, giving an empty string if component is zero // Concat components ito result starting from the first non-zero one
fn render_time_component((component, suffix): (&u128, &&str)) -> String { let result = components.iter().fold(
match component { String::with_capacity(result_capacity),
0 => String::new(), |acc, (component, suffix)| match component {
n => format!("{n}{suffix}"), 0 if acc.is_empty() => acc,
n => acc + &n.to_string() + suffix,
},
);
if show_millis {
result + &millis.to_string() + "ms"
} else {
result
} }
} }
@ -713,28 +719,36 @@ mod tests {
use super::*; use super::*;
#[test] #[test]
fn test_0ms() { fn render_time_test_0ms() {
assert_eq!(render_time(0_u128, true), "0ms") assert_eq!(render_time(0_u128, true), "0ms")
} }
#[test] #[test]
fn test_500ms() { fn render_time_test_0s() {
assert_eq!(render_time(0_u128, false), "0s")
}
#[test]
fn render_time_test_500ms() {
assert_eq!(render_time(500_u128, true), "500ms") assert_eq!(render_time(500_u128, true), "500ms")
} }
#[test] #[test]
fn test_10s() { fn render_time_test_500ms_no_millis() {
assert_eq!(render_time(10_000_u128, true), "10s") assert_eq!(render_time(500_u128, false), "0s")
} }
#[test] #[test]
fn test_90s() { fn render_time_test_10s() {
assert_eq!(render_time(90_000_u128, true), "1m30s") assert_eq!(render_time(10_000_u128, true), "10s0ms")
} }
#[test] #[test]
fn test_10110s() { fn render_time_test_90s() {
assert_eq!(render_time(10_110_000_u128, true), "2h48m30s") assert_eq!(render_time(90_000_u128, true), "1m30s0ms")
} }
#[test] #[test]
fn test_1d() { fn render_time_test_10110s() {
assert_eq!(render_time(86_400_000_u128, true), "1d") assert_eq!(render_time(10_110_000_u128, true), "2h48m30s0ms")
}
#[test]
fn render_time_test_1d() {
assert_eq!(render_time(86_400_000_u128, false), "1d0h0m0s")
} }
#[test] #[test]