use std::convert::Infallible;
use std::fmt;
use std::io::IsTerminal as _;
use std::str::FromStr;
pub mod env {
pub const BAT_THEME: &str = "BAT_THEME";
pub const BAT_THEME_DARK: &str = "BAT_THEME_DARK";
pub const BAT_THEME_LIGHT: &str = "BAT_THEME_LIGHT";
}
pub fn theme(options: ThemeOptions) -> ThemeResult {
theme_impl(options, &TerminalColorSchemeDetector)
}
pub const fn default_theme(color_scheme: ColorScheme) -> &'static str {
match color_scheme {
ColorScheme::Dark => "Monokai Extended",
ColorScheme::Light => "Monokai Extended Light",
}
}
pub fn color_scheme(when: DetectColorScheme) -> Option<ColorScheme> {
color_scheme_impl(when, &TerminalColorSchemeDetector)
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ThemeOptions {
pub theme: ThemePreference,
pub theme_dark: Option<ThemeName>,
pub theme_light: Option<ThemeName>,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ThemePreference {
Auto(DetectColorScheme),
Fixed(ThemeName),
Dark,
Light,
}
impl Default for ThemePreference {
fn default() -> Self {
ThemePreference::Auto(Default::default())
}
}
impl ThemePreference {
pub fn new(s: impl Into<String>) -> Self {
use ThemePreference::*;
let s = s.into();
match s.as_str() {
"auto" => Auto(Default::default()),
"auto:always" => Auto(DetectColorScheme::Always),
"auto:system" => Auto(DetectColorScheme::System),
"dark" => Dark,
"light" => Light,
_ => Fixed(ThemeName::new(s)),
}
}
}
impl FromStr for ThemePreference {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(ThemePreference::new(s))
}
}
impl fmt::Display for ThemePreference {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use ThemePreference::*;
match self {
Auto(DetectColorScheme::Auto) => f.write_str("auto"),
Auto(DetectColorScheme::Always) => f.write_str("auto:always"),
Auto(DetectColorScheme::System) => f.write_str("auto:system"),
Fixed(theme) => theme.fmt(f),
Dark => f.write_str("dark"),
Light => f.write_str("light"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ThemeName {
Named(String),
Default,
}
impl ThemeName {
pub fn new(s: impl Into<String>) -> Self {
let s = s.into();
if s == "default" {
ThemeName::Default
} else {
ThemeName::Named(s)
}
}
}
impl FromStr for ThemeName {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(ThemeName::new(s))
}
}
impl fmt::Display for ThemeName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ThemeName::Named(t) => f.write_str(t),
ThemeName::Default => f.write_str("default"),
}
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DetectColorScheme {
#[default]
Auto,
Always,
System,
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ColorScheme {
#[default]
Dark,
Light,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ThemeResult {
pub theme: ThemeName,
pub color_scheme: Option<ColorScheme>,
}
impl fmt::Display for ThemeResult {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.theme {
ThemeName::Named(name) => f.write_str(name),
ThemeName::Default => f.write_str(default_theme(self.color_scheme.unwrap_or_default())),
}
}
}
fn theme_impl(options: ThemeOptions, detector: &dyn ColorSchemeDetector) -> ThemeResult {
match options.theme {
ThemePreference::Fixed(theme) => ThemeResult {
theme,
color_scheme: None,
},
ThemePreference::Dark => choose_theme_opt(Some(ColorScheme::Dark), options),
ThemePreference::Light => choose_theme_opt(Some(ColorScheme::Light), options),
ThemePreference::Auto(when) => choose_theme_opt(color_scheme_impl(when, detector), options),
}
}
fn choose_theme_opt(color_scheme: Option<ColorScheme>, options: ThemeOptions) -> ThemeResult {
ThemeResult {
color_scheme,
theme: color_scheme
.and_then(|c| choose_theme(options, c))
.unwrap_or(ThemeName::Default),
}
}
fn choose_theme(options: ThemeOptions, color_scheme: ColorScheme) -> Option<ThemeName> {
match color_scheme {
ColorScheme::Dark => options.theme_dark,
ColorScheme::Light => options.theme_light,
}
}
fn color_scheme_impl(
when: DetectColorScheme,
detector: &dyn ColorSchemeDetector,
) -> Option<ColorScheme> {
let should_detect = match when {
DetectColorScheme::Auto => detector.should_detect(),
DetectColorScheme::Always => true,
DetectColorScheme::System => return color_scheme_from_system(),
};
should_detect.then(|| detector.detect()).flatten()
}
trait ColorSchemeDetector {
fn should_detect(&self) -> bool;
fn detect(&self) -> Option<ColorScheme>;
}
struct TerminalColorSchemeDetector;
impl ColorSchemeDetector for TerminalColorSchemeDetector {
fn should_detect(&self) -> bool {
std::io::stdout().is_terminal()
}
fn detect(&self) -> Option<ColorScheme> {
use terminal_colorsaurus::{theme_mode, QueryOptions, ThemeMode};
match theme_mode(QueryOptions::default()).ok()? {
ThemeMode::Dark => Some(ColorScheme::Dark),
ThemeMode::Light => Some(ColorScheme::Light),
}
}
}
#[cfg(not(target_os = "macos"))]
fn color_scheme_from_system() -> Option<ColorScheme> {
crate::bat_warning!(
"Theme 'auto:system' is only supported on macOS, \
using default."
);
None
}
#[cfg(target_os = "macos")]
fn color_scheme_from_system() -> Option<ColorScheme> {
const PREFERENCES_FILE: &str = "Library/Preferences/.GlobalPreferences.plist";
const STYLE_KEY: &str = "AppleInterfaceStyle";
let preferences_file = std::env::home_dir()
.map(|home| home.join(PREFERENCES_FILE))
.expect("Could not get home directory");
match plist::Value::from_file(preferences_file).map(|file| file.into_dictionary()) {
Ok(Some(preferences)) => match preferences.get(STYLE_KEY).and_then(|val| val.as_string()) {
Some("Dark") => Some(ColorScheme::Dark),
Some(_) | None => Some(ColorScheme::Light),
},
Ok(None) | Err(_) => None,
}
}
#[cfg(test)]
impl ColorSchemeDetector for Option<ColorScheme> {
fn should_detect(&self) -> bool {
true
}
fn detect(&self) -> Option<ColorScheme> {
*self
}
}
#[cfg(test)]
mod tests {
use super::ColorScheme::*;
use super::*;
use std::cell::Cell;
use std::iter;
mod color_scheme_detection {
use super::*;
#[test]
fn not_called_for_dark_or_light() {
for theme in [ThemePreference::Dark, ThemePreference::Light] {
let detector = DetectorStub::should_detect(Some(Dark));
let options = ThemeOptions {
theme,
..Default::default()
};
_ = theme_impl(options, &detector);
assert!(!detector.was_called.get());
}
}
#[test]
fn called_for_always() {
let detectors = [
DetectorStub::should_detect(Some(Dark)),
DetectorStub::should_not_detect(),
];
for detector in detectors {
let options = ThemeOptions {
theme: ThemePreference::Auto(DetectColorScheme::Always),
..Default::default()
};
_ = theme_impl(options, &detector);
assert!(detector.was_called.get());
}
}
#[test]
fn called_for_auto_if_should_detect() {
let detector = DetectorStub::should_detect(Some(Dark));
_ = theme_impl(ThemeOptions::default(), &detector);
assert!(detector.was_called.get());
}
#[test]
fn not_called_for_auto_if_not_should_detect() {
let detector = DetectorStub::should_not_detect();
_ = theme_impl(ThemeOptions::default(), &detector);
assert!(!detector.was_called.get());
}
}
mod precedence {
use super::*;
#[test]
fn theme_is_preferred_over_light_or_dark_themes() {
for color_scheme in optional(color_schemes()) {
for options in [
ThemeOptions {
theme: ThemePreference::Fixed(ThemeName::Named("Theme".to_string())),
..Default::default()
},
ThemeOptions {
theme: ThemePreference::Fixed(ThemeName::Named("Theme".to_string())),
theme_dark: Some(ThemeName::Named("Dark Theme".to_string())),
theme_light: Some(ThemeName::Named("Light Theme".to_string())),
},
] {
let detector = ConstantDetector(color_scheme);
assert_eq!("Theme", theme_impl(options, &detector).to_string());
}
}
}
#[test]
fn detector_is_not_called_if_theme_is_present() {
let options = ThemeOptions {
theme: ThemePreference::Fixed(ThemeName::Named("Theme".to_string())),
..Default::default()
};
let detector = DetectorStub::should_detect(Some(Dark));
_ = theme_impl(options, &detector);
assert!(!detector.was_called.get());
}
}
mod default_theme {
use super::*;
#[test]
fn default_dark_if_unable_to_detect_color_scheme() {
let detector = ConstantDetector(None);
assert_eq!(
default_theme(ColorScheme::Dark),
theme_impl(ThemeOptions::default(), &detector).to_string()
);
}
#[test]
fn default_dark_if_requested_explicitly_through_theme() {
for color_scheme in optional(color_schemes()) {
let options = ThemeOptions {
theme: ThemePreference::Fixed(ThemeName::Default),
..Default::default()
};
let detector = ConstantDetector(color_scheme);
assert_eq!(
default_theme(ColorScheme::Dark),
theme_impl(options, &detector).to_string()
);
}
}
#[test]
fn varies_depending_on_color_scheme() {
for color_scheme in color_schemes() {
for options in [
ThemeOptions::default(),
ThemeOptions {
theme_dark: Some(ThemeName::Default),
theme_light: Some(ThemeName::Default),
..Default::default()
},
] {
let detector = ConstantDetector(Some(color_scheme));
assert_eq!(
default_theme(color_scheme),
theme_impl(options, &detector).to_string()
);
}
}
}
}
mod choosing {
use super::*;
#[test]
fn chooses_default_theme_if_unknown() {
let options = ThemeOptions {
theme_dark: Some(ThemeName::Named("Dark".to_string())),
theme_light: Some(ThemeName::Named("Light".to_string())),
..Default::default()
};
let detector = ConstantDetector(None);
assert_eq!(
default_theme(ColorScheme::default()),
theme_impl(options, &detector).to_string()
);
}
#[test]
fn chooses_dark_theme_if_dark_or_unknown() {
let options = ThemeOptions {
theme_dark: Some(ThemeName::Named("Dark".to_string())),
theme_light: Some(ThemeName::Named("Light".to_string())),
..Default::default()
};
let detector = ConstantDetector(Some(ColorScheme::Dark));
assert_eq!("Dark", theme_impl(options, &detector).to_string());
}
#[test]
fn chooses_light_theme_if_light() {
let options = ThemeOptions {
theme_dark: Some(ThemeName::Named("Dark".to_string())),
theme_light: Some(ThemeName::Named("Light".to_string())),
..Default::default()
};
let detector = ConstantDetector(Some(ColorScheme::Light));
assert_eq!("Light", theme_impl(options, &detector).to_string());
}
}
mod theme_preference {
use super::*;
#[test]
fn values_roundtrip_via_display() {
let prefs = [
ThemePreference::Auto(DetectColorScheme::Auto),
ThemePreference::Auto(DetectColorScheme::Always),
ThemePreference::Auto(DetectColorScheme::System),
ThemePreference::Fixed(ThemeName::Default),
ThemePreference::Fixed(ThemeName::new("foo")),
ThemePreference::Dark,
ThemePreference::Light,
];
for pref in prefs {
assert_eq!(pref, ThemePreference::new(pref.to_string()));
}
}
}
struct DetectorStub {
should_detect: bool,
color_scheme: Option<ColorScheme>,
was_called: Cell<bool>,
}
impl DetectorStub {
fn should_detect(color_scheme: Option<ColorScheme>) -> Self {
DetectorStub {
should_detect: true,
color_scheme,
was_called: Cell::default(),
}
}
fn should_not_detect() -> Self {
DetectorStub {
should_detect: false,
color_scheme: None,
was_called: Cell::default(),
}
}
}
impl ColorSchemeDetector for DetectorStub {
fn should_detect(&self) -> bool {
self.should_detect
}
fn detect(&self) -> Option<ColorScheme> {
self.was_called.set(true);
self.color_scheme
}
}
struct ConstantDetector(Option<ColorScheme>);
impl ColorSchemeDetector for ConstantDetector {
fn should_detect(&self) -> bool {
true
}
fn detect(&self) -> Option<ColorScheme> {
self.0
}
}
fn optional<T>(value: impl Iterator<Item = T>) -> impl Iterator<Item = Option<T>> {
value.map(Some).chain(iter::once(None))
}
fn color_schemes() -> impl Iterator<Item = ColorScheme> {
[Dark, Light].into_iter()
}
}