From 0d8dc72372369b078cbbd3f194d14d8822f3e3bb Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Tue, 1 Aug 2023 15:08:21 -0400 Subject: [PATCH] feat: set lipgloss renderer color profile Add `SetColorProfile` to force change the Lip Gloss renderer color profile. Needs: https://github.com/charmbracelet/lipgloss/pull/212 Fixes: https://github.com/charmbracelet/log/pull/63 --- logger.go | 6 ++++++ pkg.go | 8 ++++++++ pkg_test.go | 2 ++ text_test.go | 16 ++++++++++++++++ 4 files changed, 32 insertions(+) diff --git a/logger.go b/logger.go index 73805bd..c72decf 100644 --- a/logger.go +++ b/logger.go @@ -282,6 +282,12 @@ func (l *Logger) SetCallerOffset(offset int) { l.callerOffset = offset } +// SetColorProfile force sets the underlying Lip Gloss renderer color profile +// for the TextFormatter. +func (l *Logger) SetColorProfile(profile termenv.Profile) { + l.re.SetColorProfile(profile) +} + // With returns a new logger with the given keyvals added. func (l *Logger) With(keyvals ...interface{}) *Logger { l.mu.Lock() diff --git a/pkg.go b/pkg.go index 2b35b4a..440180c 100644 --- a/pkg.go +++ b/pkg.go @@ -8,6 +8,8 @@ import ( "os" "sync" "time" + + "github.com/muesli/termenv" ) var ( @@ -124,6 +126,12 @@ func SetPrefix(prefix string) { defaultLogger.SetPrefix(prefix) } +// SetColorProfile force sets the underlying Lip Gloss renderer color profile +// for the TextFormatter. +func SetColorProfile(profile termenv.Profile) { + defaultLogger.SetColorProfile(profile) +} + // GetPrefix returns the prefix for the default logger. func GetPrefix() string { return defaultLogger.GetPrefix() diff --git a/pkg_test.go b/pkg_test.go index fcb4727..3ce272a 100644 --- a/pkg_test.go +++ b/pkg_test.go @@ -10,6 +10,7 @@ import ( "testing" "time" + "github.com/muesli/termenv" "github.com/stretchr/testify/assert" ) @@ -63,6 +64,7 @@ func TestPrint(t *testing.T) { SetReportTimestamp(true) SetReportCaller(false) SetTimeFormat(DefaultTimeFormat) + SetColorProfile(termenv.ANSI) Error("error") Print("print") assert.Equal(t, "0001/01/01 00:00:00 print\n", buf.String()) diff --git a/text_test.go b/text_test.go index d1590e5..75c4bfc 100644 --- a/text_test.go +++ b/text_test.go @@ -4,6 +4,7 @@ import ( "bytes" "errors" "fmt" + "io" "os" "os/exec" "path/filepath" @@ -12,6 +13,7 @@ import ( "time" "github.com/charmbracelet/lipgloss" + "github.com/muesli/termenv" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -396,3 +398,17 @@ func TestTextValueStyles(t *testing.T) { }) } } + +func TestColorProfile(t *testing.T) { + cases := []termenv.Profile{ + termenv.Ascii, + termenv.ANSI, + termenv.ANSI256, + termenv.TrueColor, + } + l := New(io.Discard) + for _, p := range cases { + l.SetColorProfile(p) + assert.Equal(t, p, l.re.ColorProfile()) + } +}