Closed matta closed 1 month ago
I wonder if #1010 will address this?
Indeed it will. What's happening, and the main problem #1010 exists to solve, is that currently Bubble Tea and Lip Gloss are both jumping on, and fighting over, stdout
at the same time. That proposal essentially brokers queries to input and output allowing them to work together in harmony.
You can try out the proposal now with:
go get github.com/charmbracelet/bubbletea@beta
go get github.com/charmbracelet/lipgloss@beta
Here's the diff:
diff --git a/main.go b/main.go
index 28529ba..36916d4 100644
--- a/main.go
+++ b/main.go
@@ -17,12 +17,12 @@ type model struct {
}
// Init implements tea.Model.
-func (outer model) Init() tea.Cmd {
+func (outer model) Init(ctx tea.Context) (tea.Model, tea.Cmd) {
slog.Debug("teaModel.Init()")
- return nil
+ return model{}, nil
}
-func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
+func (m model) Update(ctx tea.Context, msg tea.Msg) (tea.Model, tea.Cmd) {
prefix := spew.Sprintf("Update(%#v)", msg)
slog.Debug(fmt.Sprintf("%s ENTER", prefix))
defer slog.Debug(fmt.Sprintf("%s LEAVE", prefix))
@@ -38,7 +38,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil
}
-func (m model) View() string {
+func (m model) View(ctx tea.Context) string {
slog.Debug("View() ENTER")
defer slog.Debug("View() LEAVE")
Note that this is still a proposal and the API may change a bit.
Also note that, for now, you can simply force Lip Gloss to query for background color prior to running Bubble Tea. This, of course, is not ideal, but will also work for now.
diff --git a/main.go b/main.go
index 0c6907a..d873c2b 100644
--- a/main.go
+++ b/main.go
@@ -80,6 +80,8 @@ func main() {
}()
slog.Debug("program started")
+ _ = lipgloss.HasDarkBackground()
+
program := tea.NewProgram(model{}, tea.WithAltScreen())
_, err := program.Run()
if err != nil {
Thanks, great to have that confirmation. Since the issue is known and tracked in #1010 I'll close this.
Describe the bug
I have isolated a bug in either bubbletea, lipgloss, or termenv where it may hang for many seconds, or even indefinitely, in a call to lipgloss style.Render() from the model's View(). This happens under specific situations:
1) The first call to View() returns a string and does not use lipgloss at all. 2) The second call to View(), which occurs after the first
tea.WindowSizeMsg
, does create a style with alipgloss.AdaptiveColor
and calls Render on it. This hangs in a call tooutput.HasDarkBackground()
intermenv
(I figured this out by adding logging in locally hacked packages)If I change the program to use an adaptive color in the first call to View() the program works correctly.
Theory: sounds like a race condition, where the View() is interacting with the terminal and...maybe?...in parallel bubbletea is also reading/writing to the terminal in another goroutine?
Setup Please complete the following information along with version numbers, if applicable.
To Reproduce Steps to reproduce the behavior:
Source Code
Run the following go program:
Expected behavior
The program should quickly render the current screen size, then accept any key press to exit.
Screenshots
Picture of the program when hung:
Additional context
Here is a log of the program's behavior for a run where it exited after 5 seconds, after receiving a key press I did not perform:
Key points:
View()
happens before thetea.WindowSizeMsg
. In this case program'sView()
code returns a simple (no styling).tea.WindowSizeMsg
followed by a call toView()
. This call toView()
renders alipgloss.AdaptiveColor
and hangs for five seconds.