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

feat!: introduce color profiles and use term/ansi for styles #269

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

aymanbagabas
Copy link
Member

@aymanbagabas aymanbagabas commented Mar 15, 2024

This introduces color profiles similar to Termenv color profiles. Plus, it switches to using Charmbracelet term & term/ansi to query terminal background color for the default renderer, and construct styles.

However, it breaks the renderer API since it doesn't depend on Termenv anymore.

func NewRenderer(cp Profile, hasDarkBackground bool) *Renderer

// New exported types
// Profile is a color profile: NoTTY, Ascii, ANSI, ANSI256, or TrueColor.
type Profile int

const (
	// NoTTY, not a terminal profile
	NoTTY Profile = iota
	// Ascii, uncolored profile
	Ascii // nolint: revive
	// ANSI, 4-bit color profile
	ANSI
	// ANSI256, 8-bit color profile
	ANSI256
	// TrueColor, 24-bit color profile
	TrueColor
)

// New exported funcs
func DetectColorProfile(stdout *os.File, environ []string) Profile
func EnvColorProfile(environ []string) Profile
func EnableLegacyWindowsANSI(out *os.File)

This also introduces a new color profile NoTTY. This profile doesn't generate any ANSI sequences, and, as the name suggests, is used when the renderer is dealing with a non-terminal output. The Ascii profile on the other hand is used to generate all the style sequences except color ones. This works well when NO_COLOR.

color_test.go Show resolved Hide resolved
examples/go.mod Outdated Show resolved Hide resolved
go.mod Outdated Show resolved Hide resolved
profile.go Outdated Show resolved Hide resolved
renderer.go Outdated Show resolved Hide resolved
style_test.go Show resolved Hide resolved
runes_test.go Show resolved Hide resolved
Use ANSI aware, wide characters support, uniseg backed term/ansi package
to calculate string widths, truncate, and wrap strings.

Related: muesli/reflow#71
Fixes: #258
Fixes: #220
This introduces color profiles similar to Termenv color profiles.
Plus, it switches to using Charmbracelet term & term/ansi to query
terminal background color for the default renderer, and construct styles.

However, it breaks the renderer API since it doesn't depend on Termenv
anymore.
And export both EnvColorProfile and EnableLegacyWindowsANSI
The NoTTY profile generates no sequences while the Ascii profile
generates sequences with no colors (bold, italic, underline, etc)
Replace Sequence with profile logic checks
@aymanbagabas aymanbagabas force-pushed the use-ansi-style branch 2 times, most recently from 8b73e82 to 28bf0c1 Compare April 29, 2024 18:35
@bashbunni
Copy link
Member

bashbunni commented Apr 29, 2024

Nice! The latest changes fixed the Bubble Tea chat example for me. It's now using the right color profile instead of ascii. Tested all of the bubble tea examples last week and they all worked great even before the fix. Tested against the term/input branch of Bubble Tea btw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants