scripting system, hue fixes and such

This commit is contained in:
2023-08-29 00:06:57 +02:00
parent da8fd03e8a
commit 8776ed6df3
48 changed files with 1128 additions and 72 deletions
+39
View File
@@ -6,6 +6,45 @@ import (
"strings"
)
func CompactMatch(ids []string) string {
if len(ids) == 1 {
return ids[0]
}
shortestPrefix := ids[0]
for _, curr := range ids[1:] {
minLength := len(shortestPrefix)
if len(curr) < minLength {
minLength = len(curr)
}
if len(shortestPrefix) > minLength {
shortestPrefix = shortestPrefix[:minLength]
}
for i := 0; i < minLength; i++ {
if curr[i] != shortestPrefix[i] {
shortestPrefix = curr[:i]
break
}
}
}
sb := strings.Builder{}
sb.WriteString(shortestPrefix)
sb.WriteByte('{')
for i, curr := range ids {
if i != 0 {
sb.WriteByte(',')
}
sb.WriteString(curr[len(shortestPrefix):])
}
sb.WriteByte('}')
return sb.String()
}
func CompactIDList(ids []string) []string {
ids = append(ids[:0:0], ids...)
sort.Strings(ids)
+39
View File
@@ -0,0 +1,39 @@
package formattools
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestCompactMatch(t *testing.T) {
table := []struct {
O string
I []string
}{
{
"nanoleaf:172.16.119.33:{abcd,dead,beef,0000}",
[]string{
"nanoleaf:172.16.119.33:abcd", "nanoleaf:172.16.119.33:dead",
"nanoleaf:172.16.119.33:beef", "nanoleaf:172.16.119.33:0000",
},
},
{
"nanoleaf:172.16.{119.33:abcd,22.33:dead}",
[]string{
"nanoleaf:172.16.119.33:abcd", "nanoleaf:172.16.22.33:dead",
},
},
{
"{nanoleaf:172.16.119.33:abcd,hue:172.16.119.34:81bd3cc3-cf22-4401-a5cb-a4bf18687270}",
[]string{
"nanoleaf:172.16.119.33:abcd", "hue:172.16.119.34:81bd3cc3-cf22-4401-a5cb-a4bf18687270",
},
},
}
for _, row := range table {
t.Run(row.O, func(t *testing.T) {
assert.Equal(t, row.O, CompactMatch(row.I))
})
}
}