From 6baed072f56f07afc3648c5b6156939a8ef1511b Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Fri, 25 Aug 2023 20:41:45 -0400 Subject: [PATCH] fix: read fore/background colors on first access 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() ``` --- output.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/output.go b/output.go index ac7abab..20029b7 100644 --- a/output.go +++ b/output.go @@ -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() } }