feat(ui): respect BAT_THEME env var for previewer syntax highlighting theme (#201)

Fixes #169
This commit is contained in:
Alex Pasmantier 2024-12-31 18:53:20 +01:00 committed by GitHub
parent a74deceb98
commit 2acfc41ceb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 10 deletions

View File

@ -64,6 +64,7 @@ theme = "catppuccin"
# The theme to use for syntax highlighting. # The theme to use for syntax highlighting.
# Bulitin syntax highlighting uses the same syntax highlighting engine as bat. # Bulitin syntax highlighting uses the same syntax highlighting engine as bat.
# To get a list of your currently available themes, run `bat --list-themes` # 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" theme = "Coldark-Dark"
# Keybindings # Keybindings

View File

@ -9,10 +9,7 @@ use std::sync::{
Arc, Arc,
}; };
use syntect::{ use syntect::{highlighting::Theme, parsing::SyntaxSet};
highlighting::{Theme, ThemeSet},
parsing::SyntaxSet,
};
use tracing::{debug, warn}; use tracing::{debug, warn};
use super::cache::PreviewCache; use super::cache::PreviewCache;
@ -51,18 +48,23 @@ const MAX_FILE_SIZE: u64 = 4 * 1024 * 1024;
const MAX_CONCURRENT_PREVIEW_TASKS: u8 = 3; const MAX_CONCURRENT_PREVIEW_TASKS: u8 = 3;
const BAT_THEME_ENV_VAR: &str = "BAT_THEME";
impl FilePreviewer { impl FilePreviewer {
pub fn new(config: Option<FilePreviewerConfig>) -> Self { pub fn new(config: Option<FilePreviewerConfig>) -> Self {
let hl_assets = load_highlighting_assets(); let hl_assets = load_highlighting_assets();
let syntax_set = hl_assets.get_syntax_set().unwrap().clone(); let syntax_set = hl_assets.get_syntax_set().unwrap().clone();
let theme = config.map_or_else( let theme_name = match std::env::var(BAT_THEME_ENV_VAR) {
|| { Ok(t) => t,
let theme_set = ThemeSet::load_defaults(); Err(_) => match config {
theme_set.themes["base16-ocean.dark"].clone() 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 { FilePreviewer {
cache: Arc::new(Mutex::new(PreviewCache::default())), cache: Arc::new(Mutex::new(PreviewCache::default())),