diff options
| author | Andrew Voynov <37143421+Andrew15-5@users.noreply.github.com> | 2024-09-10 16:49:37 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-10 13:49:37 +0000 |
| commit | ac982f585620a88073b97b77303237290c09db81 (patch) | |
| tree | f9feebdbfd0f32c565a506149540a750efded83a /crates/typst-cli/src | |
| parent | a82256c585072f5a7eba6fb71ca8e93f94c42693 (diff) | |
Unified and fixed `Duration` formatting in the CLI (#4587)
Co-authored-by: PgBiel <9021226+PgBiel@users.noreply.github.com>
Diffstat (limited to 'crates/typst-cli/src')
| -rw-r--r-- | crates/typst-cli/src/download.rs | 38 | ||||
| -rw-r--r-- | crates/typst-cli/src/watch.rs | 13 |
2 files changed, 21 insertions, 30 deletions
diff --git a/crates/typst-cli/src/download.rs b/crates/typst-cli/src/download.rs index 8082fa52..3e3d8294 100644 --- a/crates/typst-cli/src/download.rs +++ b/crates/typst-cli/src/download.rs @@ -5,6 +5,7 @@ use std::time::{Duration, Instant}; use codespan_reporting::term; use codespan_reporting::term::termcolor::WriteColor; +use typst::utils::format; use typst_kit::download::{DownloadState, Downloader, Progress}; use crate::terminal::{self, TermOut}; @@ -61,7 +62,9 @@ pub fn display_download_progress( let total_downloaded = as_bytes_unit(state.total_downloaded); let speed_h = as_throughput_unit(speed); - let elapsed = time_suffix(Instant::now().saturating_duration_since(state.start_time)); + let elapsed = format::time_starting_with_seconds( + &Instant::now().saturating_duration_since(state.start_time), + ); match state.content_len { Some(content_len) => { @@ -69,11 +72,12 @@ pub fn display_download_progress( let remaining = content_len - state.total_downloaded; let download_size = as_bytes_unit(content_len); - let eta = time_suffix(Duration::from_secs(if speed == 0 { - 0 - } else { - (remaining / speed) as u64 - })); + let eta = + format::time_starting_with_seconds(&Duration::from_secs(if speed == 0 { + 0 + } else { + (remaining / speed) as u64 + })); writeln!( out, "{total_downloaded} / {download_size} ({percent:3.0} %) {speed_h} in {elapsed} ETA: {eta}", @@ -87,26 +91,6 @@ pub fn display_download_progress( Ok(()) } -/// Append a unit-of-time suffix. -fn time_suffix(duration: Duration) -> String { - let secs = duration.as_secs(); - match format_dhms(secs) { - (0, 0, 0, s) => format!("{s:2.0}s"), - (0, 0, m, s) => format!("{m:2.0}m {s:2.0}s"), - (0, h, m, s) => format!("{h:2.0}h {m:2.0}m {s:2.0}s"), - (d, h, m, s) => format!("{d:3.0}d {h:2.0}h {m:2.0}m {s:2.0}s"), - } -} - -/// Format the total amount of seconds into the amount of days, hours, minutes -/// and seconds. -fn format_dhms(sec: u64) -> (u64, u8, u8, u8) { - let (mins, sec) = (sec / 60, (sec % 60) as u8); - let (hours, mins) = (mins / 60, (mins % 60) as u8); - let (days, hours) = (hours / 24, (hours % 24) as u8); - (days, hours, mins, sec) -} - /// Format a given size as a unit of time. Setting `include_suffix` to true /// appends a '/s' (per second) suffix. fn as_bytes_unit(size: usize) -> String { @@ -123,7 +107,7 @@ fn as_bytes_unit(size: usize) -> String { } else if size >= KI { format!("{:5.1} KiB", size / KI) } else { - format!("{size:3.0} B") + format!("{size:3} B") } } diff --git a/crates/typst-cli/src/watch.rs b/crates/typst-cli/src/watch.rs index 348cd005..eb437ffd 100644 --- a/crates/typst-cli/src/watch.rs +++ b/crates/typst-cli/src/watch.rs @@ -11,6 +11,7 @@ use ecow::eco_format; use notify::{Event, RecommendedWatcher, RecursiveMode, Watcher as _}; use same_file::is_same_file; use typst::diag::{bail, StrResult}; +use typst::utils::format; use crate::args::{CompileCommand, Input, Output}; use crate::compile::compile_once; @@ -297,10 +298,16 @@ impl Status { fn message(&self) -> String { match self { Self::Compiling => "compiling ...".into(), - Self::Success(duration) => format!("compiled successfully in {duration:.2?}"), - Self::PartialSuccess(duration) => { - format!("compiled with warnings in {duration:.2?}") + Self::Success(duration) => { + format!( + "compiled successfully in {}", + format::time_starting_with_milliseconds(duration, 2) + ) } + Self::PartialSuccess(duration) => format!( + "compiled with warnings in {}", + format::time_starting_with_milliseconds(duration, 2) + ), Self::Error => "compiled with errors".into(), } } |
