-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[2/n] [update-engine] add
(
to the beginning of step indexes in the…
… line displayer (#6447) This was reported by @wfchandler -- not sure why I missed these originally. Also move the step index display logic out into its own displayer -- I need this (including the padded logic) in a couple of spots. This can be landed separately from #6444. Some example output from `cargo run --example update-engine-basic -- -s line`: ``` [00:00:00] Running (1/7) Downloading component: https://www.example.org [00:00:00] Progress (1/7) Downloading component: https://www.example.org: 12.50% ( 131072/1048576 bytes) after 101.22ms [00:00:00] Progress (1/7) Downloading component: https://www.example.org: 25.00% ( 262144/1048576 bytes) after 201.49ms [00:00:00] Retry (1/7) Downloading component: https://www.example.org: after 302.77ms (at attempt 1) with message: Simulated failure at 25% [00:00:00] Progress (1/7) Downloading component: https://www.example.org: 0.00% ( 0/1048576 bytes) after 101.37ms [00:00:00] Progress (1/7) Downloading component: https://www.example.org: 10.00% ( 104857/1048576 bytes) after 201.61ms [00:00:00] Progress (1/7) Downloading component: https://www.example.org: 20.00% ( 209715/1048576 bytes) after 302.86ms [00:00:00] Progress (1/7) Downloading component: https://www.example.org: 30.00% ( 314572/1048576 bytes) after 404.14ms ```
- Loading branch information
1 parent
acae8ef
commit 862414a
Showing
5 changed files
with
149 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
//! Utility displayers. | ||
use std::fmt; | ||
|
||
/// Given current and total, displays `{current}/{total}`. | ||
/// | ||
/// * If the `index_and_total` constructor is called, then `current` is `index | ||
/// + 1`. | ||
/// * If `padded` is `true`, `current` is right-aligned and padded with spaces | ||
/// to the width of `total`. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use update_engine::display::ProgressRatioDisplay; | ||
/// | ||
/// // 0-based index and total. | ||
/// let display = ProgressRatioDisplay::index_and_total(0 as u64, 8 as u64); | ||
/// assert_eq!(display.to_string(), "1/8"); | ||
/// | ||
/// // 1-based current and total. | ||
/// let display = ProgressRatioDisplay::current_and_total(82 as u64, 230 as u64); | ||
/// assert_eq!(display.to_string(), "82/230"); | ||
/// | ||
/// // With padding. | ||
/// let display = display.padded(true); | ||
/// assert_eq!(display.to_string(), " 82/230"); | ||
/// ``` | ||
#[derive(Debug)] | ||
pub struct ProgressRatioDisplay { | ||
current: u64, | ||
total: u64, | ||
padded: bool, | ||
} | ||
|
||
impl ProgressRatioDisplay { | ||
/// Create a new `ProgressRatioDisplay` with current and total values. | ||
/// | ||
/// `current` is considered to be 1-based. For example, "20/80 jobs done". | ||
pub fn current_and_total<T: ToU64>(current: T, total: T) -> Self { | ||
Self { current: current.to_u64(), total: total.to_u64(), padded: false } | ||
} | ||
|
||
/// Create a new `ProgressRatioDisplay` with index and total values. | ||
/// | ||
/// The index is 0-based (i.e. 1 is added to it). For example, step index 0 | ||
/// out of 8 total steps is shown as "1/8". | ||
pub fn index_and_total<T: ToU64>(index: T, total: T) -> Self { | ||
Self { | ||
current: index | ||
.to_u64() | ||
.checked_add(1) | ||
.expect("index can't be u64::MAX"), | ||
total: total.to_u64(), | ||
padded: false, | ||
} | ||
} | ||
|
||
/// If set to true, the current value is padded to the same width as the | ||
/// total. | ||
pub fn padded(self, padded: bool) -> Self { | ||
Self { padded, ..self } | ||
} | ||
} | ||
|
||
impl fmt::Display for ProgressRatioDisplay { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
if self.padded { | ||
let width = self.total.to_string().len(); | ||
write!(f, "{:>width$}/{}", self.current, self.total) | ||
} else { | ||
write!(f, "{}/{}", self.current, self.total) | ||
} | ||
} | ||
} | ||
|
||
/// Trait that abstracts over `usize` and `u64`. | ||
/// | ||
/// There are no `From` implementations between `usize` and `u64`, but we | ||
/// assert below that all the architectures we support are 64-bit. | ||
pub trait ToU64 { | ||
fn to_u64(self) -> u64; | ||
} | ||
|
||
const _: () = { | ||
assert!( | ||
std::mem::size_of::<usize>() == std::mem::size_of::<u64>(), | ||
"usize and u64 are the same size" | ||
); | ||
}; | ||
|
||
impl ToU64 for usize { | ||
#[inline] | ||
fn to_u64(self) -> u64 { | ||
self as u64 | ||
} | ||
} | ||
|
||
impl ToU64 for u64 { | ||
#[inline] | ||
fn to_u64(self) -> u64 { | ||
self | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters