Skip to content

Commit

Permalink
fix: read fore/background colors on first access
Browse files Browse the repository at this point in the history
Caching the colors in OutputOption makes the order of options important.
For example, if you're using WithUnsafe _after_ WithColorCache, the
colors wouldn't respect the `unsafe` option since the colors get cached
_during_ the execution of WithColorCache option.

Another example, is the use of a custom environ using the WithEnvironment
option. If WithEnvironment comes _before_ WithColorCache, reading the
terminal colors won't respect the values in the environment.

Instead, don't read the colors in WithColorCache and make the user
opt-in if they want to cache the colors right after creating a new
output using ForegroundColor and BackgroundColor.

```go
// To cache the colors after creating an output
output := NewOutput(os.Stderr)
_ = output.ForegroundColor()
_ = output.BackgroundColor()
```
  • Loading branch information
aymanbagabas committed Aug 26, 2023
1 parent 4fa0a49 commit 6baed07
Showing 1 changed file with 1 addition and 5 deletions.
6 changes: 1 addition & 5 deletions output.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,10 @@ func WithProfile(profile Profile) OutputOption {
}

// WithColorCache returns a new OutputOption with fore- and background color values
// pre-fetched and cached.
// cached on first access.
func WithColorCache(v bool) OutputOption {
return func(o *Output) {
o.cache = v

// cache the values now
_ = o.ForegroundColor()
_ = o.BackgroundColor()
}
}

Expand Down

0 comments on commit 6baed07

Please sign in to comment.