Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed May 10, 2024
1 parent 61f326c commit f9f7a8c
Show file tree
Hide file tree
Showing 14 changed files with 209 additions and 332 deletions.
2 changes: 1 addition & 1 deletion align.go
Expand Up @@ -3,7 +3,7 @@ package lipgloss
import (
"strings"

"github.com/charmbracelet/x/exp/term/ansi"
"github.com/charmbracelet/x/ansi"

Check failure on line 6 in align.go

View workflow job for this annotation

GitHub Actions / test-goos

no required module provides package github.com/charmbracelet/x/ansi; to add it:

Check failure on line 6 in align.go

View workflow job for this annotation

GitHub Actions / coverage (^1, ubuntu-latest)

no required module provides package github.com/charmbracelet/x/ansi; to add it:

Check failure on line 6 in align.go

View workflow job for this annotation

GitHub Actions / test (^1, ubuntu-latest)

no required module provides package github.com/charmbracelet/x/ansi; to add it:
)

// Perform text alignment. If the string is multi-lined, we also make all lines
Expand Down
13 changes: 6 additions & 7 deletions borders.go
Expand Up @@ -3,7 +3,7 @@ package lipgloss
import (
"strings"

"github.com/charmbracelet/x/exp/term/ansi"
"github.com/charmbracelet/x/ansi"
"github.com/rivo/uniseg"
)

Expand Down Expand Up @@ -406,14 +406,13 @@ func (s Style) styleBorder(border string, fg, bg TerminalColor) string {
return border
}

p := s.r.ColorProfile()

