-
Notifications
You must be signed in to change notification settings - Fork 799
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support terminal color profiles (#1151)
This adds support for detecting the terminal's color profile. It adds a new `ColorProfileMsg` message that get sent when the program starts. You can force a specific color profile using the `WithColorProfile` option. When a program requests the `RGB` or `Tc` terminfo capabilities, Bubble Tea will read the response, if there is one, and upgrade the cached color profile and send the new profile to the program again. Supersedes: #1142 Supersedes: #1143
- Loading branch information
Showing
12 changed files
with
162 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package main | ||
|
||
import ( | ||
"image/color" | ||
"log" | ||
|
||
tea "github.com/charmbracelet/bubbletea/v2" | ||
"github.com/charmbracelet/colorprofile" | ||
"github.com/charmbracelet/x/ansi" | ||
"github.com/lucasb-eyer/go-colorful" | ||
) | ||
|
||
var myFancyColor color.Color | ||
|
||
type model struct{} | ||
|
||
var _ tea.Model = model{} | ||
|
||
// Init implements tea.Model. | ||
func (m model) Init() (tea.Model, tea.Cmd) { | ||
return m, tea.Batch( | ||
tea.RequestCapability("RGB"), | ||
tea.RequestCapability("Tc"), | ||
) | ||
} | ||
|
||
// Update implements tea.Model. | ||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
switch msg := msg.(type) { | ||
case tea.KeyMsg: | ||
return m, tea.Quit | ||
case tea.ColorProfileMsg: | ||
return m, tea.Println("Color profile changed to ", msg) | ||
} | ||
return m, nil | ||
} | ||
|
||
// View implements tea.Model. | ||
func (m model) View() string { | ||
return "This will produce the wrong colors on Apple Terminal :)\n\n" + | ||
ansi.Style{}.ForegroundColor(myFancyColor).Styled("Howdy!") + | ||
"\n\n" + | ||
"Press any key to exit." | ||
} | ||
|
||
func main() { | ||
myFancyColor, _ = colorful.Hex("#6b50ff") | ||
|
||
p := tea.NewProgram(model{}, tea.WithColorProfile(colorprofile.TrueColor)) | ||
if _, err := p.Run(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
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
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
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
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,15 @@ | ||
package tea | ||
|
||
import "github.com/charmbracelet/colorprofile" | ||
|
||
// ColorProfileMsg is a message that describes the terminal's color profile. | ||
// This message is send to the program's update function when the program is | ||
// started. | ||
// | ||
// To upgrade the terminal color profile, use the `tea.RequestCapability` | ||
// command to request the `RGB` and `Tc` terminfo capabilities. Bubble Tea will | ||
// then cache the terminal's color profile and send a `ColorProfileMsg` to the | ||
// program's update function. | ||
type ColorProfileMsg struct { | ||
colorprofile.Profile | ||
} |
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
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