Skip to content

Commit

Permalink
feat: set lipgloss renderer color profile
Browse files Browse the repository at this point in the history
Add `SetColorProfile` to force change the Lip Gloss renderer color profile.

Needs: charmbracelet/lipgloss#212
Fixes: #63
  • Loading branch information
aymanbagabas committed Aug 22, 2023
1 parent 6699e64 commit 0d8dc72
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os"
"sync"
"time"

"github.com/muesli/termenv"
)

var (
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"
"time"

"github.com/muesli/termenv"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -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())
Expand Down
16 changes: 16 additions & 0 deletions text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
Expand All @@ -12,6 +13,7 @@ import (
"time"

"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -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())
}
}

0 comments on commit 0d8dc72

Please sign in to comment.