charmbracelet / lipgloss

Style definitions for nice terminal layouts 👄
MIT License
8.22k stars 231 forks source link

(v2) adaptive colors + writers #397

Closed meowgorithm closed 1 month ago

meowgorithm commented 1 month ago

This PR contains solutions for adaptive colors, background color detection, and colorprofile-based writers. It also updates all tests and examples for v2.

The big changes in here are:

// Background color detection.
hasDarkBG, err := lipgloss.HasDarkBackground(os.Stdin, os.Stdout)
if err != nil {
    log.Fatal("Uh oh:", err)
}

// Adaptive color helper.
lightDark := lipgloss.LightDark(hasDarkBG)
c := lightDark.Color(lightDark(0x212121, 0xf1f1f1))

// Color downsampling happens via a writer.
str := lipgloss.NewStyle().Foreground(c).Render("Hello!")
lipgloss.Println(str)

Available writer utilties are:

Supersedes #392. Supersedes #389.