Merge pull request #3686 from tstromberg/7-bit-console

Implement 7-bit ASCII prefixes for when MINIKUBE_IN_COLOR=false
pull/3680/head^2
Thomas Strömberg 2019-02-15 09:08:17 -08:00 committed by GitHub
commit 8e5fd5b275
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 40 deletions

View File

@ -46,17 +46,34 @@ func (f *fakeFile) String() string {
} }
func TestOutStyle(t *testing.T) { 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 { var tests = []struct {
t.Errorf("OutStyle() = %q, want %q", got, want) 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)
}
})
} }
} }

View File

@ -18,14 +18,22 @@ package console
import ( import (
"fmt" "fmt"
"strings"
"golang.org/x/text/message" "golang.org/x/text/message"
) )
var (
defaultLowPrefix = "- "
defautlLowIndentPrefix = " - "
)
// style describes how to stylize a message. // style describes how to stylize a message.
type style struct { type style struct {
// Prefix is a string to place in the beginning of a message // Prefix is a string to place in the beginning of a message
Prefix string 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 omits a newline at the end of a message.
OmitNewline bool OmitNewline bool
} }
@ -33,40 +41,40 @@ type style struct {
// styles is a map of style name to 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. // For consistency, ensure that emojis added render with the same width across platforms.
var styles = map[string]style{ var styles = map[string]style{
"happy": {Prefix: "😄 "}, "happy": {Prefix: "😄 ", LowPrefix: "o "},
"success": {Prefix: "✅ "}, "success": {Prefix: "✅ "},
"failure": {Prefix: "❌ "}, "failure": {Prefix: "❌ ", LowPrefix: "X "},
"conflict": {Prefix: "💥 "}, "conflict": {Prefix: "💥 ", LowPrefix: "x "},
"fatal": {Prefix: "💣 "}, "fatal": {Prefix: "💣 ", LowPrefix: "! "},
"notice": {Prefix: "📌 "}, "notice": {Prefix: "📌 ", LowPrefix: "* "},
"ready": {Prefix: "🏄 "}, "ready": {Prefix: "🏄 ", LowPrefix: "= "},
"restarting": {Prefix: "🔄 "}, "running": {Prefix: "🏃 ", LowPrefix: ": "},
"stopping": {Prefix: "✋ "}, "provisioning": {Prefix: "🌱 ", LowPrefix: "> "},
"restarting": {Prefix: "🔄 ", LowPrefix: ": "},
"stopping": {Prefix: "✋ ", LowPrefix: ": "},
"stopped": {Prefix: "🛑 "}, "stopped": {Prefix: "🛑 "},
"warning": {Prefix: "⚠️ "}, "warning": {Prefix: "⚠️ ", LowPrefix: "! "},
"waiting": {Prefix: "⌛ "}, "waiting": {Prefix: "⌛ ", LowPrefix: ": "},
"usage": {Prefix: "💡 "}, "usage": {Prefix: "💡 "},
"launch": {Prefix: "🚀 "}, "launch": {Prefix: "🚀 "},
"sad": {Prefix: "😿 ", LowPrefix: "* "},
"thumbs-up": {Prefix: "👍 "}, "thumbs-up": {Prefix: "👍 "},
"option": {Prefix: " ▪ "}, // Indented bullet "option": {Prefix: " ▪ "}, // Indented bullet
"log-entry": {Prefix: " "}, // Indent "log-entry": {Prefix: " "}, // Indent
"crushed": {Prefix: "💔 "}, "crushed": {Prefix: "💔 "},
"running": {Prefix: "🏃 "},
"provisioning": {Prefix: "🌱 "},
"sad": {Prefix: "😿 "},
"url": {Prefix: "👉 "}, "url": {Prefix: "👉 "},
// Specialized purpose styles // Specialized purpose styles
"iso-download": {Prefix: "💿 "}, "iso-download": {Prefix: "💿 ", LowPrefix: "@ "},
"file-download": {Prefix: "💾 "}, "file-download": {Prefix: "💾 ", LowPrefix: "@ "},
"caching": {Prefix: "🤹 "}, "caching": {Prefix: "🤹 ", LowPrefix: "$ "},
"starting-vm": {Prefix: "🔥 "}, "starting-vm": {Prefix: "🔥 ", LowPrefix: "> "},
"starting-none": {Prefix: "🤹 "}, "starting-none": {Prefix: "🤹 ", LowPrefix: "> "},
"resetting": {Prefix: "🔄 "}, "resetting": {Prefix: "🔄 ", LowPrefix: "# "},
"deleting-host": {Prefix: "🔥 "}, "deleting-host": {Prefix: "🔥 ", LowPrefix: "x "},
"copying": {Prefix: "✨ "}, "copying": {Prefix: "✨ "},
"connectivity": {Prefix: "📶 "}, "connectivity": {Prefix: "📶 "},
"internet": {Prefix: "🌐 "}, "internet": {Prefix: "🌐 ", LowPrefix: "o "},
"mounting": {Prefix: "📁 "}, "mounting": {Prefix: "📁 "},
"celebrate": {Prefix: "🎉 "}, "celebrate": {Prefix: "🎉 "},
"container-runtime": {Prefix: "🎁 "}, "container-runtime": {Prefix: "🎁 "},
@ -79,10 +87,10 @@ var styles = map[string]style{
"pulling": {Prefix: "🚜 "}, "pulling": {Prefix: "🚜 "},
"verifying": {Prefix: "🤔 "}, "verifying": {Prefix: "🤔 "},
"verifying-noline": {Prefix: "🤔 ", OmitNewline: true}, "verifying-noline": {Prefix: "🤔 ", OmitNewline: true},
"kubectl": {Prefix: "💗 "}, "kubectl": {Prefix: "💗 ", LowPrefix: "+ "},
"meh": {Prefix: "🙄 "}, "meh": {Prefix: "🙄 ", LowPrefix: "? "},
"embarassed": {Prefix: "🤦 "}, "embarassed": {Prefix: "🤦 ", LowPrefix: "* "},
"tip": {Prefix: "💡 "}, "tip": {Prefix: "💡 ", LowPrefix: "i "},
} }
// Add a prefix to a string // Add a prefix to a string
@ -99,6 +107,17 @@ func hasStyle(style string) bool {
return exists 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 // Apply styling to a format string
func applyStyle(style string, useColor bool, format string, a ...interface{}) (string, error) { func applyStyle(style string, useColor bool, format string, a ...interface{}) (string, error) {
p := message.NewPrinter(preferredLanguage) 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) return p.Sprintf(format, a...), fmt.Errorf("unknown style: %q", style)
} }
prefix := s.Prefix if !useColor {
if !useColor && prefix != "" { return applyPrefix(lowPrefix(s), out), nil
prefix = "-"
} }
out = applyPrefix(prefix, out) return applyPrefix(s.Prefix, out), nil
return out, nil
} }