Skip to content

Commit

Permalink
Combine color and string utils under stringutils package
Browse files Browse the repository at this point in the history
  • Loading branch information
anujc25 committed Feb 4, 2025
1 parent 978c5bd commit 45f3974
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 3 deletions.
File renamed without changes.
File renamed without changes.
9 changes: 8 additions & 1 deletion component/colorable-tty.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func init() {
NewAurora()
}

// Deprecated: NewAurora is being deprecated and will be removed in favor of using the `github.com/fatih/color` package
func NewAurora() auroraPackage.Aurora {
if aurora == nil {
aurora = auroraPackage.NewAurora(IsTTYEnabled())
Expand All @@ -36,28 +37,34 @@ func IsTTYEnabled() bool {

// Rpad adds padding to the right of a string.
// from https://github.com/spf13/cobra/blob/993cc5372a05240dfd59e3ba952748b36b2cd117/cobra.go#L29
//
// Deprecated: Rpad is being moved under `github.com/vmware-tanzu/tanzu-plugin-runtime/component/stringutils` package
func Rpad(s string, padding int) string {
tmpl := fmt.Sprintf("%%-%ds", padding)
return fmt.Sprintf(tmpl, s)
}

// Deprecated: Underline is being moved under `github.com/vmware-tanzu/tanzu-plugin-runtime/component/stringutils` package as `Sunderlinef`
func Underline(s string) string {
return aurora.Underline(s).String()
}

// Deprecated: Bold is being moved under `github.com/vmware-tanzu/tanzu-plugin-runtime/component/stringutils` package as `Sboldf`
func Bold(s string) string {
return aurora.Bold(s).String()
}

// Deprecated: TrimRightSpace is being moved under `github.com/vmware-tanzu/tanzu-plugin-runtime/component/stringutils` package
func TrimRightSpace(s string) string {
return strings.TrimRightFunc(s, unicode.IsSpace)
}

// Deprecated: BeginsWith is being moved under `github.com/vmware-tanzu/tanzu-plugin-runtime/component/stringutils` package
func BeginsWith(s, prefix string) bool {
return strings.HasPrefix(s, prefix)
}

// Green returns a green string.
// Deprecated: Green is being moved under `github.com/vmware-tanzu/tanzu-plugin-runtime/component/stringutils` package as `Ssuccessf`
func Green(s string) string {
return aurora.Green(s).String()
}
22 changes: 20 additions & 2 deletions component/printer/color.go → component/stringutils/color.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
// Copyright 2025 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// Package printer provides helpers to get different prints with different colors
package printer
// Package stringutils provides helpers to format strings
package stringutils

import (
"regexp"

"github.com/fatih/color"

Check failure on line 10 in component/stringutils/color.go

View workflow job for this annotation

GitHub Actions / Lint

File is not properly formatted (goimports)
"github.com/vmware-tanzu/tanzu-plugin-runtime/component"
)

func init() {
// Configure the global `NoColor` option within github.com/fatih/color library
// based on the user's terminal and provided options
color.NoColor = !component.IsTTYEnabled()
}

var (
FaintColor = color.New(color.Faint)
InfoColor = color.New(color.FgCyan)
SuccessColor = color.New(color.FgGreen)
WarnColor = color.New(color.FgYellow)
ErrorColor = color.New(color.FgRed)
BoldColor = color.New(color.Bold)

Underline = color.New(color.Underline)
Italic = color.New(color.Italic)
)

func Sfaintf(format string, a ...interface{}) string {
Expand All @@ -43,6 +53,14 @@ func Sboldf(format string, a ...interface{}) string {
return BoldColor.Sprintf(format, a...)
}

func Sunderlinef(format string, a ...interface{}) string {
return Underline.Sprintf(format, a...)
}

func Sitalic(format string, a ...interface{}) string {
return Italic.Sprintf(format, a...)
}

// StripColor removes ANSI escape codes from a string
func StripColor(s string) string {
ansiRegex := regexp.MustCompile("\x1b\\[(\\d+;)*\\d+m")
Expand Down
20 changes: 20 additions & 0 deletions component/stringutils/color_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2025 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// Package stringutils provides helpers to format strings
package stringutils

import (
"testing"
)

// Test StripColor function
func TestStripColor(t *testing.T) {
coloredText := SuccessColor.Sprintf("Success: %s", "operation completed")
expected := "Success: operation completed"

strippedText := StripColor(coloredText)
if strippedText != expected {
t.Errorf("StripColor(%q) = %q; want %q", coloredText, strippedText, expected)
}
}
25 changes: 25 additions & 0 deletions component/stringutils/strings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2025 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package stringutils

import (
"fmt"
"strings"
"unicode"
)

func TrimRightSpace(s string) string {
return strings.TrimRightFunc(s, unicode.IsSpace)
}

func BeginsWith(s, prefix string) bool {
return strings.HasPrefix(s, prefix)
}

// Rpad adds padding to the right of a string.
// from https://github.com/spf13/cobra/blob/993cc5372a05240dfd59e3ba952748b36b2cd117/cobra.go#L29
func Rpad(s string, padding int) string {
tmpl := fmt.Sprintf("%%-%ds", padding)
return fmt.Sprintf(tmpl, s)
}
68 changes: 68 additions & 0 deletions component/stringutils/strings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2025 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package stringutils

import (
"testing"
)

func TestTrimRightSpace(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"hello ", "hello"},
{"test\n\t", "test"},
{" no trim needed", " no trim needed"},
{"", ""},
}

for _, tt := range tests {
result := TrimRightSpace(tt.input)
if result != tt.expected {
t.Errorf("TrimRightSpace(%q) = %q; want %q", tt.input, result, tt.expected)
}
}
}

func TestBeginsWith(t *testing.T) {
tests := []struct {
s string
prefix string
expected bool
}{
{"hello world", "hello", true},
{"hello world", "world", false},
{"", "", true},
{"", "nonempty", false},
{"prefix test", "prefix", true},
}

for _, tt := range tests {
result := BeginsWith(tt.s, tt.prefix)
if result != tt.expected {
t.Errorf("BeginsWith(%q, %q) = %v; want %v", tt.s, tt.prefix, result, tt.expected)
}
}
}

func TestRpad(t *testing.T) {
tests := []struct {
s string
padding int
expected string
}{
{"test", 8, "test "},
{"hello", 3, "hello"},
{"pad", 6, "pad "},
{"", 4, " "},
}

for _, tt := range tests {
result := Rpad(tt.s, tt.padding)
if result != tt.expected {
t.Errorf("Rpad(%q, %d) = %q; want %q", tt.s, tt.padding, result, tt.expected)
}
}
}

0 comments on commit 45f3974

Please sign in to comment.