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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Features

- Make the default macOS theme depend on Dark Mode. See #2197, #1746 (@Enselic)

## Bugfixes

## Other
Expand Down
57 changes: 57 additions & 0 deletions src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,57 @@ impl HighlightingAssets {
}
}

/// The default theme.
///
/// ### Windows and Linux
///
/// Windows and most Linux distributions has a dark terminal theme by
/// default. On these platforms, this function always returns a theme that
/// looks good on a dark background.
///
/// ### macOS
///
/// On macOS the default terminal background is light, but it is common that
/// Dark Mode is active, which makes the terminal background dark. On this
/// platform, the default theme depends on
/// ```bash
/// defaults read -globalDomain AppleInterfaceStyle
/// ````
/// To avoid the overhead of the check on macOS, simply specify a theme
/// explicitly via `--theme`, `BAT_THEME`, or `~/.config/bat`.
///
/// See <https://github.com/sharkdp/bat/issues/1746> and
/// <https://github.com/sharkdp/bat/issues/1928> for more context.
pub fn default_theme() -> &'static str {
#[cfg(not(target_os = "macos"))]
{
Self::default_dark_theme()
}
#[cfg(target_os = "macos")]
{
if macos_dark_mode_active() {
Self::default_dark_theme()
} else {
Self::default_light_theme()
}
Comment on lines +100 to +104

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).

}
}

/**
* The default theme that looks good on a dark background.
*/
fn default_dark_theme() -> &'static str {
"Monokai Extended"
}

/**
* The default theme that looks good on a light background.
*/
#[cfg(target_os = "macos")]
fn default_light_theme() -> &'static str {
"Monokai Extended Light"
}

pub fn from_cache(cache_path: &Path) -> Result<Self> {
Ok(HighlightingAssets::new(
SerializedSyntaxSet::FromFile(cache_path.join("syntaxes.bin")),
Expand Down Expand Up @@ -352,6 +399,16 @@ fn asset_from_cache<T: serde::de::DeserializeOwned>(
.map_err(|_| format!("Could not parse cached {}", description).into())
}

#[cfg(target_os = "macos")]
fn macos_dark_mode_active() -> bool {
let mut defaults_cmd = std::process::Command::new("defaults");
defaults_cmd.args(&["read", "-globalDomain", "AppleInterfaceStyle"]);
match defaults_cmd.output() {
Ok(output) => output.stdout == b"Dark\n",
Err(_) => true,
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
14 changes: 14 additions & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,8 @@ fn ansi_passthrough_emit() {
fn ignored_suffix_arg() {
bat()
.arg("-f")
.arg("--theme")
.arg("Monokai Extended")
.arg("-p")
.arg("test.json~")
.assert()
Expand All @@ -1436,6 +1438,8 @@ fn ignored_suffix_arg() {

bat()
.arg("-f")
.arg("--theme")
.arg("Monokai Extended")
.arg("-p")
.arg("--ignored-suffix=.suffix")
.arg("test.json.suffix")
Expand All @@ -1446,6 +1450,8 @@ fn ignored_suffix_arg() {

bat()
.arg("-f")
.arg("--theme")
.arg("Monokai Extended")
.arg("-p")
.arg("test.json.suffix")
.assert()
Expand All @@ -1463,6 +1469,8 @@ fn highlighting_is_skipped_on_long_lines() {

bat()
.arg("-f")
.arg("--theme")
.arg("Monokai Extended")
.arg("-p")
.arg("longline.json")
.assert()
Expand All @@ -1482,6 +1490,8 @@ fn all_global_git_config_locations_syntax_mapping_work() {
bat()
.env("XDG_CONFIG_HOME", fake_home.join(".config").as_os_str())
.arg("-f")
.arg("--theme")
.arg("Monokai Extended")
.arg("-p")
.arg("git/.config/git/config")
.assert()
Expand All @@ -1492,6 +1502,8 @@ fn all_global_git_config_locations_syntax_mapping_work() {
bat()
.env("HOME", fake_home.as_os_str())
.arg("-f")
.arg("--theme")
.arg("Monokai Extended")
.arg("-p")
.arg("git/.config/git/config")
.assert()
Expand All @@ -1502,6 +1514,8 @@ fn all_global_git_config_locations_syntax_mapping_work() {
bat()
.env("HOME", fake_home.as_os_str())
.arg("-f")
.arg("--theme")
.arg("Monokai Extended")
.arg("-p")
.arg("git/.gitconfig")
.assert()
Expand Down