Skip to content

Use light variant of theme automatically when terminal background is light#1843

Open
rashil2000 wants to merge 6 commits intoClementTsang:mainfrom
rashil2000:detect-background
Open

Use light variant of theme automatically when terminal background is light#1843
rashil2000 wants to merge 6 commits intoClementTsang:mainfrom
rashil2000:detect-background

Conversation

@rashil2000
Copy link

Description

This PR adds the ability to detect the background color of the terminal, using the terminal-colorsaurus library. This library has extensive support for terminals, and is pretty quick at detecting the color or failing gracefully. Popular tools such as delta and bat also use this.

Issue

Closes: #1284

Testing

If relevant, please state how this was tested. All changes must be tested to work:

If this is a code change, please also indicate which platforms were tested:

  • Windows
  • macOS
  • Linux

Checklist

If relevant, ensure the following have been met:

  • Areas your change affects have been linted using rustfmt (cargo fmt)
  • The change has been tested and doesn't appear to cause any unintended breakage
  • Documentation has been added/updated if needed (README.md, help menu, doc pages, etc.)
  • The pull request passes the provided CI pipeline
  • There are no merge conflicts
  • If relevant, new tests were added (don't worry too much about coverage)

@codecov
Copy link

codecov bot commented Nov 2, 2025

Codecov Report

❌ Patch coverage is 91.42857% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 35.84%. Comparing base (873ba20) to head (a9355d5).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
src/options/config/style.rs 91.42% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #1843       +/-   ##
===========================================
- Coverage   53.95%   35.84%   -18.11%     
===========================================
  Files         115      115               
  Lines       16096    16121       +25     
===========================================
- Hits         8684     5778     -2906     
- Misses       7412    10343     +2931     
Flag Coverage Δ
macos-14 37.00% <91.42%> (-0.99%) ⬇️
ubuntu-latest 37.00% <91.42%> (-19.02%) ⬇️
windows-2022 37.32% <91.42%> (-1.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@rashil2000
Copy link
Author

The CI seems to be failing in a file unrelated to my changes, should I fix it?

@ClementTsang
Copy link
Owner

ClementTsang commented Nov 2, 2025

Ugh, that's my bad. I think it's fine, it's because the new stable rust means I'm failing a clippy check in the main branch. I'll push a fix to main later.

@rashil2000
Copy link
Author

I see. And should I add tests for this as well? Codecov seems to be complaining.

@ClementTsang
Copy link
Owner

ClementTsang commented Nov 2, 2025

If you could add a test that might be nice, but it might be tricky to add one? Especially for CI, it might make sense to skip it there (or I guess ensure it handles it as expected if the terminal is a CI one). I'll leave that call up to you I suppose.

@ClementTsang
Copy link
Owner

ClementTsang commented Nov 2, 2025

Will review in a bit (probably sometime in the next few days, currently travelling). Just fixed main so that CI shouldn't break either.

@rashil2000
Copy link
Author

Okay sure, no rush

Copy link
Owner

@ClementTsang ClementTsang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this looks fine, only thing to change for sure is please don't change the v0.9 schema.

My main outstanding question I guess is if we should still have a way to override the automatic detection (or skip it, same difference). I would prefer to still have a way to force a dark/light mode setting, though how that might look, I'm open for ideas.

Comment on lines -234 to -241
# - "default-light"
# - "gruvbox"
# - "gruvbox-light"
# - "nord"
# - "nord-light".
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, my gut reaction is that I'm not a huge fan of not being able to override the automatic settings, though I'm also not sure how certain interactions should behave in that case.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The (ugly?) workarounds off the top of my head to support this are either:

  • Add -dark variants to mirror -light, and ignore auto-detection for those.
  • Add a flag to explicitly set light/dark modes for existing themes, though I'm not a huge fan of having another flag for this unless necessary.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Open to any other suggestions/opinions)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a few ideas in mind, but all of them involve some amount of breaking changes:

  • we could keep the light variants as is: if the user has explicitly specified a light variant, we pick it, if not, we do detection. This would still confuse users if on the off chance they decide to launch bottom on a light terminal background and expect the dark variant to show up.
  • Add corresponding separate dark variants (as you mentioned) - if variants are specified explicitly, we don't do detection, and if variants are not specified (just the name is specified), we do detection. This will cause a similar gotcha as above (although that's still a very niche edge case).

I wanted an implementation as clean as possible without confusing existing users.

There is an argument that can be made for allowing users to override the theme - which is, the auto-detection for some terminal is so bad that they have to specify the light variant. I'm not sure how probable this is though.

Comment on lines 178 to 182
"default-light",
"gruvbox",
"gruvbox-light",
"nord",
"nord-light"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't change this file, this is the schema for an old release so this doesn't really make sense, since these changes wouldn't apply to that version.

If you're going to update the schema, run the schema updating script ./scripts/schema/generate.sh to update nightly's schema automatically. We can then cut the nightly schema for a stable release when necessary.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, my bad. I have reverted the change.

I tried running ./scripts/schema/generate.sh - it ran successfully but didn't make any changes in the schema/nightly/bottom.json file.

None => match config.styles.as_ref().and_then(|s| s.theme.as_ref()) {
Some(theme) => Self::from_theme(theme)?,
None => Self::default(),
None => Self::from_theme("default")?,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Any reason to change this line?

Copy link
Author

@rashil2000 rashil2000 Nov 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self::default() here will skip automatic detection if the user has no theme specified in the config file.

@ClementTsang ClementTsang self-assigned this Jan 16, 2026
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 this pull request may close these issues.

Support for different themes based on Terminal color scheme

2 participants