update string colorizer

mine
derailed 2019-06-07 23:55:01 -06:00
parent 765b721b87
commit d9d3f36aa1
7 changed files with 49 additions and 24 deletions

View File

@ -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()
}

View File

@ -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))
}
}()

View File

@ -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))
}

View File

@ -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
}

View File

@ -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))
})
}
}

View File

@ -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)
}

View File

@ -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.