-
Notifications
You must be signed in to change notification settings - Fork 815
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
Automatically detect terminal background color #4674
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3275,6 +3275,15 @@ async def _on_app_blur(self, event: events.AppBlur) -> None: | |
self.app_focus = False | ||
self.screen.refresh_bindings() | ||
|
||
def _is_dark_color(self, r: int, g: int, b: int) -> bool: | ||
# perceived brightness formula | ||
perceived_brightness = 0.2126 * r + 0.7152 * g + 0.0722 * b | ||
return perceived_brightness < 128 | ||
|
||
async def _on_background_color(self, event: events.BackgroundColor) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't want to do this automatically, unless the dev explicitly requests this behaviour. Sending the message is probably fine, until we have a mechanism for the dev to request automatic light / dark mode. |
||
"""Background color detected""" | ||
self.dark = self._is_dark_color(event.r & 0xFF, event.g & 0xFF, event.b & 0xFF) | ||
|
||
def _detach_from_dom(self, widgets: list[Widget]) -> list[Widget]: | ||
"""Detach a list of widgets from the DOM. | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -248,6 +248,7 @@ def on_terminal_resize(signum, stack) -> None: | |
self.flush() | ||
self._key_thread = Thread(target=self._run_input_thread) | ||
send_size_event() | ||
self.write("\x1b]11;?\x07") # Detect background color | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAIK some terminals respond with nothing when they see a sequence they don't recognize/support, i.e. if you send requests There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I try to handle this case gracefully by only parsing the RGB colors if the length is as expected. But, please take a good look at the code to ensure I do not have missed anything. I would also like to look into writing some unit tests for this, including the case you mention here |
||
self._key_thread.start() | ||
self._request_terminal_sync_mode_support() | ||
self._enable_bracketed_paste() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have a policy in Textual of no abbreviations, so BG should be BACKGROUND.
I think this would be better done as a regex.
Do you have a reference to the docs? Can you be certain it is sent in a two hex characters?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will fix the abbreviation. Here is a link to some documentation on the escape sequence:
https://www.xfree86.org/current/ctlseqs.html
Search for "Operating System Controls"