Skip to content

Commit

Permalink
fix(config): check for duplicates when adding multiple values (#2963)
Browse files Browse the repository at this point in the history
  • Loading branch information
rxri authored Apr 15, 2024
1 parent d31784f commit 1ba6ef0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 26 deletions.
2 changes: 1 addition & 1 deletion jsHelper/spicetifyWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2390,7 +2390,7 @@ Spicetify.Playbar = (() => {
const prNumber = match[3];
const prLink = match[4];
let text = "<li>";
if (feature) text += `<strong>${feature}</strong>: `;
if (feature) text += `<strong>${feature}</strong>${!feature.endsWith(":") ? ": " : " "}`;
text += `${description} (<a href="${prLink}">${prNumber}</a>)</li>`;
return text;
})
Expand Down
1 change: 1 addition & 0 deletions src/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ func insertCustomApp(jsPath string, flags Flag) {

if (len(reactSymbs) < 2) || (len(eleSymbs) == 0) {
utils.PrintError("Spotify version mismatch with Spicetify. Please report it on our github repository.")
utils.PrintInfo("Spicetify might have been updated for this version already. Please run `spicetify update` to check for a new version. If one isn't available yet, please wait for the update to be released.")
return content
}

Expand Down
74 changes: 49 additions & 25 deletions src/cmd/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"fmt"
"log"
"os"
"strings"
Expand Down Expand Up @@ -121,41 +122,64 @@ func arrayType(section *ini.Section, field, value string) {
utils.Fatal(err)
}

allExts := key.Strings("|")
allExts := make(map[string]bool)
for _, v := range key.Strings("|") {
allExts[v] = true
}

isSubstract := value[len(value)-1] == '-'
if isSubstract {
value = value[0 : len(value)-1]
found := false
newList := []string{}
for _, v := range allExts {
if value == v {
found = true
} else {
newList = append(newList, v)
}
}
values := strings.Split(value, "|")
duplicates := []string{}
inputValues := make(map[string]bool)
modifiedValues := 0

if !found {
unchangeWarning(field, value+" is not on the list.")
return
for _, value := range values {
isSubstract := strings.HasSuffix(value, "-")
if isSubstract {
value = value[:len(value)-1]
}

allExts = newList
} else {
for _, ext := range allExts {
if value == ext {
unchangeWarning(field, value+" is already in the list.")
if isSubstract {
if _, found := allExts[value]; !found {
unchangeWarning(field, fmt.Sprintf("%s is not on the list.", value))
return
}

modifiedValues++
delete(allExts, value)
} else {
if _, found := allExts[value]; found && !inputValues[value] {
duplicates = append(duplicates, value)
} else if _, found := allExts[value]; !found {
allExts[value] = true
modifiedValues++
}

inputValues[value] = true
}
}

allExts = append(allExts, value)
if len(duplicates) > 0 {
unchangeWarning(field, fmt.Sprintf("%s %s already in the list.", strings.Join(duplicates, ", "), pluralize(len(duplicates), "is", "are")))
}

newList := strings.Join(allExts, "|")
key.SetValue(newList)
changeSuccess(field, newList)
if modifiedValues == 0 {
return
}

newList := make([]string, 0, len(allExts))
for k := range allExts {
newList = append(newList, k)
}

key.SetValue(strings.Join(newList, "|"))
changeSuccess(field, strings.Join(newList, "|"))
}

func pluralize(count int, singular, plural string) string {
if count == 1 {
return singular
}
return plural
}

func stringType(section *ini.Section, field, value string) {
Expand Down

0 comments on commit 1ba6ef0

Please sign in to comment.