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

fix: improve progress bar duration display #680

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions crates/rattler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dirs = { workspace = true }
fs-err = { workspace = true }
futures = { workspace = true }
fxhash = { workspace = true }
humantime = { workspace = true }
indexmap = { workspace = true }
indicatif = { workspace = true, optional = true }
itertools = { workspace = true }
Expand Down
43 changes: 36 additions & 7 deletions crates/rattler/src/install/installer/indicatif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ pub struct ProgressStyleProperties {

/// The type of progress to show.
pub progress_type: ProgressType,

/// The progress bar
pub track: ProgressTrack,
}

/// A trait that can be used to customize the style of different progress bars
Expand Down Expand Up @@ -131,8 +134,15 @@ impl ProgressFormatter for DefaultProgressFormatter {

// Add progress indicator
if props.determinate && props.status != ProgressStatus::Finished {
result.push_str("[{elapsed_precise}] [{bar:20!.bright.yellow/dim.white}] ");
if props.status == ProgressStatus::Active {
result.push_str("[{elapsed_precise}] [{bar:20!.bright.yellow/dim.white}] ");
} else {
result.push_str("[{elapsed_precise}] [{bar:20!.dim.yellow/dim.white}] ");
}
match props.progress_type {
ProgressType::Generic if props.track == ProgressTrack::Linking => {
result.push_str("{human_pos:>5}/{human_len} ");
}
ProgressType::Generic => {
// Don't show position and total, because these are visible
// through text anyway.
Expand Down Expand Up @@ -309,6 +319,7 @@ impl<F: ProgressFormatter> IndicatifReporterInner<F> {
status: ProgressStatus::Paused,
determinate: true,
progress_type: ProgressType::Generic,
track: ProgressTrack::Validation,
}));
}
}
Expand All @@ -323,18 +334,19 @@ impl<F: ProgressFormatter> IndicatifReporterInner<F> {
status: ProgressStatus::Finished,
determinate: true,
progress_type: ProgressType::Generic,
track: ProgressTrack::Validation,
}));
validation_progress.finish_using_style();
if let (Some(start), Some(end)) = (self.start_validating, self.end_validating) {
validation_progress.set_message(format!(
"{} {} in {:?}",
"{} {} in {}",
self.packages_validated.len(),
if self.packages_validated.len() == 1 {
"package"
} else {
"packages"
},
end - start
format_duration(end - start)
));
}
}
Expand Down Expand Up @@ -417,8 +429,9 @@ impl<F: ProgressFormatter + Send> Reporter for IndicatifReporter<F> {
status: ProgressStatus::Pending,
determinate: true,
progress_type: ProgressType::Generic,
track: ProgressTrack::Linking,
}))
.with_prefix("update prefix")
.with_prefix("installing packages")
.with_finish(ProgressFinish::AndLeave);
link_progress.enable_steady_tick(Duration::from_millis(100));

Expand Down Expand Up @@ -482,6 +495,7 @@ impl<F: ProgressFormatter + Send> Reporter for IndicatifReporter<F> {
status: ProgressStatus::Active,
determinate: true,
progress_type: ProgressType::Generic,
track: ProgressTrack::Validation,
}))
.with_prefix("validate cache")
.with_finish(ProgressFinish::AndLeave);
Expand All @@ -500,6 +514,7 @@ impl<F: ProgressFormatter + Send> Reporter for IndicatifReporter<F> {
status: ProgressStatus::Active,
determinate: true,
progress_type: ProgressType::Generic,
track: ProgressTrack::Validation,
}));

inner.update_validating_message();
Expand Down Expand Up @@ -550,6 +565,7 @@ impl<F: ProgressFormatter + Send> Reporter for IndicatifReporter<F> {
status: ProgressStatus::Active,
determinate: true,
progress_type: ProgressType::Generic,
track: ProgressTrack::DownloadAndExtract,
}))
.with_prefix("download & extract")
.with_finish(ProgressFinish::AndLeave);
Expand All @@ -567,6 +583,7 @@ impl<F: ProgressFormatter + Send> Reporter for IndicatifReporter<F> {
status: ProgressStatus::Active,
determinate: true,
progress_type: ProgressType::Bytes,
track: ProgressTrack::DownloadAndExtract,
}));
download_progress.set_length(inner.total_download_size as u64);

Expand Down Expand Up @@ -604,6 +621,7 @@ impl<F: ProgressFormatter + Send> Reporter for IndicatifReporter<F> {
status: ProgressStatus::Paused,
determinate: true,
progress_type: ProgressType::Bytes,
track: ProgressTrack::DownloadAndExtract,
}));
}

Expand All @@ -622,19 +640,20 @@ impl<F: ProgressFormatter + Send> Reporter for IndicatifReporter<F> {
status: ProgressStatus::Finished,
determinate: true,
progress_type: ProgressType::Bytes,
track: ProgressTrack::DownloadAndExtract,
}));
download_pb.finish_using_style();
if let (Some(start), Some(end)) = (inner.start_downloading, inner.end_downloading) {
download_pb.set_message(format!(
"{} {} ({}) in {:?}",
"{} {} ({}) in {}",
inner.packages_downloaded.len(),
if inner.packages_downloaded.len() == 1 {
"package"
} else {
"packages"
},
HumanBytes(inner.bytes_downloaded.iter().sum::<usize>() as u64),
end - start
format_duration(end - start)
));
}
}
Expand All @@ -656,6 +675,7 @@ impl<F: ProgressFormatter + Send> Reporter for IndicatifReporter<F> {
status: ProgressStatus::Active,
determinate: true,
progress_type: ProgressType::Generic,
track: ProgressTrack::Linking,
}));
}

Expand All @@ -678,6 +698,7 @@ impl<F: ProgressFormatter + Send> Reporter for IndicatifReporter<F> {
status: ProgressStatus::Paused,
determinate: true,
progress_type: ProgressType::Generic,
track: ProgressTrack::Linking,
}));
}

Expand All @@ -699,6 +720,7 @@ impl<F: ProgressFormatter + Send> Reporter for IndicatifReporter<F> {
status: ProgressStatus::Active,
determinate: true,
progress_type: ProgressType::Generic,
track: ProgressTrack::Linking,
}));
}

Expand All @@ -721,6 +743,7 @@ impl<F: ProgressFormatter + Send> Reporter for IndicatifReporter<F> {
status: ProgressStatus::Paused,
determinate: true,
progress_type: ProgressType::Generic,
track: ProgressTrack::Linking,
}));
}

Expand All @@ -735,7 +758,7 @@ impl<F: ProgressFormatter + Send> Reporter for IndicatifReporter<F> {
if let (Some(link_pb), Some(start), Some(end)) =
(&inner.link_progress, inner.start_linking, inner.end_linking)
{
link_pb.set_message(format!("took {:?}", end - start));
link_pb.set_message(format!("took {}", format_duration(end - start)));
}

for (pb, track) in [
Expand All @@ -755,6 +778,7 @@ impl<F: ProgressFormatter + Send> Reporter for IndicatifReporter<F> {
} else {
ProgressType::Generic
},
track,
}));
if inner.clear_when_done {
pb.finish_and_clear();
Expand All @@ -764,3 +788,8 @@ impl<F: ProgressFormatter + Send> Reporter for IndicatifReporter<F> {
}
}
}

/// Formats a durations. Rounds to milliseconds and uses human-readable format.
fn format_duration(duration: Duration) -> humantime::FormattedDuration {
humantime::format_duration(Duration::from_millis(duration.as_millis() as u64))
}
1 change: 1 addition & 0 deletions py-rattler/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading