Replace the color library, colorize channel age

pull/85/head
rkfg 2 years ago
parent 9cdac2a3d4
commit 76454a420a

@ -4,8 +4,8 @@ go 1.16
require (
github.com/BurntSushi/toml v0.3.1
github.com/fatih/color v1.7.0
github.com/gofrs/uuid v4.0.0+incompatible
github.com/gookit/color v1.5.2
github.com/jroimartin/gocui v0.4.0
github.com/lightningnetwork/lnd v0.15.0-beta
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d // indirect

@ -179,7 +179,6 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fergusstrange/embedded-postgres v1.10.0 h1:YnwF6xAQYmKLAXXrrRx4rHDLih47YJwVPvg8jeKfdNg=
github.com/fergusstrange/embedded-postgres v1.10.0/go.mod h1:a008U8/Rws5FtIOTGYDYa7beVWsT3qVKyqExqYYjL+c=
@ -281,6 +280,8 @@ github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm4
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/gookit/color v1.5.2 h1:uLnfXcaFjlrDnQDT+NCBcfhrXqYTx/rcCa6xn01Y8yI=
github.com/gookit/color v1.5.2/go.mod h1:w8h4bGiHeeBpvQVePTutdbERIUf3oJE5lZ8HM0UgXyg=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
@ -506,13 +507,11 @@ github.com/masterzen/xmlpath v0.0.0-20140218185901-13f4951698ad/go.mod h1:A0zPC5
github.com/mattn/go-colorable v0.0.6/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
github.com/mattn/go-colorable v0.1.6 h1:6Su7aK7lXmJ/U79bYtBjLNaha4Fs1Rg9plHpcH+vvnE=
github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.0-20160806122752-66b8e73f3f5c/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
@ -635,16 +634,18 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70Z7CTTCmYQn2CKbY8j86K7/FAIr141uY=
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc=
@ -661,6 +662,8 @@ github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 h1:nIPpBwaJSVYIxUFsDv3M8ofm
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMxjDjgmT5uz5wzYJKVo23qUhYTos=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 h1:eY9dn8+vbi4tKz5Qo6v2eYzo7kUS51QINcR5jNpbZS8=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
@ -1134,8 +1137,9 @@ gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

@ -1,35 +1,41 @@
package color
import "github.com/fatih/color"
import "github.com/gookit/color"
type Color color.Color
var (
yellow = color.New(color.FgYellow).SprintFunc()
yellowBold = color.New(color.FgYellow, color.Bold).SprintFunc()
green = color.New(color.FgGreen).SprintFunc()
greenBold = color.New(color.FgGreen, color.Bold).SprintFunc()
greenBg = color.New(color.FgBlack, color.BgGreen).SprintFunc()
magentaBg = color.New(color.FgBlack, color.BgMagenta).SprintFunc()
red = color.New(color.FgRed).SprintFunc()
redBold = color.New(color.FgRed, color.Bold).SprintFunc()
cyan = color.New(color.FgCyan).SprintFunc()
cyanBold = color.New(color.FgCyan, color.Bold).SprintFunc()
cyanBg = color.New(color.BgCyan, color.FgBlack).SprintFunc()
white = color.New().SprintFunc()
whiteBold = color.New(color.Bold).SprintFunc()
blackBg = color.New(color.BgBlack, color.FgWhite).SprintFunc()
black = color.New(color.FgBlack).SprintFunc()
yellow = SprintFunc(color.New(color.FgYellow))
yellowBold = SprintFunc(color.New(color.FgYellow, color.Bold))
green = SprintFunc(color.New(color.FgGreen))
greenBold = SprintFunc(color.New(color.FgGreen, color.Bold))
greenBg = SprintFunc(color.New(color.FgBlack, color.BgGreen))
magentaBg = SprintFunc(color.New(color.FgBlack, color.BgMagenta))
red = SprintFunc(color.New(color.FgRed))
redBold = SprintFunc(color.New(color.FgRed, color.Bold))
cyan = SprintFunc(color.New(color.FgCyan))
cyanBold = SprintFunc(color.New(color.FgCyan, color.Bold))
cyanBg = SprintFunc(color.New(color.BgCyan, color.FgBlack))
white = SprintFunc(color.New())
whiteBold = SprintFunc(color.New(color.Bold))
blackBg = SprintFunc(color.New(color.BgBlack, color.FgWhite))
black = SprintFunc(color.New(color.FgBlack))
)
func SprintFunc(c color.Style) func(args ...interface{}) string {
return func(args ...interface{}) string {
return c.Sprint(args...)
}
}
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 {
@ -40,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
}
@ -65,43 +71,43 @@ 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

@ -11,7 +11,7 @@ import (
)
func Run(ctx context.Context, app *app.App, sub chan *events.Event) error {
g, err := gocui.NewGui(gocui.OutputNormal)
g, err := gocui.NewGui(gocui.Output256)
if err != nil {
return err
}

@ -651,7 +651,7 @@ func NewChannels(cfg *config.View, chans *models.Channels) *Channels {
if c.ID == 0 {
return fmt.Sprintf("%10s", "")
}
return color.White(opts...)(fmt.Sprintf("%10s", FormatAge(c.Age)))
return ColorizeAge(c.Age, printer.Sprintf("%10s", FormatAge(c.Age)), opts...)
},
}

@ -7,8 +7,10 @@ import (
"github.com/pkg/errors"
"github.com/edouardparis/lntop/config"
lntcolor "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 {
@ -120,15 +122,30 @@ func ToScid(id uint64) string {
func FormatAge(age uint32) string {
if age < 6 {
return fmt.Sprintf("%02dm", age*10)
}
if age < 144 {
} else if age < 144 {
return fmt.Sprintf("%02dh", age/6)
}
if age < 4383 {
} else if age < 4383 {
return fmt.Sprintf("%02dd%02dh", age/144, (age%144)/6)
}
if age < 52596 {
} else if age < 52596 {
return fmt.Sprintf("%02dm%02dd%02dh", age/4383, (age%4383)/144, (age%144)/6)
}
return fmt.Sprintf("%02dy%02dm%02dd", age/52596, (age%52596)/4383, (age%4383)/144)
}
func ColorizeAge(age uint32, text string, opts ...lntcolor.Option) string {
s := 1.0
l := 0.5
if age < 52596 {
s = float64(age) / 52596
}
if age < 8766 { // increase brightness slightly for 16 color terminals or else it renders as black
l = 0.66
}
val := color.HSL(22./360, s, l).C256().Value()
c := color.S256(val)
options := lntcolor.NewOptions(opts)
if options.Bold {
c.AddOpts(color.Bold)
}
return c.Sprint(text)
}

Loading…
Cancel
Save