Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(v2) feat: complete color support #420

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions color.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"image/color"
"strconv"

"github.com/charmbracelet/colorprofile"
"github.com/charmbracelet/x/ansi"
"github.com/lucasb-eyer/go-colorful"
)
Expand Down Expand Up @@ -179,3 +180,47 @@
_, _, l := col.Hsl()
return l < 0.5 //nolint:mnd
}

// CompleteFunc is a function that returns the appropriate color based on the
// given color profile.
//
// Example usage:
//
// p := colorprofile.Detect(os.Stderr, os.Environ())
// complete := lipgloss.Complete(p)
// color := complete(
// lipgloss.Color(1), // ANSI
// lipgloss.Color(124), // ANSI256
// lipgloss.Color("#ff34ac"), // TrueColor
// )
// fmt.Println("Ooh, pretty color: ", color)
//
// For more info see [Complete].
type CompleteFunc func(ansi, ansi256, truecolor color.Color) color.Color

// Complete returns a function that will return the appropriate color based on
// the given color profile.
//
// Example usage:
//
// p := colorprofile.Detect(os.Stderr, os.Environ())
// complete := lipgloss.Complete(p)
// color := complete(
// lipgloss.Color(1), // ANSI
// lipgloss.Color(124), // ANSI256
// lipgloss.Color("#ff34ac"), // TrueColor
// )
// fmt.Println("Ooh, pretty color: ", color)
func Complete(p colorprofile.Profile) CompleteFunc {
return func(ansi, ansi256, truecolor color.Color) color.Color {
switch p {

Check failure on line 216 in color.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (ubuntu-latest)

missing cases in switch of type colorprofile.Profile: colorprofile.Ascii, colorprofile.NoTTY (exhaustive)

Check failure on line 216 in color.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (macos-latest)

missing cases in switch of type colorprofile.Profile: colorprofile.Ascii, colorprofile.NoTTY (exhaustive)

Check failure on line 216 in color.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (windows-latest)

missing cases in switch of type colorprofile.Profile: colorprofile.Ascii, colorprofile.NoTTY (exhaustive)

Check failure on line 216 in color.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (ubuntu-latest)

missing cases in switch of type colorprofile.Profile: colorprofile.Ascii, colorprofile.NoTTY (exhaustive)

Check failure on line 216 in color.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (macos-latest)

missing cases in switch of type colorprofile.Profile: colorprofile.Ascii, colorprofile.NoTTY (exhaustive)

Check failure on line 216 in color.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (windows-latest)

missing cases in switch of type colorprofile.Profile: colorprofile.Ascii, colorprofile.NoTTY (exhaustive)
case colorprofile.ANSI:
return ansi
case colorprofile.ANSI256:
return ansi256
case colorprofile.TrueColor:
return truecolor
}
return noColor
}
}
Loading