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

Automatic choice of dark/light theme based on terminal's colours #2419

Open
barisione opened this issue Apr 28, 2023 · 3 comments · May be fixed by #4674
Open

Automatic choice of dark/light theme based on terminal's colours #2419

barisione opened this issue Apr 28, 2023 · 3 comments · May be fixed by #4674

Comments

@barisione
Copy link

In lots of terminals, it's possible to detect the background colour of the terminal with something like this:

#!/bin/bash

oldstty=$(stty -g)
stty raw -echo min 0 time 0
printf "\033]11;?\033\\"
result=""
while [ -z "$result" ]; do
    read -r result
done
stty "$oldstty"

echo "$result" | sed -E 's/.*(rgb:[a-f0-9/]{14}).*/\1/'

Then you get output like rgb:0000/0000/0000, rgb:ffff/ffff/ffff, rgb:158e/193a/1e75, etc.

Textual could use this to select a dark or light theme by default. This would be nice as apps would behave and look as users expect them without any configuration.

I would prefer this to happen automatically but, if you think this is not good as some terminals don't support this, then textual/rich could at least provide code to detect this.

I mention it in https://discord.com/channels/1026214085173461072/1033752510848053399/1075543323516866662 a while ago, but then forgot to open an issue.

@Textualize Textualize deleted a comment from github-actions bot Apr 29, 2023
@willmcgugan
Copy link
Collaborator

If this can be done without breaking terminals that don't support it, then I'm all for it.

@barisione
Copy link
Author

I'd be happy to investigate this further but I don't fully understand how this ANSI-escape-based magic works. Is there some other code in rich/textual where you write something from a TTY and read back the reply?

@willmcgugan
Copy link
Collaborator

In Textual there is. The driver detects support for the synchronisation protocol by writing an escape sequence, and parsing the response.

If you want to tackle it, search the code for TerminalSupportsSynchronizedOutput

@dhallas dhallas linked a pull request Jun 25, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants