-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds a compatibility layer for Lip Gloss that provides a way to continue using `AdaptiveColor` and `CompleteColor`. It's impure because it uses global variables, is not thread-safe, and only works with the default standard I/O streams. Users are encouraged to use the pure API when possible.
- Loading branch information
1 parent
0208749
commit 94fe7b7
Showing
3 changed files
with
87 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package impure | ||
|
||
import ( | ||
"image/color" | ||
"os" | ||
|
||
"github.com/charmbracelet/colorprofile" | ||
"github.com/charmbracelet/lipgloss/v2" | ||
) | ||
|
||
var ( | ||
// HasDarkBackground is true if the terminal has a dark background. | ||
HasDarkBackground = func() bool { | ||
hdb, _ := lipgloss.HasDarkBackground(os.Stdin, os.Stdout) | ||
return hdb | ||
}() | ||
|
||
// Writer is the default writer that prints to stdout, automatically | ||
// downsampling colors when necessary. | ||
Writer = colorprofile.NewWriter(os.Stdout, os.Environ()) | ||
) | ||
|
||
// AdaptiveColor provides color options for light and dark backgrounds. The | ||
// appropriate color will be returned at runtime based on the darkness of the | ||
// terminal background color. | ||
// | ||
// Example usage: | ||
// | ||
// color := lipgloss.AdaptiveColor{Light: "#0000ff", Dark: "#000099"} | ||
type AdaptiveColor struct { | ||
Light color.Color | ||
Dark color.Color | ||
} | ||
|
||
// RGBA returns the RGBA value of this color. This satisfies the Go Color | ||
// interface. | ||
func (c AdaptiveColor) RGBA() (uint32, uint32, uint32, uint32) { | ||
if HasDarkBackground { | ||
return c.Dark.RGBA() | ||
} | ||
return c.Light.RGBA() | ||
} | ||
|
||
// CompleteColor specifies exact values for truecolor, ANSI256, and ANSI color | ||
// profiles. Automatic color degradation will not be performed. | ||
type CompleteColor struct { | ||
TrueColor color.Color | ||
ANSI256 color.Color | ||
ANSI color.Color | ||
} | ||
|
||
// RGBA returns the RGBA value of this color. This satisfies the Go Color | ||
// interface. | ||
func (c CompleteColor) RGBA() (uint32, uint32, uint32, uint32) { | ||
switch Writer.Profile { | ||
case colorprofile.TrueColor: | ||
return c.TrueColor.RGBA() | ||
case colorprofile.ANSI256: | ||
return c.ANSI256.RGBA() | ||
case colorprofile.ANSI: | ||
return c.ANSI.RGBA() | ||
} | ||
return lipgloss.NoColor{}.RGBA() | ||
} | ||
|
||
// CompleteAdaptiveColor specifies exact values for truecolor, ANSI256, and ANSI color | ||
// profiles, with separate options for light and dark backgrounds. Automatic | ||
// color degradation will not be performed. | ||
type CompleteAdaptiveColor struct { | ||
Light CompleteColor | ||
Dark CompleteColor | ||
} | ||
|
||
// RGBA returns the RGBA value of this color. This satisfies the Go Color | ||
// interface. | ||
func (c CompleteAdaptiveColor) RGBA() (uint32, uint32, uint32, uint32) { | ||
if HasDarkBackground { | ||
return c.Dark.RGBA() | ||
} | ||
return c.Light.RGBA() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Package impure is a compatibility layer for Lip Gloss that provides a way to | ||
// deal with the hassle of setting up a writer. It's impure because it uses | ||
// global variables, is not thread-safe, and only works with the default | ||
// standard I/O streams. | ||
package impure |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters