diff --git a/ui/color/color.go b/ui/color/color.go index b1b53d3..7920c9e 100644 --- a/ui/color/color.go +++ b/ui/color/color.go @@ -31,11 +31,11 @@ func SprintFunc(c color.Style) func(args ...interface{}) string { type Option func(*options) type options struct { - Bold bool - Bg bool + bold bool + bg bool } -func NewOptions(opts []Option) options { +func newOptions(opts []Option) options { options := options{} for i := range opts { if opts[i] == nil { @@ -46,24 +46,24 @@ func NewOptions(opts []Option) options { return options } -func Bold(o *options) { o.Bold = true } -func Background(o *options) { o.Bg = true } +func Bold(o *options) { o.bold = true } +func Background(o *options) { o.bg = true } func Yellow(opts ...Option) func(a ...interface{}) string { - options := NewOptions(opts) - if options.Bold { + options := newOptions(opts) + if options.bold { return yellowBold } return yellow } func Green(opts ...Option) func(a ...interface{}) string { - options := NewOptions(opts) - if options.Bold { + options := newOptions(opts) + if options.bold { return greenBold } - if options.Bg { + if options.bg { return greenBg } @@ -71,44 +71,63 @@ func Green(opts ...Option) func(a ...interface{}) string { } func Red(opts ...Option) func(a ...interface{}) string { - options := NewOptions(opts) - if options.Bold { + options := newOptions(opts) + if options.bold { return redBold } return red } func White(opts ...Option) func(a ...interface{}) string { - options := NewOptions(opts) - if options.Bold { + options := newOptions(opts) + if options.bold { return whiteBold } return white } func Cyan(opts ...Option) func(a ...interface{}) string { - options := NewOptions(opts) - if options.Bold { + options := newOptions(opts) + if options.bold { return cyanBold } - if options.Bg { + if options.bg { return cyanBg } return cyan } func Black(opts ...Option) func(a ...interface{}) string { - options := NewOptions(opts) - if options.Bg { + options := newOptions(opts) + if options.bg { return blackBg } return black } func Magenta(opts ...Option) func(a ...interface{}) string { - options := NewOptions(opts) - if options.Bg { + options := newOptions(opts) + if options.bg { return magentaBg } return magentaBg } + +func HSL256(h, s, l float64, opts ...Option) func(a ...interface{}) string { + options := newOptions(opts) + val := color.HSL(h, s, l).C256().Value() + c := color.S256(val) + if options.bg { + fg := color.White.C256().Value() + if l > 0.5 { + fg = color.Black.C256().Value() + } + c = color.S256(fg, val) + } + if options.bold { + c.AddOpts(color.Bold) + } + return func(a ...interface{}) string { + return c.Sprint(a...) + } +} diff --git a/ui/views/views.go b/ui/views/views.go index bfde012..cccf67e 100644 --- a/ui/views/views.go +++ b/ui/views/views.go @@ -7,10 +7,9 @@ import ( "github.com/pkg/errors" "github.com/edouardparis/lntop/config" - lntcolor "github.com/edouardparis/lntop/ui/color" + "github.com/edouardparis/lntop/ui/color" "github.com/edouardparis/lntop/ui/cursor" "github.com/edouardparis/lntop/ui/models" - "github.com/gookit/color" ) type View interface { @@ -139,7 +138,7 @@ func interp(a, b [3]float64, r float64) (result [3]float64) { return } -func ColorizeAge(age uint32, text string, opts ...lntcolor.Option) string { +func ColorizeAge(age uint32, text string, opts ...color.Option) string { ageColors := [][3]float64{ {120, 0.9, 0.9}, {60, 0.9, 0.6}, @@ -153,11 +152,5 @@ func ColorizeAge(age uint32, text string, opts ...lntcolor.Option) string { } else { cur = ageColors[2] } - val := color.HSL(cur[0]/360, cur[1], cur[2]).C256().Value() - c := color.S256(val) - options := lntcolor.NewOptions(opts) - if options.Bold { - c.AddOpts(color.Bold) - } - return c.Sprint(text) + return color.HSL256(cur[0]/360, cur[1], cur[2], opts...)(text) }