From bb30dc8ff648b62562461d272ba8870f3f2517b2 Mon Sep 17 00:00:00 2001 From: gfanton <8671905+gfanton@users.noreply.github.com> Date: Sun, 31 Mar 2024 11:57:07 +0200 Subject: [PATCH] fix: cleanup color Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com> --- contribs/gnodev/main.go | 19 ++++++-- contribs/gnodev/pkg/logger/colors.go | 17 ++++--- contribs/gnodev/pkg/logger/log_column.go | 57 +++++++++++++++--------- contribs/gnodev/pkg/logger/log_zap.go | 5 +-- 4 files changed, 62 insertions(+), 36 deletions(-) diff --git a/contribs/gnodev/main.go b/contribs/gnodev/main.go index af535f4f455..841f544dd00 100644 --- a/contribs/gnodev/main.go +++ b/contribs/gnodev/main.go @@ -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" @@ -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" @@ -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) @@ -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) } diff --git a/contribs/gnodev/pkg/logger/colors.go b/contribs/gnodev/pkg/logger/colors.go index 2731bc2bd85..e09ae55661b 100644 --- a/contribs/gnodev/pkg/logger/colors.go +++ b/contribs/gnodev/pkg/logger/colors.go @@ -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 } @@ -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) } diff --git a/contribs/gnodev/pkg/logger/log_column.go b/contribs/gnodev/pkg/logger/log_column.go index 58ab7d2ab48..32ec4a566b2 100644 --- a/contribs/gnodev/pkg/logger/log_column.go +++ b/contribs/gnodev/pkg/logger/log_column.go @@ -6,13 +6,14 @@ 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, @@ -20,19 +21,8 @@ func NewColumnLogger(w io.Writer, level slog.Level, profile termenv.Profile) *sl }) // 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) @@ -49,35 +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) + } + + // 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) } - fg := ColorFromString(name, 0.6, 0.4) 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) - 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 { diff --git a/contribs/gnodev/pkg/logger/log_zap.go b/contribs/gnodev/pkg/logger/log_zap.go index a06fadece6b..b7e5b34cc4e 100644 --- a/contribs/gnodev/pkg/logger/log_zap.go +++ b/contribs/gnodev/pkg/logger/log_zap.go @@ -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 @@ -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) }