File: templates.go

package info (click to toggle)
golang-github-smallstep-cli 0.15.16%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,404 kB
  • sloc: sh: 512; makefile: 99
file content (93 lines) | stat: -rw-r--r-- 3,436 bytes parent folder | download | duplicates (2)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package ui

import (
	"fmt"
	"runtime"

	"github.com/chzyer/readline"
	"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)("▸")
)

func init() {
	// Set VT100 characters for windows too
	if runtime.GOOS == "windows" {
		promptui.KeyEnter = readline.CharEnter
		promptui.KeyBackspace = readline.CharBackspace
		promptui.KeyPrev = readline.CharPrev
		promptui.KeyPrevDisplay = "↑"
		promptui.KeyNext = readline.CharNext
		promptui.KeyNextDisplay = "↓"
		promptui.KeyBackward = readline.CharBackward
		promptui.KeyBackwardDisplay = "←"
		promptui.KeyForward = readline.CharForward
		promptui.KeyForwardDisplay = "→"
	}
}

// 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),
	}
}