-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support <Ask> and <Select> prompts in templates
Ref #375
- Loading branch information
1 parent
04ef16e
commit c409d48
Showing
3 changed files
with
201 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package forms | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
type Select struct { | ||
Prompt string | ||
Options []Option | ||
} | ||
|
||
type Ask struct { | ||
Prompt string | ||
Multiline bool | ||
} | ||
|
||
type Option struct { | ||
Item string | ||
Value string | ||
} | ||
|
||
func promptAsks(str string, promptFn func(Ask) string) string { | ||
re := regexp.MustCompile(`(?i)<Ask\s+([^,]+)(,[^>]+)?>`) | ||
for { | ||
tokens := re.FindAllStringSubmatch(str, -1) | ||
if len(tokens) == 0 { | ||
return str | ||
} | ||
replace, prompt, options := tokens[0][0], tokens[0][1], strings.TrimPrefix(tokens[0][2], ",") | ||
a := Ask{Prompt: prompt, Multiline: strings.EqualFold(options, "MU")} | ||
ans := promptFn(a) | ||
str = strings.Replace(str, replace, ans, 1) | ||
} | ||
} | ||
|
||
func promptSelects(str string, promptFn func(Select) Option) string { | ||
re := regexp.MustCompile(`(?i)<Select\s+([^,]+)(,[^>]+)?>`) | ||
for { | ||
tokens := re.FindAllStringSubmatch(str, -1) | ||
if len(tokens) == 0 { | ||
return str | ||
} | ||
replace, prompt, options := tokens[0][0], tokens[0][1], strings.Split(strings.TrimPrefix(tokens[0][2], ","), ",") | ||
s := Select{Prompt: prompt} | ||
for _, opt := range options { | ||
item, value, ok := strings.Cut(opt, "=") | ||
if !ok { | ||
value = item | ||
} | ||
s.Options = append(s.Options, Option{Item: item, Value: value}) | ||
} | ||
ans := promptFn(s) | ||
str = strings.Replace(str, replace, ans.Value, 1) | ||
} | ||
} | ||
|
||
func promptVars(str string, promptFn func(string) string) string { | ||
re := regexp.MustCompile(`(?i)<Var\s+(\w+)\s*>`) | ||
for { | ||
tokens := re.FindAllStringSubmatch(str, -1) | ||
if len(tokens) == 0 { | ||
return str | ||
} | ||
replace, key := tokens[0][0], tokens[0][1] | ||
ans := promptFn(key) | ||
if ans == "" { | ||
ans = "blank" | ||
} | ||
str = strings.Replace(str, replace, ans, 1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package forms | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestReplaceSelect(t *testing.T) { | ||
tests := []struct { | ||
In, Expect string | ||
Answer func(Select) Option | ||
}{ | ||
{ | ||
In: "", | ||
Expect: "", | ||
Answer: nil, | ||
}, | ||
{ | ||
In: "foobar", | ||
Expect: "foobar", | ||
Answer: nil, | ||
}, | ||
{ | ||
In: `Subj: //WL2K <Select Prioritet:,Routine=R/,Priority=P/,Immediate=O/,Flash=Z/> <Callsign>/<SeqNum> - <Var Subject>`, | ||
Expect: `Subj: //WL2K R/ <Callsign>/<SeqNum> - <Var Subject>`, | ||
Answer: func(s Select) Option { return s.Options[0] }, | ||
}, | ||
} | ||
for i, tt := range tests { | ||
t.Run(fmt.Sprint(i), func(t *testing.T) { | ||
got := promptSelects(tt.In, tt.Answer) | ||
if got != tt.Expect { | ||
t.Errorf("Expected %q, got %q", tt.Expect, got) | ||
} | ||
}) | ||
} | ||
} |