Skip to content

Commit

Permalink
feat: add color to progress mark and bar (#1344)
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <jinzha1@microsoft.com>
  • Loading branch information
qweeah committed Apr 15, 2024
1 parent 202bcf4 commit cc74e50
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
14 changes: 11 additions & 3 deletions cmd/oras/internal/display/status/progress/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(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
} 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
Expand Down
6 changes: 3 additions & 3 deletions cmd/oras/internal/display/status/progress/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cc74e50

Please sign in to comment.