Skip to content

Commit 6163a00

Browse files
Cloud0310rami3l
authored andcommitted
fix: install message misalignment.
Use dynamic name_width for progress templates so component status texts are correctly aligned. Pass computed max_name_width from manifestation into DownloadCfg/ComponentBinary and update progress_style and callers accordingly.
1 parent e89d05f commit 6163a00

2 files changed

Lines changed: 39 additions & 9 deletions

File tree

src/dist/download.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,12 @@ impl<'a> DownloadCfg<'a> {
288288
pub(crate) fn status_for(
289289
&self,
290290
component_name: impl Into<Cow<'static, str>>,
291+
name_width: usize,
291292
) -> DownloadStatus {
292293
let progress = ProgressBar::hidden();
293294
progress.set_style(
294295
DownloadStatus::progress_style(
296+
name_width,
295297
"downloading [{bar:15}] {total_bytes:>11} ({bytes_per_sec}, ETA: {eta})",
296298
)
297299
.progress_chars("## "),
@@ -302,6 +304,7 @@ impl<'a> DownloadCfg<'a> {
302304
DownloadStatus {
303305
progress,
304306
retry_time: Mutex::new(None),
307+
name_width,
305308
}
306309
}
307310

@@ -345,6 +348,8 @@ pub(crate) struct DownloadStatus {
345348
/// bar would reappear immediately, not allowing the user to correctly see the message,
346349
/// before the progress bar starts again.
347350
retry_time: Mutex<Option<Instant>>,
351+
/// The dynamic maximum width of the component names for alignment
352+
name_width: usize,
348353
}
349354

350355
impl DownloadStatus {
@@ -363,6 +368,7 @@ impl DownloadStatus {
363368
*retry_time = None;
364369
self.progress.set_style(
365370
DownloadStatus::progress_style(
371+
self.name_width,
366372
"downloading [{bar:15}] {total_bytes:>11} ({bytes_per_sec}, ETA: {eta})",
367373
)
368374
.progress_chars("## "),
@@ -371,28 +377,33 @@ impl DownloadStatus {
371377

372378
pub(crate) fn finished(&self) {
373379
self.progress.set_style(DownloadStatus::progress_style(
380+
self.name_width,
374381
"pending installation {total_bytes:>20}",
375382
));
376383
self.progress.tick(); // A tick is needed for the new style to appear, as it is static.
377384
}
378385

379386
pub(crate) fn failed(&self) {
380387
self.progress.set_style(DownloadStatus::progress_style(
388+
self.name_width,
381389
"download failed after {elapsed}",
382390
));
383391
self.progress.finish();
384392
}
385393

386394
pub(crate) fn retrying(&self) {
387395
*self.retry_time.lock().unwrap() = Some(Instant::now());
388-
self.progress
389-
.set_style(DownloadStatus::progress_style("retrying download..."));
396+
self.progress.set_style(DownloadStatus::progress_style(
397+
self.name_width,
398+
"retrying download...",
399+
));
390400
}
391401

392402
pub(crate) fn unpack<T: Read>(&self, inner: T) -> ProgressBarIter<T> {
393403
self.progress.reset();
394404
self.progress.set_style(
395405
DownloadStatus::progress_style(
406+
self.name_width,
396407
"unpacking [{bar:15}] {total_bytes:>11} ({bytes_per_sec}, ETA: {eta})",
397408
)
398409
.progress_chars("## "),
@@ -402,21 +413,25 @@ impl DownloadStatus {
402413

403414
pub(crate) fn installing(&self) {
404415
self.progress.set_style(
405-
DownloadStatus::progress_style("installing {spinner:.green} {total_bytes:>28}")
406-
.tick_chars(r"|/-\ "),
416+
DownloadStatus::progress_style(
417+
self.name_width,
418+
"installing {spinner:.green} {total_bytes:>28}",
419+
)
420+
.tick_chars(r"|/-\ "),
407421
);
408422
self.progress.enable_steady_tick(Duration::from_millis(100));
409423
}
410424

411425
pub(crate) fn installed(&self) {
412426
self.progress.set_style(DownloadStatus::progress_style(
427+
self.name_width,
413428
"installed {total_bytes:>31}",
414429
));
415430
self.progress.finish();
416431
}
417432

418-
fn progress_style(suffix: &str) -> ProgressStyle {
419-
let template = format!("{{msg:>13.bold}} {suffix}");
433+
fn progress_style(name_width: usize, suffix: &str) -> ProgressStyle {
434+
let template = format!("{{msg:>{name_width}.bold}} {suffix}");
420435
ProgressStyle::with_template(&template).unwrap()
421436
}
422437
}

src/dist/manifestation.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,24 @@ impl Manifestation {
161161
}
162162
}
163163

164+
const DEFAULT_MIN_NAME_WIDTH: usize = 13;
165+
let max_name_width = update
166+
.components_to_install
167+
.iter()
168+
// The same as the component name's length used in `ComponentBinary::new`
169+
// As it's used for status output
170+
.map(|component| new_manifest.short_name(component).len())
171+
.max()
172+
.unwrap_or(0)
173+
.max(DEFAULT_MIN_NAME_WIDTH);
174+
164175
// Download component packages and validate hashes
165176
let components = update
166177
.components_to_install
167178
.into_iter()
168-
.filter_map(|component| ComponentBinary::new(component, &new_manifest, download_cfg))
179+
.filter_map(|component| {
180+
ComponentBinary::new(component, &new_manifest, download_cfg, max_name_width)
181+
})
169182
.collect::<Result<Vec<_>>>()?;
170183

171184
const DEFAULT_CONCURRENT_DOWNLOADS: usize = 2;
@@ -416,7 +429,8 @@ impl Manifestation {
416429
.unwrap()
417430
.replace(DEFAULT_DIST_SERVER, dl_cfg.tmp_cx.dist_server.as_str());
418431

419-
let status = dl_cfg.status_for("rust");
432+
let component_name = "rust";
433+
let status = dl_cfg.status_for(component_name, component_name.len());
420434
let dl = dl_cfg
421435
.download_and_check(&url, Some(update_hash), Some(&status), ".tar.gz")
422436
.await?;
@@ -752,14 +766,15 @@ impl<'a> ComponentBinary<'a> {
752766
component: Component,
753767
manifest: &'a Manifest,
754768
download_cfg: &'a DownloadCfg<'a>,
769+
name_width: usize,
755770
) -> Option<Result<Self>> {
756771
Some(Ok(ComponentBinary {
757772
binary: match manifest.binary(&component) {
758773
Ok(Some(b)) => b,
759774
Ok(None) => return None,
760775
Err(e) => return Some(Err(e)),
761776
},
762-
status: download_cfg.status_for(manifest.short_name(&component).to_owned()),
777+
status: download_cfg.status_for(manifest.short_name(&component).to_owned(), name_width),
763778
component,
764779
manifest,
765780
download_cfg,

0 commit comments

Comments
 (0)