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

Truncate progress text when needed to avoid hiding the progress bar #7

Merged
merged 1 commit into from
Aug 23, 2023
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
16 changes: 11 additions & 5 deletions internal/widgets/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/pterm/pterm"
"github.com/sirupsen/logrus"
"github.com/vmware-labs/distribution-tooling-for-helm/utils"
)

// ProgressBar defines a ProgressBar widget
Expand Down Expand Up @@ -76,13 +77,18 @@ func (p *PrettyProgressBar) Warnf(fmt string, args ...interface{}) {
p.printMessage(&pterm.Warning, fmt, args...)
}

// UpdateTitle updates the progress bar title
func (p *PrettyProgressBar) UpdateTitle(title string) ProgressBar {
minTitleLength := int(float32(p.ProgressbarPrinter.MaxWidth) * 0.6)
formattedTitle := fmt.Sprintf("%-*s", minTitleLength, title)
func (p *PrettyProgressBar) formatTitle(title string) string {
// We prefix with a leading " " so we align with other printers, that
// start with a leading space
p.ProgressbarPrinter.UpdateTitle(" " + p.padding + formattedTitle)
paddedTitle := " " + p.padding + title
maxTitleLength := int(float32(p.ProgressbarPrinter.MaxWidth) * 0.70)
truncatedTitle := utils.TruncateStringWithEllipsis(paddedTitle, maxTitleLength)
return fmt.Sprintf("%-*s", maxTitleLength, truncatedTitle)
}

// UpdateTitle updates the progress bar title
func (p *PrettyProgressBar) UpdateTitle(title string) ProgressBar {
p.ProgressbarPrinter.UpdateTitle(p.formatTitle(title))
return p
}

Expand Down
20 changes: 20 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,23 @@ func ExecuteWithRetry(retries int, cb func(try int, prevErr error) error) error
}
return nil
}

// TruncateStringWithEllipsis returns a truncated version of text
func TruncateStringWithEllipsis(text string, maxLength int) string {
if len(text) <= maxLength {
return text
}
if maxLength <= 0 {
return ""
}

ellipsis := "[...]"

// If the maxLength is so small the ellipsis does not fit, just return the prefix
if maxLength <= len(ellipsis) {
return text[0:maxLength]
}
startSplit := (maxLength - len(ellipsis)) / 2
endSplit := len(text) - (maxLength - startSplit - len(ellipsis))
return text[0:startSplit] + ellipsis + text[endSplit:]
}
41 changes: 39 additions & 2 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import (
"regexp"
"testing"

tu "github.com/vmware-labs/distribution-tooling-for-helm/internal/testutil"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
tu "github.com/vmware-labs/distribution-tooling-for-helm/internal/testutil"
)

var (
Expand Down Expand Up @@ -236,3 +235,41 @@ func validateError(t *testing.T, expectedErr string, err error) {
t.Errorf("got error = %v but expected to succeed", err)
}
}

func TestTruncateStringWithEllipsis(t *testing.T) {

tests := map[string]struct {
text string
maxLength int
want string
}{
"String short enough": {
"hello world", 20, "hello world",
},
"Truncated string odd length": {
"This is a long string to truncate", 15, "This [...]ncate",
},
"Truncated string even length": {
"This is a long string to truncate", 20, "This is[...]truncate",
},
"Max length too small": {
"This is a long string to truncate", 5, "This ",
},
"Max length too small (2)": {
"This is a long string to truncate", 1, "T",
},
"Max length too small (3)": {
"This is a long string to truncate", 0, "",
},
"Negative max length too small": {
"This is a long string to truncate", -5, "",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
if got := TruncateStringWithEllipsis(tt.text, tt.maxLength); got != tt.want {
t.Errorf("TruncateStringWithEllipsis() = %v, want %v", got, tt.want)
}
})
}
}