var style ansi.Style
if fg != noColor && p > Ascii {
style = style.ForegroundColor(fg.color(s.r))
isColorable := s.profile < Ascii
if fg != noColor && isColorable {
style = style.ForegroundColor(fg.color(s.profile, s.hasLightBackground))
}
if bg != noColor && p > Ascii {
style = style.BackgroundColor(bg.color(s.r))
if bg != noColor && isColorable {
style = style.BackgroundColor(bg.color(s.profile, s.hasLightBackground))
}

return style.Styled(border)
Expand Down
33 changes: 16 additions & 17 deletions color.go
Expand Up @@ -3,13 +3,13 @@ package lipgloss
import (
"image/color"

"github.com/charmbracelet/x/exp/term/ansi"
"github.com/charmbracelet/x/ansi"
"github.com/lucasb-eyer/go-colorful"
)

// TerminalColor is a color intended to be rendered in the terminal.
type TerminalColor interface {
color(*Renderer) ansi.Color
color(p Profile, hasLightBg bool) ansi.Color
}

var noColor = NoColor{}
Expand All @@ -23,7 +23,7 @@ var noColor = NoColor{}
// var style = someStyle.Copy().Background(lipgloss.NoColor{})
type NoColor struct{}

func (NoColor) color(*Renderer) ansi.Color {
func (NoColor) color(Profile, bool) ansi.Color {
return color.Black
}

Expand All @@ -42,8 +42,8 @@ func (n NoColor) RGBA() (r, g, b, a uint32) {
// hexColor := lipgloss.Color("#0000ff")
type Color string

func (c Color) color(r *Renderer) ansi.Color {
return r.ColorProfile().Color(string(c))
func (c Color) color(p Profile, _ bool) ansi.Color {
return p.Color(string(c))
}

// ANSIColor is a color specified by an ANSI256 color value.
Expand All @@ -54,8 +54,8 @@ func (c Color) color(r *Renderer) ansi.Color {
// colorB := lipgloss.ANSIColor(134)
type ANSIColor uint8

func (ac ANSIColor) color(r *Renderer) ansi.Color {
return r.ColorProfile().Convert(ansi.ExtendedColor(ac))
func (ac ANSIColor) color(p Profile, _ bool) ansi.Color {
return p.Convert(ansi.ExtendedColor(ac))
}

// AdaptiveColor provides color options for light and dark backgrounds. The
Expand All @@ -70,11 +70,11 @@ type AdaptiveColor struct {
Dark string
}

func (ac AdaptiveColor) color(r *Renderer) ansi.Color {
if r.HasDarkBackground() {
return Color(ac.Dark).color(r)
func (ac AdaptiveColor) color(p Profile, hasLightBg bool) ansi.Color {
if hasLightBg {
return Color(ac.Light).color(p, hasLightBg)
}
return Color(ac.Light).color(r)
return Color(ac.Dark).color(p, hasLightBg)
}

// CompleteColor specifies exact values for truecolor, ANSI256, and ANSI color
Expand All @@ -85,8 +85,7 @@ type CompleteColor struct {
ANSI string
}

func (c CompleteColor) color(r *Renderer) ansi.Color {
p := r.ColorProfile()
func (c CompleteColor) color(p Profile, hasLightBg bool) ansi.Color {
switch p { //nolint:exhaustive
case TrueColor:
return p.Color(c.TrueColor)
Expand All @@ -107,11 +106,11 @@ type CompleteAdaptiveColor struct {
Dark CompleteColor
}

func (cac CompleteAdaptiveColor) color(r *Renderer) ansi.Color {
if r.HasDarkBackground() {
return cac.Dark.color(r)
func (cac CompleteAdaptiveColor) color(p Profile, hasLightBg bool) ansi.Color {
if hasLightBg {
return cac.Light.color(p, hasLightBg)
}
return cac.Light.color(r)
return cac.Light.color(p, hasLightBg)
}

// ConvertToRGB converts a Color to a colorful.Color.
Expand Down
43 changes: 35 additions & 8 deletions env.go
@@ -1,11 +1,13 @@
package lipgloss

import (
"log"
"os"
"strconv"
"strings"

"github.com/charmbracelet/x/exp/term"
"github.com/charmbracelet/x/term"

Check failure on line 9 in env.go

View workflow job for this annotation

GitHub Actions / test-goos

no required module provides package github.com/charmbracelet/x/term; to add it:

Check failure on line 9 in env.go

View workflow job for this annotation

GitHub Actions / coverage (^1, ubuntu-latest)

no required module provides package github.com/charmbracelet/x/term; to add it:

Check failure on line 9 in env.go

View workflow job for this annotation

GitHub Actions / test (^1, ubuntu-latest)

no required module provides package github.com/charmbracelet/x/term; to add it:
"github.com/lucasb-eyer/go-colorful"
)

// DetectColorProfile returns the color profile based on the terminal output,
Expand All @@ -27,20 +29,45 @@ func DetectColorProfile(stdout *os.File, environ []string) Profile {
p = NoTTY
}

if envNoColor(env) && p > Ascii {
log.Print("Init Color profile: ", p)
log.Printf("NoColor env: %v", envNoColor(env))
if envNoColor(env) && p < Ascii {
return Ascii
}

if cliColorForced(env) && p <= Ascii {
if cliColorForced(env) && p >= Ascii {
p = ANSI
if cp := envColorProfile(env); cp > p {
if cp := envColorProfile(env); cp < p {
p = cp
}
}

return p
}

// HasLightBackground returns true if the terminal has a light background.
func HasLightBackground(in *os.File, out *os.File) bool {
state, err := term.MakeRaw(in.Fd())
if err != nil {
return false
}

defer term.Restore(in.Fd(), state) // nolint:errcheck

c, err := term.QueryBackgroundColor(in, out)
if err != nil {
return false
}

col, ok := colorful.MakeColor(c)
if !ok {
return false
}

_, _, l := col.Hsl()
return l > 0.5
}

// EnvColorProfile returns the color profile based on environment variables.
//
// This respects the NO_COLOR and CLICOLOR/CLICOLOR_FORCE environment
Expand All @@ -54,13 +81,13 @@ func EnvColorProfile(environ []string) Profile {

env := environMap(environ)
p := envColorProfile(env)
if envNoColor(env) && p > Ascii {
if envNoColor(env) && p < Ascii {
return Ascii
}

if cliColorForced(env) && p <= Ascii {
if cliColorForced(env) && p >= Ascii {
p = ANSI
if cp := envColorProfile(env); cp > p {
if cp := envColorProfile(env); cp < p {
p = cp
}
}
Expand All @@ -87,7 +114,7 @@ func cliColorForced(env map[string]string) bool {
// envColorProfile returns infers the color profile from the environment.
func envColorProfile(env map[string]string) (p Profile) {
setProfile := func(profile Profile) {
if profile > p {
if profile < p {
p = profile
}
}
Expand Down

0 comments on commit f9f7a8c

Please sign in to comment.