From d9d3f36aa15e97e1f0aa08db4b3c7641109163b8 Mon Sep 17 00:00:00 2001 From: derailed Date: Fri, 7 Jun 2019 23:55:01 -0600 Subject: [PATCH] update string colorizer --- cmd/info.go | 8 ++++---- cmd/root.go | 8 ++++---- cmd/version.go | 8 ++++---- internal/{printer => color}/colorize.go | 12 ++++++------ internal/color/colorize_test.go | 25 +++++++++++++++++++++++++ internal/resource/log_options.go | 6 +++--- internal/resource/pod.go | 6 +++--- 7 files changed, 49 insertions(+), 24 deletions(-) rename internal/{printer => color}/colorize.go (51%) create mode 100644 internal/color/colorize_test.go diff --git a/cmd/info.go b/cmd/info.go index 57d1c67f..19e52be2 100644 --- a/cmd/info.go +++ b/cmd/info.go @@ -3,8 +3,8 @@ package cmd import ( "fmt" + "github.com/derailed/k9s/internal/color" "github.com/derailed/k9s/internal/config" - "github.com/derailed/k9s/internal/printer" "github.com/derailed/k9s/internal/views" "github.com/spf13/cobra" ) @@ -23,15 +23,15 @@ func infoCmd() *cobra.Command { func printInfo() { const sectionFmt = "%-15s " - printLogo(printer.Cyan) + printLogo(color.Cyan) printTuple(sectionFmt, "Configuration", config.K9sConfigFile) printTuple(sectionFmt, "Logs", config.K9sLogs) printTuple(sectionFmt, "Screen Dumps", config.K9sDumpDir) } -func printLogo(color printer.Color) { +func printLogo(c color.Paint) { for _, l := range views.LogoSmall { - fmt.Println(printer.Colorize(l, color)) + fmt.Println(color.Colorize(l, c)) } fmt.Println() } diff --git a/cmd/root.go b/cmd/root.go index 4cc04a01..3fb8ae14 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -5,9 +5,9 @@ import ( "fmt" "runtime/debug" + "github.com/derailed/k9s/internal/color" "github.com/derailed/k9s/internal/config" "github.com/derailed/k9s/internal/k8s" - "github.com/derailed/k9s/internal/printer" "github.com/derailed/k9s/internal/views" "github.com/rs/zerolog" "github.com/rs/zerolog/log" @@ -65,9 +65,9 @@ func run(cmd *cobra.Command, args []string) { if err := recover(); err != nil { log.Error().Msgf("Boom! %v", err) log.Error().Msg(string(debug.Stack())) - printLogo(printer.Red) - fmt.Printf(printer.Colorize("Boom!! ", printer.Red)) - fmt.Println(printer.Colorize(fmt.Sprintf("%v.", err), printer.White)) + printLogo(color.Red) + fmt.Printf(color.Colorize("Boom!! ", color.Red)) + fmt.Println(color.Colorize(fmt.Sprintf("%v.", err), color.White)) } }() diff --git a/cmd/version.go b/cmd/version.go index 03b4a9ca..2eb644a3 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -3,7 +3,7 @@ package cmd import ( "fmt" - "github.com/derailed/k9s/internal/printer" + "github.com/derailed/k9s/internal/color" "github.com/spf13/cobra" ) @@ -21,13 +21,13 @@ func versionCmd() *cobra.Command { func printVersion() { const secFmt = "%-10s " - printLogo(printer.Cyan) + printLogo(color.Cyan) printTuple(secFmt, "Version", version) printTuple(secFmt, "Commit", commit) printTuple(secFmt, "Date", date) } func printTuple(format, section, value string) { - fmt.Printf(printer.Colorize(fmt.Sprintf(format, section+":"), printer.Cyan)) - fmt.Println(printer.Colorize(value, printer.White)) + fmt.Printf(color.Colorize(fmt.Sprintf(format, section+":"), color.Cyan)) + fmt.Println(color.Colorize(value, color.White)) } diff --git a/internal/printer/colorize.go b/internal/color/colorize.go similarity index 51% rename from internal/printer/colorize.go rename to internal/color/colorize.go index ad898abb..80d8f20c 100644 --- a/internal/printer/colorize.go +++ b/internal/color/colorize.go @@ -1,15 +1,15 @@ -package printer +package color import ( "fmt" ) -// Color describes a terminal color. -type Color int +// Paint describes a terminal color. +type Paint int // Defines basic ANSI colors. const ( - Black Color = iota + 30 + Black Paint = iota + 30 Red Green Yellow @@ -22,8 +22,8 @@ const ( Bold = 1 ) -// Colorize a string based on given color. -func Colorize(s string, c Color) string { +// Colorize returns an ASCII colored string based on given color. +func Colorize(s string, c Paint) string { if c == 0 { c = White } diff --git a/internal/color/colorize_test.go b/internal/color/colorize_test.go new file mode 100644 index 00000000..126f54d1 --- /dev/null +++ b/internal/color/colorize_test.go @@ -0,0 +1,25 @@ +package color + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestColorize(t *testing.T) { + uu := map[string]struct { + s string + c Paint + e string + }{ + "white": {"blee", White, "\x1b[37mblee\x1b[0m"}, + "black": {"blee", Black, "\x1b[30mblee\x1b[0m"}, + "default": {"blee", 0, "\x1b[37mblee\x1b[0m"}, + } + + for k, u := range uu { + t.Run(k, func(t *testing.T) { + assert.Equal(t, u.e, Colorize(u.s, u.c)) + }) + } +} diff --git a/internal/resource/log_options.go b/internal/resource/log_options.go index 8a0152ed..19143142 100644 --- a/internal/resource/log_options.go +++ b/internal/resource/log_options.go @@ -3,7 +3,7 @@ package resource import ( "strings" - "github.com/derailed/k9s/internal/printer" + "github.com/derailed/k9s/internal/color" ) // LogOptions represent logger options. @@ -11,7 +11,7 @@ type LogOptions struct { Namespace, Name, Container string Lines int64 Previous bool - Color printer.Color + Color color.Paint } // HasContainer checks if a container is present. @@ -47,5 +47,5 @@ func (o LogOptions) NormalizeName() string { if o.Color == 0 { return "" } - return printer.Colorize(o.Name+":"+o.Container+" ", o.Color) + return color.Colorize(o.Name+":"+o.Container+" ", o.Color) } diff --git a/internal/resource/pod.go b/internal/resource/pod.go index c7834f13..1c6f96b1 100644 --- a/internal/resource/pod.go +++ b/internal/resource/pod.go @@ -9,8 +9,8 @@ import ( "sync/atomic" "time" + "github.com/derailed/k9s/internal/color" "github.com/derailed/k9s/internal/k8s" - "github.com/derailed/k9s/internal/printer" "github.com/derailed/k9s/internal/watch" "github.com/rs/zerolog/log" v1 "k8s.io/api/core/v1" @@ -118,12 +118,12 @@ func (r *Pod) Containers(path string, includeInit bool) ([]string, error) { return r.Resource.(k8s.Loggable).Containers(ns, po, includeInit) } -func asColor(n string) printer.Color { +func asColor(n string) color.Paint { var sum int for _, r := range n { sum += int(r) } - return printer.Color(30 + 1 + sum%6) + return color.Paint(30 + 1 + sum%6) } // PodLogs tail logs for all containers in a running Pod.