Merge pull request #3686 from tstromberg/7-bit-console
Implement 7-bit ASCII prefixes for when MINIKUBE_IN_COLOR=falsepull/3680/head^2
commit
8e5fd5b275
|
@ -46,17 +46,34 @@ func (f *fakeFile) String() string {
|
|||
}
|
||||
|
||||
func TestOutStyle(t *testing.T) {
|
||||
os.Setenv(OverrideEnv, "1")
|
||||
f := newFakeFile()
|
||||
SetOutFile(f)
|
||||
if err := OutStyle("happy", "This is a happy message."); err != nil {
|
||||
t.Errorf("unexpected error: %q", err)
|
||||
}
|
||||
got := f.String()
|
||||
want := "😄 This is a happy message.\n"
|
||||
|
||||
if got != want {
|
||||
t.Errorf("OutStyle() = %q, want %q", got, want)
|
||||
var tests = []struct {
|
||||
style string
|
||||
envValue string
|
||||
message string
|
||||
want string
|
||||
}{
|
||||
{"happy", "true", "This is happy.", "😄 This is happy.\n"},
|
||||
{"Docker", "true", "This is Docker.", "🐳 This is Docker.\n"},
|
||||
{"option", "true", "This is option.", " ▪ This is option.\n"},
|
||||
|
||||
{"happy", "false", "This is happy.", "o This is happy.\n"},
|
||||
{"Docker", "false", "This is Docker.", "- This is Docker.\n"},
|
||||
{"option", "false", "This is option.", " - This is option.\n"},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.style+"-"+tc.envValue, func(t *testing.T) {
|
||||
os.Setenv(OverrideEnv, tc.envValue)
|
||||
f := newFakeFile()
|
||||
SetOutFile(f)
|
||||
if err := OutStyle(tc.style, tc.message); err != nil {
|
||||
t.Errorf("unexpected error: %q", err)
|
||||
}
|
||||
got := f.String()
|
||||
if got != tc.want {
|
||||
t.Errorf("OutStyle() = %q, want %q", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -18,14 +18,22 @@ package console
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/text/message"
|
||||
)
|
||||
|
||||
var (
|
||||
defaultLowPrefix = "- "
|
||||
defautlLowIndentPrefix = " - "
|
||||
)
|
||||
|
||||
// style describes how to stylize a message.
|
||||
type style struct {
|
||||
// Prefix is a string to place in the beginning of a message
|
||||
Prefix string
|
||||
// LowPrefix is the 7-bit compatible prefix we fallback to for less-awesome terminals
|
||||
LowPrefix string
|
||||
// OmitNewline omits a newline at the end of a message.
|
||||
OmitNewline bool
|
||||
}
|
||||
|
@ -33,40 +41,40 @@ type style struct {
|
|||
// styles is a map of style name to style struct
|
||||
// For consistency, ensure that emojis added render with the same width across platforms.
|
||||
var styles = map[string]style{
|
||||
"happy": {Prefix: "😄 "},
|
||||
"happy": {Prefix: "😄 ", LowPrefix: "o "},
|
||||
"success": {Prefix: "✅ "},
|
||||
"failure": {Prefix: "❌ "},
|
||||
"conflict": {Prefix: "💥 "},
|
||||
"fatal": {Prefix: "💣 "},
|
||||
"notice": {Prefix: "📌 "},
|
||||
"ready": {Prefix: "🏄 "},
|
||||
"restarting": {Prefix: "🔄 "},
|
||||
"stopping": {Prefix: "✋ "},
|
||||
"failure": {Prefix: "❌ ", LowPrefix: "X "},
|
||||
"conflict": {Prefix: "💥 ", LowPrefix: "x "},
|
||||
"fatal": {Prefix: "💣 ", LowPrefix: "! "},
|
||||
"notice": {Prefix: "📌 ", LowPrefix: "* "},
|
||||
"ready": {Prefix: "🏄 ", LowPrefix: "= "},
|
||||
"running": {Prefix: "🏃 ", LowPrefix: ": "},
|
||||
"provisioning": {Prefix: "🌱 ", LowPrefix: "> "},
|
||||
"restarting": {Prefix: "🔄 ", LowPrefix: ": "},
|
||||
"stopping": {Prefix: "✋ ", LowPrefix: ": "},
|
||||
"stopped": {Prefix: "🛑 "},
|
||||
"warning": {Prefix: "⚠️ "},
|
||||
"waiting": {Prefix: "⌛ "},
|
||||
"warning": {Prefix: "⚠️ ", LowPrefix: "! "},
|
||||
"waiting": {Prefix: "⌛ ", LowPrefix: ": "},
|
||||
"usage": {Prefix: "💡 "},
|
||||
"launch": {Prefix: "🚀 "},
|
||||
"sad": {Prefix: "😿 ", LowPrefix: "* "},
|
||||
"thumbs-up": {Prefix: "👍 "},
|
||||
"option": {Prefix: " ▪ "}, // Indented bullet
|
||||
"log-entry": {Prefix: " "}, // Indent
|
||||
"crushed": {Prefix: "💔 "},
|
||||
"running": {Prefix: "🏃 "},
|
||||
"provisioning": {Prefix: "🌱 "},
|
||||
"sad": {Prefix: "😿 "},
|
||||
"url": {Prefix: "👉 "},
|
||||
|
||||
// Specialized purpose styles
|
||||
"iso-download": {Prefix: "💿 "},
|
||||
"file-download": {Prefix: "💾 "},
|
||||
"caching": {Prefix: "🤹 "},
|
||||
"starting-vm": {Prefix: "🔥 "},
|
||||
"starting-none": {Prefix: "🤹 "},
|
||||
"resetting": {Prefix: "🔄 "},
|
||||
"deleting-host": {Prefix: "🔥 "},
|
||||
"iso-download": {Prefix: "💿 ", LowPrefix: "@ "},
|
||||
"file-download": {Prefix: "💾 ", LowPrefix: "@ "},
|
||||
"caching": {Prefix: "🤹 ", LowPrefix: "$ "},
|
||||
"starting-vm": {Prefix: "🔥 ", LowPrefix: "> "},
|
||||
"starting-none": {Prefix: "🤹 ", LowPrefix: "> "},
|
||||
"resetting": {Prefix: "🔄 ", LowPrefix: "# "},
|
||||
"deleting-host": {Prefix: "🔥 ", LowPrefix: "x "},
|
||||
"copying": {Prefix: "✨ "},
|
||||
"connectivity": {Prefix: "📶 "},
|
||||
"internet": {Prefix: "🌐 "},
|
||||
"internet": {Prefix: "🌐 ", LowPrefix: "o "},
|
||||
"mounting": {Prefix: "📁 "},
|
||||
"celebrate": {Prefix: "🎉 "},
|
||||
"container-runtime": {Prefix: "🎁 "},
|
||||
|
@ -79,10 +87,10 @@ var styles = map[string]style{
|
|||
"pulling": {Prefix: "🚜 "},
|
||||
"verifying": {Prefix: "🤔 "},
|
||||
"verifying-noline": {Prefix: "🤔 ", OmitNewline: true},
|
||||
"kubectl": {Prefix: "💗 "},
|
||||
"meh": {Prefix: "🙄 "},
|
||||
"embarassed": {Prefix: "🤦 "},
|
||||
"tip": {Prefix: "💡 "},
|
||||
"kubectl": {Prefix: "💗 ", LowPrefix: "+ "},
|
||||
"meh": {Prefix: "🙄 ", LowPrefix: "? "},
|
||||
"embarassed": {Prefix: "🤦 ", LowPrefix: "* "},
|
||||
"tip": {Prefix: "💡 ", LowPrefix: "i "},
|
||||
}
|
||||
|
||||
// Add a prefix to a string
|
||||
|
@ -99,6 +107,17 @@ func hasStyle(style string) bool {
|
|||
return exists
|
||||
}
|
||||
|
||||
// lowPrefix returns a 7-bit compatible prefix for a style
|
||||
func lowPrefix(s style) string {
|
||||
if s.LowPrefix != "" {
|
||||
return s.LowPrefix
|
||||
}
|
||||
if strings.HasPrefix(s.Prefix, " ") {
|
||||
return defautlLowIndentPrefix
|
||||
}
|
||||
return defaultLowPrefix
|
||||
}
|
||||
|
||||
// Apply styling to a format string
|
||||
func applyStyle(style string, useColor bool, format string, a ...interface{}) (string, error) {
|
||||
p := message.NewPrinter(preferredLanguage)
|
||||
|
@ -114,10 +133,8 @@ func applyStyle(style string, useColor bool, format string, a ...interface{}) (s
|
|||
return p.Sprintf(format, a...), fmt.Errorf("unknown style: %q", style)
|
||||
}
|
||||
|
||||
prefix := s.Prefix
|
||||
if !useColor && prefix != "" {
|
||||
prefix = "-"
|
||||
if !useColor {
|
||||
return applyPrefix(lowPrefix(s), out), nil
|
||||
}
|
||||
out = applyPrefix(prefix, out)
|
||||
return out, nil
|
||||
return applyPrefix(s.Prefix, out), nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue