Skip to content

Commit 330f4bc

Browse files
authored
[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 ```
1 parent 27191f0 commit 330f4bc

File tree

5 files changed

+149
-26
lines changed

5 files changed

+149
-26
lines changed

update-engine/src/display/group_display.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ use swrite::{swrite, SWrite};
1212
use unicode_width::UnicodeWidthStr;
1313

1414
use crate::{
15-
errors::UnknownReportKey, events::EventReport, EventBuffer,
16-
ExecutionStatus, ExecutionTerminalInfo, StepSpec, TerminalKind,
15+
display::ProgressRatioDisplay, errors::UnknownReportKey,
16+
events::EventReport, EventBuffer, ExecutionStatus, ExecutionTerminalInfo,
17+
StepSpec, TerminalKind,
1718
};
1819

1920
use super::{
@@ -309,11 +310,13 @@ impl GroupDisplayStats {
309310
};
310311

311312
swrite!(line, "{:>HEADER_WIDTH$} ", header.style(header_style));
312-
let terminal_count = self.terminal_count();
313313
swrite!(
314314
line,
315-
"{terminal_count}/{}: {} running, {} {}",
316-
self.total,
315+
"{}: {} running, {} {}",
316+
ProgressRatioDisplay::current_and_total(
317+
self.terminal_count(),
318+
self.total
319+
),
317320
self.running.style(formatter.styles().meta_style),
318321
self.completed.style(formatter.styles().meta_style),
319322
"completed".style(formatter.styles().progress_style),

update-engine/src/display/line_display_shared.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use owo_colors::OwoColorize;
1616
use swrite::{swrite, SWrite as _};
1717

1818
use crate::{
19+
display::ProgressRatioDisplay,
1920
events::{
2021
ProgressCounter, ProgressEvent, ProgressEventKind, StepEvent,
2122
StepEventKind, StepInfo, StepOutcome,
@@ -633,10 +634,12 @@ fn format_progress_counter(counter: &ProgressCounter) -> String {
633634
let percent = (counter.current as f64 / total as f64) * 100.0;
634635
// <12.34> is 5 characters wide.
635636
let percent_width = 5;
636-
let counter_width = total.to_string().len();
637637
format!(
638-
"{:>percent_width$.2}% ({:>counter_width$}/{} {})",
639-
percent, counter.current, total, counter.units,
638+
"{:>percent_width$.2}% ({} {})",
639+
percent,
640+
ProgressRatioDisplay::current_and_total(counter.current, total)
641+
.padded(true),
642+
counter.units,
640643
)
641644
}
642645
None => format!("{} {}", counter.current, counter.units),
@@ -716,17 +719,16 @@ impl LineDisplayFormatter {
716719
) {
717720
ld_step_info.nest_data.add_prefix(line);
718721

719-
// Print out "<step index>/<total steps>)". Leave space such that we
720-
// print out e.g. "1/8)" and " 3/14)".
721-
// Add 1 to the index to make it 1-based.
722-
let step_index = ld_step_info.step_info.index + 1;
723-
let step_index_width = ld_step_info.total_steps.to_string().len();
722+
// Print out "(<current>/<total>)" in a padded way, so that successive
723+
// steps are vertically aligned.
724724
swrite!(
725725
line,
726-
"{:width$}/{:width$}) ",
727-
step_index,
728-
ld_step_info.total_steps,
729-
width = step_index_width
726+
"({}) ",
727+
ProgressRatioDisplay::index_and_total(
728+
ld_step_info.step_info.index,
729+
ld_step_info.total_steps
730+
)
731+
.padded(true),
730732
);
731733

732734
swrite!(

update-engine/src/display/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111
//! * [`LineDisplay`]: a line-oriented display suitable for the command line.
1212
//! * [`GroupDisplay`]: manages state and shows the results of several
1313
//! [`LineDisplay`]s at once.
14+
//! * Some utility displayers which can be used to build custom displayers.
1415
1516
mod group_display;
1617
mod line_display;
1718
mod line_display_shared;
19+
mod utils;
1820

1921
pub use group_display::GroupDisplay;
2022
pub use line_display::{LineDisplay, LineDisplayStyles};
2123
use line_display_shared::*;
24+
pub use utils::*;

update-engine/src/display/utils.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
5+
//! Utility displayers.
6+
7+
use std::fmt;
8+
9+
/// Given current and total, displays `{current}/{total}`.
10+
///
11+
/// * If the `index_and_total` constructor is called, then `current` is `index
12+
/// + 1`.
13+
/// * If `padded` is `true`, `current` is right-aligned and padded with spaces
14+
/// to the width of `total`.
15+
///
16+
/// # Examples
17+
///
18+
/// ```
19+
/// use update_engine::display::ProgressRatioDisplay;
20+
///
21+
/// // 0-based index and total.
22+
/// let display = ProgressRatioDisplay::index_and_total(0 as u64, 8 as u64);
23+
/// assert_eq!(display.to_string(), "1/8");
24+
///
25+
/// // 1-based current and total.
26+
/// let display = ProgressRatioDisplay::current_and_total(82 as u64, 230 as u64);
27+
/// assert_eq!(display.to_string(), "82/230");
28+
///
29+
/// // With padding.
30+
/// let display = display.padded(true);
31+
/// assert_eq!(display.to_string(), " 82/230");
32+
/// ```
33+
#[derive(Debug)]
34+
pub struct ProgressRatioDisplay {
35+
current: u64,
36+
total: u64,
37+
padded: bool,
38+
}
39+
40+
impl ProgressRatioDisplay {
41+
/// Create a new `ProgressRatioDisplay` with current and total values.
42+
///
43+
/// `current` is considered to be 1-based. For example, "20/80 jobs done".
44+
pub fn current_and_total<T: ToU64>(current: T, total: T) -> Self {
45+
Self { current: current.to_u64(), total: total.to_u64(), padded: false }
46+
}
47+
48+
/// Create a new `ProgressRatioDisplay` with index and total values.
49+
///
50+
/// The index is 0-based (i.e. 1 is added to it). For example, step index 0
51+
/// out of 8 total steps is shown as "1/8".
52+
pub fn index_and_total<T: ToU64>(index: T, total: T) -> Self {
53+
Self {
54+
current: index
55+
.to_u64()
56+
.checked_add(1)
57+
.expect("index can't be u64::MAX"),
58+
total: total.to_u64(),
59+
padded: false,
60+
}
61+
}
62+
63+
/// If set to true, the current value is padded to the same width as the
64+
/// total.
65+
pub fn padded(self, padded: bool) -> Self {
66+
Self { padded, ..self }
67+
}
68+
}
69+
70+
impl fmt::Display for ProgressRatioDisplay {
71+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72+
if self.padded {
73+
let width = self.total.to_string().len();
74+
write!(f, "{:>width$}/{}", self.current, self.total)
75+
} else {
76+
write!(f, "{}/{}", self.current, self.total)
77+
}
78+
}
79+
}
80+
81+
/// Trait that abstracts over `usize` and `u64`.
82+
///
83+
/// There are no `From` implementations between `usize` and `u64`, but we
84+
/// assert below that all the architectures we support are 64-bit.
85+
pub trait ToU64 {
86+
fn to_u64(self) -> u64;
87+
}
88+
89+
const _: () = {
90+
assert!(
91+
std::mem::size_of::<usize>() == std::mem::size_of::<u64>(),
92+
"usize and u64 are the same size"
93+
);
94+
};
95+
96+
impl ToU64 for usize {
97+
#[inline]
98+
fn to_u64(self) -> u64 {
99+
self as u64
100+
}
101+
}
102+
103+
impl ToU64 for u64 {
104+
#[inline]
105+
fn to_u64(self) -> u64 {
106+
self
107+
}
108+
}

