diff --git a/parser.go b/parser.go index 91fe82b..39a67ca 100644 --- a/parser.go +++ b/parser.go @@ -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) @@ -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)) @@ -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)" @@ -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 { diff --git a/parser_test.go b/parser_test.go index f0bbda5..203a676 100644 --- a/parser_test.go +++ b/parser_test.go @@ -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,