Debian Codegraph @

ui/templates.go

raw symbols SCIP
1 package ui
2
3 import (
4 "fmt"
5 "runtime"
6
7 "github.com/chzyer/readline"
8 "github.com/manifoldco/promptui"
9 )
10
11 var (
12 // IconInitial is the icon used when starting in prompt mode and the icon next to the label when
13 // starting in select mode.
14 IconInitial = promptui.Styler(promptui.FGBlue)("?")
15
16 // IconGood is the icon used when a good answer is entered in prompt mode.
17 IconGood = promptui.Styler(promptui.FGGreen)("✔")
18
19 // IconWarn is the icon used when a good, but potentially invalid answer is entered in prompt mode.
20 IconWarn = promptui.Styler(promptui.FGYellow)("⚠")
21
22 // IconBad is the icon used when a bad answer is entered in prompt mode.
23 IconBad = promptui.Styler(promptui.FGRed)("✗")
24
25 // IconSelect is the icon used to identify the currently selected item in select mode.
26 IconSelect = promptui.Styler(promptui.FGBold)("▸")
27 )
28
29 func init() {
30 // Set VT100 characters for windows too
31 if runtime.GOOS == "windows" {
32 promptui.KeyEnter = readline.CharEnter
33 promptui.KeyBackspace = readline.CharBackspace
34 promptui.KeyPrev = readline.CharPrev
35 promptui.KeyPrevDisplay = "↑"
36 promptui.KeyNext = readline.CharNext
37 promptui.KeyNextDisplay = "↓"
38 promptui.KeyBackward = readline.CharBackward
39 promptui.KeyBackwardDisplay = "←"
40 promptui.KeyForward = readline.CharForward
41 promptui.KeyForwardDisplay = "→"
42 }
43 }
44
45 // PrintSelectedTemplate returns the default template used in PrintSelected.
46 func PrintSelectedTemplate() string {
47 return fmt.Sprintf(`{{ "%s" | green }} {{ .Name | bold }}{{ ":" | bold }} {{ .Value }}`, IconGood) + "\n"
48 }
49
50 // PromptTemplates is the default style for a prompt.
51 func PromptTemplates() *promptui.PromptTemplates {
52 bold := promptui.Styler(promptui.FGBold)
53 return &promptui.PromptTemplates{
54 Prompt: fmt.Sprintf("%s {{ . | bold }}%s ", IconInitial, bold(":")),
55 Success: fmt.Sprintf("%s {{ . | bold }}%s ", bold(IconGood), bold(":")),
56 // Confirm: fmt.Sprintf(`{{ "%s" | bold }} {{ . | bold }}? {{ "[]" | faint }} `, IconInitial),
57 Valid: fmt.Sprintf("%s {{ . | bold }}%s ", bold(IconGood), bold(":")),
58 Invalid: fmt.Sprintf("%s {{ . | bold }}%s ", bold(IconBad), bold(":")),
59 }
60 }
61
62 // SimplePromptTemplates is a prompt with a simple style, used by default on password prompts.
63 func SimplePromptTemplates() *promptui.PromptTemplates {
64 return &promptui.PromptTemplates{
65 Prompt: "{{ . }}: ",
66 Success: "{{ . }}: ",
67 Valid: "{{ . }}: ",
68 Invalid: "{{ . }}: ",
69 }
70 }
71
72 // SelectTemplates returns the default promptui.SelectTemplate for string
73 // slices. The given name is the prompt of the selected option.
74 func SelectTemplates(name string) *promptui.SelectTemplates {
75 return &promptui.SelectTemplates{
76 Label: fmt.Sprintf("%s {{ . }}: ", IconInitial),
77 Active: fmt.Sprintf("%s {{ . | underline }}", IconSelect),
78 Inactive: " {{ . }}",
79 Selected: fmt.Sprintf(`{{ "%s" | green }} {{ "%s:" | bold }} {{ .Name }}`, IconGood, name),
80 }
81 }
82
83 // NamedSelectTemplates returns the default promptui.SelectTemplate for struct
84 // slices with a name property. The given name is the prompt of the selected
85 // option.
86 func NamedSelectTemplates(name string) *promptui.SelectTemplates {
87 return &promptui.SelectTemplates{
88 Label: fmt.Sprintf("%s {{.Name}}: ", IconInitial),
89 Active: fmt.Sprintf("%s {{ .Name | underline }}", IconSelect),
90 Inactive: " {{.Name}}",
91 Selected: fmt.Sprintf(`{{ "%s" | green }} {{ "%s:" | bold }} {{ .Name }}`, IconGood, name),
92 }
93 }