Implement 7-bit ASCII prefixes

pull/3686/head
Thomas Stromberg 2019-02-15 06:52:33 -08:00
parent 7e6c688116
commit 3838442440
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) {
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)
}
})
}
}

View File

@ -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,39 +41,39 @@ 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: "🏄 "},
"running": {Prefix: "🏃 "},
"provisioning": {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: "😿 "},
"sad": {Prefix: "😿 ", LowPrefix: "* "},
"thumbs-up": {Prefix: "👍 "},
"option": {Prefix: " ▪ "}, // Indented bullet
"url": {Prefix: "👉 "},
"crushed": {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: "& "},
"mounting": {Prefix: "📁 "},
"celebrate": {Prefix: "🎉 "},
"container-runtime": {Prefix: "🎁 "},
@ -78,10 +86,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
@ -98,6 +106,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)
@ -113,10 +132,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
}