-
Notifications
You must be signed in to change notification settings - Fork 1
/
grail.go
75 lines (66 loc) · 2.4 KB
/
grail.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"os"
"strconv"
"github.com/burl/inquire"
"github.com/burl/inquire/widget"
)
func widgetTest() {
// vars that will receive the answers to the questions
var (
name, quest, weight, passwd string
red, green, blue, proceed bool
)
name = "Sir Lancelot" // if you want a default value
quest = "grail" // for any of the widgets, then
green = true // just assign values to the vars
inquire.Query().
// if you just have a plain old question, pass nil as the final arg
Input(&name, "What is your name", nil).
Menu(&quest, "What is your quest", func(w *widget.Menu) {
// if you want to do a bit more, pass a callback - each kind
// of widget will pass a type into the callback where you can...
w.Hint("use arrow keys, pick one") // set up custom hint text
w.Item("shrub", "find a shrubbery") // set up the values and prompts
w.Item("grail", "find the grail") // the &quest var will be set
w.Item("nuts", "find coconuts") // to one of: shrub, grail or nuts
}).
Input(&weight, "What is the weight of an unladen swallow", func(w *widget.Input) {
// this question will only be shown when the value of quest is "nuts"
w.WhenEqual(&quest, "nuts") // there is also a generic form
w.Valid(func(value string) string {
// and things can be validated ...
n, err := strconv.Atoi(value)
if err != nil || n < 1 {
// invalid input should return a non-empty error message
return "not good, you need to enter a number"
}
// if the data is valid, return an empty string
return ""
})
}).
Select("what are your favorite colors", func(w *widget.Select) {
w.Hint("use arrow/space, select multiple")
w.Item(&red, "red") // the select or "checkbox" widget will
w.Item(&blue, "blue") // toggle the value of the referenced
w.Item(&green, "green") // boolean variable
}).
Input(&passwd, "What is your secret", func(w *widget.Input) {
w.MaskInput() // or, w.MaskInput('*')
}).
YesNo(&proceed, "Continue"). // simple yes/no
Exec() // render all the questions.
if !proceed {
fmt.Println("aborted.")
os.Exit(1)
}
fmt.Printf("\nHere are the answers:\n---------------------\n")
fmt.Printf("name : %s\n", name)
fmt.Printf("quest : %s\n", quest)
fmt.Printf("colors: red:%v, green:%v, blue:%v\n", red, green, blue)
fmt.Printf("secret: %s (shhh!)\n", passwd)
}
func main() {
widgetTest()
}