Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2/n] [update-engine] add ( to the beginning of step indexes in the line displayer #6447

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions update-engine/src/display/line_display_shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use owo_colors::OwoColorize;
use swrite::{swrite, SWrite as _};

use crate::{
display::StepIndexDisplay,
events::{
ProgressCounter, ProgressEvent, ProgressEventKind, StepEvent,
StepEventKind, StepInfo, StepOutcome,
Expand Down Expand Up @@ -716,17 +717,16 @@ impl LineDisplayFormatter {
) {
ld_step_info.nest_data.add_prefix(line);

// Print out "<step index>/<total steps>)". Leave space such that we
// print out e.g. "1/8)" and " 3/14)".
// Add 1 to the index to make it 1-based.
let step_index = ld_step_info.step_info.index + 1;
let step_index_width = ld_step_info.total_steps.to_string().len();
// Print out "(<current>/<total>)" in a padded way, so that successive
// steps are vertically aligned.
swrite!(
line,
"{:width$}/{:width$}) ",
step_index,
ld_step_info.total_steps,
width = step_index_width
"({}) ",
StepIndexDisplay::new(
ld_step_info.step_info.index,
ld_step_info.total_steps
)
.padded(true),
);

swrite!(
Expand Down
3 changes: 3 additions & 0 deletions update-engine/src/display/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
//! * [`LineDisplay`]: a line-oriented display suitable for the command line.
//! * [`GroupDisplay`]: manages state and shows the results of several
//! [`LineDisplay`]s at once.
//! * Some utility displayers which can be used to build custom displayers.

mod group_display;
mod line_display;
mod line_display_shared;
mod utils;

pub use group_display::GroupDisplay;
pub use line_display::{LineDisplay, LineDisplayStyles};
use line_display_shared::*;
pub use utils::*;
58 changes: 58 additions & 0 deletions update-engine/src/display/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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 an index and a count of total steps, displays `{current}/{total}`.
///
/// Here:
///
/// * `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::StepIndexDisplay;
///
/// let display = StepIndexDisplay::new(0, 8);
/// assert_eq!(display.to_string(), "1/8");
/// let display = StepIndexDisplay::new(82, 230);
/// assert_eq!(display.to_string(), "83/230");
/// let display = display.padded(true);
/// assert_eq!(display.to_string(), " 83/230");
/// ```
#[derive(Debug)]
pub struct StepIndexDisplay {
index: usize,
total: usize,
padded: bool,
}

impl StepIndexDisplay {
/// Create a new `StepIndexDisplay`.
///
/// The index is 0-based (i.e. 1 is added to it when it is displayed).
pub fn new(index: usize, total: usize) -> Self {
Self { index, total, padded: false }
}

pub fn padded(self, padded: bool) -> Self {
Self { padded, ..self }
}
}

impl fmt::Display for StepIndexDisplay {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.padded {
let width = self.total.to_string().len();
write!(f, "{:>width$}/{}", self.index + 1, self.total)
} else {
write!(f, "{}/{}", self.index + 1, self.total)
}
}
}
25 changes: 16 additions & 9 deletions wicket/src/ui/panes/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use ratatui::widgets::{
use ratatui::Frame;
use slog::{info, o, Logger};
use tui_tree_widget::{Tree, TreeItem, TreeState};
use update_engine::display::StepIndexDisplay;
use update_engine::{
AbortReason, CompletionReason, ExecutionStatus, FailureReason, StepKey,
TerminalKind, WillNotBeRunReason,
Expand Down Expand Up @@ -1984,9 +1985,11 @@ impl ComponentUpdateListState {
));
status_text.push(Span::styled(
format!(
" (step {}/{})",
step_key.index + 1,
summary.total_steps,
" (step {})",
StepIndexDisplay::new(
step_key.index,
summary.total_steps,
)
),
style::plain_text(),
));
Expand Down Expand Up @@ -2015,9 +2018,11 @@ impl ComponentUpdateListState {
));
status_text.push(Span::styled(
format!(
" at step {}/{}",
info.step_key.index + 1,
summary.total_steps,
" at step {}",
StepIndexDisplay::new(
info.step_key.index,
summary.total_steps,
)
),
style::plain_text(),
));
Expand All @@ -2033,9 +2038,11 @@ impl ComponentUpdateListState {
));
status_text.push(Span::styled(
format!(
" at step {}/{}",
info.step_key.index + 1,
summary.total_steps,
" at step {}",
StepIndexDisplay::new(
info.step_key.index,
summary.total_steps,
)
),
style::plain_text(),
));
Expand Down
Loading