forked from sohomdatta1/promptui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_prompt_test.go
41 lines (34 loc) · 1018 Bytes
/
example_prompt_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package promptui
import (
"fmt"
"strconv"
)
// This example shows how to use the prompt validator and templates to create a stylized prompt.
// The validator will make sure the value entered is a parseable float while the templates will
// color the value to show validity.
func ExamplePrompt() {
// The validate function follows the required validator signature.
validate := func(input string) error {
_, err := strconv.ParseFloat(input, 64)
return err
}
// Each template displays the data received from the prompt with some formatting.
templates := &PromptTemplates{
Prompt: "{{ . }} ",
Valid: "{{ . | green }} ",
Invalid: "{{ . | red }} ",
Success: "{{ . | bold }} ",
}
prompt := Prompt{
Label: "Spicy Level",
Templates: templates,
Validate: validate,
}
result, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}
// The result of the prompt, if valid, is displayed in a formatted message.
fmt.Printf("You answered %s\n", result)
}