Skip to content

Commit 9a742ad

Browse files
authored
feat: enhance progress bar status handling and alignment (#845)
1 parent af20848 commit 9a742ad

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

cmd/cli/desktop/progress.go

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -176,37 +176,57 @@ func displayProgressSimple(body io.Reader, printer standalone.StatusPrinter) (st
176176
return finalMessage, progressShown, nil
177177
}
178178

179+
// Status strings used in progress display. All are padded to
180+
// progressStatusWidth so that progress bars line up at the same column.
181+
const (
182+
progressStatusWaiting = "Waiting"
183+
progressStatusDownloading = "Downloading"
184+
progressStatusPullComplete = "Pull complete"
185+
progressStatusUploading = "Uploading"
186+
progressStatusPushComplete = "Push complete"
187+
188+
// progressStatusWidth is the column width to which all status strings
189+
// are left-padded, keeping progress bars horizontally aligned.
190+
progressStatusWidth = max(
191+
len(progressStatusWaiting),
192+
len(progressStatusDownloading),
193+
len(progressStatusPullComplete),
194+
len(progressStatusUploading),
195+
len(progressStatusPushComplete),
196+
)
197+
)
198+
179199
// writeDockerProgress writes a progress update in Docker's JSONMessage format
180200
func writeDockerProgress(w io.Writer, msg *oci.ProgressMessage) error {
181201
layerID := msg.Layer.ID
182202
if layerID == "" {
183203
return nil
184204
}
185205

186-
// Detect if this is a push operation based on the sentinel layer ID
187-
isPush := msg.Mode == "push"
206+
// Detect if this is a push operation.
207+
isPush := msg.Mode == oci.ModePush
188208

189-
// Determine status based on progress
209+
// Determine status based on progress.
190210
var status string
191211
var progressDetail *jsonstream.Progress
192212

193213
if msg.Layer.Current == 0 {
194-
status = "Waiting"
214+
status = progressStatusWaiting
195215
} else if msg.Layer.Current < msg.Layer.Size {
196216
if isPush {
197-
status = "Uploading"
217+
status = progressStatusUploading
198218
} else {
199-
status = "Downloading"
219+
status = progressStatusDownloading
200220
}
201221
progressDetail = &jsonstream.Progress{
202222
Current: int64(msg.Layer.Current),
203223
Total: int64(msg.Layer.Size),
204224
}
205225
} else if msg.Layer.Current >= msg.Layer.Size && msg.Layer.Size > 0 {
206226
if isPush {
207-
status = "Push complete"
227+
status = progressStatusPushComplete
208228
} else {
209-
status = "Pull complete"
229+
status = progressStatusPullComplete
210230
}
211231
progressDetail = &jsonstream.Progress{
212232
Current: int64(msg.Layer.Current),
@@ -218,15 +238,17 @@ func writeDockerProgress(w io.Writer, msg *oci.ProgressMessage) error {
218238
return nil
219239
}
220240

221-
// Shorten layer ID for display (similar to Docker)
241+
// Shorten layer ID for display (similar to Docker).
222242
displayID := strings.TrimPrefix(layerID, "sha256:")
223243
if len(displayID) > 12 {
224244
displayID = displayID[:12]
225245
}
226246

227247
dockerMsg := jsonstream.Message{
228-
ID: displayID,
229-
Status: status,
248+
ID: displayID,
249+
// Pad status to a fixed width so all progress bars start at the
250+
// same column regardless of status string length.
251+
Status: fmt.Sprintf("%-*s", progressStatusWidth, status),
230252
Progress: progressDetail,
231253
}
232254

0 commit comments

Comments
 (0)