Skip to content

Commit

Permalink
fix: cleanup colors
Browse files Browse the repository at this point in the history
Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com>
  • Loading branch information
gfanton committed Mar 31, 2024
1 parent b5aec97 commit 4d9483e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 40 deletions.
19 changes: 15 additions & 4 deletions contribs/gnodev/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"
"time"

"github.com/charmbracelet/lipgloss"
"github.com/fsnotify/fsnotify"
"github.com/gnolang/gno/contribs/gnodev/pkg/dev"
gnodev "github.com/gnolang/gno/contribs/gnodev/pkg/dev"
Expand All @@ -21,6 +22,7 @@ import (
"github.com/gnolang/gno/contribs/gnodev/pkg/rawterm"
"github.com/gnolang/gno/contribs/gnodev/pkg/watcher"
"github.com/gnolang/gno/gno.land/pkg/gnoweb"
zaplog "github.com/gnolang/gno/gno.land/pkg/log"
"github.com/gnolang/gno/gnovm/pkg/gnoenv"
"github.com/gnolang/gno/gnovm/pkg/gnomod"
"github.com/gnolang/gno/tm2/pkg/commands"
Expand Down Expand Up @@ -237,8 +239,7 @@ func execDev(cfg *devCfg, args []string, io commands.IO) error {
// Add node pkgs to watcher
watcher.AddPackages(devNode.ListPkgs()...)

logger.WithGroup("---- READY").
Info("for commands and help, press `h`", "helper", helper)
logger.WithGroup("--- READY").Info("for commands and help, press `h`")

// Run the main event loop
return runEventLoop(ctx, logger, rt, devNode, watcher)
Expand Down Expand Up @@ -486,9 +487,19 @@ func setuplogger(cfg *devCfg, out io.Writer) *slog.Logger {
}

if cfg.serverMode {
return logger.NewZapLogger(out, level)
zaplogger := logger.NewZapLogger(out, level)
return zaplog.ZapLoggerToSlog(zaplogger)
}

// Detect term color profile
colorProfile := termenv.DefaultOutput().Profile
return logger.NewColumnLogger(out, level, colorProfile)
clogger := logger.NewColumnLogger(out, level, colorProfile)

// Register well known group color with system colors
clogger.RegisterGroupColor(NodeLogName, lipgloss.Color("3"))
clogger.RegisterGroupColor(WebLogName, lipgloss.Color("4"))
clogger.RegisterGroupColor(KeyPressLogName, lipgloss.Color("5"))
clogger.RegisterGroupColor(EventServerLogName, lipgloss.Color("6"))

return slog.New(clogger)
}
17 changes: 10 additions & 7 deletions contribs/gnodev/pkg/logger/colors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,25 @@ import (
"github.com/charmbracelet/lipgloss"
)

func ColorFromString(s string, saturation, lightness float64) lipgloss.Color {
const jitter = 0

func colorFromString(s string, saturation, lightness float64) lipgloss.Color {
hue := float64(hash(s) % 360)
r, g, b := HSLToRGB(hue, saturation, lightness)
hex := rgbToHex(int(r*255), int(g*255), int(b*255))

r, g, b := hslToRGB(float64(hue), saturation, lightness)
hex := rgbToHex(r, g, b)
return lipgloss.Color(hex)
}

func hash(s string) uint32 {
h := fnv.New32a()
h.Write([]byte(s))
return h.Sum32()
return h.Sum32() + jitter
}

// from: https://www.rapidtables.com/convert/color/hsl-to-rgb.html
// HSLToRGB converts an HSL triple to an RGB triple.
func HSLToRGB(h, s, l float64) (r, g, b uint8) {
// hslToRGB converts an HSL triple to an RGB triple.
func hslToRGB(h, s, l float64) (r, g, b uint8) {
if h < 0 || h >= 360 || s < 0 || s > 1 || l < 0 || l > 1 {
return 0, 0, 0
}
Expand Down Expand Up @@ -54,6 +57,6 @@ func HSLToRGB(h, s, l float64) (r, g, b uint8) {
return r, g, b
}

func rgbToHex(r, g, b int) string {
func rgbToHex(r, g, b uint8) string {
return fmt.Sprintf("#%02X%02X%02X", r, g, b)
}
61 changes: 35 additions & 26 deletions contribs/gnodev/pkg/logger/log_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,23 @@ import (
"io"
"log/slog"
"strings"
"sync"

"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
"github.com/muesli/termenv"
)

func NewColumnLogger(w io.Writer, level slog.Level, profile termenv.Profile) *slog.Logger {
func NewColumnLogger(w io.Writer, level slog.Level, profile termenv.Profile) *ColumnLogger {
charmLogger := log.NewWithOptions(w, log.Options{
ReportTimestamp: false,
ReportCaller: false,
Prefix: "",
})

// Default column output
charmLogger.SetOutput(&columnWriter{
style: lipgloss.NewStyle(),
prefix: "",
writer: w,
})

columnHandler := &columnLogger{
Logger: charmLogger,
writer: w,
prefix: charmLogger.GetPrefix(),
}

charmLogger.SetOutput(newColumeWriter(lipgloss.NewStyle(), "", w))
defaultOutput := newColumeWriter(lipgloss.NewStyle(), "", w)
charmLogger.SetOutput(defaultOutput)
charmLogger.SetStyles(defaultStyles())
charmLogger.SetColorProfile(profile)
charmLogger.SetReportCaller(false)
Expand All @@ -49,39 +39,58 @@ func NewColumnLogger(w io.Writer, level slog.Level, profile termenv.Profile) *sl
panic("invalid slog level")
}

return slog.New(columnHandler)
return &ColumnLogger{
Logger: charmLogger,
writer: w,
prefix: charmLogger.GetPrefix(),
colors: map[string]lipgloss.Color{},
}
}

type columnLogger struct {
type ColumnLogger struct {
*log.Logger

prefix string
writer io.Writer
colorProfile termenv.Profile

colors map[string]lipgloss.Color
muColors sync.RWMutex
}

func (cl *columnLogger) WithGroup(name string) slog.Handler {
func (cl *ColumnLogger) WithGroup(group string) slog.Handler {
cl.muColors.RLock()
defer cl.muColors.RUnlock()

if cl.prefix != "" {
name = fmt.Sprintf("%.1s.%s", cl.prefix, name)
group = fmt.Sprintf("%.1s.%s", cl.prefix, group)
}

fg := ColorFromString(name, 0.6, 0.4)
baseStyle := lipgloss.NewStyle().Foreground(fg)
// check if we already know this group
fg, ok := cl.colors[group]
if !ok {
// generate bright color based on the group name
fg = colorFromString(group, 0.5, 0.6)
}

styles := defaultStyles()
styles.Message = styles.Message.Foreground(fg)
baseStyle := lipgloss.NewStyle().Foreground(fg)

nlog := cl.Logger.With() // clone logger
nlog.SetOutput(newColumeWriter(baseStyle, name, cl.writer))
nlog.SetOutput(newColumeWriter(baseStyle, group, cl.writer))
nlog.SetColorProfile(cl.colorProfile)
nlog.SetStyles(styles)
return &columnLogger{
return &ColumnLogger{
Logger: nlog,
prefix: name,
prefix: group,
writer: cl.writer,
}
}

func (cl *ColumnLogger) RegisterGroupColor(group string, color lipgloss.Color) {
cl.muColors.Lock()
cl.colors[group] = color
cl.muColors.Unlock()
}

var lf = []byte{'\n'}

type columnWriter struct {
Expand Down
5 changes: 2 additions & 3 deletions contribs/gnodev/pkg/logger/log_zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"go.uber.org/zap/zapcore"
)

func NewZapLogger(w io.Writer, slevel slog.Level) *slog.Logger {
func NewZapLogger(w io.Writer, slevel slog.Level) *zap.Logger {
// Build encoder config
consoleConfig := zap.NewDevelopmentEncoderConfig()
consoleConfig.EncodeCaller = zapcore.FullCallerEncoder
Expand All @@ -34,6 +34,5 @@ func NewZapLogger(w io.Writer, slevel slog.Level) *slog.Logger {
panic("invalid slog level")
}

zapLog := log.NewZapLogger(enc, w, level)
return log.ZapLoggerToSlog(zapLog)
return log.NewZapLogger(enc, w, level)
}

0 comments on commit 4d9483e

Please sign in to comment.