Skip to content

Commit

Permalink
🐛 Remove use of upstream package global variable
Browse files Browse the repository at this point in the history
The upstream package which detected and set background colour was using
a global variable which was cached. Tests relying on different states
were giving incorrect results.

Replaced lipgloss detection with termenv (which lipgloss uses) and
instantiated a new output to do detection against.
  • Loading branch information
mikelorant committed Feb 4, 2023
1 parent dfaebaa commit c741a68
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
5 changes: 4 additions & 1 deletion internal/ui/theme/theme.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package theme

import (
"io"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
tint "github.com/lrstanley/bubbletint"
"github.com/mikelorant/committed/internal/config"
"github.com/muesli/termenv"
)

type Theme struct {
Expand All @@ -25,7 +28,7 @@ func New(clr config.Colour) Theme {
lipgloss.SetHasDarkBackground(false)
t = lightTints()
default:
switch lipgloss.HasDarkBackground() {
switch termenv.NewOutput(io.Discard).HasDarkBackground() {
case true:
t = darkTints()
case false:
Expand Down
44 changes: 23 additions & 21 deletions internal/ui/theme/theme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,32 +75,32 @@ func TestNextTint(t *testing.T) {
}{
{
name: "first",
id: "builtin_light",
id: "builtin_dark",
},
{
name: "one",
count: 1,
id: "builtin_solarized_light",
id: "dracula",
},
{
name: "three",
count: 3,
id: "gruvbox_light",
count: 4,
id: "retrowave",
},
{
name: "last",
count: 4,
id: "tokyo_night_light",
count: 6,
id: "tokyo_night",
},
{
name: "last_plus_one",
count: 5,
id: "builtin_light",
count: 7,
id: "builtin_dark",
},
{
name: "last_plus_two",
count: 6,
id: "builtin_solarized_light",
count: 8,
id: "dracula",
},
}

Expand All @@ -125,11 +125,13 @@ func TestListTints(t *testing.T) {
{
name: "default",
want: []string{
"builtin_light",
"builtin_solarized_light",
"builtin_tango_light",
"gruvbox_light",
"tokyo_night_light",
"builtin_dark",
"dracula",
"gruvbox_dark",
"nord",
"retrowave",
"solarized_dark_higher_contrast",
"tokyo_night",
},
},
}
Expand All @@ -152,13 +154,13 @@ func TestGetTint(t *testing.T) {
}{
{
name: "valid",
id: "builtin_light",
want: "builtin_light",
id: "builtin_dark",
want: "builtin_dark",
},
{
name: "invalid",
id: "invalid",
want: "builtin_light",
want: "builtin_dark",
},
}

Expand Down Expand Up @@ -187,17 +189,17 @@ func TestSetTint(t *testing.T) {
}{
{
name: "valid",
id: "builtin_tango_light",
id: "dracula",
want: want{
id: "builtin_tango_light",
id: "dracula",
ok: true,
},
},
{
name: "invalid",
id: "test",
want: want{
id: "builtin_light",
id: "builtin_dark",
},
},
}
Expand Down

0 comments on commit c741a68

Please sign in to comment.