fix: handle partial reads in queryTerminal#619
Conversation
Nothing guarantees that we will read a complete sequence from in, and when we don't, we should not pass an incomplete one to filter or continue while discarding those bytes.
|
@aymanbagabas anything I can do to help you review this? |
aymanbagabas
left a comment
There was a problem hiding this comment.
@MartinodF This is great!
Thank you so much for your patience and contribution. I will merge this and cut a release soon 🙂
Thanks! Then you'll be happy because I have another contribution for you 😂 #620 |
|
fwiw it looks like this got missed off the changelog for v2.0.1 - I found this after noticing Updating to v2.0.1 looks to have resolved this, assumingly because of this change 🎉 |
…erminal (#2630) I noticed that it was taking ~4 seconds for `osv-scanner --help` to resolve, which looks to be due to bug in `lipgloss` when running on Windows Terminal that was fixed via charmbracelet/lipgloss#619 This would have gotten fixed automatically by the next minor versions update, but I figured I might as well do a dedicated PR for it since I noticed it 🤷
Note: re-opening #616 after
v2-expmergeWhat is broken?
This all started with
lipgloss.HasDarkBackgroundtiming out most (80%?) of the time when running in Windows Terminal.After ignoring the problem for a few weeks, I finally decided to figure out what was happening:
queryBackgroundColorsends an OSC 11 and a CSI DA1 request one after the other and is supposed to receive back (at least) one response of each type.The code (with a couple of debug statements) running in the VSCode integrated terminal:
The same code in Windows Terminal:
As you can see, the terminal response is being received in chunks. Lipgloss assumes that somehow we will always end the read on a sequence boundary, and:
filter()regardless of the current parser state.This causes
filter()to:What am I changing?
The patch makes sure that we only ever send complete sequences to
filter(), while maintaining the parser state in between reads so that we can successfully complete parsing a sequence even if it's split inconveniently.If you have any better ideas on how to implement this, please feel free to propose them :)
Side note, in all terminals I use the CSI DA1 request causes the terminal to return the background and foreground colors in addition to the DA1 response. I feel like maybe there's an opportunity to simplify
queryBackgroundColor, but I don't know how this behaves in other terminal emulators.CONTRIBUTING.md.