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

Make the default macOS theme depend on Dark Mode #2197

Merged
merged 3 commits into from
May 24, 2022

Conversation

Enselic
Copy link
Collaborator

@Enselic Enselic commented May 21, 2022

We frequently get complaints from macOS users that bat does not work on
their default macOS terminal background, which is white.

Pay the price of slightly increased startup time to get a better default
on macOS. To avoid the slightly increased startup time, simply
specify a theme explicitly via --theme, BAT_THEME, or
~/.config/bat.

I did some quick testing, and it seems to work. With Dark Mode:

Skärmavbild 2022-05-21 kl  18 22 52

With default system theme:

Skärmavbild 2022-05-21 kl  18 22 33

Startup time is increased slightly due to this check:

Command Mean [ms] Min [ms] Max [ms] Relative
./target/release/bat -f examples/simple.rs 12.9 ± 1.3 12.1 23.2 1.50 ± 0.16
bat-v0.21.0 -f examples/simple.rs 8.6 ± 0.2 8.3 9.3 1.00

But there is no difference if an explicit theme is selected:

Command Mean [ms] Min [ms] Max [ms] Relative
./target/release/bat -f --theme "Monokai Extended" examples/simple.rs 8.7 ± 0.2 8.3 9.5 1.00
bat-v0.21.0 -f --theme "Monokai Extended" examples/simple.rs 8.7 ± 0.6 8.3 19.0 1.01 ± 0.08

Note that if there is an error when we check if Dark Mode is enabled, we
behave the same as on Windows and Linux; assume that the terminal
background is dark. This harmonizes behavior across platforms, and makes
bat behave the same as before, when Dark Mode was always assumed to be
enabled.

@Enselic
Copy link
Collaborator Author

Enselic commented May 21, 2022

Some tests are failing. Will investigate some other day.

We frequently get complaints from macOS users that bat does not work on
their default macOS terminal background, which is white.

Pay the price of slightly increased startup time to get a better default
on macOS. To avoid the slightly increased startup time, simply specify a
theme explicitly via `--theme`, `BAT_THEME`, or `~/.config/bat`.

Note that if there is an error when we check if Dark Mode is enabled, we
behave the same as on Windows and Linux; assume that the terminal
background is dark. This harmonizes behavior across platforms, and makes
bat behave the same as before, when Dark Mode was always assumed to be
enabled.
@Enselic
Copy link
Collaborator Author

Enselic commented May 22, 2022

Thanks for review @keith-hall 🙏

The tests were failing because it seems like the CI runner uses a light system theme by default on macOS. Making the tests explicitly set a theme fixed the failures.

I have contemplated to add automated tests for this, but since the test must also pass on systems that uses Dark Mode, and since I don't want our testing code to change macOS system theme, I don't currently plan on adding automated tests for this. If we get problems with regressions going ahead, I will reconsider. We could make use of dependency injection here, for example.

But as it stands, this PR is fundamentally ready to be merged. I want to keep it open for at least a few days though, maybe a week, to give time for more feedback.

@Enselic
Copy link
Collaborator Author

Enselic commented May 22, 2022

Just realised I also need to add an entry to CHANGELOG.md ;)

Copy link
Owner

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

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

Nice, thank you!

@Enselic
Copy link
Collaborator Author

Enselic commented May 24, 2022

Thanks for review! Seems unlikely that more feedback will come in, so I will merge this now.

@Enselic Enselic merged commit 3339eee into sharkdp:master May 24, 2022
@Enselic Enselic deleted the macos-default-theme branch May 24, 2022 17:29
@bjschafer bjschafer mentioned this pull request Sep 15, 2022
Amorymeltzer added a commit to Amorymeltzer/dotfiles that referenced this pull request Jan 5, 2023
The comment summarizes it pretty well, but basically:

- bat (as of 0.22 <sharkdp/bat#2197>) defaults to switching theme based on macOS' light/dark mode
- That isn't the same thing as whether the terminal theme is light or dark
- There are ways to do that, both in Apple's terminal and in iTerm2 (in the 2.5 beta), but until then/if it's not desired, this works
- One day there will [hopefully](sharkdp/bat#1746) be a way to set both light and dark themes for `bat`
- None of this makes much of any difference for `delta`
Repository owner deleted a comment from sh471 Jan 11, 2023
Comment on lines +100 to +104
if macos_dark_mode_active() {
Self::default_dark_theme()
} else {
Self::default_light_theme()
}

Choose a reason for hiding this comment

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

could this logic be extended to other OSes too?
In my opinion, the macos_dark_mode_active function should be renamed to os_dark_mode_active and then handle the specific logic of several OSes (including macos).

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.

None yet

4 participants