aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/manifoldco/promptui/codes.go
diff options
context:
space:
mode:
authorValentin Rothberg <rothberg@redhat.com>2020-10-07 16:58:53 +0200
committerValentin Rothberg <rothberg@redhat.com>2020-11-13 15:40:06 +0100
commit8e4a42aa429c6dec0d5face7c69554d8a0677e96 (patch)
treebbfff77e7b32a8b46af6f57d42965a7751bec18e /vendor/github.com/manifoldco/promptui/codes.go
parent0b1a60ec27928a40ac827148c1517098612616bd (diff)
downloadpodman-8e4a42aa429c6dec0d5face7c69554d8a0677e96.tar.gz
podman-8e4a42aa429c6dec0d5face7c69554d8a0677e96.tar.bz2
podman-8e4a42aa429c6dec0d5face7c69554d8a0677e96.zip
short-name aliasing
Add support for short-name aliasing. Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
Diffstat (limited to 'vendor/github.com/manifoldco/promptui/codes.go')
-rw-r--r--vendor/github.com/manifoldco/promptui/codes.go120
1 files changed, 120 insertions, 0 deletions
diff --git a/vendor/github.com/manifoldco/promptui/codes.go b/vendor/github.com/manifoldco/promptui/codes.go
new file mode 100644
index 000000000..8138c40d6
--- /dev/null
+++ b/vendor/github.com/manifoldco/promptui/codes.go
@@ -0,0 +1,120 @@
+package promptui
+
+import (
+ "fmt"
+ "strconv"
+ "strings"
+ "text/template"
+)
+
+const esc = "\033["
+
+type attribute int
+
+// The possible state of text inside the application, either Bold, faint, italic or underline.
+//
+// These constants are called through the use of the Styler function.
+const (
+ reset attribute = iota
+
+ FGBold
+ FGFaint
+ FGItalic
+ FGUnderline
+)
+
+// The possible colors of text inside the application.
+//
+// These constants are called through the use of the Styler function.
+const (
+ FGBlack attribute = iota + 30
+ FGRed
+ FGGreen
+ FGYellow
+ FGBlue
+ FGMagenta
+ FGCyan
+ FGWhite
+)
+
+// The possible background colors of text inside the application.
+//
+// These constants are called through the use of the Styler function.
+const (
+ BGBlack attribute = iota + 40
+ BGRed
+ BGGreen
+ BGYellow
+ BGBlue
+ BGMagenta
+ BGCyan
+ BGWhite
+)
+
+// ResetCode is the character code used to reset the terminal formatting
+var ResetCode = fmt.Sprintf("%s%dm", esc, reset)
+
+const (
+ hideCursor = esc + "?25l"
+ showCursor = esc + "?25h"
+ clearLine = esc + "2K"
+)
+
+// FuncMap defines template helpers for the output. It can be extended as a regular map.
+//
+// The functions inside the map link the state, color and background colors strings detected in templates to a Styler
+// function that applies the given style using the corresponding constant.
+var FuncMap = template.FuncMap{
+ "black": Styler(FGBlack),
+ "red": Styler(FGRed),
+ "green": Styler(FGGreen),
+ "yellow": Styler(FGYellow),
+ "blue": Styler(FGBlue),
+ "magenta": Styler(FGMagenta),
+ "cyan": Styler(FGCyan),
+ "white": Styler(FGWhite),
+ "bgBlack": Styler(BGBlack),
+ "bgRed": Styler(BGRed),
+ "bgGreen": Styler(BGGreen),
+ "bgYellow": Styler(BGYellow),
+ "bgBlue": Styler(BGBlue),
+ "bgMagenta": Styler(BGMagenta),
+ "bgCyan": Styler(BGCyan),
+ "bgWhite": Styler(BGWhite),
+ "bold": Styler(FGBold),
+ "faint": Styler(FGFaint),
+ "italic": Styler(FGItalic),
+ "underline": Styler(FGUnderline),
+}
+
+func upLine(n uint) string {
+ return movementCode(n, 'A')
+}
+
+func movementCode(n uint, code rune) string {
+ return esc + strconv.FormatUint(uint64(n), 10) + string(code)
+}
+
+// Styler is a function that accepts multiple possible styling transforms from the state,
+// color and background colors constants and transforms them into a templated string
+// to apply those styles in the CLI.
+//
+// The returned styling function accepts a string that will be extended with
+// the wrapping function's styling attributes.
+func Styler(attrs ...attribute) func(interface{}) string {
+ attrstrs := make([]string, len(attrs))
+ for i, v := range attrs {
+ attrstrs[i] = strconv.Itoa(int(v))
+ }
+
+ seq := strings.Join(attrstrs, ";")
+
+ return func(v interface{}) string {
+ end := ""
+ s, ok := v.(string)
+ if !ok || !strings.HasSuffix(s, ResetCode) {
+ end = ResetCode
+ }
+ return fmt.Sprintf("%s%sm%v%s", esc, seq, v, end)
+ }
+}