Skip to content

Commit

Permalink
[fix] support light state text like "#8000FFL0T1500" in parse state q…
Browse files Browse the repository at this point in the history
…uery (#14)

* parse state text

* negative cases

* Update examples
  • Loading branch information
hyorigo committed Oct 27, 2023
1 parent 296f19a commit 5474dec
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
12 changes: 11 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var (
titleRegexPat *regexp.Regexp
repeatRegexPat *regexp.Regexp
commentRegexPat *regexp.Regexp
stateTextRegexPat *regexp.Regexp
colorRegexPats = make(map[string]*regexp.Regexp)
colorRegexOrder []string
fadeMsecRegexPats = make(map[int]*regexp.Regexp)
Expand All @@ -36,6 +37,7 @@ func initRegex() {
repeatRegexPat = regexp.MustCompile(`\brepeat\s*[:=]*\s*(\d+|\bonce|\btwice|\bthrice|\bforever|\balways|\binfinite(?:ly)?)\b|\b(infinite(?:ly)?|forever|always|once|twice|thrice)\s+repeat\b`)
commentRegexPat = regexp.MustCompile(`(\/\/.*?$)`)
titleRegexPat = regexp.MustCompile(`(?i)\b(title|topic|idea|subject)\s*[:=]*\s*([^\s].*?[^\s])\s*$`)
stateTextRegexPat = regexp.MustCompile(`(?i)^#[0-9A-Fa-f]{6}L\dT\d+$`)

// for colors
colorWords := make([]string, 0, len(presetColorMap))
Expand Down Expand Up @@ -141,7 +143,7 @@ func ParseColor(query string) (color.Color, error) {
}

// ParseStateQuery parses the case-insensitive unstructured description of light state and returns the structured LightState.
// The query can contain information about the color, fade time, and LED index. For example, "turn off all lights right now", "set led 1 to color #ff00ff over 2 sec".
// The query can contain information about the color, fade time, and LED index. For example, "turn off all lights right now", "set led 1 to color #ff00ff over 2 sec", "#FF0000L1T500".
// If the query is empty, it returns an error.
//
// Color can be specified by name, hex code, or RGB/HSB values, e.g. "red", "#FF0000", "rgb(255,0,0)", "hsb(0,100,100)"
Expand All @@ -163,6 +165,14 @@ func ParseStateQuery(query string) (LightState, error) {
// remove comments
query = commentRegexPat.ReplaceAllString(query, emptyStr)

// attempt to parse full as state
if stateTextRegexPat.MatchString(query) {
var st LightState
if err := st.UnmarshalText([]byte(query)); err == nil {
return st, nil
}
}

// parse each part
var err error
if state.Color, err = parseColorQuery(query); err != nil {
Expand Down
20 changes: 20 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,26 @@ func TestParseStateQuery(t *testing.T) {
query: `led=1 color=yellow now // led=2 color=blue time=500ms`,
want: b1.LightState{Color: b1.ColorYellow, LED: b1.LED1, FadeTime: 0},
},
{
query: `#8000FFL0T1500`,
want: b1.LightState{Color: color.RGBA{R: 0x80, G: 0x0, B: 0xff, A: 0xff}, LED: b1.LEDAll, FadeTime: 1500 * time.Millisecond},
},
{
query: `#123456l2t20 `,
want: b1.LightState{Color: color.RGBA{R: 0x12, G: 0x34, B: 0x56, A: 0xff}, LED: b1.LED2, FadeTime: 20 * time.Millisecond},
},
{
query: `#8000FGL0T1500`,
wantErr: true,
},
{
query: `#123456l2x20`,
wantErr: true,
},
{
query: `123456l2t20`,
wantErr: true,
},
{
query: `led=1 color=yellow`,
wantErr: true,
Expand Down

0 comments on commit 5474dec

Please sign in to comment.