Skip to content

Commit

Permalink
Strip ESO color codes from Author and Description strings as well
Browse files Browse the repository at this point in the history
  • Loading branch information
dyoung522 committed Sep 7, 2024
1 parent 11a9e8f commit 78d6c01
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 60 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var cfgFile string
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "esotools",
Version: "0.2.1",
Version: "0.2.2",
Short: "tools used to list, install, and validate ESO AddOns",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
verbosity, _ := cmd.Flags().GetCount("verbose")
Expand Down
48 changes: 31 additions & 17 deletions pkg/esoAddOns/esoAddOns.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func (A AddOn) String() string {
return fmt.Sprintf(
"Title: %s, Description: %s, Author: %v, Version: %s, AddOnVersion: %s, APIVersion: %s, SavedVariables: %s, DependsOn: %s, OptionalDependsOn: %s, IsDependency: %v, IsLibrary: %v",
A.CleanTitle(),
A.Description,
A.Author,
A.CleanDescription(),
A.CleanAuthor(),
A.Version,
A.AddOnVersion,
A.APIVersion,
Expand Down Expand Up @@ -116,7 +116,7 @@ func (A AddOn) ToMarkdown() string {
var description string

if A.Description != "" {
description = fmt.Sprintf("\n%s\n", A.Description)
description = fmt.Sprintf("\n%s\n", A.CleanDescription())
}

return fmt.Sprintf("## %s\n%s", A.TitleString(), description)
Expand Down Expand Up @@ -144,7 +144,7 @@ func (A AddOn) TitleString() string {
var (
title = cyan.Sprint(A.CleanTitle())
version = blue.Sprintf("v%s", A.Version)
author = A.Author
author = A.CleanAuthor()
)

if viper.GetBool("noColor") {
Expand Down Expand Up @@ -230,22 +230,19 @@ func (A *AddOn) Validate() bool {
return len(A.Errors()) == 0
}

// CleanAuthor returns the Author without any ESO color codes
func (A AddOn) CleanAuthor() string {
return StripESOColorCodes(A.Author)
}

// CleanTitle returns the Title without any ESO color codes
func (A AddOn) CleanTitle() string {
if !strings.Contains(A.Title, `|c`) {
return A.Title
}

colorRE := regexp.MustCompile(`\|c[[:xdigit:]]{6}`)
re := regexp.MustCompile(`\|c[[:xdigit:]]{6}(.*?)(?:\|r)`)

cleanTitle := re.ReplaceAllStringFunc(A.Title, func(match string) string {
parts := re.FindStringSubmatch(match)
return parts[1]
})
return StripESOColorCodes(A.Title)
}

// Strip out any remaining color codes from the clean title.
return colorRE.ReplaceAllString(cleanTitle, "")
// CleanDescription returns the Author without any ESO color codes
func (A AddOn) CleanDescription() string {
return StripESOColorCodes(A.Description)
}

/*
Expand Down Expand Up @@ -370,3 +367,20 @@ func (A AddOns) Print(format string) string {
func ToKey(input string) string {
return strings.ReplaceAll(strings.TrimSpace(input), " ", "-")
}

func StripESOColorCodes(input string) string {
if !strings.Contains(input, `|c`) {
return input
}

colorRE := regexp.MustCompile(`\|c[[:xdigit:]]{6}`)
re := regexp.MustCompile(`\|c[[:xdigit:]]{6}(.*?)(?:\|r)`)

cleanString := re.ReplaceAllStringFunc(input, func(match string) string {
parts := re.FindStringSubmatch(match)
return parts[1]
})

// Strip out any remaining color codes from the clean title.
return colorRE.ReplaceAllString(cleanString, "")
}
81 changes: 39 additions & 42 deletions pkg/esoAddOns/esoAddOns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,47 +170,44 @@ func TestValidateTitle_InvalidAddon(t *testing.T) {
assert.Equal(t, addon1.Errors(), expectedErrors)
}

func TestCleanTitle_WithColorCode(t *testing.T) {
addon1 := esoAddOns.AddOn{Title: "|c121212Addon|r One", Author: "Author One", Version: "1.0"}

output := addon1.CleanTitle()
expected := "Addon One"

assert.Equal(t, expected, output)
}

func TestCleanTitle_WithMultipleColorCode(t *testing.T) {
addon1 := esoAddOns.AddOn{Title: "|c121212Addon|r One |c323232Forever|r Clean", Author: "Author One", Version: "1.0"}

output := addon1.CleanTitle()
expected := "Addon One Forever Clean"

assert.Equal(t, expected, output)
}

func TestCleanTitle_WithMultipleColorCodeAtEnd(t *testing.T) {
addon1 := esoAddOns.AddOn{Title: "|c121212Addon|r One |c323232Forever|r", Author: "Author One", Version: "1.0"}

output := addon1.CleanTitle()
expected := "Addon One Forever"

assert.Equal(t, expected, output)
}

func TestCleanTitle_WithInvalidColorCodes(t *testing.T) {
addon1 := esoAddOns.AddOn{Title: "|c00FF00Addon |cFFFF00One|r", Author: "Author One", Version: "1.0"}

output := addon1.CleanTitle()
expected := "Addon One"

assert.Equal(t, expected, output)
}

func TestCleanTitle_WithoutColorCode(t *testing.T) {
addon1 := esoAddOns.AddOn{Title: "Addon One", Author: "Author One", Version: "1.0"}

output := addon1.CleanTitle()
expected := "Addon One"
func TestStripESOColorCodes(t *testing.T) {
tests := []struct {
name string
input string
expect string
}{
{
name: "no color codes",
input: "Test String",
expect: "Test String",
},
{
name: "single color code",
input: "|c121212Test|r String",
expect: "Test String",
},
{
name: "multiple color codes",
input: "|c121212Test|r String |c323232Multiple|r Codes",
expect: "Test String Multiple Codes",
},
{
name: "color codes at end of string",
input: "|c121212Test|r String |c323232Multiple|r",
expect: "Test String Multiple",
},
{
name: "invalid color codes",
input: "|c00FF00Test |cFFFF00String|r",
expect: "Test String",
},
}

assert.Equal(t, expected, output)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
actual := esoAddOns.StripESOColorCodes(tt.input)
assert.Equal(tt.expect, actual, "Expected %q, but got %q", tt.expect, actual)
})
}
}

0 comments on commit 78d6c01

Please sign in to comment.