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

feat(putils): add CenterText in putils #399

Merged
merged 2 commits into from Sep 5, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions internal/center_text.go
Expand Up @@ -7,8 +7,12 @@ import (
)

// CenterText returns a centered string with a padding left and right
// If width is 0, it will be calculated automatically
func CenterText(text string, width int) string {
var lines []string
if width == 0 {
width = GetStringMaxWidth(text)
}
linesTmp := strings.Split(text, "\n")
for _, line := range linesTmp {
if len(color.ClearCode(line)) > width {
Expand Down
1 change: 1 addition & 0 deletions putils/README.md
Expand Up @@ -14,6 +14,7 @@ Feel free to contribute your utility functions via pull request!
func BulletListFromString(s string, padding string) pterm.BulletListPrinter
func BulletListFromStrings(s []string, padding string) pterm.BulletListPrinter
func BulletListItemFromString(text string, padding string) pterm.BulletListItem
func CenterText(text string) string
func DefaultTableFromStructSlice(structSlice interface{}) *pterm.TablePrinter
func DownloadFileWithDefaultProgressbar(title, outputPath, url string, mode os.FileMode) error
func DownloadFileWithProgressbar(progressbar *pterm.ProgressbarPrinter, outputPath, url string, mode os.FileMode) error
Expand Down
8 changes: 8 additions & 0 deletions putils/center_text.go
@@ -0,0 +1,8 @@
package putils

import "github.com/pterm/pterm/internal"

// CenterText returns a centered string with each line centered in respect to the longest line.
func CenterText(text string) string {
return internal.CenterText(text, 0)
}
11 changes: 11 additions & 0 deletions putils/center_text_test.go
@@ -0,0 +1,11 @@
package putils

import (
"testing"

"github.com/MarvinJWendt/testza"
)

func TestCenterText(t *testing.T) {
testza.AssertEqual(t, "Hello Wolrd\n !!! ", CenterText("Hello Wolrd\n!!!"))
}