mirror of
https://github.com/sharkdp/bat.git
synced 2024-11-22 07:43:39 +01:00
Make the default macOS theme depend on Dark Mode (#2197)
* Make the default macOS theme depend on Dark Mode 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. * src/assets.rs: Fix typo * Update CHANGELOG.md
This commit is contained in:
parent
e9f8370b13
commit
3339eee2dc
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
|
- Make the default macOS theme depend on Dark Mode. See #2197, #1746 (@Enselic)
|
||||||
|
|
||||||
## Bugfixes
|
## Bugfixes
|
||||||
|
|
||||||
## Other
|
## Other
|
||||||
|
@ -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 {
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default theme that looks good on a dark background.
|
||||||
|
*/
|
||||||
|
fn default_dark_theme() -> &'static str {
|
||||||
"Monokai Extended"
|
"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> {
|
pub fn from_cache(cache_path: &Path) -> Result<Self> {
|
||||||
Ok(HighlightingAssets::new(
|
Ok(HighlightingAssets::new(
|
||||||
SerializedSyntaxSet::FromFile(cache_path.join("syntaxes.bin")),
|
SerializedSyntaxSet::FromFile(cache_path.join("syntaxes.bin")),
|
||||||
@ -352,6 +399,16 @@ fn asset_from_cache<T: serde::de::DeserializeOwned>(
|
|||||||
.map_err(|_| format!("Could not parse cached {}", description).into())
|
.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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -1427,6 +1427,8 @@ fn ansi_passthrough_emit() {
|
|||||||
fn ignored_suffix_arg() {
|
fn ignored_suffix_arg() {
|
||||||
bat()
|
bat()
|
||||||
.arg("-f")
|
.arg("-f")
|
||||||
|
.arg("--theme")
|
||||||
|
.arg("Monokai Extended")
|
||||||
.arg("-p")
|
.arg("-p")
|
||||||
.arg("test.json~")
|
.arg("test.json~")
|
||||||
.assert()
|
.assert()
|
||||||
@ -1436,6 +1438,8 @@ fn ignored_suffix_arg() {
|
|||||||
|
|
||||||
bat()
|
bat()
|
||||||
.arg("-f")
|
.arg("-f")
|
||||||
|
.arg("--theme")
|
||||||
|
.arg("Monokai Extended")
|
||||||
.arg("-p")
|
.arg("-p")
|
||||||
.arg("--ignored-suffix=.suffix")
|
.arg("--ignored-suffix=.suffix")
|
||||||
.arg("test.json.suffix")
|
.arg("test.json.suffix")
|
||||||
@ -1446,6 +1450,8 @@ fn ignored_suffix_arg() {
|
|||||||
|
|
||||||
bat()
|
bat()
|
||||||
.arg("-f")
|
.arg("-f")
|
||||||
|
.arg("--theme")
|
||||||
|
.arg("Monokai Extended")
|
||||||
.arg("-p")
|
.arg("-p")
|
||||||
.arg("test.json.suffix")
|
.arg("test.json.suffix")
|
||||||
.assert()
|
.assert()
|
||||||
@ -1463,6 +1469,8 @@ fn highlighting_is_skipped_on_long_lines() {
|
|||||||
|
|
||||||
bat()
|
bat()
|
||||||
.arg("-f")
|
.arg("-f")
|
||||||
|
.arg("--theme")
|
||||||
|
.arg("Monokai Extended")
|
||||||
.arg("-p")
|
.arg("-p")
|
||||||
.arg("longline.json")
|
.arg("longline.json")
|
||||||
.assert()
|
.assert()
|
||||||
@ -1482,6 +1490,8 @@ fn all_global_git_config_locations_syntax_mapping_work() {
|
|||||||
bat()
|
bat()
|
||||||
.env("XDG_CONFIG_HOME", fake_home.join(".config").as_os_str())
|
.env("XDG_CONFIG_HOME", fake_home.join(".config").as_os_str())
|
||||||
.arg("-f")
|
.arg("-f")
|
||||||
|
.arg("--theme")
|
||||||
|
.arg("Monokai Extended")
|
||||||
.arg("-p")
|
.arg("-p")
|
||||||
.arg("git/.config/git/config")
|
.arg("git/.config/git/config")
|
||||||
.assert()
|
.assert()
|
||||||
@ -1492,6 +1502,8 @@ fn all_global_git_config_locations_syntax_mapping_work() {
|
|||||||
bat()
|
bat()
|
||||||
.env("HOME", fake_home.as_os_str())
|
.env("HOME", fake_home.as_os_str())
|
||||||
.arg("-f")
|
.arg("-f")
|
||||||
|
.arg("--theme")
|
||||||
|
.arg("Monokai Extended")
|
||||||
.arg("-p")
|
.arg("-p")
|
||||||
.arg("git/.config/git/config")
|
.arg("git/.config/git/config")
|
||||||
.assert()
|
.assert()
|
||||||
@ -1502,6 +1514,8 @@ fn all_global_git_config_locations_syntax_mapping_work() {
|
|||||||
bat()
|
bat()
|
||||||
.env("HOME", fake_home.as_os_str())
|
.env("HOME", fake_home.as_os_str())
|
||||||
.arg("-f")
|
.arg("-f")
|
||||||
|
.arg("--theme")
|
||||||
|
.arg("Monokai Extended")
|
||||||
.arg("-p")
|
.arg("-p")
|
||||||
.arg("git/.gitconfig")
|
.arg("git/.gitconfig")
|
||||||
.assert()
|
.assert()
|
||||||
|
Loading…
Reference in New Issue
Block a user