|
1 | 1 | package cmd
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
4 | 5 | "log"
|
5 | 6 | "os"
|
6 | 7 | "strings"
|
@@ -121,41 +122,64 @@ func arrayType(section *ini.Section, field, value string) {
|
121 | 122 | utils.Fatal(err)
|
122 | 123 | }
|
123 | 124 |
|
124 |
| - allExts := key.Strings("|") |
| 125 | + allExts := make(map[string]bool) |
| 126 | + for _, v := range key.Strings("|") { |
| 127 | + allExts[v] = true |
| 128 | + } |
125 | 129 |
|
126 |
| - isSubstract := value[len(value)-1] == '-' |
127 |
| - if isSubstract { |
128 |
| - value = value[0 : len(value)-1] |
129 |
| - found := false |
130 |
| - newList := []string{} |
131 |
| - for _, v := range allExts { |
132 |
| - if value == v { |
133 |
| - found = true |
134 |
| - } else { |
135 |
| - newList = append(newList, v) |
136 |
| - } |
137 |
| - } |
| 130 | + values := strings.Split(value, "|") |
| 131 | + duplicates := []string{} |
| 132 | + inputValues := make(map[string]bool) |
| 133 | + modifiedValues := 0 |
138 | 134 |
|
139 |
| - if !found { |
140 |
| - unchangeWarning(field, value+" is not on the list.") |
141 |
| - return |
| 135 | + for _, value := range values { |
| 136 | + isSubstract := strings.HasSuffix(value, "-") |
| 137 | + if isSubstract { |
| 138 | + value = value[:len(value)-1] |
142 | 139 | }
|
143 | 140 |
|
144 |
| - allExts = newList |
145 |
| - } else { |
146 |
| - for _, ext := range allExts { |
147 |
| - if value == ext { |
148 |
| - unchangeWarning(field, value+" is already in the list.") |
| 141 | + if isSubstract { |
| 142 | + if _, found := allExts[value]; !found { |
| 143 | + unchangeWarning(field, fmt.Sprintf("%s is not on the list.", value)) |
149 | 144 | return
|
150 | 145 | }
|
| 146 | + |
| 147 | + modifiedValues++ |
| 148 | + delete(allExts, value) |
| 149 | + } else { |
| 150 | + if _, found := allExts[value]; found && !inputValues[value] { |
| 151 | + duplicates = append(duplicates, value) |
| 152 | + } else if _, found := allExts[value]; !found { |
| 153 | + allExts[value] = true |
| 154 | + modifiedValues++ |
| 155 | + } |
| 156 | + |
| 157 | + inputValues[value] = true |
151 | 158 | }
|
| 159 | + } |
152 | 160 |
|
153 |
| - allExts = append(allExts, value) |
| 161 | + if len(duplicates) > 0 { |
| 162 | + unchangeWarning(field, fmt.Sprintf("%s %s already in the list.", strings.Join(duplicates, ", "), pluralize(len(duplicates), "is", "are"))) |
154 | 163 | }
|
155 | 164 |
|
156 |
| - newList := strings.Join(allExts, "|") |
157 |
| - key.SetValue(newList) |
158 |
| - changeSuccess(field, newList) |
| 165 | + if modifiedValues == 0 { |
| 166 | + return |
| 167 | + } |
| 168 | + |
| 169 | + newList := make([]string, 0, len(allExts)) |
| 170 | + for k := range allExts { |
| 171 | + newList = append(newList, k) |
| 172 | + } |
| 173 | + |
| 174 | + key.SetValue(strings.Join(newList, "|")) |
| 175 | + changeSuccess(field, strings.Join(newList, "|")) |
| 176 | +} |
| 177 | + |
| 178 | +func pluralize(count int, singular, plural string) string { |
| 179 | + if count == 1 { |
| 180 | + return singular |
| 181 | + } |
| 182 | + return plural |
159 | 183 | }
|
160 | 184 |
|
161 | 185 | func stringType(section *ini.Section, field, value string) {
|
|
0 commit comments