From 2acfc41ceb9654e3bb1bf28a51bd9afc2b395293 Mon Sep 17 00:00:00 2001 From: Alex Pasmantier <47638216+alexpasmantier@users.noreply.github.com> Date: Tue, 31 Dec 2024 18:53:20 +0100 Subject: [PATCH] feat(ui): respect BAT_THEME env var for previewer syntax highlighting theme (#201) Fixes #169 --- .config/config.toml | 1 + .../src/previewers/files.rs | 22 ++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/.config/config.toml b/.config/config.toml index d309fa4..e937afd 100644 --- a/.config/config.toml +++ b/.config/config.toml @@ -64,6 +64,7 @@ theme = "catppuccin" # The theme to use for syntax highlighting. # Bulitin syntax highlighting uses the same syntax highlighting engine as bat. # To get a list of your currently available themes, run `bat --list-themes` +# Note that setting the BAT_THEME environment variable will override this setting. theme = "Coldark-Dark" # Keybindings diff --git a/crates/television-previewers/src/previewers/files.rs b/crates/television-previewers/src/previewers/files.rs index 42a7d80..bd0920d 100644 --- a/crates/television-previewers/src/previewers/files.rs +++ b/crates/television-previewers/src/previewers/files.rs @@ -9,10 +9,7 @@ use std::sync::{ Arc, }; -use syntect::{ - highlighting::{Theme, ThemeSet}, - parsing::SyntaxSet, -}; +use syntect::{highlighting::Theme, parsing::SyntaxSet}; use tracing::{debug, warn}; use super::cache::PreviewCache; @@ -51,18 +48,23 @@ const MAX_FILE_SIZE: u64 = 4 * 1024 * 1024; const MAX_CONCURRENT_PREVIEW_TASKS: u8 = 3; +const BAT_THEME_ENV_VAR: &str = "BAT_THEME"; + impl FilePreviewer { pub fn new(config: Option) -> Self { let hl_assets = load_highlighting_assets(); let syntax_set = hl_assets.get_syntax_set().unwrap().clone(); - let theme = config.map_or_else( - || { - let theme_set = ThemeSet::load_defaults(); - theme_set.themes["base16-ocean.dark"].clone() + let theme_name = match std::env::var(BAT_THEME_ENV_VAR) { + Ok(t) => t, + Err(_) => match config { + Some(c) => c.theme, + // this will error and default back nicely + None => "unknown".to_string(), }, - |c| hl_assets.get_theme_no_output(&c.theme).clone(), - ); + }; + + let theme = hl_assets.get_theme_no_output(&theme_name).clone(); FilePreviewer { cache: Arc::new(Mutex::new(PreviewCache::default())),