Skip to content

Commit

Permalink
Merge pull request #399 from pterm/putils_center_text
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinJWendt committed Sep 5, 2022
2 parents b33cb19 + 5bad05a commit da3b3f5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
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!!!"))
}

0 comments on commit da3b3f5

Please sign in to comment.