wicket/src/ui/panes/update.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use ratatui::widgets::{
2929
use ratatui::Frame;
3030
use slog::{info, o, Logger};
3131
use tui_tree_widget::{Tree, TreeItem, TreeState};
32+
use update_engine::display::ProgressRatioDisplay;
3233
use update_engine::{
3334
AbortReason, CompletionReason, ExecutionStatus, FailureReason, StepKey,
3435
TerminalKind, WillNotBeRunReason,
@@ -1984,9 +1985,11 @@ impl ComponentUpdateListState {
19841985
));
19851986
status_text.push(Span::styled(
19861987
format!(
1987-
" (step {}/{})",
1988-
step_key.index + 1,
1989-
summary.total_steps,
1988+
" (step {})",
1989+
ProgressRatioDisplay::index_and_total(
1990+
step_key.index,
1991+
summary.total_steps,
1992+
)
19901993
),
19911994
style::plain_text(),
19921995
));
@@ -2015,9 +2018,11 @@ impl ComponentUpdateListState {
20152018
));
20162019
status_text.push(Span::styled(
20172020
format!(
2018-
" at step {}/{}",
2019-
info.step_key.index + 1,
2020-
summary.total_steps,
2021+
" at step {}",
2022+
ProgressRatioDisplay::index_and_total(
2023+
info.step_key.index,
2024+
summary.total_steps,
2025+
)
20212026
),
20222027
style::plain_text(),
20232028
));
@@ -2033,9 +2038,11 @@ impl ComponentUpdateListState {
20332038
));
20342039
status_text.push(Span::styled(
20352040
format!(
2036-
" at step {}/{}",
2037-
info.step_key.index + 1,
2038-
summary.total_steps,
2041+
" at step {}",
2042+
ProgressRatioDisplay::index_and_total(
2043+
info.step_key.index,
2044+
summary.total_steps,
2045+
)
20392046
),
20402047
style::plain_text(),
20412048
));

0 commit comments

Comments
 (0)