Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: set lipgloss renderer color profile #69

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
}
}