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 1, 2023
1 parent 97dd8c9 commit ceb70d2
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/charmbracelet/log
go 1.17

require (
github.com/charmbracelet/lipgloss v0.7.1
github.com/charmbracelet/lipgloss v0.7.2-0.20230801122447-b9dc267bfb97
github.com/go-logfmt/logfmt v0.6.0
github.com/muesli/termenv v0.15.2
github.com/stretchr/testify v1.8.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiE
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/charmbracelet/lipgloss v0.7.1 h1:17WMwi7N1b1rVWOjMT+rCh7sQkvDU75B2hbZpc5Kc1E=
github.com/charmbracelet/lipgloss v0.7.1/go.mod h1:yG0k3giv8Qj8edTCbbg6AlQ5e8KNWpFujkNawKNhE2c=
github.com/charmbracelet/lipgloss v0.7.2-0.20230801122447-b9dc267bfb97 h1:RPT8YsyfzoQdtruljiWTpcpx6X0DQQepCN39z/gIXTU=
github.com/charmbracelet/lipgloss v0.7.2-0.20230801122447-b9dc267bfb97/go.mod h1:p4eYUZZJ/0oXTuCQKFF8mqyKCz0ja6y+7DniDDw5KKU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
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 ceb70d2

Please sign in to comment.