refactor: make not ugly

This commit is contained in:
Stevan Freeborn
2025-12-20 13:44:07 -06:00
parent 195e12103a
commit e59deac7c9
3 changed files with 160 additions and 152 deletions
+40
View File
@@ -0,0 +1,40 @@
// Package button implements a button that can control multiple switches.
package button
import (
"fmt"
"regexp"
"strconv"
)
type Button interface {
Switches() []int
}
type button struct {
switches []int
}
func (b button) String() string {
return fmt.Sprintf("%v", b.switches)
}
func From(line string) button {
buttonRegex := regexp.MustCompile(`\d+`)
matches := buttonRegex.FindAllString(line, -1)
switches := []int{}
for _, m := range matches {
num, _ := strconv.Atoi(m)
switches = append(switches, num)
}
return button{
switches: switches,
}
}
func (b button) Switches() []int {
return b.switches
}