-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
592 additions
and
147 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
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,77 @@ | ||
package compat | ||
|
||
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 = lipgloss.HasDarkBackground(os.Stdin, os.Stdout) | ||
|
||
// Profile is the color profile of the terminal. | ||
Profile = colorprofile.Detect(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 Profile { | ||
Check failure on line 51 in compat/color.go GitHub Actions / lint / lint-soft (ubuntu-latest)
Check failure on line 51 in compat/color.go GitHub Actions / lint / lint-soft (macos-latest)
Check failure on line 51 in compat/color.go GitHub Actions / lint / lint-soft (ubuntu-latest)
Check failure on line 51 in compat/color.go GitHub Actions / lint / lint-soft (macos-latest)
Check failure on line 51 in compat/color.go GitHub Actions / lint / lint-soft (windows-latest)
|
||
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,21 @@ | ||
// Package compat 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. | ||
// | ||
// In case you want [os.Stderr] to be used as the default writer, you can set | ||
// both [Writer] and [HasDarkBackground] to use [os.Stderr] with | ||
// the following code: | ||
// | ||
// import ( | ||
// "os" | ||
// | ||
// "github.com/charmbracelet/colorprofile" | ||
// "github.com/charmbracelet/lipgloss/v2/impure" | ||
// ) | ||
// | ||
// func init() { | ||
// impure.Writer = colorprofile.NewWriter(os.Stderr, os.Environ()) | ||
// impure.HasDarkBackground, _ = lipgloss.HasDarkBackground(os.Stdin, os.Stderr) | ||
// } | ||
package compat |
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
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
Oops, something went wrong.