forked from smallstep/cli
/
templates.go
75 lines (63 loc) · 2.83 KB
/
templates.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 ui
import (
"fmt"
"github.com/manifoldco/promptui"
)
var (
// IconInitial is the icon used when starting in prompt mode and the icon next to the label when
// starting in select mode.
IconInitial = promptui.Styler(promptui.FGBlue)("?")
// IconGood is the icon used when a good answer is entered in prompt mode.
IconGood = promptui.Styler(promptui.FGGreen)("✔")
// IconWarn is the icon used when a good, but potentially invalid answer is entered in prompt mode.
IconWarn = promptui.Styler(promptui.FGYellow)("⚠")
// IconBad is the icon used when a bad answer is entered in prompt mode.
IconBad = promptui.Styler(promptui.FGRed)("✗")
// IconSelect is the icon used to identify the currently selected item in select mode.
IconSelect = promptui.Styler(promptui.FGBold)("▸")
)
// PrintSelectedTemplate returns the default template used in PrintSelected.
func PrintSelectedTemplate() string {
return fmt.Sprintf(`{{ "%s" | green }} {{ .Name | bold }}{{ ":" | bold }} {{ .Value }}`, IconGood) + "\n"
}
// PromptTemplates is the default style for a prompt.
func PromptTemplates() *promptui.PromptTemplates {
bold := promptui.Styler(promptui.FGBold)
return &promptui.PromptTemplates{
Prompt: fmt.Sprintf("%s {{ . | bold }}%s ", IconInitial, bold(":")),
Success: fmt.Sprintf("%s {{ . | bold }}%s ", bold(IconGood), bold(":")),
// Confirm: fmt.Sprintf(`{{ "%s" | bold }} {{ . | bold }}? {{ "[]" | faint }} `, IconInitial),
Valid: fmt.Sprintf("%s {{ . | bold }}%s ", bold(IconGood), bold(":")),
Invalid: fmt.Sprintf("%s {{ . | bold }}%s ", bold(IconBad), bold(":")),
}
}
// SimplePromptTemplates is a prompt with a simple style, used by default on password prompts.
func SimplePromptTemplates() *promptui.PromptTemplates {
return &promptui.PromptTemplates{
Prompt: "{{ . }}: ",
Success: "{{ . }}: ",
Valid: "{{ . }}: ",
Invalid: "{{ . }}: ",
}
}
// SelectTemplates returns the default promptui.SelectTemplate for string
// slices. The given name is the prompt of the selected option.
func SelectTemplates(name string) *promptui.SelectTemplates {
return &promptui.SelectTemplates{
Label: fmt.Sprintf("%s {{ . }}: ", IconInitial),
Active: fmt.Sprintf("%s {{ . | underline }}", IconSelect),
Inactive: " {{ . }}",
Selected: fmt.Sprintf(`{{ "%s" | green }} {{ "%s:" | bold }} {{ .Name }}`, IconGood, name),
}
}
// NamedSelectTemplates returns the default promptui.SelectTemplate for struct
// slices with a name property. The given name is the prompt of the selected
// option.
func NamedSelectTemplates(name string) *promptui.SelectTemplates {
return &promptui.SelectTemplates{
Label: fmt.Sprintf("%s {{.Name}}: ", IconInitial),
Active: fmt.Sprintf("%s {{ .Name | underline }}", IconSelect),
Inactive: " {{.Name}}",
Selected: fmt.Sprintf(`{{ "%s" | green }} {{ "%s:" | bold }} {{ .Name }}`, IconGood, name),
}
}