From cb6d39a3d54f56529e18197e13afee973eeea299 Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Mon, 15 Apr 2024 04:39:52 +0000 Subject: [PATCH 1/4] feat: add color to progress mark and bar Signed-off-by: Billy Zha --- .../internal/display/status/progress/status.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/cmd/oras/internal/display/status/progress/status.go b/cmd/oras/internal/display/status/progress/status.go index aa2e90f0b..3a73ca894 100644 --- a/cmd/oras/internal/display/status/progress/status.go +++ b/cmd/oras/internal/display/status/progress/status.go @@ -35,6 +35,12 @@ const ( zeroDigest = " └─ loading digest..." ) +var ( + spinnerColor = aec.LightYellowF + doneMarkColor = aec.LightGreenF + progressColor = aec.LightBlueB +) + // status is used as message to update progress view. type status struct { done bool // done is true when the end time is set @@ -128,13 +134,15 @@ func (s *status) String(width int) (string, string) { lenLeft := 0 if !s.done { lenBar := int(percent * barLength) - bar := fmt.Sprintf("[%s%s]", aec.Inverse.Apply(strings.Repeat(" ", lenBar)), strings.Repeat(".", barLength-lenBar)) + bar := fmt.Sprintf("[%s%s]", progressColor.Apply(strings.Repeat(" ", lenBar)), strings.Repeat(".", barLength-lenBar)) speed := s.calculateSpeed() - left = fmt.Sprintf("%c %s(%*s/s) %s %s", s.mark.symbol(), bar, speedLength, speed, s.prompt, name) + left = fmt.Sprintf("%s %s(%*s/s) %s %s", + spinnerColor.Apply(fmt.Sprintf("%c", s.mark.symbol())), + bar, speedLength, speed, s.prompt, name) // bar + wrapper(2) + space(1) + speed + "/s"(2) + wrapper(2) = len(bar) + len(speed) + 7 lenLeft = barLength + speedLength + 7 } else { - left = fmt.Sprintf("✓ %s %s", s.prompt, name) + left = fmt.Sprintf("%s %s %s", doneMarkColor.Apply("✓"), s.prompt, name) } // mark(1) + space(1) + prompt + space(1) + name = len(prompt) + len(name) + 3 lenLeft += utf8.RuneCountInString(s.prompt) + utf8.RuneCountInString(name) + 3 From 59eccdd013c9bda2de4d7ed12b1515c6102b4a66 Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Mon, 15 Apr 2024 04:47:54 +0000 Subject: [PATCH 2/4] fix unit test Signed-off-by: Billy Zha --- cmd/oras/internal/display/status/progress/status_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/oras/internal/display/status/progress/status_test.go b/cmd/oras/internal/display/status/progress/status_test.go index 2f36f9761..16d234a44 100644 --- a/cmd/oras/internal/display/status/progress/status_test.go +++ b/cmd/oras/internal/display/status/progress/status_test.go @@ -48,12 +48,12 @@ func Test_status_String(t *testing.T) { }) // full name statusStr, digestStr := s.String(120) - if err := testutils.OrderedMatch(statusStr+digestStr, " [\x1b[7m\x1b[0m....................]", s.prompt, s.descriptor.MediaType, "0.00/2 B", "0.00%", s.descriptor.Digest.String()); err != nil { + if err := testutils.OrderedMatch(statusStr+digestStr, "\x1b[0m....................]", s.prompt, s.descriptor.MediaType, "0.00/2 B", "0.00%", s.descriptor.Digest.String()); err != nil { t.Error(err) } // partial name statusStr, digestStr = s.String(console.MinWidth) - if err := testutils.OrderedMatch(statusStr+digestStr, " [\x1b[7m\x1b[0m....................]", s.prompt, "application/v.", "0.00/2 B", "0.00%", s.descriptor.Digest.String()); err != nil { + if err := testutils.OrderedMatch(statusStr+digestStr, "\x1b[0m....................]", s.prompt, "application/v.", "0.00/2 B", "0.00%", s.descriptor.Digest.String()); err != nil { t.Error(err) } // done From 5d9d9a97c2ce702615ee16ee072531174db0a0be Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Mon, 15 Apr 2024 04:50:45 +0000 Subject: [PATCH 3/4] fix ut Signed-off-by: Billy Zha --- cmd/oras/internal/display/status/progress/status_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/oras/internal/display/status/progress/status_test.go b/cmd/oras/internal/display/status/progress/status_test.go index 16d234a44..fab24e73f 100644 --- a/cmd/oras/internal/display/status/progress/status_test.go +++ b/cmd/oras/internal/display/status/progress/status_test.go @@ -89,7 +89,7 @@ func Test_status_String_zeroWitdth(t *testing.T) { }) // not done statusStr, digestStr := s.String(120) - if err := testutils.OrderedMatch(statusStr+digestStr, " [\x1b[7m\x1b[0m....................]", s.prompt, s.descriptor.MediaType, "0.00/0 B", "0.00%", s.descriptor.Digest.String()); err != nil { + if err := testutils.OrderedMatch(statusStr+digestStr, "\x1b[0m....................]", s.prompt, s.descriptor.MediaType, "0.00/0 B", "0.00%", s.descriptor.Digest.String()); err != nil { t.Error(err) } // done From 0f6e9e2118db840303d85b7bd88373643bbcc615 Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Mon, 15 Apr 2024 05:10:38 +0000 Subject: [PATCH 4/4] resolve comment Signed-off-by: Billy Zha --- cmd/oras/internal/display/status/progress/status.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/oras/internal/display/status/progress/status.go b/cmd/oras/internal/display/status/progress/status.go index 3a73ca894..fb7803202 100644 --- a/cmd/oras/internal/display/status/progress/status.go +++ b/cmd/oras/internal/display/status/progress/status.go @@ -137,7 +137,7 @@ func (s *status) String(width int) (string, string) { bar := fmt.Sprintf("[%s%s]", progressColor.Apply(strings.Repeat(" ", lenBar)), strings.Repeat(".", barLength-lenBar)) speed := s.calculateSpeed() left = fmt.Sprintf("%s %s(%*s/s) %s %s", - spinnerColor.Apply(fmt.Sprintf("%c", s.mark.symbol())), + spinnerColor.Apply(string(s.mark.symbol())), bar, speedLength, speed, s.prompt, name) // bar + wrapper(2) + space(1) + speed + "/s"(2) + wrapper(2) = len(bar) + len(speed) + 7 lenLeft = barLength + speedLength + 7