dalance / termbg Goto Github PK
View Code? Open in Web Editor NEWA Rust library for terminal background color detection
License: Apache License 2.0
A Rust library for terminal background color detection
License: Apache License 2.0
For unsupported terminals it would be nice to have some fallback.
According to https://unix.stackexchange.com/questions/245378/common-environment-variable-to-set-dark-or-light-terminal-background / https://github.com/rocky/shell-term-background there's no well-established standard, but the COLORFGBG
environment variable has some potential: Value 15;0
is for a dark background (technically white on black) and 0;15
(technically black on white) is for a light background.
Would you be willing to adopting this?
termbg
is pretty great and easy to use / test via cargo run
.
However, I noticed that when using macOS Terminal.app, with the default profile ("Basic"), the results are incorrect.
It seems that termbg
(or one of its dependencies) does not take into account the macOS systemwide appearance, which changes the color of the default terminal from white (Light mode) to black (Dark mode). This setting may change at any time, and can be fetched with:
$ defaults read -globalDomain AppleInterfaceStyle
# prints "Dark" or nothing
Note that this is the ONLY Terminal.app Profile which behaves this way (AFAIK).
It would be neat if termbg
were installable via brew install termbg
.
This would allow me to e.g. write shell scripts which have different color schemes for different background colors. For example, using yellow text to indicate a warning works on a dark background -- but is hard to read on a white background.
Tmux is listed as one of the verified terminals, but for me it doesn't actually work:
With a dark background:
$ ./src/misc/termbg/target/release/termbg
Check terminal background color
Term : Tmux
Latency: 630.001µs
Color: R=0, G=0, B=0
Theme: Dark
And with a light background:
$ ./src/misc/termbg/target/release/termbg
Check terminal background color
Term : Tmux
Latency: 1.319794ms
Color: R=0, G=0, B=0
Theme: Dark
In both cases the detection appears to be error-free, but the background is detected incorrectly as dark.
Outside tmux (in a plain Konsole window), the color is detected correctly:
~$ ./src/misc/termbg/target/release/termbg
Check terminal background color
Term : XtermCompatible
Latency: 1.449628ms
Color: R=0, G=0, B=0
Theme: Dark
~$ ./src/misc/termbg/target/release/termbg
Check terminal background color
Term : XtermCompatible
Latency: 2.596755ms
Color: R=ffff, G=ffff, B=ffff
Theme: Light
Interestingly, the color is detected correctly inside screen
:
~$ ./src/misc/termbg/target/release/termbg
Check terminal background color
Term : Screen
Latency: 891.835µs
Color: R=ffff, G=ffff, B=ffff
Theme: Light
~$ ./src/misc/termbg/target/release/termbg
Check terminal background color
Term : Screen
Latency: 867.376µs
Color: R=0, G=0, B=0
Theme: Dark
Note that the tmux color isn't even recognized when the terminal is explicitly set to a particular color from within tmux:
~$ tmux set window-style 'fg=#d0cfcc,bg=#171421'
~$ ./src/misc/termbg/target/release/termbg
Check terminal background color
Term : Tmux
Latency: 409.917µs
Color: R=0, G=0, B=0
Theme: Dark
~$ tmux set window-style 'fg=#171421,bg=#ffffff'
~$ ./src/misc/termbg/target/release/termbg
Check terminal background color
Term : Tmux
Latency: 669.626µs
Color: R=0, G=0, B=0
Theme: Dark
Related: tmux/tmux#1919
From the documentation, we know that neovim embeds a VT220/xterm terminal emulator based on libvterm.
But, termbg currently can't get background color in neovim terminal emulator.
nvim -u NONE
:terminal
cargo run
in nvim terminal emulatorCheck terminal background color
Term : XtermCompatible
Color: detection failed Timeout { source: Timeout }
Theme: detection failed Timeout { source: Timeout }
https://conemu.github.io/ is a popular terminal replacement on Windows, which works by hiding the "real console" and re-rendering its contents in a ConEmu window.
ConEmu lets the user set up color schemes, which are applied when drawing to the ConEmu window, so termbg does not detect them, reporting the color of the "real console" instead. Same goes for Cmder, which relies on ConEmu under the hood.
Since ConEmu already has some support for xterm codes, we would probably need ConEmu to add support for querying the color scheme, similarly to microsoft/terminal#3718.
In the meantime, could it be added to the "unsupported" list in the README?
termbg should release stdin if unable to get background colour for xterm. Currently, it returns an error, but the thread querying the colour continues to hold onto stdin and runs in the background. This does not let other threads lock on stdin.
When "\x1b]11;?\x1b\\"
is used to query the background color, the loop reading stdin breaks after receiving the 0x1b
byte. But there is still one byte left in stdin to be read (In my case 0x5c
) which causes problems if I try to read stdin after this.
I'm not sure if this also happens in the other two cases as well.
Lines 175 to 181 in 6a1bdcb
I'm running bash in alacritty.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.